Java tutorial
/** * 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; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.ws.rs.core.MediaType; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.odata4j.edm.EdmSimpleType; import io.personium.core.PersoniumUnitConfig; import io.personium.test.jersey.AbstractCase; import io.personium.test.jersey.PersoniumRequest; import io.personium.test.jersey.PersoniumResponse; import io.personium.test.jersey.PersoniumRestAdapter; import io.personium.test.jersey.ODataCommon; 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; import io.personium.test.utils.UserDataUtils; import com.sun.jersey.test.framework.WebAppDescriptor; /** * UserData?. */ public abstract class AbstractUserDataTest extends AbstractCase { String cellName = "testcell1"; String boxName = "box1"; String colName = "setodata"; String entityTypeName = "Category"; String navPropName = null; /** * . */ public AbstractUserDataTest() { super("io.personium.core.rs"); } /** * . * @param build WebAppDescriptor */ public AbstractUserDataTest(WebAppDescriptor build) { super(build); } /** * ??. * @param body * @param sc ? * @return ? */ protected TResponse createUserData(JSONObject body, int sc) { TResponse response = Http.request("box/odatacol/create.txt").with("cell", cellName).with("box", boxName) .with("collection", colName).with("entityType", entityTypeName) .with("accept", MediaType.APPLICATION_JSON).with("contentType", MediaType.APPLICATION_JSON) .with("token", "Bearer " + PersoniumUnitConfig.getMasterToken()).with("body", body.toJSONString()) .returns().statusCode(sc).debug(); return response; } /** * ??. * @param userdataId1 1???ID * @param userdataId2 2???ID */ @SuppressWarnings("unchecked") public void createUserDataList(String userdataId1, String userdataId2) { // JSONObject body = new JSONObject(); body.put("__id", userdataId1); body.put("dynamicProperty1", "dynamicPropertyValue1"); body.put("dynamicProperty2", "dynamicPropertyValue2"); body.put("dynamicProperty3", "dynamicPropertyValue3"); JSONObject body2 = new JSONObject(); body2.put("__id", userdataId2); body2.put("dynamicProperty1", "dynamicPropertyValueA"); body2.put("dynamicProperty2", "dynamicPropertyValueB"); body2.put("dynamicProperty3", "dynamicPropertyValueC"); // ? createUserData(body, HttpStatus.SC_CREATED); createUserData(body2, HttpStatus.SC_CREATED); } /** * ??(Etag?). * @param userdataId1 1???ID * @param userdataId2 2???ID * @param etag etag */ @SuppressWarnings("unchecked") public void createUserDataList(String userdataId1, String userdataId2, Map<String, String> etag) { // JSONObject body = new JSONObject(); body.put("__id", userdataId1); body.put("dynamicProperty1", "dynamicPropertyValue1"); body.put("dynamicProperty2", "dynamicPropertyValue2"); body.put("dynamicProperty3", "dynamicPropertyValue3"); JSONObject body2 = new JSONObject(); body2.put("__id", userdataId2); body2.put("dynamicProperty1", "dynamicPropertyValueA"); body2.put("dynamicProperty2", "dynamicPropertyValueB"); body2.put("dynamicProperty3", "dynamicPropertyValueC"); // ? TResponse response = createUserData(body, HttpStatus.SC_CREATED); // Etag? etag.put(userdataId1, response.getHeader(HttpHeaders.ETAG)); // ? response = createUserData(body2, HttpStatus.SC_CREATED); // Etag? etag.put(userdataId2, response.getHeader(HttpHeaders.ETAG)); } /** * ??. * @param body * @param sc ? * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @return ? */ protected TResponse createUserData(JSONObject body, int sc, String cell, String box, String col, String entityType) { TResponse response = Http.request("box/odatacol/create.txt").with("cell", cell).with("box", box) .with("collection", col).with("entityType", entityType).with("accept", MediaType.APPLICATION_JSON) .with("contentType", MediaType.APPLICATION_JSON) .with("token", "Bearer " + PersoniumUnitConfig.getMasterToken()).with("body", body.toJSONString()) .returns().statusCode(sc).debug(); return response; } /** * ??. * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @return ? */ protected TResponse getUserDataList(String cell, String box, String col, String entityType) { TResponse response = Http.request("box/odatacol/list.txt").with("cell", cell).with("box", box) .with("collection", col).with("entityType", entityType).with("query", "") .with("accept", MediaType.APPLICATION_JSON).with("token", PersoniumUnitConfig.getMasterToken()) .returns().statusCode(HttpStatus.SC_OK).debug(); return response; } /** * ??. * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @param query * @return ? */ protected TResponse getUserDataList(String cell, String box, String col, String entityType, String query) { TResponse response = Http.request("box/odatacol/list.txt").with("cell", cell).with("box", box) .with("collection", col).with("entityType", entityType).with("query", query) .with("accept", MediaType.APPLICATION_JSON).with("token", PersoniumUnitConfig.getMasterToken()) .returns().statusCode(HttpStatus.SC_OK).debug(); return response; } /** * ??. * @param cell ?? * @param box ?? * @param col ?? * @param targetEntityTypeName ?? * @param query * @return ??? */ protected static PersoniumResponse getUserDataWithDcClient(String cell, String box, String col, String targetEntityTypeName, String query) { PersoniumRestAdapter rest = new PersoniumRestAdapter(); PersoniumResponse res = null; // HashMap<String, String> requestheaders = new HashMap<String, String>(); requestheaders.put(HttpHeaders.AUTHORIZATION, "Bearer " + PersoniumUnitConfig.getMasterToken()); requestheaders.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); // ? try { res = rest.getAcceptEncodingGzip(UrlUtils.userData(cell, box, col, targetEntityTypeName) + query, requestheaders); } catch (Exception e) { fail(e.getMessage()); } return res; } /** * ??. * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @param userDataId ID * @param token ? * @param sc ? * @return ? */ protected TResponse getUserData(String cell, String box, String col, String entityType, String userDataId, String token, int sc) { return getUserData(cell, box, col, entityType, userDataId, token, "", sc); } /** * ??. * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @param userDataId ID * @param token ? * @param query * @param sc ? * @return ? */ protected TResponse getUserData(String cell, String box, String col, String entityType, String userDataId, String token, String query, int sc) { TResponse response = Http.request("box/odatacol/get.txt").with("cell", cell).with("box", box) .with("collection", col).with("entityType", entityType).with("id", userDataId).with("token", token) .with("accept", MediaType.APPLICATION_JSON).with("query", query).returns().statusCode(sc).debug(); return response; } /** * ?. * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @param userDataId ID * @param body * @return ? */ protected TResponse updateUserData(String cell, String box, String col, String entityType, String userDataId, JSONObject body) { return Http.request("box/odatacol/update.txt").with("cell", cell).with("box", box).with("collection", col) .with("entityType", entityType).with("id", userDataId).with("accept", MediaType.APPLICATION_JSON) .with("contentType", MediaType.APPLICATION_JSON).with("ifMatch", "*") .with("token", PersoniumUnitConfig.getMasterToken()).with("body", body.toJSONString()).returns() .statusCode(HttpStatus.SC_NO_CONTENT).debug(); } /** * ??. * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @param body * @return ??? */ protected PersoniumResponse createUserDataWithDcClient(String cell, String box, String col, String entityType, JSONObject body) { PersoniumRestAdapter rest = new PersoniumRestAdapter(); PersoniumResponse res = null; // HashMap<String, String> requestheaders = new HashMap<String, String>(); requestheaders.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); // ? try { res = rest.post(UrlUtils.userData(cell, box, col, entityType), body.toJSONString(), requestheaders); } catch (Exception e) { fail(e.getMessage()); } return res; } /** * NP???. * @param id ID * @param body * @param sc ? * @return ? */ protected TResponse createUserDataWithNP(String id, JSONObject body, int sc) { TResponse response = Http.request("box/odatacol/createNP.txt").with("cell", cellName).with("box", boxName) .with("collection", colName).with("entityType", entityTypeName).with("id", id) .with("navPropName", "_" + navPropName).with("accept", MediaType.APPLICATION_JSON) .with("contentType", MediaType.APPLICATION_JSON).with("token", PersoniumUnitConfig.getMasterToken()) .with("body", body.toJSONString()).returns().statusCode(sc).debug(); return response; } /** * ComplexTypeNP???. * @param id ID * @param reqBody * @return ? */ protected PersoniumResponse createComplexTypeUserDataWithNP(String id, HashMap<String, Object> reqBody) { // UserData? String requestUrl = UrlUtils.userdataNP(Setup.TEST_CELL1, Setup.TEST_BOX1, UserDataListWithNPTest.ODATA_COLLECTION, entityTypeName, id, navPropName); PersoniumRequest req = PersoniumRequest.post(requestUrl); req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); for (String key : reqBody.keySet()) { req.addJsonBody(key, reqBody.get(key)); } // return request(req); } /** * ?. * @param userDataId ID */ protected void deleteUserData(String userDataId) { // Http.request("box/odatacol/delete.txt").with("cell", cellName).with("box", boxName) .with("collection", colName).with("entityType", entityTypeName).with("id", userDataId) .with("token", PersoniumUnitConfig.getMasterToken()).with("ifMatch", "*").returns().statusCode(-1); } /** * ?. * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @param userDataId ID * @param token ? * @param sc ? */ protected void deleteUserData(String cell, String box, String col, String entityType, String userDataId, String token, int sc) { // deleteUserData(cell, box, col, entityType, userDataId, token, "*", sc); } /** * ?. * @param cell ?? * @param box ?? * @param col ?? * @param entityType ?? * @param userDataId ID * @param token ? * @param ifMatch ifMatch * @param sc ? */ protected void deleteUserData(String cell, String box, String col, String entityType, String userDataId, String token, String ifMatch, int sc) { // Http.request("box/odatacol/delete.txt").with("cell", cell).with("box", box).with("collection", col) .with("entityType", entityType).with("id", userDataId).with("token", token).with("ifMatch", ifMatch) .returns().statusCode(sc); } /** * ?. * @param id1 1???ID * @param id2 2???ID */ public void deleteUserDataList(String id1, String id2) { deleteUserData(id1); deleteUserData(id2); } /** * ??. * @param userDataId ID * @param navPropId ?NavigationProperty?ID */ protected void deleteUserDataLinks(String userDataId, String navPropId) { // Http.request("box/odatacol/delete-link.txt").with("cell", cellName).with("box", boxName) .with("collection", colName).with("entityType", entityTypeName).with("id", userDataId) .with("navProp", "_" + navPropName).with("navKey", navPropId) .with("contentType", MediaType.APPLICATION_JSON).with("token", PersoniumUnitConfig.getMasterToken()) .with("ifMatch", "*").returns().statusCode(HttpStatus.SC_NO_CONTENT); } /** * UserData??????. * @param entityType ?? * @return UserData???? */ protected String getNameSpace(String entityType) { // NameSpace??????? return getNameSpace(entityType, colName); } /** * UserData??????. * @param entityType ?? * @param col ?? * @return UserData???? */ protected String getNameSpace(String entityType, String col) { // NameSpace??????? TResponse res = Http.request("box/$metadata-$metadata-get.txt").with("path", "\\$metadata").with("col", col) .with("accept", "application/xml").with("token", PersoniumUnitConfig.getMasterToken()).returns() .statusCode(HttpStatus.SC_OK).debug(); Pattern pattern = Pattern.compile("Namespace=\"([^\"]+)\">"); Matcher matcher = pattern.matcher(res.getBody()); matcher.find(); return matcher.group(1) + "." + entityType; } /** * ????. * @param linkColName ?? * @param linkEntityTypeName ?? * @param links ???? * @return ?? */ protected Map<String, Object> getLinkCheckData(String linkColName, String linkEntityTypeName, ArrayList<String> links) { String baseUrl = UrlUtils.userData(cellName, boxName, linkColName, linkEntityTypeName + "('parent')/_"); Map<String, Object> additional = new HashMap<String, Object>(); additional.put("__id", "parent"); for (String link : links) { Map<String, Object> uri = new HashMap<String, Object>(); Map<String, Object> deferred = new HashMap<String, Object>(); uri.put("uri", baseUrl + link); deferred.put("__deferred", uri); additional.put("_" + link, deferred); } return additional; } /** * 4?ComplexType??. */ protected void create4ComplexTypeSchema() { UserDataComplexTypeUtils.createComplexTypeSchema(UserDataComplexTypeUtils.ENTITY_TYPE_NAME, UserDataComplexTypeUtils.COMPLEX_TYPE_NAME, UserDataComplexTypeUtils.ET_STRING_PROP, UserDataComplexTypeUtils.ET_CT1ST_PROP, UserDataComplexTypeUtils.CT1ST_STRING_PROP); addComplexType(UserDataComplexTypeUtils.COMPLEX_TYPE_NAME, "ct1stComplexProp", "complexType2nd", "ct2ndStrProp"); addComplexType("complexType2nd", "ct2ndComplexProp", "complexType3rd", "ct3rdStrProp"); } /** * ?. * @param parentComplex ComplexType * @param parentComplexProperty ComplexType??ComplexTypeProperty * @param addComplex ComplexType * @param addComplexProerty ComplexType? */ protected void addComplexType(String parentComplex, String parentComplexProperty, String addComplex, String addComplexProerty) { // ComplexType? UserDataUtils.createComplexType(Setup.TEST_CELL1, Setup.TEST_BOX1, Setup.TEST_ODATA, addComplex); // complexTypeProperty? UserDataUtils.createComplexTypeProperty(Setup.TEST_CELL1, Setup.TEST_BOX1, Setup.TEST_ODATA, parentComplexProperty, parentComplex, addComplex, false, null, null); UserDataUtils.createComplexTypeProperty(Setup.TEST_CELL1, Setup.TEST_BOX1, Setup.TEST_ODATA, addComplexProerty, addComplex, EdmSimpleType.STRING.getFullyQualifiedTypeName(), false, null, null); } /** * 5?ComplexType?. */ protected void delete5ComplexTypeSchema() { deleteComplexType("complexType3rd", "ct3rdComplexProp", "complexType4th", "ct4thStrProp"); deleteComplexType("complexType2nd", "ct2ndComplexProp", "complexType3rd", "ct3rdStrProp"); deleteComplexType(UserDataComplexTypeUtils.COMPLEX_TYPE_NAME, "ct1stComplexProp", "complexType2nd", "ct2ndStrProp"); UserDataComplexTypeUtils.deleteComplexTypeSchema(); } /** * ?. * @param parentComplex ComplexType * @param parentComplexProperty ComplexType??ComplexTypeProperty * @param delComplex ComplexType * @param delComplexProerty ComplexType? */ protected void deleteComplexType(String parentComplex, String parentComplexProperty, String delComplex, String delComplexProerty) { String ctplocationUrl = UrlUtils.complexTypeProperty(Setup.TEST_CELL1, Setup.TEST_BOX1, Setup.TEST_ODATA, delComplexProerty, delComplex); String pctplocationUrl = UrlUtils.complexTypeProperty(Setup.TEST_CELL1, Setup.TEST_BOX1, Setup.TEST_ODATA, parentComplexProperty, parentComplex); String ctlocationUrl = UrlUtils.complexType(Setup.TEST_CELL1, Setup.TEST_BOX1, Setup.TEST_ODATA, delComplex); // ???ComplexTypeProperty ODataCommon.deleteOdataResource(ctplocationUrl); ODataCommon.deleteOdataResource(pctplocationUrl); // ???ComplexType ODataCommon.deleteOdataResource(ctlocationUrl); } /** * ?????. * @param json ? * @return OData */ protected JSONObject getResult(JSONObject json) { return (JSONObject) ((JSONObject) json.get("d")).get("results"); } /** * ?????. * @param userDataId ODataID * @param json ? * @return ??ID?OData */ protected JSONObject getResultsFromId(String userDataId, JSONObject json) { JSONArray results = ((JSONArray) ((JSONObject) json.get("d")).get("results")); for (Object result : results) { JSONObject entity = (JSONObject) result; if (userDataId.equals(entity.get("__id").toString())) { return entity; } } return null; } }