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

Java tutorial

Introduction

Here is the source code for io.personium.test.jersey.cell.ctl.CellCtlUtils.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 org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.json.simple.JSONObject;

import io.personium.common.utils.PersoniumCoreUtils;
import io.personium.test.jersey.AbstractCase;
import io.personium.test.jersey.PersoniumRequest;
import io.personium.test.utils.Http;
import io.personium.test.utils.TResponse;

/**
 * Cell??.
 */
public class CellCtlUtils {

    /**
     * .
     */
    private CellCtlUtils() {
    };

    /**
     * ??url???.
     * @param url URL
     */
    static void deleteOdataResource(String url) {
        PersoniumRequest req = PersoniumRequest.delete(url)
                .header(HttpHeaders.AUTHORIZATION, AbstractCase.BEARER_MASTER_TOKEN)
                .header(HttpHeaders.IF_MATCH, "*");
        AbstractCase.request(req);
    }

    /**
     * ???????Relation??.
     * @param cellName ??
     * @param testRelationName ??
     * @param boxname ??
     * @return ?
     */
    @SuppressWarnings("unchecked")
    public static TResponse createRelation(String cellName, String testRelationName, String boxname) {
        JSONObject body = new JSONObject();
        body.put("Name", testRelationName);
        body.put("_Box.Name", boxname);

        return Http.request("relation-create.txt").with("token", AbstractCase.BEARER_MASTER_TOKEN)
                .with("cellPath", cellName).with("body", body.toString()).returns()
                .statusCode(HttpStatus.SC_CREATED);
    }

    /**
     * ????????Relation??.
     * @param cellName ??
     * @param testRelationName ??
     * @return ?
     */
    @SuppressWarnings("unchecked")
    public static TResponse createRelation(String cellName, String testRelationName) {
        JSONObject body = new JSONObject();
        body.put("Name", testRelationName);
        body.put("_Box.Name", null);

        return Http.request("relation-create.txt").with("token", AbstractCase.BEARER_MASTER_TOKEN)
                .with("cellPath", cellName).with("body", body.toString()).returns()
                .statusCode(HttpStatus.SC_CREATED);
    }

    /**
     * ???????Relation?.
     * @param cellName ??
     * @param testRelationName ??
     * @param boxname ??
     */
    public static void deleteRelation(String cellName, String testRelationName, String boxname) {
        Http.request("relation-delete.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName)
                .with("relationname", testRelationName).with("boxname", "'" + boxname + "'").returns()
                .statusCode(HttpStatus.SC_NO_CONTENT);
    }

    /**
     * ????????Relation?.
     * @param cellName ??
     * @param testRelationName ??
     */
    public static void deleteRelation(String cellName, String testRelationName) {
        Http.request("relation-delete.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName)
                .with("relationname", testRelationName).with("boxname", "null").returns()
                .statusCode(HttpStatus.SC_NO_CONTENT);
    }

    /**
     * ?????????.
     * @param cellName ??
     * @param testRoleName ??
     * @param boxname ??
     * @return ?
     */
    @SuppressWarnings("unchecked")
    public static TResponse createRole(String cellName, String testRoleName, String boxname) {
        // Role?
        JSONObject body = new JSONObject();
        body.put("Name", testRoleName);
        body.put("_Box.Name", boxname);
        return Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName).with("body", body.toString()).returns()
                .statusCode(HttpStatus.SC_CREATED);
    }

    /**
     * ??????????.
     * @param cellName ??
     * @param testRoleName ??
     * @return ?
     */
    @SuppressWarnings("unchecked")
    public static TResponse createRole(String cellName, String testRoleName) {

        JSONObject body = new JSONObject();
        body.put("Name", testRoleName);
        body.put("_Box.Name", null);

        return Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName).with("body", body.toString()).returns()
                .statusCode(HttpStatus.SC_CREATED);
    }

    /**
     * ????????.
     * @param cellName ??
     * @param testRoleName ??
     * @param boxname ??
     */
    public static void deleteRole(String cellName, String testRoleName, String boxname) {
        Http.request("role-delete.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName)
                .with("rolename", testRoleName).with("boxname", "'" + boxname + "'").returns()
                .statusCode(HttpStatus.SC_NO_CONTENT);
    }

    /**
     * ?????????.
     * @param cellName ??
     * @param testRoleName ??
     */
    public static void deleteRole(String cellName, String testRoleName) {
        Http.request("role-delete.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName)
                .with("rolename", testRoleName).with("boxname", "null").returns()
                .statusCode(HttpStatus.SC_NO_CONTENT);
    }

    /**
     * ???????ExtRole??.
     * @param cellName ??
     * @param testExtRoleName ??
     * @param relationName ??
     * @param relationBoxName ??
     * @param relationNameEmpty _Relation.Name???
     * @param relationBoxNameEmpty _Relation._Box.Name???
     */
    @SuppressWarnings({ "unchecked", "unused" })
    public static void createExtRole(String cellName, String testExtRoleName, String relationName,
            String relationBoxName, boolean relationNameEmpty, boolean relationBoxNameEmpty) {
        JSONObject body = new JSONObject();
        body.put("ExtRole", testExtRoleName);
        if (!relationNameEmpty) {
            body.put("_Relation.Name", relationName);
        }
        if (!relationBoxNameEmpty) {
            body.put("_Relation._Box.Name", relationBoxName);
        }
        TResponse response = Http.request("cell/extRole/extRole-create.txt")
                .with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName)
                .with("body", body.toString()).returns().statusCode(HttpStatus.SC_CREATED);
    }

    /**
     * ???????ExtRole??.
     * @param cellName ??
     * @param testExtRoleName ??
     * @param relationName ??
     * @param relationBoxName ??
     */
    public static void createExtRole(String cellName, String testExtRoleName, String relationName,
            String relationBoxName) {
        createExtRole(cellName, testExtRoleName, relationName, relationBoxName, false, false);
    }

    /**
     * ???????ExtRole?.
     * @param cellName ??
     * @param testExtRoleName ??
     * @param relationName ??
     * @param relationBoxName ??
     */
    public static void deleteExtRole(String cellName, String testExtRoleName, String relationName,
            String relationBoxName) {

        String relName;
        if (relationName == null) {
            relName = "null";
        } else {
            relName = ("'" + relationName + "'");
        }
        String relBoxName;
        if (relationBoxName == null) {
            relBoxName = "null";
        } else {
            relBoxName = ("'" + relationBoxName + "'");
        }
        Http.request("cell/extRole/extRole-delete.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName).with("extRoleName", PersoniumCoreUtils.encodeUrlComp(testExtRoleName))
                .with("relationName", relName).with("relationBoxName", relBoxName).returns()
                .statusCode(HttpStatus.SC_NO_CONTENT);
    }

    /**
     * ???????ExtRole?.
     * @param cellName ??
     * @param testExtRoleName ??
     * @param relationName ??
     */
    public static void deleteExtRole(String cellName, String testExtRoleName, String relationName) {
        Http.request("cell/extRole/extRole-delete.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName).with("extRoleName", PersoniumCoreUtils.encodeUrlComp(testExtRoleName))
                .with("relationName", "'" + relationName + "'").with("relationBoxName", "null").returns()
                .statusCode(HttpStatus.SC_NO_CONTENT);
    }

    /**
     * ????????ExtRole?.
     * @param cellName ??
     * @param testExtRoleName ??
     */
    public static void deleteExtRole(String cellName, String testExtRoleName) {
        Http.request("cell/extRole/extRole-delete.txt").with("token", AbstractCase.MASTER_TOKEN_NAME)
                .with("cellPath", cellName).with("extRoleName", PersoniumCoreUtils.encodeUrlComp(testExtRoleName))
                .with("relationName", "null").with("relationBoxName", "null").returns()
                .statusCode(HttpStatus.SC_NO_CONTENT);
    }

    /**
     * ?????????.
     * @param value 
     * @return ??
     */
    public static String addSingleQuarto(String value) {
        if (value == null) {
            return "null";
        } else {
            return "'" + value + "'";
        }
    }
}