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.HttpStatus; import org.json.simple.JSONObject; import org.junit.Test; import org.junit.experimental.categories.Category; import io.personium.common.auth.token.Role; import io.personium.core.PersoniumCoreException; 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.unit.core.UrlUtils; import io.personium.test.utils.Http; import io.personium.test.utils.TResponse; /** * ROLE??. */ @Category({ Unit.class, Integration.class, Regression.class }) public class RoleUpdateTest extends ODataCommon { private static String cellName = "testcell1"; private static String testRoleName = "testrole"; private static final String ROLE_TYPE = "CellCtl.Role"; /** * . ?super????? */ public RoleUpdateTest() { super("io.personium.core.rs"); } /** * ROLE? _Box.Name=null, _Box.Name=?Box. */ @Test public void ROLE_BoxName?null_BoxName??Box???????204 ????() { String boxname = "box1"; try { // createRoleAndCheckResponse(false); // Http.request("role-update.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("rolenamekey", testRoleName) .with("boxnamekey", "null") .with("rolename", testRoleName) .with("boxname", "\"" + boxname + "\"") .returns() .debug() .statusCode(HttpStatus.SC_NO_CONTENT); // ? TResponse response = Http.request("role-retrieve.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("rolename", testRoleName) .with("boxname", "'" + boxname + "'") .returns() .statusCode(HttpStatus.SC_OK); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote(cellName, Role.EDM_TYPE_NAME, "Name='" + testRoleName + "',_Box.Name='" + boxname + "'"); ODataCommon.checkCommonResponseHeader(response); // ??? 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); } finally { CellCtlUtils.deleteRole(cellName, testRoleName, boxname); } } /** * ROLE? _Box.Name=?Box, _Box.Name=null. */ @Test public void ROLE_BoxName??Box_BoxName?null?????204 ????() { String boxname = "box1"; try { // createRole(boxname); // Http.request("role-update.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName) .with("rolenamekey", testRoleName).with("boxnamekey", "'" + boxname + "'") .with("rolename", testRoleName).with("boxname", "null").returns().debug() .statusCode(HttpStatus.SC_NO_CONTENT); // ? TResponse response = Http.request("role-retrieve.txt").with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("rolename", testRoleName).with("boxname", "null").returns() .statusCode(HttpStatus.SC_OK); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote(cellName, Role.EDM_TYPE_NAME, "Name='" + testRoleName + "',_Box.Name=null"); ODataCommon.checkCommonResponseHeader(response); // ??? 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); } finally { CellCtlUtils.deleteRole(cellName, testRoleName); } } /** * ROLE? _Box.Name=?Box, _Box.Name=?Box. */ @Test public void ROLE_? ?Role?????????204 ????() { String boxname = "box1"; try { // createRole(boxname); // Http.request("role-update.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName) .with("rolenamekey", testRoleName).with("boxnamekey", "'" + boxname + "'") .with("rolename", testRoleName).with("boxname", "\"" + boxname + "\"").returns().debug() .statusCode(HttpStatus.SC_NO_CONTENT); // ? TResponse response = Http.request("role-retrieve.txt").with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("rolename", testRoleName).with("boxname", "'" + boxname + "'") .returns().statusCode(HttpStatus.SC_OK); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote(cellName, Role.EDM_TYPE_NAME, "Name='" + testRoleName + "',_Box.Name='" + boxname + "'"); ODataCommon.checkCommonResponseHeader(response); // ??? 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); } finally { CellCtlUtils.deleteRole(cellName, testRoleName, boxname); } } /** * ROLE? _Box.Name=null, _Box.Name=null. */ @Test public void ROLE_? ?Role?????????204 ????() { try { // createRoleAndCheckResponse(false); // Http.request("role-update.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName) .with("rolenamekey", testRoleName).with("boxnamekey", "null").with("rolename", testRoleName) .with("boxname", "null").returns().debug().statusCode(HttpStatus.SC_NO_CONTENT); // ? TResponse response = Http.request("role-retrieve.txt").with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("rolename", testRoleName).with("boxname", "null").returns() .statusCode(HttpStatus.SC_OK); // ??? String location = UrlUtils.cellCtlWithoutSingleQuote(cellName, Role.EDM_TYPE_NAME, "Name='" + testRoleName + "',_Box.Name=null"); ODataCommon.checkCommonResponseHeader(response); // ??? 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); } finally { CellCtlUtils.deleteRole(cellName, testRoleName); } } /** * Role??URL????Box?????404?????. */ @Test public void Role??URL????Box?????404?????() { String boxname = "dummy"; try { // createRoleAndCheckResponse(false); // Http.request("role-update.txt").with("token", AbstractCase.MASTER_TOKEN_NAME).with("cellPath", cellName) .with("rolenamekey", testRoleName).with("boxnamekey", "'" + boxname + "'") .with("rolename", testRoleName).with("boxname", "\"box1\"").returns().debug() .statusCode(HttpStatus.SC_NOT_FOUND); } finally { CellCtlUtils.deleteRole(cellName, testRoleName); } } /** * Role??????Box?????400?????. */ @Test public void Role??????Box?????400?????() { String boxname = "dummy"; try { // createRoleAndCheckResponse(false); // TResponse res = Http.request("role-update.txt").with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName).with("rolenamekey", testRoleName).with("boxnamekey", "null") .with("rolename", testRoleName).with("boxname", "\"" + boxname + "\"").returns().debug() .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 { CellCtlUtils.deleteRole(cellName, testRoleName); } } /** * ?????????. * @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 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); } }