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.cell.ctl; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.json.simple.JSONObject; import org.junit.Test; import org.junit.experimental.categories.Category; import io.personium.core.PersoniumCoreException; import io.personium.core.model.Box; 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.PersoniumRequest; import io.personium.test.jersey.PersoniumResponse; 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.RelationUtils; import io.personium.test.utils.TResponse; /** * Relation??. */ @Category({ Unit.class, Integration.class, Regression.class }) public class RelationCreateTest extends ODataCommon { private static final String RELATION_TYPE = "CellCtl.Relation"; private static String cellName = "testcell1"; private static String testRelationName = "testrelation"; /** * . ?super????? */ public RelationCreateTest() { super("io.personium.core.rs"); } /** * Relation?? Name, _Box.Name?. */ @Test public void Relation???() { String boxname = "box1"; try { createRelation(boxname); } finally { CellCtlUtils.deleteRelation(cellName, testRelationName, boxname); } } /** * Relation?? _Box.Name?null. */ @Test public void Relation????() { try { createRelationAndCheckResponse(false); } finally { CellCtlUtils.deleteRelation(cellName, testRelationName); } } /** * Relation?? _Box.Name??. */ @Test public void Relation??? ???() { try { createRelationAndCheckResponse(true); // TODO ??400?? } finally { CellCtlUtils.deleteRelation(cellName, testRelationName); } } /** * Relation??_??__published???400???. */ @Test public void Relation??_??__published???400???() { errCreateRelation("box1", PUBLISHED, "/Date(0)/", HttpStatus.SC_BAD_REQUEST); } /** * Relation??_??__updated???400???. */ @Test public void Relation??_??__updated???400???() { errCreateRelation("box1", UPDATED, "/Date(0)/", HttpStatus.SC_BAD_REQUEST); } /** * Relation??_??__metadata???400???. */ @Test public void Relation??_??__metadata???400???() { errCreateRelation("box1", METADATA, null, HttpStatus.SC_BAD_REQUEST); } /** * Relation?Name????400????. */ @Test public void Relation?Name????400????() { String relationName = ""; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name???????400????. */ @Test public void Relation?Name???????400????() { String relationName = "_testRelation"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name??????400????. */ @Test public void Relation?Name??????400????() { String relationName = ":testRelation"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name???????201????. */ @Test public void Relation?Name???????201????() { String relationName = "-testRelation"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name??????201????. */ @Test public void Relation?Name??????201????() { String relationName = "+testRelation"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name??????201????. */ @Test public void Relation?Name? ?????201????() { String relationName = "+:keeps"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name???????201????. */ @Test public void Relation?Name? ?????201????() { String relationName = "-:keeps"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name?+????201????. */ @Test public void Relation?Name?????201????() { String relationName = "test+Relation"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name?-????201????. */ @Test public void Relation?Name?????201????() { String relationName = "test-Relation"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name?_????201????. */ @Test public void Relation?Name??????201????() { String relationName = "test_Relation"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name?????400????. */ @Test public void Relation?Name?????400????() { String relationName = "test/Relation"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name?__ctl???400????. */ @Test public void Relation?Name?__ctl???400????() { String relationName = "__ctl"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name????????201????. */ @Test public void Relation?Name????????201????() { String relationName = "1"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name???????201????. */ @Test public void Relation?Name???????201????() { String relationName = "1234567890123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567x"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name????????400????. */ @Test public void Relation?Name????????400????() { String relationName = "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890123456789012345678x"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?Name????400????. */ @Test public void Relation?Name????400????() { String relationName = ""; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?_Box.Name????400????. */ @Test public void Relation?Box??????400????() { String relationName = testRelationName; String boxname = ""; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?_Box.Name???????400????. */ @Test public void Relation?Box?????????400????() { String relationName = testRelationName; String boxname = "_boxname"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?_Box.Name???????400????. */ @Test public void Relation?Box?????????400????() { String relationName = testRelationName; String boxname = "-boxname"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?_Box.Name?????400????. */ @Test public void Relation?Box???????400????() { String relationName = testRelationName; String boxname = "box/name"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?_Box.Name?__ctl???400????. */ @Test public void Relation?Box???__ctl???400????() { String relationName = testRelationName; String boxname = "__ctl"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation?_Box.Name????????201????. */ @Test public void Relation?Box??????????201????() { String relationName = testRelationName; String boxname = "1"; String locationHeader = null; String locationHeaderBox = null; try { // Box? PersoniumResponse resBox = createBox(boxname); locationHeaderBox = resBox.getFirstHeader(HttpHeaders.LOCATION); TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } if (locationHeaderBox != null) { CellCtlUtils.deleteOdataResource(locationHeaderBox); } } } /** * Relation?_Box.Name???????201????. */ @Test public void Relation?Box?????????201????() { String relationName = testRelationName; String boxname = "1234567890123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567x"; String locationHeader = null; String locationHeaderBox = null; try { // Box? PersoniumResponse resBox = createBox(boxname); locationHeaderBox = resBox.getFirstHeader(HttpHeaders.LOCATION); TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_CREATED); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } if (locationHeaderBox != null) { CellCtlUtils.deleteOdataResource(locationHeaderBox); } } } /** * Relation?_Box.Name????????400????. */ @Test public void Relation?Box??????????400????() { String relationName = testRelationName; String boxname = "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890123456789012345678x"; String locationHeader = null; String locationHeaderBox = null; try { // Box? PersoniumResponse resBox = createBox(boxname); locationHeaderBox = resBox.getFirstHeader(HttpHeaders.LOCATION); TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } if (locationHeaderBox != null) { CellCtlUtils.deleteOdataResource(locationHeaderBox); } } } /** * Relation?_Box.Name????400????. */ @Test public void Relation?Box??????400????() { String relationName = testRelationName; String boxname = ""; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); res.statusCode(HttpStatus.SC_BAD_REQUEST); locationHeader = res.getLocationHeader(); } finally { if (locationHeader != null) { deleteOdataResource(locationHeader); } } } /** * Relation??_Box.Name????Box?????400????. */ @Test public final void Relation?????Box?????400????() { String relationName = testRelationName; String boxname = "dummy"; String locationHeader = null; try { TResponse res = createRelation(relationName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); // ? ODataCommon.checkErrorResponseBody(res, PersoniumCoreException.OData.BODY_NTKP_NOT_FOUND_ERROR.getCode(), PersoniumCoreException.OData.BODY_NTKP_NOT_FOUND_ERROR.params(boxname).getMessage()); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * Box?NP?Relation????201????. */ @Test public final void Box?NP?Relation????201????() { String relationName = "npRelation"; try { RelationUtils.createViaNP(Setup.TEST_CELL1, Setup.MASTER_TOKEN_NAME, Setup.TEST_BOX1, relationName, HttpStatus.SC_CREATED); } finally { RelationUtils.delete(Setup.TEST_CELL1, Setup.MASTER_TOKEN_NAME, relationName, Setup.TEST_BOX1, -1); } } /** * ???????Relation??. * @param boxname */ @SuppressWarnings("unchecked") private void createRelation(String boxname) { JSONObject body = new JSONObject(); body.put("Name", testRelationName); body.put("_Box.Name", boxname); TResponse response = RelationUtils.create(cellName, AbstractCase.MASTER_TOKEN_NAME, body, HttpStatus.SC_CREATED); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote("testcell1", "Relation", "Name='" + testRelationName + "',_Box.Name='" + boxname + "'"); ODataCommon.checkCommonResponseHeader(response, location); // ??? Map<String, Object> additional = new HashMap<String, Object>(); additional.put("Name", testRelationName); additional.put("_Box.Name", boxname); ODataCommon.checkResponseBody(response.bodyAsJson(), location, RELATION_TYPE, additional); } /** * ???????Relation??(). * @param boxname * @param errKey * @param errValue * @param errSC ? */ @SuppressWarnings("unchecked") private void errCreateRelation(String boxname, String errKey, String errValue, int errSC) { JSONObject body = new JSONObject(); body.put("Name", testRelationName); body.put("_Box.Name", boxname); body.put(errKey, errValue); RelationUtils.create(cellName, AbstractCase.MASTER_TOKEN_NAME, body, errSC); } /** * ???????Relation??. * @param relationName * @param boxname * @return ? */ @SuppressWarnings("unchecked") private TResponse createRelation(String relationName, String boxname) { JSONObject body = new JSONObject(); body.put("Name", relationName); body.put("_Box.Name", boxname); return Http.request("relation-create.txt").with("token", "Bearer " + AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("body", body.toString()).returns().debug(); } /** * ????????Relation??. * @param boxNameEmpty _Box.Name??? */ @SuppressWarnings("unchecked") private void createRelationAndCheckResponse(boolean boxNameEmpty) { JSONObject body = new JSONObject(); body.put("Name", testRelationName); if (!boxNameEmpty) { body.put("_Box.Name", null); } TResponse response = RelationUtils.create(cellName, AbstractCase.MASTER_TOKEN_NAME, body, HttpStatus.SC_CREATED); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote("testcell1", "Relation", "Name='" + testRelationName + "',_Box.Name=null"); ODataCommon.checkCommonResponseHeader(response, location); // ??? Map<String, Object> additional = new HashMap<String, Object>(); additional.put("Name", testRelationName); additional.put("_Box.Name", null); ODataCommon.checkResponseBody(response.bodyAsJson(), location, RELATION_TYPE, additional); } private PersoniumResponse createBox(String boxname) { PersoniumRequest req = PersoniumRequest.post(UrlUtils.cellCtl(cellName, Box.EDM_TYPE_NAME)); req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", boxname); return request(req); } }