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

Java tutorial

Introduction

Here is the source code for io.personium.test.jersey.cell.ctl.ExtCellLinkTest.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.io.UnsupportedEncodingException;
    import java.net.URLEncoder;

    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 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.PersoniumIntegTestRunner;
    import io.personium.test.jersey.ODataCommon;
    import io.personium.test.unit.core.UrlUtils;
    import io.personium.test.utils.ExtCellUtils;
    import io.personium.test.utils.Http;
    import io.personium.test.utils.TResponse;

    /**
     * Box?Relation?$links?.
     */
@RunWith(PersoniumIntegTestRunner.class)
@Category({Unit.class, Integration.class, Regression.class })
public class ExtCellLinkTest extends ODataCommon {

    private static final String CELL_NAME = "testcell1";
    private static final String EXTCELL_NAME = "cellHoge";
    private static final String ENTITY_SET_EXTCELL = "ExtCell";
    private static final String ENTITY_SET_ROLE = "Role";
    private static final String NAV_PROP_EXTCELL = "_ExtCell";
    private static final String NAV_PROP_ROLE = "_Role";
    private static final String TOKEN = AbstractCase.MASTER_TOKEN_NAME;
    private static String roleUri;
    private static String roleKey;

    /**
     * . ?super?????
     */
    public ExtCellLinkTest() {
        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 + "'";
            }
            roleUri = UrlUtils.cellCtlWithoutSingleQuote(CELL_NAME, ENTITY_SET_ROLE, roleKey);
        }
    }

    /**
     * ExtCell?Id??Role?link??response?400????.
     */
    @Test
    public final void ExtCell?Id??Role?link??response?400????() {
        String extCellUrl = UrlUtils.cellRoot(EXTCELL_NAME);
        String extCellUrlEncoded = null;
        try {
            extCellUrlEncoded = URLEncoder.encode(UrlUtils.cellRoot(EXTCELL_NAME), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            extCellUrlEncoded = extCellUrl;
        }
        String linkPath = "__ctl/" + ENTITY_SET_EXTCELL + "\\('" + extCellUrlEncoded + "'\\)/\\$links/" + NAV_PROP_ROLE;

        try {
            ExtCellUtils.create(TOKEN, CELL_NAME, extCellUrl, -1);

            // 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 {
            ExtCellUtils.delete(TOKEN, CELL_NAME, extCellUrl, -1);
        }
    }

    /**
     * ExtCell?Id?Role?link??response?400????.
     */
    @Test
    public final void ExtCell?Id?Role?link??response?400????() {
        String extCellUrl = UrlUtils.cellRoot(EXTCELL_NAME);
        String extCellUrlEncoded = null;
        try {
            extCellUrlEncoded = URLEncoder.encode(UrlUtils.cellRoot(EXTCELL_NAME), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            extCellUrlEncoded = extCellUrl;
        }
        String linkPath = "__ctl/" + ENTITY_SET_EXTCELL + "\\('" + extCellUrlEncoded + "'\\)/\\$links/"
                + NAV_PROP_ROLE + "\\(" + roleKey + "\\)";

        try {
            ExtCellUtils.create(TOKEN, CELL_NAME, extCellUrl, -1);

            // 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 {
            ExtCellUtils.delete(TOKEN, CELL_NAME, extCellUrl, -1);
        }
    }

    /**
     * Role?Id??ExtCell?link??response?400????.
     */
    @Test
    public final void Role?Id??ExtCell?link??response?400????() {
        String extCellUrl = UrlUtils.cellRoot(EXTCELL_NAME);
        String linkPath = "__ctl/" + ENTITY_SET_ROLE + "\\('" + roleKey + "'\\)/\\$links/" + NAV_PROP_EXTCELL;

        try {
            ExtCellUtils.create(TOKEN, CELL_NAME, extCellUrl, -1);

            Http.request("link-update-with-body.txt")
                    .with("cellPath", CELL_NAME)
                    .with("linkPath", linkPath)
                    .with("token", AbstractCase.MASTER_TOKEN_NAME)
                    .with("body", "\\{\\\"uri\\\":\\\"" + extCellUrl + "\\\"")
                    .returns()
                    .statusCode(HttpStatus.SC_BAD_REQUEST);
        } finally {
            ExtCellUtils.delete(TOKEN, CELL_NAME, extCellUrl, -1);
        }
    }

    /**
     * Role?Id?ExtCell?link??response?501????.
     */
    @Test
    public final void Role?Id?ExtCell?link??response?501????() {
        String extCellUrl = UrlUtils.cellRoot(EXTCELL_NAME);
        String extCellUrlEncoded = null;
        try {
            extCellUrlEncoded = URLEncoder.encode(UrlUtils.cellRoot(EXTCELL_NAME), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            extCellUrlEncoded = extCellUrl;
        }
        String linkPath = "__ctl/" + ENTITY_SET_ROLE + "\\(" + roleKey + "\\)/\\$links/"
                + NAV_PROP_EXTCELL + "\\('" + extCellUrlEncoded + "'\\)";

        try {
            ExtCellUtils.create(TOKEN, CELL_NAME, extCellUrl, -1);
            ExtCellUtils.get(TOKEN, CELL_NAME, extCellUrl, HttpStatus.SC_OK);

            // 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\\\":\\\"" + extCellUrl + "\\\"")
                    .returns()
                    .statusCode(HttpStatus.SC_NOT_IMPLEMENTED);
        } finally {
            ExtCellUtils.delete(TOKEN, CELL_NAME, extCellUrl, -1);
        }
    }

}