io.personium.test.jersey.cell.ctl.BoxRoleLinkTest.java Source code

Java tutorial

Introduction

Here is the source code for io.personium.test.jersey.cell.ctl.BoxRoleLinkTest.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 io.personium.test.jersey.cell.ctl;

    import static org.junit.Assert.assertEquals;

    import java.util.ArrayList;

    import org.apache.http.HttpStatus;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.experimental.categories.Category;
    import org.junit.runner.RunWith;

    import com.sun.jersey.test.framework.JerseyTest;

    import io.personium.test.categories.Integration;
    import io.personium.test.categories.Regression;
    import io.personium.test.categories.Unit;
    import io.personium.test.jersey.AbstractCase;
    import io.personium.test.jersey.ODataCommon;
    import io.personium.test.jersey.PersoniumIntegTestRunner;
    import io.personium.test.setup.Setup;
    import io.personium.test.unit.core.UrlUtils;
    import io.personium.test.utils.BoxUtils;
    import io.personium.test.utils.DavResourceUtils;
    import io.personium.test.utils.Http;
    import io.personium.test.utils.RelationUtils;
    import io.personium.test.utils.RoleUtils;
    import io.personium.test.utils.TResponse;

    /**
     * Box?Role?$links?.
     */
    @RunWith(PersoniumIntegTestRunner.class)
    @Category({ Unit.class, Integration.class, Regression.class })
    public class BoxRoleLinkTest extends JerseyTest {

        private static final String CELL_NAME = "testcell1";
        private static final String ENTITY_SET_BOX = "Box";
        private static final String ENTITY_SET_ROLE = "Role";
        private static final String KEY = "'box1'";
        private static final String NAV_PROP_BOX = "_Box";
        private static final String NAV_PROP_ROLE = "_Role";
        private static String roleUri;
        private static String roleKey;
        private static String roleChangedKey;
        static final String TOKEN = AbstractCase.MASTER_TOKEN_NAME;

        /**
         * . ?super?????
         */
        public BoxRoleLinkTest() {
            super("io.personium.core.rs");
        }

        /**
         * ?????.
         */
        @Before
        public final void before() {
            if (roleUri == null) {
                TResponse response = Http.request("role-list.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                        .with("cellPath", CELL_NAME).returns().statusCode(HttpStatus.SC_OK);
                JSONObject d = (JSONObject) response.bodyAsJson().get("d");
                JSONArray results = (JSONArray) d.get("results");
                String name = (String) ((JSONObject) results.get(0)).get("Name");
                String boxName = (String) ((JSONObject) results.get(0)).get("_Box.Name");
                if (boxName == null) {
                    roleKey = "Name='" + name + "',_Box.Name=null";
                } else {
                    roleKey = "Name='" + name + "',_Box.Name='" + boxName + "'";
                }
                roleChangedKey = "_Box.Name=" + KEY + ",Name='" + name + "'";
                roleUri = UrlUtils.cellCtlWithoutSingleQuote(CELL_NAME, ENTITY_SET_ROLE, roleKey);
            }
        }

        /**
         * Box?Role?link?response?204????.
         */
        @Test
        public final void Box?Role?link?response?204????()
        {
            try {
                Http.request("links-request-with-body.txt").with("method", "POST")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                        .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE)
                        .with("uri", roleUri).returns().statusCode(HttpStatus.SC_NO_CONTENT);
            } finally {
                deleteLink();
            }
        }

        /**
         * Role?Box?link?response?204????.
         */
        @Test
        public final void Role?Box?link?response?204????()
        {
            String boxUri = UrlUtils.cellCtlWithoutSingleQuote(CELL_NAME, ENTITY_SET_BOX, KEY);
            try {
                Http.request("links-request-with-body.txt").with("method", "POST")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                        .with("entitySet", ENTITY_SET_ROLE).with("key", roleKey).with("navProp", NAV_PROP_BOX)
                        .with("uri", boxUri).returns().statusCode(HttpStatus.SC_NO_CONTENT);
            } finally {
                deleteLink();
            }
        }

        /**
         * Role?Box?link?Role???Box??????409????.
         */
        @Test
        public final void Role?Box?link?Role???Box??????409????()
        {
        String cellName = Setup.TEST_CELL1;
        String baseBoxName = "RoleBoxLinkTestBaseBox";
        String boxName = "RoleBoxLinkTestBox";
        String roleName = "RoleBoxLinkTestRole";
        try {
            // Role?
            RoleUtils.create(cellName, TOKEN, roleName, HttpStatus.SC_CREATED);

            // Box?
            BoxUtils.create(cellName, baseBoxName, TOKEN);
            BoxUtils.create(cellName, boxName, TOKEN);

            // Role-Box $links
            RoleUtils.createLink(cellName, TOKEN, RoleUtils.keyString(roleName), "Box", "'" + baseBoxName + "'",
                    HttpStatus.SC_NO_CONTENT);
            RoleUtils.createLink(cellName, TOKEN, RoleUtils.keyString(roleName, baseBoxName),
                    "Box", "'" + boxName + "'",
                    HttpStatus.SC_CONFLICT);
        } finally {
            // Role
            RoleUtils.delete(cellName, TOKEN, roleName, null, -1);
            RoleUtils.delete(cellName, TOKEN, roleName, baseBoxName, -1);

            // Box
            BoxUtils.delete(cellName, TOKEN, baseBoxName, -1);
            BoxUtils.delete(cellName, TOKEN, boxName, -1);

        }
    }

    /**
     * Role?Box?link???????409????.
     */
    @Test
    public final void Role?Box?link???????409????()
        {
            String cellName = Setup.TEST_CELL1;
            String boxName = "RoleBoxLinkTestBox";
            String roleName = "RoleBoxLinkTestRole";
            try {
                // Role?
                RoleUtils.create(cellName, TOKEN, roleName, HttpStatus.SC_CREATED);

                // Box?
                BoxUtils.create(cellName, boxName, TOKEN);

                // Role-Box $links
                RoleUtils.createLink(cellName, TOKEN, RoleUtils.keyString(roleName), "Box", "'" + boxName + "'",
                        HttpStatus.SC_NO_CONTENT);
                RoleUtils.createLink(cellName, TOKEN, RoleUtils.keyString(roleName, boxName), "Box",
                        "'" + boxName + "'", HttpStatus.SC_CONFLICT);
            } finally {
                // Role
                RoleUtils.delete(cellName, TOKEN, roleName, boxName, -1);

                // Box
                BoxUtils.delete(cellName, TOKEN, boxName, -1);

            }
        }

    /**
     * ???Box??Role?link???response?404????.
     */
    @Test
    public final void ???Box??Role?link???response?404????()
        {
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", "'boxx'").with("navProp", NAV_PROP_ROLE)
                    .with("uri", roleUri).returns().statusCode(HttpStatus.SC_NOT_FOUND);
        }

    /**
     * ???Role??Box?link???response?400????.
     */
    @Test
    public final void ???Role??Box?link???response?400????()
        {
            String noRoleUri = UrlUtils.cellCtlWithoutSingleQuote(CELL_NAME, ENTITY_SET_ROLE,
                    "Name='keyx',_Box.Name=" + KEY);
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE)
                    .with("uri", noRoleUri).returns().statusCode(HttpStatus.SC_BAD_REQUEST);
        }

    /**
     * ???EntitySet?Role?link?response?404????.
     */
    @Test
    public final void ???EntitySet?Role?link?response?404????()
        {
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME).with("entitySet", "test")
                    .with("key", KEY).with("navProp", NAV_PROP_ROLE).with("uri", roleUri).returns()
                    .statusCode(HttpStatus.SC_NOT_FOUND);
        }

    /**
     * Box????NavigationPropeties??link?response?400????.
     */
    @Test
    public final void Box????NavigationPropeties??link?response?400????()
        {
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", "_test").with("uri", roleUri)
                    .returns().statusCode(HttpStatus.SC_BAD_REQUEST);
        }

    /**
     * ???links????Box??Role???response?409????.
     */
    @Test
    public final void ???links????Box??Role???response?409????()
        {
            try {
                createLink();

                // ?????409?????
                Http.request("links-request-with-body.txt").with("method", "POST")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                        .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE)
                        .with("uri", UrlUtils.cellCtlWithoutSingleQuote(CELL_NAME, ENTITY_SET_ROLE, roleChangedKey))
                        .returns().statusCode(HttpStatus.SC_CONFLICT);
            } finally {
                deleteLink();
            }
        }

    /**
     * Box?Role?link?uri???????400????.
     */
    @Test
    public final void Box?Role?link?uri???????400????()
        {
            String targetUri = UrlUtils.cellRoot(Setup.TEST_CELL1)
                    + "__ctl/RoleName='confidentialClient',_Box.Name=null)";
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE)
                    .with("uri", targetUri).returns().statusCode(HttpStatus.SC_BAD_REQUEST);
        }

    /**
     * Box?Role?link?uri???????400????.
     */
    @Test
    public final void Box?Role?link?uri???????400????()
        {
            String targetUri = UrlUtils.cellRoot(Setup.TEST_CELL1)
                    + "__ctl/Role(Name='confidentialClient',_Box.Name=null";
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE)
                    .with("uri", targetUri).returns().statusCode(HttpStatus.SC_BAD_REQUEST);
        }

    /**
     * Box?Role?link?URL?NP?????????400????.
     */
    @Test
    public final void Box?Role?link?URL?NP?????????400????()
        {
            String targetUri = UrlUtils.cellRoot(Setup.TEST_CELL_SCHEMA1)
                    + "__ctl/Role(Name='confidentialClient',_Box.Name=null)";
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", Setup.TEST_CELL_SCHEMA1)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", "_Relation")
                    .with("uri", targetUri).returns().statusCode(HttpStatus.SC_BAD_REQUEST);
        }

    /**
     * Box?Id??Role?link??response?400????.
     */
    @Test
    public final void Box?Id??Role?link??response?400????()
        {
            String linkPath = "__ctl/" + ENTITY_SET_BOX + "\\('" + KEY + "'\\)/\\$links/" + NAV_PROP_ROLE;

            try {
                Http.request("links-request-with-body.txt").with("method", "POST")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                        .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE)
                        .with("uri", roleUri).returns().statusCode(HttpStatus.SC_NO_CONTENT);

                // Id??Role??$link
                Http.request("link-update-with-body.txt").with("cellPath", CELL_NAME).with("linkPath", linkPath)
                        .with("token", AbstractCase.MASTER_TOKEN_NAME)
                        .with("body", "\\{\\\"uri\\\":\\\"" + roleUri + "\\\"").returns()
                        .statusCode(HttpStatus.SC_BAD_REQUEST);
            } finally {
                deleteLink();
            }
        }

    /**
     * Box?Id?Role?link??response?501????.
     */
    @Test
    public final void Box?Id?Role?link??response?501????()
        {
            String linkPath = "__ctl/" + ENTITY_SET_BOX + "\\(" + KEY + "\\)/\\$links/" + NAV_PROP_ROLE + "\\("
                    + roleKey + "\\)";

            try {
                Http.request("links-request-with-body.txt").with("method", "POST")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                        .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE)
                        .with("uri", roleUri).returns().statusCode(HttpStatus.SC_NO_CONTENT);

                // Id?Role??$link
                Http.request("link-update-with-body.txt").with("cellPath", CELL_NAME).with("linkPath", linkPath)
                        .with("token", AbstractCase.MASTER_TOKEN_NAME)
                        .with("body", "\\{\\\"uri\\\":\\\"" + roleUri + "\\\"").returns()
                        .statusCode(HttpStatus.SC_NOT_IMPLEMENTED);
            } finally {
                deleteLink();
            }
        }

    /**
     * Box?Role?link?response?204????.
     */
    @Test
    public final void Box?Role?link?response?204????()
        {
            createLink();

            Http.request("links-request.txt").with("method", "DELETE").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_BOX).with("key", KEY)
                    .with("navProp", NAV_PROP_ROLE).with("navKey", roleChangedKey).returns()
                    .statusCode(HttpStatus.SC_NO_CONTENT);
        }

        /**
         * Role?Box?link?response?204????.
         */
        @Test
        public final void Role?Box?link?response?204????()
        {
            createLink();

            Http.request("links-request.txt").with("method", "DELETE").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_ROLE).with("key", roleChangedKey)
                    .with("navProp", NAV_PROP_BOX).with("navKey", KEY).returns().statusCode(HttpStatus.SC_NO_CONTENT);
        }

        /**
         * ???Box?Role?link?response?404????.
         */
        @Test
        public final void ???Box?Role?link?response?404????()
        {
            Http.request("links-request.txt").with("method", "DELETE").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_BOX).with("key", "'boxx'")
                    .with("navProp", NAV_PROP_ROLE).with("navKey", roleKey).returns()
                    .statusCode(HttpStatus.SC_NOT_FOUND);
        }

        /**
         * Box?Link?????Role?link?response?404????.
         */
        @Test
        public final void Box?Link?????Role?link?response?404????()
        {
            Http.request("links-request.txt").with("method", "DELETE").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_BOX).with("key", "'box2'")
                    .with("navProp", NAV_PROP_ROLE).with("navKey", roleKey).returns()
                    .statusCode(HttpStatus.SC_NOT_FOUND);
        }

        /**
         * Box????Role?link?response?404????.
         */
        @Test
        public final void Box????Role?link?response?404????()
        {
            Http.request("links-request.txt").with("method", "DELETE").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_BOX).with("key", KEY)
                    .with("navProp", NAV_PROP_ROLE).with("navKey", "'rolex'").returns()
                    .statusCode(HttpStatus.SC_NOT_FOUND);
        }

        /**
         * Box?Role?linkRole?NavPropKey??????response?400????.
         */
        @Test
        public final void Box?Role?linkRole?NavPropKey??????response?400????()
        {
            Http.request("links-request-no-navkey.txt").with("method", "DELETE")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE).returns()
                    .statusCode(HttpStatus.SC_BAD_REQUEST);
        }

        /**
         * Box?Role?link??response?200????.
         */
        @Test
        public final void Box?Role?link??response?200????()
        {
            String roleName = "role_BoxRoleLinkTest";
            String boxName = "box_BoxRoleLinkTest";

            TResponse resBox = null;
            TResponse resRole = null;
            try {
                // Box?
                resBox = BoxUtils.create(CELL_NAME, boxName, TOKEN);

                // Box????Role
                resRole = CellCtlUtils.createRole(CELL_NAME, roleName, boxName);

                // Box????Role?link?
                TResponse res = Http.request("links-request-no-navkey.txt").with("method", "GET").with("token", TOKEN)
                        .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_BOX)
                        .with("key", "Name='" + boxName + "'").with("navProp", NAV_PROP_ROLE).returns().debug()
                        .statusCode(HttpStatus.SC_OK);

                // ???
                ODataCommon.checkCommonResponseHeader(res);

                // ???
                ArrayList<String> uri = new ArrayList<String>();
                uri.add(resRole.getLocationHeader());
                ODataCommon.checkLinResponseBody(res.bodyAsJson(), uri);

            } finally {
                // Role
                CellCtlUtils.deleteOdataResource(resRole.getLocationHeader());

                // Box
                CellCtlUtils.deleteOdataResource(resBox.getLocationHeader());
            }
        }

        /**
         * Role?Box?link??response?200????.
         */
        @Test
        public final void Role?Box?link??response?200????()
        {
            try {
                createLink();

                TResponse response = Http.request("links-request-no-navkey.txt").with("method", "GET")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                        .with("entitySet", ENTITY_SET_ROLE).with("key", roleChangedKey).with("navProp", NAV_PROP_BOX)
                        .returns().statusCode(HttpStatus.SC_OK);
                String boxUri = UrlUtils.cellCtlWithoutSingleQuote(CELL_NAME, ENTITY_SET_BOX, KEY);
                checkResposeJson(response, boxUri);
            } finally {
                deleteLink();
            }
        }

        /**
         * Links??????Box?Role?link??response?200????.
         */
        @Test
        public final void Links??????Box?Role?link??response?200????()
        {
            Http.request("links-request-no-navkey.txt").with("method", "GET")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE).returns()
                    .statusCode(HttpStatus.SC_OK);
        }

        /**
         * ???Box?Role?link??response?404????.
         */
        @Test
        public final void ???Box?Role?link??response?404????()
        {
            Http.request("links-request-no-navkey.txt").with("method", "GET")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", "'boxx'").with("navProp", NAV_PROP_ROLE).returns()
                    .statusCode(HttpStatus.SC_NOT_FOUND);
        }

        /**
         * ???NavProp??link??response?400????.
         */
        @Test
        public final void ???NavProp??link??response?400????()
        {
            Http.request("links-request-no-navkey.txt").with("method", "GET")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", "_test").returns()
                    .statusCode(HttpStatus.SC_BAD_REQUEST);
        }

        /**
         * Role?Id??Box?link??response?400????.
         */
        @Test
        public final void Role?Id??Box?link??response?400????()
        {
            String linkPath = "__ctl/" + ENTITY_SET_ROLE + "\\('" + roleKey + "'\\)/\\$links/" + NAV_PROP_BOX;
            String boxUri = UrlUtils.cellCtlWithoutSingleQuote(CELL_NAME, ENTITY_SET_BOX, KEY);

            try {
                Http.request("links-request-with-body.txt").with("method", "POST")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                        .with("entitySet", ENTITY_SET_ROLE).with("key", roleKey).with("navProp", NAV_PROP_BOX)
                        .with("uri", boxUri).returns().statusCode(HttpStatus.SC_NO_CONTENT);

                // Id??Role??$link
                Http.request("link-update-with-body.txt").with("cellPath", CELL_NAME).with("linkPath", linkPath)
                        .with("token", AbstractCase.MASTER_TOKEN_NAME)
                        .with("body", "\\{\\\"uri\\\":\\\"" + roleUri + "\\\"").returns()
                        .statusCode(HttpStatus.SC_BAD_REQUEST);
            } finally {
                deleteLink();
            }
        }

        /**
         * Role?Id?Box?link??response?501????.
         */
        @Test
        public final void Role?Id?Box?link??response?501????()
        {
            String linkPath = "__ctl/" + ENTITY_SET_ROLE + "\\(" + roleKey + "\\)/\\$links/" + NAV_PROP_BOX + "\\("
                    + KEY + "\\)";
            String boxUri = UrlUtils.cellCtlWithoutSingleQuote(CELL_NAME, ENTITY_SET_BOX, KEY);

            try {
                Http.request("links-request-with-body.txt").with("method", "POST")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                        .with("entitySet", ENTITY_SET_ROLE).with("key", roleKey).with("navProp", NAV_PROP_BOX)
                        .with("uri", boxUri).returns().statusCode(HttpStatus.SC_NO_CONTENT);

                // Id?Role??$link
                Http.request("link-update-with-body.txt").with("cellPath", CELL_NAME).with("linkPath", linkPath)
                        .with("token", AbstractCase.MASTER_TOKEN_NAME)
                        .with("body", "\\{\\\"uri\\\":\\\"" + roleUri + "\\\"").returns()
                        .statusCode(HttpStatus.SC_NOT_IMPLEMENTED);
            } finally {
                deleteLink();
            }
        }

        /**
         * Role?Link???Box??response?409????.
         */
        @Test
        public final void Role?Link???Box??response?409????()
        {

            String boxName = "roleLinkBox";
            String roleName = "boxLinkRole";
            try {
                // Box??
                BoxUtils.create(CELL_NAME, boxName, TOKEN, HttpStatus.SC_CREATED);

                // ?Box??????Role?
                RoleUtils.create(CELL_NAME, TOKEN, roleName, boxName, HttpStatus.SC_CREATED);

                // Box?(??????????409)
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, HttpStatus.SC_CONFLICT);

                // ??????
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, boxName, HttpStatus.SC_NO_CONTENT);

                // Box?(??????????????204)
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, HttpStatus.SC_NO_CONTENT);
            } finally {
                // ??????
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, boxName, -1);

                // Box?
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, -1);
            }
        }

        /**
         * Relation?Link???Box??response?409????.
         */
        @SuppressWarnings("unchecked")
        @Test
        public final void Relation?Link???Box??response?409????()
        {

            String boxName = "relationLinkBox";
            String relationName = "boxLinkrelation";
            JSONObject body = new JSONObject();
            body.put("Name", relationName);
            body.put("_Box.Name", boxName);
            try {
                // Box??
                BoxUtils.create(CELL_NAME, boxName, TOKEN, HttpStatus.SC_CREATED);

                // ?Box??????Relation?
                RelationUtils.create(CELL_NAME, TOKEN, body, HttpStatus.SC_CREATED);

                // Box?(?????Relation?????409)
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, HttpStatus.SC_CONFLICT);

                // ?????Relation?
                RelationUtils.delete(CELL_NAME, TOKEN, relationName, boxName, HttpStatus.SC_NO_CONTENT);

                // Box?(?????Relation?????????204)
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, HttpStatus.SC_NO_CONTENT);
            } finally {
                // ?????Relation?
                RelationUtils.delete(CELL_NAME, TOKEN, relationName, boxName, -1);

                // Box?
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, -1);
            }
        }

        /**
         * Box?Role?Link??????Role???409????.
         */
        @Test
        public final void Box?Role?Link?? ????Role???409 ????()
        {
            final String boxName = "roleLinkBox";
            final String roleName = "boxLinkRole";
            try {
                // Box??
                BoxUtils.create(CELL_NAME, boxName, TOKEN, HttpStatus.SC_CREATED);

                // ?Box??????Role?
                RoleUtils.create(CELL_NAME, TOKEN, roleName, boxName, HttpStatus.SC_CREATED);

                // ?Box???????Role?
                RoleUtils.create(CELL_NAME, TOKEN, roleName, null, HttpStatus.SC_CREATED);

                String roleKeyName = "_Box.Name='" + boxName + "',Name='" + roleName + "'";

                // Box?Role?Link??Role?????????(409))
                deleteBoxRoleLink(boxName, roleKeyName, HttpStatus.SC_CONFLICT);
                // Role?Box?Link??????409
                deleteRoleBoxLink(roleKeyName, boxName, HttpStatus.SC_CONFLICT);

                // ?
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, null, HttpStatus.SC_NO_CONTENT);

                // Box?Role?Link??Role?????????)
                deleteBoxRoleLink(boxName, roleKeyName, HttpStatus.SC_NO_CONTENT);

                // ??????
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, null, HttpStatus.SC_NO_CONTENT);

                // Box?
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, HttpStatus.SC_NO_CONTENT);
            } finally {
                // ??????
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, boxName, -1);
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, null, -1);

                // Box?
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, -1);
            }
        }

        /**
         * Box?Role?Link??????Role???409????.
         */
        @Test
        public final void Box?Role?Link?? ????Role???409 ????()
        {
            final String boxName = "roleLinkBox";
            final String roleName = "boxLinkRole";
            try {
                // Box??
                BoxUtils.create(CELL_NAME, boxName, TOKEN, HttpStatus.SC_CREATED);

                // ?Box??????Role?
                RoleUtils.create(CELL_NAME, TOKEN, roleName, boxName, HttpStatus.SC_CREATED);

                // ?Box???????Role?
                RoleUtils.create(CELL_NAME, TOKEN, roleName, null, HttpStatus.SC_CREATED);

                String roleKeyName = "_Box.Name='" + boxName + "',Name='" + roleName + "'";

                // Box?Role?Link???Role??????????(409))
                createBoxRoleLink(boxName, roleName, HttpStatus.SC_CONFLICT);
                // Role?Box?Link???????409
                createRoleBoxLink(roleName, boxName, HttpStatus.SC_CONFLICT);

                // ?
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, null, HttpStatus.SC_NO_CONTENT);

                // Box?Role?Link
                deleteBoxRoleLink(boxName, roleKeyName, HttpStatus.SC_NO_CONTENT);

                // ??????
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, null, HttpStatus.SC_NO_CONTENT);

                // Box?
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, HttpStatus.SC_NO_CONTENT);
            } finally {
                // ??????
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, boxName, -1);
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, null, -1);

                // Box?
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, -1);
            }
        }

        /**
         * Box???????Roleexpand??????.
         */
        @Test
        public final void Box???????Roleexpand??????()
        {
            final String roleName = "boxLinkRole";
            try {
                // Role?
                RoleUtils.create(CELL_NAME, TOKEN, roleName, null, HttpStatus.SC_CREATED);

                // Box????Role?link?
                RoleUtils.list(TOKEN, CELL_NAME, "\\$expand=_Box&\\$filter=startswith(Name,'boxLinkRole')",
                        HttpStatus.SC_OK);
            } finally {
                // ?
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, null, -1);
            }
        }

        /**
         * Box?Rolelink??expand??????.
         */
        @Test
        public final void Box?Rolelink??expand??????()
        {
            final String boxName = "roleLinkBox";
            final String roleName = "boxLinkRole";
            try {
                // Box??
                BoxUtils.create(CELL_NAME, boxName, TOKEN, HttpStatus.SC_CREATED);

                // Collection??
                DavResourceUtils.createODataCollection(TOKEN, HttpStatus.SC_CREATED, CELL_NAME, boxName, "test");

                // ?Box??????Role?
                RoleUtils.create(CELL_NAME, TOKEN, roleName, boxName, HttpStatus.SC_CREATED);

                // ?Box??????Role?
                RoleUtils.create(CELL_NAME, TOKEN, "boxLinkRole2", boxName, HttpStatus.SC_CREATED);

                // Role????Box?link?
                RoleUtils.list(TOKEN, CELL_NAME, "\\$expand=_Box&\\$filter=startswith(Name,'boxLinkRole')",
                        HttpStatus.SC_OK);
            } finally {
                // ??????
                RoleUtils.delete(CELL_NAME, TOKEN, roleName, boxName, -1);
                RoleUtils.delete(CELL_NAME, TOKEN, "boxLinkRole2", boxName, -1);

                // Collection?
                DavResourceUtils.deleteCollection(CELL_NAME, boxName, "test", TOKEN, -1);

                // Box?
                BoxUtils.delete(CELL_NAME, TOKEN, boxName, -1);
            }
        }

        private void deleteBoxRoleLink(final String boxName, final String linkRoleKey, final int status) {
            Http.request("links-request.txt").with("method", "DELETE").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_BOX).with("key", "'" + boxName + "'")
                    .with("navProp", NAV_PROP_ROLE).with("navKey", linkRoleKey).returns().debug().statusCode(status);
        }

        private void deleteRoleBoxLink(final String roleName, final String linkBoxKey, final int status) {
            Http.request("links-request.txt").with("method", "DELETE").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_ROLE).with("key", roleName)
                    .with("navProp", NAV_PROP_BOX).with("navKey", "'" + linkBoxKey + "'").returns().debug()
                    .statusCode(status);
        }

        private void createBoxRoleLink(final String boxName, final String roleName, final int expectedStatus) {
            String url = UrlUtils.cellCtl(CELL_NAME, ENTITY_SET_ROLE, roleName);
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", "'" + boxName + "'").with("navProp", NAV_PROP_ROLE)
                    .with("uri", url).returns().debug().statusCode(expectedStatus);
        }

        private void createRoleBoxLink(final String roleName, final String boxName, final int expectedStatus) {
            String url = UrlUtils.cellCtl(CELL_NAME, ENTITY_SET_BOX, boxName);
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_ROLE).with("key", "'" + roleName + "'").with("navProp", NAV_PROP_BOX)
                    .with("uri", url).returns().debug().statusCode(expectedStatus);
        }

        /**
         * Link?.
         */
        private void createLink() {
            Http.request("links-request-with-body.txt").with("method", "POST")
                    .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", CELL_NAME)
                    .with("entitySet", ENTITY_SET_BOX).with("key", KEY).with("navProp", NAV_PROP_ROLE)
                    .with("uri", roleUri).returns().statusCode(HttpStatus.SC_NO_CONTENT);
        }

        /**
         * Link?.
         */
        private void deleteLink() {
            Http.request("links-request.txt").with("method", "DELETE").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", CELL_NAME).with("entitySet", ENTITY_SET_BOX).with("key", KEY)
                    .with("navProp", NAV_PROP_ROLE).with("navKey", roleChangedKey).returns().statusCode(-1);
        }

        // /**
        // * ??XML??.
        // * @param TResponse ?
        // */
        // private void checkResposeXml(TResponse response, String baseUri) {
        // String xml = response.getBody().replaceAll(
        // " xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices\"", "");
        // Link linksUri = JAXB.unmarshal(new StringReader(xml), Link.class);
        // List<String> uris = linksUri.getUri();
        // for (String uri : uris) {
        // assertEquals(baseUri, uri);
        // }
        //
        // }

        /**
         * ??JSON??.
         * @param TResponse ?
         */
        private void checkResposeJson(TResponse response, String baseUri) {
            JSONArray results = (JSONArray) ((JSONObject) response.bodyAsJson().get("d")).get("results");
            for (Object result : results) {
                assertEquals(baseUri, (String) ((JSONObject) result).get("uri"));
            }
        }
    }