io.personium.test.jersey.box.odatacol.schema.property.PropertyUtils.java Source code

Java tutorial

Introduction

Here is the source code for io.personium.test.jersey.box.odatacol.schema.property.PropertyUtils.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.box.odatacol.schema.property;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.core.MediaType;

import org.apache.http.HttpHeaders;
import org.json.simple.JSONObject;

import io.personium.core.PersoniumUnitConfig;
import io.personium.core.auth.OAuth2Helper;
import io.personium.core.model.ctl.Common;
import io.personium.core.model.ctl.EntityType;
import io.personium.core.model.ctl.Property;
import io.personium.test.jersey.AbstractCase;
import io.personium.test.jersey.PersoniumRequest;
import io.personium.test.jersey.PersoniumResponse;
import io.personium.test.setup.Setup;
import io.personium.test.unit.core.UrlUtils;
import io.personium.test.utils.Http;
import io.personium.test.utils.TResponse;

/**
 * PropertyUtils.
 */
public class PropertyUtils {
    /**
     * .
     */
    private PropertyUtils() {
    }

    /** Property NameKey??. */
    public static final String PROPERTY_NAME_KEY = Property.P_NAME.getName().toString();

    /** Property _EntityTypeKey??. */
    public static final String PROPERTY_ENTITYTYPE_NAME_KEY = Property.P_ENTITYTYPE_NAME.getName().toString();

    /** Property TypeKey??. */
    public static final String PROPERTY_TYPE_KEY = Property.P_TYPE.getName().toString();

    /** Property NullableKey??. */
    public static final String PROPERTY_NULLABLE_KEY = Property.P_NULLABLE.getName().toString();

    /** Property DefaultValueKey??. */
    public static final String PROPERTY_DEFAULT_VALUE_KEY = Property.P_DEFAULT_VALUE.getName().toString();

    /** Property CollectionKindKey??. */
    public static final String PROPERTY_COLLECTION_KIND_KEY = Property.P_COLLECTION_KIND.getName().toString();

    /** Property IsKeyKey??. */
    public static final String PROPERTY_IS_KEY_KEY = Property.P_IS_KEY.getName().toString();

    /** Property UniqueKeyKey??. */
    public static final String PROPERTY_UNIQUE_KEY_KEY = Property.P_UNIQUE_KEY.getName().toString();

    /** Property isDeclared??. */
    public static final String PROPERTY_IS_DECLARED_KEY = Property.P_IS_DECLARED.getName().toString();

    /** PropertyURL. */
    public static final String REQUEST_URL = UrlUtils.property(Setup.TEST_CELL1, Setup.TEST_BOX1, Setup.TEST_ODATA,
            null, null);

    /** ???. */
    public static final String NAMESPACE = Common.EDM_NS_ODATA_SVC_SCHEMA + "." + Property.EDM_TYPE_NAME;

    /**
     * PropertyEntityType??NP??.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param entityTypeName EntityType??
     * @param propertyName Property??
     * @param type Property?Type
     * @param nullable Property?Nullable
     * @param defaultValue Property?DefaultValue
     * @param collectionKind Property?collectionKind
     * @param isKey Property?isKey
     * @param uniqueKey Property?UniqueKey
     * @param code ??
     * @return ?
     */
    @SuppressWarnings("unchecked")
    public static TResponse createViaPropertyNP(String token, String cell, String box, String collection,
            String entityTypeName, String propertyName, String type, Boolean nullable, String defaultValue,
            String collectionKind, Boolean isKey, String uniqueKey, int code) {

        // ???
        JSONObject body = new JSONObject();
        body.put("Name", propertyName);
        body.put("_EntityType.Name", entityTypeName);
        body.put("Type", type);
        body.put("Nullable", nullable);
        body.put("DefaultValue", defaultValue);
        body.put("CollectionKind", collectionKind);
        body.put("IsKey", isKey);
        body.put("UniqueKey", uniqueKey);

        TResponse res = Http.request("box/odatacol/schema/createViaNP.txt").with("cell", cell).with("box", box)
                .with("collection", collection).with("accept", MediaType.APPLICATION_JSON)
                .with("contentType", MediaType.APPLICATION_JSON).with("token", PersoniumUnitConfig.getMasterToken())
                .with("entityType", "EntityType").with("id", entityTypeName).with("navPropName", "_Property")
                .with("body", body.toJSONString()).returns().statusCode(code).debug();
        return res;
    }

    /**
     * PropertyEntityType??NP???.
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param entityTypeName EntityType??
     * @param code ??
     * @return ?
     */
    public static TResponse listViaPropertyNP(String cell, String box, String collection, String entityTypeName,
            int code) {

        TResponse res = Http.request("box/odatacol/schema/listViaNP.txt").with("cell", cell).with("box", box)
                .with("collection", collection).with("accept", MediaType.APPLICATION_JSON)
                .with("token", AbstractCase.MASTER_TOKEN_NAME).with("entityType", "EntityType")
                .with("id", entityTypeName).with("navPropName", "_Property").returns().statusCode(code).debug();
        return res;
    }

    /**
     * Property?.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param entityTypeName EntityType??
     * @param propertyName Property??
     * @param type Property?Type
     * @param nullable Property?Nullable
     * @param defaultValue Property?DefaultValue
     * @param collectionKind Property?collectionKind
     * @param isKey Property?isKey
     * @param uniqueKey Property?UniqueKey
     * @param code ??
     * @return ?
     */
    public static PersoniumResponse create(String token, String cell, String box, String collection,
            String entityTypeName, String propertyName, String type, boolean nullable, Object defaultValue,
            String collectionKind, boolean isKey, String uniqueKey, int code) {

        String url = UrlUtils.property(cell, box, collection, null, null);
        PersoniumRequest req = PersoniumRequest.post(url);
        req.header(HttpHeaders.AUTHORIZATION, token);
        req.addJsonBody(PROPERTY_NAME_KEY, propertyName);
        req.addJsonBody(PROPERTY_ENTITYTYPE_NAME_KEY, entityTypeName);
        req.addJsonBody(PROPERTY_TYPE_KEY, type);
        req.addJsonBody(PROPERTY_NULLABLE_KEY, nullable);
        req.addJsonBody(PROPERTY_DEFAULT_VALUE_KEY, defaultValue);
        req.addJsonBody(PROPERTY_COLLECTION_KIND_KEY, collectionKind);
        req.addJsonBody(PROPERTY_IS_KEY_KEY, isKey);
        req.addJsonBody(PROPERTY_UNIQUE_KEY_KEY, uniqueKey);

        // 
        PersoniumResponse response = AbstractCase.request(req);
        if (code != -1) {
            assertEquals(code, response.getStatusCode());
        }
        return response;
    }

    /**
     * Property?Location URL??.
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param propertyName Property??
     * @param entityTypeName EntityType??
     * @return Location URL
     */
    public static String composeLocationUrl(String cell, String box, String collection, String propertyName,
            String entityTypeName) {
        return UrlUtils.property(cell, box, collection, propertyName, entityTypeName);
    }

    /**
     * Property??.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param propertyName Property??
     * @param entityTypeName EntityType??
     * @return ?
     */
    public static PersoniumResponse get(String token, String cell, String box, String collection,
            String propertyName, String entityTypeName) {
        String locationUrl = UrlUtils.property(cell, box, collection, propertyName, entityTypeName);

        // Property?
        PersoniumRequest req = PersoniumRequest.get(locationUrl);
        req.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
        req.header(HttpHeaders.AUTHORIZATION, OAuth2Helper.Scheme.BEARER + " " + token);
        return AbstractCase.request(req);
    }

    /**
     * Property??.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @return ?
     */
    public static PersoniumResponse list(String token, String cell, String box, String collection) {
        return list(token, cell, box, collection, null);
    }

    /**
     * Property??.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param query 
     * @return ?
     */
    public static PersoniumResponse list(String token, String cell, String box, String collection, String query) {
        String locationUrl = UrlUtils.property(cell, box, collection, null, null);
        if (null != query) {
            locationUrl += query;
        }

        // Property?
        PersoniumRequest req = PersoniumRequest.get(locationUrl);
        req.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
        req.header(HttpHeaders.AUTHORIZATION, OAuth2Helper.Scheme.BEARER + " " + token);
        return AbstractCase.request(req);
    }

    /**
     * Property?.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param srcPropertyName ?Property??
     * @param srcEntityTypeName ?EntityType??
     * @param propertyName ??Property??
     * @param entityTypeName ??EntityType??
     * @param type Property?Type
     * @param nullable Property?Nullable
     * @param defaultValue Property?DefaultValue
     * @param collectionKind Property?collectionKind
     * @param isKey Property?isKey
     * @param uniqueKey Property?UniqueKey
     * @return ?
     */
    @SuppressWarnings("unchecked")
    public static PersoniumResponse update(String token, String cell, String box, String collection,
            String srcPropertyName, String srcEntityTypeName, String propertyName, String entityTypeName,
            String type, Boolean nullable, Object defaultValue, String collectionKind, Boolean isKey,
            String uniqueKey) {

        // ???
        JSONObject body = new JSONObject();
        body.put("Name", propertyName);
        body.put("_EntityType.Name", entityTypeName);
        body.put("Type", type);
        body.put("Nullable", nullable);
        body.put("DefaultValue", defaultValue);
        body.put("CollectionKind", collectionKind);
        body.put("IsKey", isKey);
        body.put("UniqueKey", uniqueKey);

        return update(token, cell, box, collection, srcPropertyName, srcEntityTypeName, body);

    }

    /**
     * Property?.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param srcPropertyName ?Property??
     * @param srcEntityTypeName ?EntityType??
     * @param body 
     * @return ?
     */
    public static PersoniumResponse update(String token, String cell, String box, String collection,
            String srcPropertyName, String srcEntityTypeName, JSONObject body) {

        // 
        PersoniumRequest req = PersoniumRequest
                .put(UrlUtils.property(cell, box, collection, srcPropertyName, srcEntityTypeName));
        req.header(HttpHeaders.AUTHORIZATION, OAuth2Helper.Scheme.BEARER + " " + token);
        req.header(HttpHeaders.IF_MATCH, "*");
        req.addStringBody(body.toJSONString());

        // 
        return AbstractCase.request(req);

    }

    /**
     * Property?.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param entityTypeName EntityType??
     * @param propertyName Property??
     * @param code ??
     * @return ?
     */
    public static PersoniumResponse delete(String token, String cell, String box, String collection,
            String entityTypeName, String propertyName, int code) {

        String url = UrlUtils.property(cell, box, collection, propertyName, entityTypeName);
        PersoniumRequest req = PersoniumRequest.delete(url);
        req.header(HttpHeaders.AUTHORIZATION, token);

        // 
        PersoniumResponse response = AbstractCase.request(req);
        if (code != -1) {
            assertEquals(code, response.getStatusCode());
        }
        return response;
    }

    /**
     * EntityType??NP?Property??.
     * @param token 
     * @param cell ??
     * @param box ??
     * @param collection ??
     * @param entityTypeName EntityType??
     * @param code ??
     * @return ?
     */
    public static TResponse listLinks(String token, String cell, String box, String collection,
            String entityTypeName, int code) {

        String path = String.format("\\$metadata/%s('%s')", EntityType.EDM_TYPE_NAME, entityTypeName);
        return Http.request("box/odatacol/list-link.txt").with("cellPath", cell).with("boxPath", box)
                .with("colPath", collection).with("srcPath", path).with("trgPath", Property.EDM_TYPE_NAME)
                .with("token", token).with("accept", MediaType.APPLICATION_JSON).returns().statusCode(code).debug();
    }
}