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.ODataCommon; 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.RoleUtils; import io.personium.test.utils.TResponse; /** * ROLE??. */ @Category({ Unit.class, Integration.class, Regression.class }) public class RoleCreateTest extends ODataCommon { private static String cellName = "testcell1"; private static String testRoleName = "testrole"; private static final String ROLE_TYPE = "CellCtl.Role"; /** * . ?super????? */ public RoleCreateTest() { super("io.personium.core.rs"); } /** * ROLE?? Name, _Box.Name?. */ @Test public void ROLE???() { String boxname = "box1"; try { createRole(boxname); } finally { CellCtlUtils.deleteRole(cellName, testRoleName, boxname); } } /** * ROLE?? _Box.Name?null. */ @Test public void ROLE????() { try { createRoleAndCheckResponse(false); } finally { CellCtlUtils.deleteRole(cellName, testRoleName); } } /** * ROLE?? _Box.Name??. */ @Test public void ROLE??? ???() { try { createRoleAndCheckResponse(true); // TODO ??400?? } finally { CellCtlUtils.deleteRole(cellName, testRoleName); } } /** * ROLE??_??__published???400???. */ @Test public void ROLE??_??__published???400???() { errCreateRole("box1", PUBLISHED, "/Date(0)/", HttpStatus.SC_BAD_REQUEST); } /** * ROLE??_??__updated???400???. */ @Test public void ROLE??_??__updated???400???() { errCreateRole("box1", UPDATED, "/Date(0)/", HttpStatus.SC_BAD_REQUEST); } /** * ROLE??_??__metadata???400???. */ @Test public void ROLE??_??__metadata???400???() { errCreateRole("box1", METADATA, null, HttpStatus.SC_BAD_REQUEST); } /** * ROLE??Name???400????. */ @Test public final void ROLE??Name????400????() { String roleName = ""; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??Name???????400????. */ @Test public final void ROLE??Name???????400????() { String roleName = "_testRole"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??Name???????400????. */ @Test public final void ROLE??Name???????400????() { String roleName = "-testRole"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??Name?????400????. */ @Test public final void ROLE??Name?????400????() { String roleName = "test/Role"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??Name?__ctl???400????. */ @Test public final void ROLE??Name?__ctl???400????() { String roleName = "__ctl"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??Name?1???201????. */ @Test public final void ROLE??Name?1 ???201????() { String roleName = "1"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_CREATED); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??Name?128???201????. */ @Test public final void ROLE??Name?128 ???201????() { String roleName = "1234567890123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567x"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_CREATED); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??Name?129???400????. */ @Test public final void ROLE??Name?129 ???400????() { String roleName = "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890123456789012345678x"; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??Name????400????. */ @Test public final void ROLE??Name????400????() { String roleName = ""; String boxname = "box1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??_Box.Name???400????. */ @Test public final void ROLE??Box??????400????() { String roleName = testRoleName; String boxname = ""; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??_Box.Name???????400????. */ @Test public final void ROLE??Box?????????400????() { String roleName = testRoleName; String boxname = "_box"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??_Box.Name???????400????. */ @Test public final void ROLE??Box?????????400????() { String roleName = testRoleName; String boxname = "-box"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??_Box.Name?????400????. */ @Test public final void ROLE??Box???????400????() { String roleName = testRoleName; String boxname = "box/1"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??_Box.Name?__ctl???400????. */ @Test public final void ROLE??Box???__ctl???400????() { String roleName = testRoleName; String boxname = "__ctl"; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??_Box.Name?1???201????. */ @Test public final void ROLE??Box???1 ???201????() { String roleName = testRoleName; String boxname = "1"; String locationHeader = null; String locationHeaderBox = null; try { // Box? PersoniumResponse resBox = createBox(boxname); locationHeaderBox = resBox.getFirstHeader(HttpHeaders.LOCATION); TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_CREATED); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } if (locationHeaderBox != null) { CellCtlUtils.deleteOdataResource(locationHeaderBox); } } } /** * ROLE??_Box.Name?128???201????. */ @Test public final void ROLE??Box???128 ???201????() { String roleName = testRoleName; String boxname = "1234567890123456789012345678901234567890123456789012345678901234567890" + "123456789012345678901234567890123456789012345678901234567x"; String locationHeader = null; String locationHeaderBox = null; try { // Box? PersoniumResponse resBox = createBox(boxname); locationHeaderBox = resBox.getFirstHeader(HttpHeaders.LOCATION); TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_CREATED); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } if (locationHeaderBox != null) { CellCtlUtils.deleteOdataResource(locationHeaderBox); } } } /** * ROLE??_Box.Name?129???400????. */ @Test public final void ROLE??Box???129 ???400????() { String roleName = testRoleName; String boxname = "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890123456789012345678x"; String locationHeader = null; String locationHeaderBox = null; try { // Box? PersoniumResponse resBox = createBox(boxname); locationHeaderBox = resBox.getFirstHeader(HttpHeaders.LOCATION); TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); if (locationHeaderBox != null) { CellCtlUtils.deleteOdataResource(locationHeaderBox); } } } } /** * ROLE??_Box.Name????400????. */ @Test public final void ROLE??Box??????400????() { String roleName = testRoleName; String boxname = ""; String locationHeader = null; try { TResponse res = createRole(roleName, boxname); locationHeader = res.getLocationHeader(); res.statusCode(HttpStatus.SC_BAD_REQUEST); } finally { if (locationHeader != null) { CellCtlUtils.deleteOdataResource(locationHeader); } } } /** * ROLE??_Box.Name????Box?????400????. */ @Test public final void ROLE?????Box?????400????() { String roleName = testRoleName; String boxname = "dummy"; String locationHeader = null; try { TResponse res = createRole(roleName, 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?ROLE????201????. */ @Test public final void Box?NP?ROLE????201????() { String roleName = "npRole"; try { RoleUtils.createViaNP(Setup.TEST_CELL1, Setup.MASTER_TOKEN_NAME, "Box", "'" + Setup.TEST_BOX1 + "'", roleName, HttpStatus.SC_CREATED); } finally { RoleUtils.delete(Setup.TEST_CELL1, Setup.MASTER_TOKEN_NAME, roleName, Setup.TEST_BOX1, -1); } } /** * ?????????. * @param boxname */ @SuppressWarnings("unchecked") private void createRole(String boxname) { JSONObject body = new JSONObject(); body.put("Name", testRoleName); body.put("_Box.Name", boxname); TResponse response = Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("body", body.toString()).returns() .statusCode(HttpStatus.SC_CREATED); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote("testcell1", "Role", "Name='" + testRoleName + "',_Box.Name='" + boxname + "'"); ODataCommon.checkCommonResponseHeader(response, location); // ??? Map<String, Object> additional = new HashMap<String, Object>(); additional.put("Name", testRoleName); additional.put("_Box.Name", boxname); ODataCommon.checkResponseBody(response.bodyAsJson(), location, ROLE_TYPE, additional); } /** * ?????????(). * @param boxname * @param errKey * @param errValue * @param ? */ @SuppressWarnings("unchecked") private void errCreateRole(String boxname, String errKey, String errValue, int errSC) { JSONObject body = new JSONObject(); body.put("Name", testRoleName); body.put("_Box.Name", boxname); body.put(errKey, errValue); Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName) .with("body", body.toString()).returns().statusCode(errSC); } /** * ?????????(). * @param roleName * @param boxname * @param ? * @return */ @SuppressWarnings("unchecked") private TResponse createRole(String roleName, String boxname) { JSONObject body = new JSONObject(); body.put("Name", roleName); 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().debug(); } /** * ??????????. * @param boxNameEmpty _Box.Name??? */ @SuppressWarnings("unchecked") private void createRoleAndCheckResponse(boolean boxNameEmpty) { JSONObject body = new JSONObject(); body.put("Name", testRoleName); if (!boxNameEmpty) { body.put("_Box.Name", null); } TResponse response = Http.request("role-create.txt").with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("body", body.toString()).returns() .statusCode(HttpStatus.SC_CREATED); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote("testcell1", "Role", "Name='" + testRoleName + "',_Box.Name=null"); ODataCommon.checkCommonResponseHeader(response, location); // ??? Map<String, Object> additional = new HashMap<String, Object>(); additional.put("Name", testRoleName); additional.put("_Box.Name", null); ODataCommon.checkResponseBody(response.bodyAsJson(), location, ROLE_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); } }