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

Java tutorial

Introduction

Here is the source code for io.personium.test.jersey.cell.ctl.ExtRoleCreateTest.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 java.util.HashMap;
    import java.util.Map;

    import org.apache.http.HttpStatus;
    import org.json.simple.JSONObject;
    import org.junit.Test;
    import org.junit.experimental.categories.Category;

    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.setup.Setup;
    import io.personium.test.unit.core.UrlUtils;
    import io.personium.test.utils.ExtRoleUtils;
    import io.personium.test.utils.Http;
    import io.personium.test.utils.TResponse;

    /**
     * ExtRole??.
     */
    @Category({ Unit.class, Integration.class, Regression.class })
    public class ExtRoleCreateTest extends ODataCommon {

        private static String cellName = "testcell1";
        private static final String EXTROLE_PATH_DEFAULT = UrlUtils.roleResource(cellName, "__", "testrole");
        private static final String EXT_ROLE_TYPE = "CellCtl.ExtRole";

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

        /**
         * ExtRole?? ExtRole, _Relation.Name, _Relation._Box.Name?.
         */
        @Test
        public void ExtRole???????????()
        {
            String relationName = "testrelation";
            String relationBoxName = "box1";
            try {
                CellCtlUtils.createRelation(cellName, relationName, relationBoxName);
                createExtRole(relationName, relationBoxName);
            } finally {
                CellCtlUtils.deleteExtRole(cellName, EXTROLE_PATH_DEFAULT, relationName, relationBoxName);
                CellCtlUtils.deleteRelation(cellName, relationName, relationBoxName);
            }
        }

        /**
         * ExtRole?? _Relation.Name, _Relation._Box.Name?null.
         */
        @Test
        public void ExtRole????????????()
        {
            String relationName = "testrelation";
            try {
                CellCtlUtils.createRelation(cellName, relationName);
                createExtRole(relationName, null);
            } finally {
                CellCtlUtils.deleteExtRole(cellName, EXTROLE_PATH_DEFAULT, relationName);
                CellCtlUtils.deleteRelation(cellName, relationName);
            }
        }

        /**
         * ExtRole??null???400????.
         */
        @Test
        public void ExtRole??null???400????()
        {
            createExtRole(false, false);
        }

        /**
         * ExtRole?????400????.
         */
        @Test
        public void ExtRole?????400????()
        {
            createExtRole(true, true);
        }

        /**
         * ExtRole?????400????.
         */
        @Test
        public void ExtRole?????400????()
        {
            TResponse res = createExtRole(EXTROLE_PATH_DEFAULT, "", null);
            res.statusCode(HttpStatus.SC_BAD_REQUEST);
        }

        /**
         * ExtRole???????400????.
         */
        @Test
        public void ExtRole???????400????()
        {
            TResponse res = createExtRole(EXTROLE_PATH_DEFAULT, "", null);
            res.statusCode(HttpStatus.SC_BAD_REQUEST);
        }

        /**
         * ExtRole???????????.
         */
        @Test
        public void ExtRole???????????()
        {
            TResponse res = createExtRole(EXTROLE_PATH_DEFAULT, "", null);
            res.statusCode(HttpStatus.SC_BAD_REQUEST);
        }

        /**
         * ExtRole???400????.
         */
        @Test
        public final void ExtRole???400????()
        {
        String extRolePath = "";
        String relationName = "testrelation";
        try {
            CellCtlUtils.createRelation(cellName, relationName);
            TResponse res = createExtRole(extRolePath, relationName, null);
            res.statusCode(HttpStatus.SC_BAD_REQUEST);
        } finally {
            CellCtlUtils.deleteRelation(cellName, relationName);
        }
    }

    /**
     * ExtRole?????400????.
     */
    @Test
    @SuppressWarnings("unchecked")
    public final void ExtRole?????400????()
        {
            String relationName = "testrelation";
            try {
                CellCtlUtils.createRelation(cellName, relationName);

                JSONObject body = new JSONObject();
                body.put("_Relation.Name", relationName);
                body.put("_Relation._Box.Name", null);
                TResponse res = Http.request("cell/extRole/extRole-create.txt")
                        .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName)
                        .with("body", body.toString()).returns().debug();
                res.statusCode(HttpStatus.SC_BAD_REQUEST);
            } finally {
                CellCtlUtils.deleteRelation(cellName, relationName);
            }
        }

    /**
     * ExtRole?null??400????.
     */
    @Test
    public final void ExtRole?null??400????()
        {
            String extRolePath = null;
            TResponse res = createExtRole(extRolePath, null, null);
            res.statusCode(HttpStatus.SC_BAD_REQUEST);
        }

    /**
     * ExtRole?URL?????400????.
     */
    @Test
    public final void ExtRole?URL?????400????()
        {
            String extRolePath = "testExtRole";
            String relationName = "testrelation";
            try {
                CellCtlUtils.createRelation(cellName, relationName);
                TResponse res = createExtRole(extRolePath, relationName, null);
                res.statusCode(HttpStatus.SC_BAD_REQUEST);
            } finally {
                CellCtlUtils.deleteRelation(cellName, relationName);
            }
        }

    /**
     * ExtRole?1024???????.
     */
    @Test
    public final void ExtRole?1024???????()
        {
            String extRolePath = "http://localhost:8080/personium-core/testextRole"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax";
            String relationName = "testrelation";
            try {
                CellCtlUtils.createRelation(cellName, relationName);
                TResponse res = createExtRole(extRolePath, relationName, null);
                res.statusCode(HttpStatus.SC_CREATED);
            } finally {
                CellCtlUtils.deleteExtRole(cellName, extRolePath, relationName);
                CellCtlUtils.deleteRelation(cellName, relationName);
            }
        }

        /**
         * ExtRole?1025???????.
         */
        @Test
        public final void ExtRole?1025????????()
        {
            String extRolePath = "http://localhost:8080/personium-core/testextRole"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
                    + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax";
            String relationName = "testrelation";
            try {
                CellCtlUtils.createRelation(cellName, relationName);
                TResponse res = createExtRole(extRolePath, relationName, null);
                res.statusCode(HttpStatus.SC_BAD_REQUEST);
            } finally {
                CellCtlUtils.deleteRelation(cellName, relationName);
            }
        }

    /**
     * ExtRole?1025???????.
     */
    @Test
    public final void ExtRole?scheme????400????()
        {
            String extRolePath = "ftp://localhost:21/personium-core/testextRole";
            String relationName = "testrelation";
            try {
                CellCtlUtils.createRelation(cellName, relationName);
                TResponse res = createExtRole(extRolePath, relationName, null);
                res.statusCode(HttpStatus.SC_BAD_REQUEST);
            } finally {
                CellCtlUtils.deleteRelation(cellName, relationName);
            }
        }

    /**
     * ExtRole??_??__published???400???.
     */
    @Test
    public void ExtRole??_??__published???400???()
        {
            String relationName = "testrelation";
            String relationBoxName = "box1";
            try {
                CellCtlUtils.createRelation(cellName, relationName, relationBoxName);
                errCreateExtRole(relationName, relationBoxName, PUBLISHED, "/Date(0)/", HttpStatus.SC_BAD_REQUEST);
            } finally {
                CellCtlUtils.deleteRelation(cellName, relationName, relationBoxName);
            }
        }

    /**
     * ExtRole??_??__updated???400???.
     */
    @Test
    public void ExtRole??_??__updated???400???()
        {
            String relationName = "testrelation";
            String relationBoxName = "box1";
            try {
                CellCtlUtils.createRelation(cellName, relationName, relationBoxName);
                errCreateExtRole(relationName, relationBoxName, UPDATED, "/Date(0)/", HttpStatus.SC_BAD_REQUEST);
            } finally {
                CellCtlUtils.deleteRelation(cellName, relationName, relationBoxName);
            }
        }

    /**
     * ExtRole??_??__metadata???400???.
     */
    @Test
    public void ExtRole??_??__metadata???400???()
        {
            String relationName = "testrelation";
            String relationBoxName = "box1";
            try {
                CellCtlUtils.createRelation(cellName, relationName, relationBoxName);
                errCreateExtRole(relationName, relationBoxName, METADATA, null, HttpStatus.SC_BAD_REQUEST);
            } finally {
                CellCtlUtils.deleteRelation(cellName, relationName, relationBoxName);
            }
        }

    /**
     * Relation?NP?ExtRole????201????.
     */
    @Test
    public void Relation?NP?ExtRole????201????()
        {
            String relationName = "testrelation";
            String extRoleUrl = "http://fqdn/cell/__role/__/testrole";
            try {
                CellCtlUtils.createRelation(cellName, relationName);
                ExtRoleUtils.createViaNP(Setup.TEST_CELL1, Setup.MASTER_TOKEN_NAME, relationName, null, extRoleUrl,
                        HttpStatus.SC_CREATED);
            } finally {
                CellCtlUtils.deleteExtRole(cellName, extRoleUrl, relationName);
                CellCtlUtils.deleteRelation(cellName, relationName);
            }
        }

        /**
         * ???????ExtRole??.
         * @param relationName ??
         * @param relationBoxName ??
         */
        private void createExtRole(String relationName, String relationBoxName) {
            TResponse response = createExtRole(EXTROLE_PATH_DEFAULT, relationName, relationBoxName);

            response.statusCode(HttpStatus.SC_CREATED);

            // ???
            String location = UrlUtils.cellCtlWithoutSingleQuote("testcell1", "ExtRole",
                    "ExtRole=" + CellCtlUtils.addSingleQuarto(EXTROLE_PATH_DEFAULT) + ",_Relation.Name="
                            + CellCtlUtils.addSingleQuarto(relationName) + ",_Relation._Box.Name="
                            + CellCtlUtils.addSingleQuarto(relationBoxName));

            ODataCommon.checkCommonResponseHeader(response, location);

            // ???
            Map<String, Object> additional = new HashMap<String, Object>();
            additional.put("ExtRole", EXTROLE_PATH_DEFAULT);
            additional.put("_Relation.Name", relationName);
            additional.put("_Relation._Box.Name", relationBoxName);
            ODataCommon.checkResponseBody(response.bodyAsJson(), location, EXT_ROLE_TYPE, additional);
        }

        /**
         * ???????ExtRole??.
         * @param extRolePath ExtRole?
         * @param relationName ??
         * @param relationBoxName ??
         */
        @SuppressWarnings("unchecked")
        private TResponse createExtRole(String extRolePath, String relationName, String relationBoxName) {
            JSONObject body = new JSONObject();
            body.put("ExtRole", extRolePath);
            body.put("_Relation.Name", relationName);
            body.put("_Relation._Box.Name", relationBoxName);
            return Http.request("cell/extRole/extRole-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", cellName).with("body", body.toString()).returns().debug();
        }

        /**
         * ???????ExtRole??().
         * @param relationName ??
         * @param relationBoxName ??
         * @param errKey 
         * @param errValue 
         * @param ?
         */
        @SuppressWarnings("unchecked")
        private void errCreateExtRole(String relationName, String relationBoxName, String errKey, String errValue,
                int errSC) {
            JSONObject body = new JSONObject();
            body.put("ExtRole", EXTROLE_PATH_DEFAULT);
            body.put("_Relation.Name", relationName);
            body.put("_Relation._Box.Name", relationBoxName);
            body.put(errKey, errValue);

            Http.request("cell/extRole/extRole-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", cellName).with("body", body.toString()).returns().statusCode(errSC);
        }

        /**
         * ????????ExtRole??.
         * @param relationNameEmpty _Relation.Name???
         * @param relationBoxNameEmpty _Relation._Box.Name???
         */
        @SuppressWarnings("unchecked")
        private void createExtRole(boolean relationNameEmpty, boolean relationBoxNameEmpty) {
            JSONObject body = new JSONObject();
            body.put("ExtRole", EXTROLE_PATH_DEFAULT);
            if (!relationNameEmpty) {
                body.put("_Relation.Name", null);
            }
            if (!relationBoxNameEmpty) {
                body.put("_Relation._Box.Name", null);
            }
            Http.request("cell/extRole/extRole-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("cellPath", cellName).with("body", body.toString()).returns()
                    .statusCode(HttpStatus.SC_BAD_REQUEST);
        }

    }