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.ArrayList; import javax.ws.rs.core.MediaType; 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 org.junit.runner.RunWith; import io.personium.core.PersoniumUnitConfig; 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.PersoniumIntegTestRunner; import io.personium.test.setup.Setup; import io.personium.test.utils.Http; import io.personium.test.utils.TResponse; /** * Account?Role???. */ @RunWith(PersoniumIntegTestRunner.class) @Category({Unit.class, Integration.class, Regression.class }) public class AccountRoleLinkListTest extends AccountTest { /** * . ?super????? */ public AccountRoleLinkListTest() { super(); } /** * Account?Role?????????. */ @Test public void Account?Role?????????() { String testRoleName = "testRole"; String testAccountName = "test_account"; String testAccountPass = "password"; String accountUrl = null; String roleUrl = null; try { accountUrl = this.createAccount(testAccountName, testAccountPass); roleUrl = this.createRole(testRoleName); // ?$link createAccountRoleLink(testAccountName, roleUrl); // ?? TResponse resList = Http.request("link-account-role-list.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("username", testAccountName) .returns() .statusCode(HttpStatus.SC_OK) .debug(); ArrayList<String> expectedUriList = new ArrayList<String>(); expectedUriList.add(roleUrl); // ??? checkLinResponseBody(resList.bodyAsJson(), expectedUriList); } finally { deleteLinks(testAccountName, testRoleName); deleteRole(roleUrl); deleteAccount(accountUrl); } } /** * Account?Role??????????. */ @Test public void Account?Role??????????() { String testRoleName = "testRole"; String testAccountName = "test_account"; String testAccountPass = "password"; String testLinkPath = "__ctl/Account\\('" + testAccountName + "'\\)/\\$links/_Role"; String accountUrl = null; String roleUrl = null; try { accountUrl = this.createAccount(testAccountName, testAccountPass); roleUrl = this.createRole(testRoleName); // ?$link createAccountRoleLink(testAccountName, roleUrl); // ? Http.request("link-update-with-body.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("linkPath", testLinkPath) .with("body", "{\"Name\":\"testRole\"}") .returns() .statusCode(HttpStatus.SC_BAD_REQUEST) .debug(); } finally { deleteLinks(testAccountName, testRoleName); deleteRole(roleUrl); deleteAccount(accountUrl); } } /** * Account?Role?????????. */ @Test public void Account?Role?????????() { String testRoleName = "testRole"; String testRoleName2 = "testRole2"; String testAccountName = "test_account"; String testAccountPass = "password"; String accountUrl = null; String roleUrl = null; String roleUrl2 = null; try { accountUrl = this.createAccount(testAccountName, testAccountPass); roleUrl = this.createRole(testRoleName); roleUrl2 = this.createRole(testRoleName2); // ?$link createAccountRoleLink(testAccountName, roleUrl); createAccountRoleLink(testAccountName, roleUrl2); // ?? TResponse resList = Http.request("link-account-role-list.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("username", testAccountName) .returns() .statusCode(HttpStatus.SC_OK) .debug(); ArrayList<String> expectedUriList = new ArrayList<String>(); expectedUriList.add(roleUrl); expectedUriList.add(roleUrl2); // ??? checkLinResponseBody(resList.bodyAsJson(), expectedUriList); } finally { deleteLinks(testAccountName, testRoleName); deleteLinks(testAccountName, testRoleName2); deleteRole(roleUrl); deleteRole(roleUrl2); deleteAccount(accountUrl); } } /** * Account?Box????Role?????????. */ @Test public void Account?Box????Role?????????() { String testRoleName = "testRole"; String testRoleName2 = "testRole2"; String testAccountName = "test_account"; String testAccountPass = "password"; String accountUrl = null; TResponse role = null; TResponse role2 = null; String roleUrl = null; String roleUrl2 = null; try { createBox2(Setup.TEST_BOX1, null); createBox2(Setup.TEST_BOX2, null); accountUrl = this.createAccount(testAccountName, testAccountPass); role = createRole(testRoleName, Setup.TEST_BOX1); role2 = createRole(testRoleName2, Setup.TEST_BOX2); roleUrl = role.getHeader("Location"); roleUrl2 = role2.getHeader("Location"); // ?$link createAccountRoleLink(testAccountName, roleUrl); createAccountRoleLink(testAccountName, roleUrl2); // ?? TResponse resList = Http.request("link-account-role-list.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("username", testAccountName) .returns() .statusCode(HttpStatus.SC_OK) .debug(); ArrayList<String> expectedUriList = new ArrayList<String>(); expectedUriList.add(roleUrl); expectedUriList.add(roleUrl2); // ??? checkLinResponseBody(resList.bodyAsJson(), expectedUriList); } finally { deleteLinks(testAccountName, testRoleName, Setup.TEST_BOX1); deleteLinks(testAccountName, testRoleName2, Setup.TEST_BOX2); deleteRole(roleUrl); deleteRole(roleUrl2); deleteAccount(accountUrl); } } /** * Account?Role?????????. */ @Test public void Account?Role?????????() { String testAccountName = "test_account"; String testAccountPass = "password"; String accountUrl = null; try { accountUrl = this.createAccount(testAccountName, testAccountPass); // ?? TResponse resList = Http.request("link-account-role-list.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("username", testAccountName) .returns() .statusCode(HttpStatus.SC_OK) .debug(); ArrayList<String> expectedUriList = new ArrayList<String>(); // ??? checkLinResponseBody(resList.bodyAsJson(), expectedUriList); } finally { deleteAccount(accountUrl); } } private void createAccountRoleLink(String accountName, String roleUrl) { Http.request("link-account-role.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("username", accountName) .with("roleUrl", roleUrl) .returns() .statusCode(HttpStatus.SC_NO_CONTENT) .debug(); } private void deleteAccount(String accountUrl) { PersoniumRequest req = PersoniumRequest.delete(accountUrl) .header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN) .header(HttpHeaders.IF_MATCH, "*"); request(req); } private void deleteRole(String roleUrl) { PersoniumRequest req = PersoniumRequest.delete(roleUrl) .header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN) .header(HttpHeaders.IF_MATCH, "*"); request(req); } /** * ?????????. * @param roleName * @param boxname * @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 accountname ?Account?Name * @param roleUrl ?Role?uri * @param code ?? * @return ? */ protected TResponse createLinks(String accountname, String roleUrl, int code) { // return Http.request("link-account-role.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("username", accountname) .with("roleUrl", roleUrl) .returns() .statusCode(code) .debug(); } /** * ??(?uri?null). * @param accountname ?Account?Name * @param code ?? * @return ? */ protected TResponse createLinksLinkNull(String accountname, int code) { // return Http.request("link-account-roleWithBodyNull.txt") .with("token", AbstractCase.MASTER_TOKEN_NAME) .with("cellPath", cellName) .with("username", accountname) .returns() .statusCode(code) .debug(); } /** * ?. * @param accountname ?Account?Name * @param rolename ?Role?Name * @param boxName ?Role?Box?? * @return ? */ protected TResponse deleteLinks(String accountname, String rolename, String boxName) { // return Http.request("cell/link-delete-account-role.txt") .with("cellPath", cellName) .with("accountKey", accountname) .with("roleKey", "Name='" + rolename + "',_Box.Name='" + boxName + "'") .with("contentType", MediaType.APPLICATION_JSON) .with("token", PersoniumUnitConfig.getMasterToken()) .with("ifMatch", "*") .returns(); } /** * ?. * @param accountname ?Account?Name * @param rolename ?Role?Name * @return ? */ protected TResponse deleteLinks(String accountname, String rolename) { // return Http.request("cell/link-delete-account-role.txt") .with("cellPath", cellName) .with("accountKey", accountname) .with("roleKey", "Name='" + rolename + "',_Box.Name=null") .with("contentType", MediaType.APPLICATION_JSON) .with("token", PersoniumUnitConfig.getMasterToken()) .with("ifMatch", "*") .returns(); } }