com.fujitsu.dc.test.jersey.cell.PropPatchTest.java Source code

Java tutorial

Introduction

Here is the source code for com.fujitsu.dc.test.jersey.cell.PropPatchTest.java

Source

/**
 * personium.io
 * Copyright 2014 FUJITSU LIMITED
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fujitsu.dc.test.jersey.cell;

    import static org.junit.Assert.assertEquals;

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;

    import org.apache.http.HttpStatus;
    import org.junit.Test;
    import org.junit.experimental.categories.Category;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

    import com.fujitsu.dc.test.categories.Integration;
    import com.fujitsu.dc.test.categories.Regression;
    import com.fujitsu.dc.test.categories.Unit;
    import com.fujitsu.dc.test.jersey.AbstractCase;
    import com.fujitsu.dc.test.unit.core.UrlUtils;
    import com.fujitsu.dc.test.utils.CellUtils;
    import com.fujitsu.dc.test.utils.DavResourceUtils;
    import com.fujitsu.dc.test.utils.TResponse;
    import com.sun.jersey.test.framework.JerseyTest;

    /**
     * CellPROPPATCH?.
     */
    @Category({ Unit.class, Integration.class, Regression.class })
    public class PropPatchTest extends JerseyTest {

        static final String TEST_CELL1 = "testcell1";
        static final String DEPTH = "0";

        /**
         * .
         */
        public PropPatchTest() {
            super("com.fujitsu.dc.core.rs");
        }

    /**
     * CellPROPPATCH?.
     */
    @Test
    public final void CellPROPPATCH?() {

        String token = AbstractCase.MASTER_TOKEN_NAME;
        String author1 = "Test User1";
        String hoge1 = "Hoge";
        String author2 = "Author1 update";
        String hoge2 = "Fuga";
        String authorKey = "Author";
        String hogeKey = "hoge";
        TResponse tresponseWebDav = null;

        try {

            // PROPPATCH
            DavResourceUtils.setProppatch(TEST_CELL1, token, HttpStatus.SC_MULTI_STATUS, author1, hoge1);

            // ??
            tresponseWebDav = CellUtils.propfind(TEST_CELL1, token, DEPTH, HttpStatus.SC_MULTI_STATUS);
            Element root = tresponseWebDav.bodyAsXml().getDocumentElement();
            String resorce = UrlUtils.cellRoot(TEST_CELL1);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(authorKey, author1);
            map.put(hogeKey, hoge1);
            proppatchResponseTest(root, resorce, map);

            // ?
            DavResourceUtils.setProppatch(TEST_CELL1, token, HttpStatus.SC_MULTI_STATUS, author2, hoge2);

            // ??
            tresponseWebDav = CellUtils.propfind(TEST_CELL1, token, DEPTH, HttpStatus.SC_MULTI_STATUS);
            Element root2 = tresponseWebDav.bodyAsXml().getDocumentElement();
            HashMap<String, String> map2 = new HashMap<String, String>();
            map.put(authorKey, author2);
            map.put(hogeKey, hoge2);
            proppatchResponseTest(root2, resorce, map2);

        } finally {
            // ?
            DavResourceUtils.resetProppatch(TEST_CELL1, token);
        }
    }

        /**
         * CellPROPPATCH?_Cell??.
         */
        @Test
        public final void CellPROPPATCH?_Cell??() {

        String token = AbstractCase.MASTER_TOKEN_NAME;
        String author1 = "Test User1";
        String hoge1 = "Hoge";
        String authorKey = "Author";
        String hogeKey = "hoge";
        TResponse tresponseWebDav = null;

        try {
            // PROPPATCH
            DavResourceUtils.setProppatch(TEST_CELL1, token, HttpStatus.SC_MULTI_STATUS, author1, hoge1);

            // Cell?
            String updateCellName = TEST_CELL1;
            CellUtils.update(TEST_CELL1, updateCellName, AbstractCase.MASTER_TOKEN_NAME, HttpStatus.SC_NO_CONTENT);

            // ??
            tresponseWebDav = CellUtils.propfind(TEST_CELL1, token, DEPTH, HttpStatus.SC_MULTI_STATUS);
            Element root = tresponseWebDav.bodyAsXml().getDocumentElement();
            String resorce = UrlUtils.cellRoot(TEST_CELL1);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(authorKey, author1);
            map.put(hogeKey, hoge1);
            proppatchResponseTest(root, resorce, map);
        } finally {
            // ?
            DavResourceUtils.resetProppatch(TEST_CELL1, token);
        }
    }

        /**
        * CellPROPPATCH?prop?????.
        */
        @Test
        public final void CellPROPPATCH?prop?????() {

        String token = AbstractCase.MASTER_TOKEN_NAME;
        String author1 = "Test User1";
        String hoge1 = "Hoge";

        // PROPPATCH
        DavResourceUtils.setProppatchSetPropKey(TEST_CELL1, token, HttpStatus.SC_BAD_REQUEST, "props", author1, hoge1);

    }

        /**
         * CellPROPPATCH?prop?????.
         */
        @Test
        public final void CellPROPPATCH?prop?????() {

        String token = AbstractCase.MASTER_TOKEN_NAME;

        // PROPPATCH
        DavResourceUtils.resetProppatchSetPropKey(TEST_CELL1, token, HttpStatus.SC_BAD_REQUEST, "props");

    }

    /**
     * PROPPATCH????.
     * @param doc ??XML
     * @param resorce PROPPATCH??
     * @param map ???KeyValue Key?Value???????? Valuenull???Key??????remove????
     */
    private void proppatchResponseTest(Element doc, String resorce, Map<String, String> map) {
        StringBuffer sb = new StringBuffer(resorce);
        sb.deleteCharAt(resorce.length() - 1);

        NodeList response = doc.getElementsByTagName("response");
        assertEquals(1, response.getLength());
        Element node = (Element) response.item(0);
        assertEquals(
                sb.toString(),
                node.getElementsByTagName("href").item(0).getFirstChild().getNodeValue());
        assertEquals(
                "HTTP/1.1 200 OK",
                node.getElementsByTagName("status").item(0).getFirstChild().getNodeValue());

        for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) {
            Object key = it.next();
            Object value = map.get(key);
            String textContext = null;
            NodeList tmp = node.getElementsByTagName("prop").item(0).getChildNodes();
            for (int i = 0; i < tmp.getLength(); i++) {
                Node child = tmp.item(i);
                if (child instanceof Element) {
                    Element childElement = (Element) child;
                    if (childElement.getLocalName().equals(key)) {
                        textContext = childElement.getTextContent();
                        break;
                    }
                }
            }
            assertEquals(value, textContext);
        }
    }

    }