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; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.Calendar; import java.util.HashMap; import javax.ws.rs.core.MediaType; import org.apache.http.Header; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.json.simple.JSONObject; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; import org.w3c.dom.NodeList; import io.personium.core.PersoniumCoreException; import io.personium.core.model.Cell; 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.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; /** * Cell??IT. */ @Category({ Unit.class, Integration.class, Regression.class }) public class UpdateTest extends AbstractCase { private String cellName; private String cellNameToDelete; private PersoniumResponse res; private static String eTag = ""; private static String published = ""; /** * ETag?. */ public static final String ETAG_ASTA = "*"; /** * . ?super????? */ public UpdateTest() { super("io.personium.core.rs"); } /** * ??????. */ @BeforeClass public static void beforeClass() { } /** * ???????. */ @SuppressWarnings("unchecked") @Before public void before() { // Cell? // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); // DB???????????Cell????409????????????????Cell??? this.cellName = "cellname" + Long.toString(Calendar.getInstance().getTimeInMillis()); this.cellNameToDelete = cellName; // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); PersoniumResponse beforeres = createCell(headers, requestBody); // ??Cell?ID??? // Header[] resHeadersLocate = beforeres.getResponseHeaders(HttpHeaders.LOCATION); // cellName = resHeadersLocate[0].getValue().split("'")[1]; // ETag?????????Etag????????? Header[] resHeadersEtag = beforeres.getResponseHeaders(HttpHeaders.ETAG); if (resHeadersEtag != null && resHeadersEtag.length == 1) { eTag = resHeadersEtag[0].getValue(); } // eTag = ETAG_ASTA; // __published? published = ODataCommon.getPublished(beforeres); } /** * ??????. */ @After public void after() { deleteCell(); } /** * Cell??204?????. */ @SuppressWarnings("unchecked") @Test public final void Cell??204 ?????() { // ? HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, eTag); // ? JSONObject requestBody = new JSONObject(); String updateCellName = "cellname" + Long.toString(Calendar.getInstance().getTimeInMillis()); requestBody.put("Name", updateCellName); // ? res = updateCell(headers, requestBody); cellNameToDelete = updateCellName; // __published?? PersoniumResponse getResp = restGet(getUrl(updateCellName)); String resPublished = ODataCommon.getPublished(getResp); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); assertEquals(published, resPublished); } /** * ACL??Cell??204?????. */ @SuppressWarnings("unchecked") @Test public final void ACL ??Cell??204 ?????() { // ACL(all/all)Cell? Http.request("cell/acl-setting-all.txt").with("url", cellName).with("token", Setup.BEARER_MASTER_TOKEN) .with("roleBaseUrl", UrlUtils.roleResource(cellName, null, "")).returns() .statusCode(HttpStatus.SC_OK); // PROPFIND?Cell?ACL? TResponse tresponse = Http.request("cell/propfind-cell-allprop.txt").with("url", cellName) .with("depth", "0").with("token", Setup.MASTER_TOKEN_NAME).returns(); tresponse.statusCode(HttpStatus.SC_MULTI_STATUS); // principal?all???????? NodeList nodeListPrincipal = tresponse.bodyAsXml().getElementsByTagName("principal").item(0) .getChildNodes(); boolean existsPrincipalAll = false; for (int i = 0; i < nodeListPrincipal.getLength(); i++) { String nodename = nodeListPrincipal.item(i).getNodeName(); if ("all".equals(nodename)) { existsPrincipalAll = true; } } assertTrue(existsPrincipalAll); // privilege?all???????? NodeList nodeListPrivilege = tresponse.bodyAsXml().getElementsByTagName("privilege").item(0) .getChildNodes(); boolean existsPrivilegeAll = false; for (int i = 0; i < nodeListPrivilege.getLength(); i++) { String nodename = nodeListPrivilege.item(i).getNodeName(); // all -> root ???? if ("root".equals(nodename)) { existsPrivilegeAll = true; } } assertTrue(existsPrivilegeAll); // ?? HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); String updateCellName = "cellname" + Long.toString(Calendar.getInstance().getTimeInMillis()); requestBody.put("Name", updateCellName); // ? res = updateCell(headers, requestBody); cellNameToDelete = updateCellName; // __published?? PersoniumResponse getResp = restGet(getUrl(updateCellName)); String resPublished = ODataCommon.getPublished(getResp); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); assertEquals(published, resPublished); // PROPFIND?Cell?ACL? TResponse tresponsePut = Http.request("cell/propfind-cell-allprop.txt").with("url", updateCellName) .with("depth", "0").with("token", Setup.MASTER_TOKEN_NAME).returns(); tresponse.statusCode(HttpStatus.SC_MULTI_STATUS); // principal?all???????? assertNotNull(tresponsePut.bodyAsXml().getElementsByTagName("principal").item(0)); nodeListPrincipal = tresponsePut.bodyAsXml().getElementsByTagName("principal").item(0).getChildNodes(); existsPrincipalAll = false; for (int i = 0; i < nodeListPrincipal.getLength(); i++) { String nodename = nodeListPrincipal.item(i).getNodeName(); if ("all".equals(nodename)) { existsPrincipalAll = true; } } assertTrue(existsPrincipalAll); // privilege?all???????? assertNotNull(tresponsePut.bodyAsXml().getElementsByTagName("privilege").item(0)); nodeListPrivilege = tresponsePut.bodyAsXml().getElementsByTagName("privilege").item(0).getChildNodes(); existsPrivilegeAll = false; for (int i = 0; i < nodeListPrivilege.getLength(); i++) { String nodename = nodeListPrivilege.item(i).getNodeName(); // all -> root ???? if ("root".equals(nodename)) { existsPrivilegeAll = true; } } assertTrue(existsPrivilegeAll); } /** * Cell???????????400????. */ @Test public final void Cell???????????400????() { // $format ?? // Accept ?? String cellNameAsInteger = "123456"; String url = getUrlWithOutQuote(cellNameAsInteger, null); PersoniumResponse response = this.restPut(url, ""); assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode()); checkErrorResponse(response.bodyAsJson(), PersoniumCoreException.OData.ENTITY_KEY_PARSE_ERROR.getCode(), PersoniumCoreException.OData.ENTITY_KEY_PARSE_ERROR.getMessage()); } /** * Cell??Name?? ???. */ @Test public final void Cell??Name?????() { cellNameNull(null); } /** * Cell??Name?? JSON??. */ @Test public final void Cell??Name??JSON??() { cellNameNull(MediaType.APPLICATION_JSON); } /** * Cell??Name?? ATOM??. */ @Test public final void Cell??Name??ATOM??() { cellNameNull(MediaType.APPLICATION_ATOM_XML); } /** * Cell??Name???. * @param accept ? */ private void cellNameNull(String accept) { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); if (accept != null) { headers.put(HttpHeaders.ACCEPT, accept); } else { accept = MediaType.APPLICATION_ATOM_XML; } // ? JSONObject requestBody = new JSONObject(); res = updateCell(headers, requestBody); assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); // ?? checkErrorResponse(this.res.bodyAsJson(), "PR400-OD-0009"); } /** * Cell??Name???. */ @SuppressWarnings("unchecked") @Test public final void Cell??Name???() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", ""); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); this.checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell??Name?1??. */ @SuppressWarnings("unchecked") @Test public final void Cell??Name?1??() { HashMap<String, String> headers = new HashMap<String, String>(); try { // Cell // headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", "0"); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } finally { // ?????after?? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); this.updateCellName(headers, requestBody, "0"); } } /** * Cell??Name?128??. */ @SuppressWarnings("unchecked") @Test public final void Cell??Name?128??() { String id = "01234567890123456789012345678901234567890123456789" + "012345678901234567890123456789012345678901234567890123456789012345678901234567"; HashMap<String, String> headers = new HashMap<String, String>(); try { // Cell // headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", id); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } finally { // ?????after?? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); this.updateCellName(headers, requestBody, id); } } /** * Cell??Name?129??. */ @SuppressWarnings("unchecked") @Test public final void Cell??Name?129??() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); String name = "01234567890123456789012345678901234567890123456789" + "0123456789012345678901234567890123456789012345678901234567890123456789012345678"; requestBody.put("Name", name); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); this.checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell??Name???"-","_"??. */ @SuppressWarnings("unchecked") @Test public final void Cell??Name????????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); this.cellNameToDelete = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"; requestBody.put("Name", this.cellNameToDelete); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??Name???"-","_"??. */ @SuppressWarnings("unchecked") @Test public final void Cell??Name????????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", "?"); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); this.checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell??Name?__??????????. */ @SuppressWarnings("unchecked") @Test public final void Cell??Name?__??????????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", "__"); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); this.checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell??Name?__ctl??????????. */ @SuppressWarnings("unchecked") @Test public final void Cell??Name?__ctl??????????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", "__ctl"); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); this.checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell??__published???????????. */ @SuppressWarnings("unchecked") @Test public final void Cell??__published???????????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); String updateCellName = "cellname" + Long.toString(Calendar.getInstance().getTimeInMillis()); requestBody.put("Name", updateCellName); requestBody.put(PUBLISHED, "/Date(0)/"); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); } /** * Cell??__updated???????????. */ @SuppressWarnings("unchecked") @Test public final void Cell??__updated???????????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); String updateCellName = "cellname" + Long.toString(Calendar.getInstance().getTimeInMillis()); requestBody.put("Name", updateCellName); requestBody.put(UPDATED, "/Date(0)/"); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); } /** * Cell??__metadata???????????. */ @SuppressWarnings("unchecked") @Test public final void Cell??__metadata???????????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); String updateCellName = "cellname" + Long.toString(Calendar.getInstance().getTimeInMillis()); requestBody.put("Name", updateCellName); requestBody.put(METADATA, "test"); res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); } /** * Cell??JSON ??. */ @Test public final void Cell??JSON???() { cellJsonFormatErr(null); } /** * Cell??JSON JSON?. */ @Test public final void Cell??JSON?JSON?() { cellJsonFormatErr(MediaType.APPLICATION_JSON); } /** * Cell??JSON ATOM?. */ @Test public final void Cell??JSON?ATOM?() { cellJsonFormatErr(MediaType.APPLICATION_ATOM_XML); } /** * Cell??JSON. * @param accept ? */ private void cellJsonFormatErr(String accept) { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); if (accept != null) { headers.put(HttpHeaders.ACCEPT, accept); } else { accept = MediaType.APPLICATION_ATOM_XML; } // ? PersoniumRestAdapter rest = new PersoniumRestAdapter(); String data = "\"test\""; try { // res = rest.post(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME), data, headers); } catch (Exception e) { fail(e.getMessage()); } // Cell??? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); this.checkErrorResponse(res.bodyAsJson(), "PR400-OD-0001"); } /** * Cell??XML ??. */ @Test public final void Cell??XML???() { cellXmlFormatErr(null); } /** * Cell??XML JSON?. */ @Test public final void Cell??XML?JSON?() { cellXmlFormatErr(MediaType.APPLICATION_JSON); } /** * Cell??XML ATOM?. */ @Test public final void Cell??XML?ATOM?() { cellXmlFormatErr(MediaType.APPLICATION_ATOM_XML); } /** * Cell??XML. * @param accept ? */ private void cellXmlFormatErr(String accept) { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML); if (accept != null) { headers.put(HttpHeaders.ACCEPT, accept); } else { accept = MediaType.APPLICATION_ATOM_XML; } // ? PersoniumRestAdapter rest = new PersoniumRestAdapter(); String data = "\"test\""; try { // res = rest.post(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME), data, headers); } catch (Exception e) { fail(e.getMessage()); } // Cell??? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, res.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); this.checkErrorResponse(res.bodyAsJson(), "PR400-OD-0001"); } /** * Cell???? ??. */ @Test public final void Cell??????() { cellAuthHeaderNone(null); } /** * Cell???? JSON?. */ @Test public final void Cell????JSON?() { cellAuthHeaderNone(MediaType.APPLICATION_JSON); } /** * Cell???? ATOM?. */ @Test public final void Cell????ATOM?() { cellAuthHeaderNone(MediaType.APPLICATION_ATOM_XML); } /** * Cell?????. * @param accept ? */ @SuppressWarnings("unchecked") private void cellAuthHeaderNone(String accept) { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); if (accept != null) { headers.put(HttpHeaders.ACCEPT, accept); } else { accept = MediaType.APPLICATION_ATOM_XML; } // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); this.res = updateCell(headers, requestBody); // Cell??? // 401????? assertEquals(HttpStatus.SC_UNAUTHORIZED, res.getStatusCode()); // ContentType?? Header[] resContentTypeHeaders = res.getResponseHeaders(HttpHeaders.CONTENT_TYPE); assertEquals(1, resContentTypeHeaders.length); assertEquals(resContentTypeHeaders[0].getValue(), MediaType.APPLICATION_JSON); // ?? checkErrorResponse(this.res.bodyAsJson(), "PR401-AU-0001"); } /** * Cell??????. */ @SuppressWarnings("unchecked") @Test public final void Cell?????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, "test"); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); res = updateCell(headers, requestBody); // Cell??? // 401????? assertEquals(HttpStatus.SC_UNAUTHORIZED, res.getStatusCode()); // ContentType?? Header[] resContentTypeHeaders = res.getResponseHeaders(HttpHeaders.CONTENT_TYPE); assertEquals(1, resContentTypeHeaders.length); assertEquals(MediaType.APPLICATION_JSON, resContentTypeHeaders[0].getValue()); // ?? checkErrorResponse(this.res.bodyAsJson(), "PR401-AU-0003"); } /** * Cell??????. */ @SuppressWarnings("unchecked") @Test public final void Cell??????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, ""); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); res = updateCell(headers, requestBody); // Cell??? // 401????? assertEquals(HttpStatus.SC_UNAUTHORIZED, res.getStatusCode()); // ContentType?? Header[] resContentTypeHeaders = res.getResponseHeaders(HttpHeaders.CONTENT_TYPE); assertEquals(1, resContentTypeHeaders.length); assertEquals(MediaType.APPLICATION_JSON, resContentTypeHeaders[0].getValue()); // ?? checkErrorResponse(this.res.bodyAsJson(), "PR401-AU-0003"); } /** * Cell??post?Accept??. */ @Test public final void Cell??post?Accept??() { cellPost(null); } /** * Cell??postAccept?JSON?. */ @Test public final void Cell??postAccept?JSON?() { cellPost(MediaType.APPLICATION_JSON); } /** * Cell??postAccept?ATOM?. */ @Test public final void Cell??postAccept?ATOM?() { cellPost(MediaType.APPLICATION_ATOM_XML); } /** * Cell??POST?. * @param accept ? */ @SuppressWarnings("unchecked") private void cellPost(String accept) { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); if (accept != null) { headers.put(HttpHeaders.ACCEPT, accept); } else { accept = MediaType.APPLICATION_ATOM_XML; } PersoniumRestAdapter rest = new PersoniumRestAdapter(); try { // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); // res = rest.post(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME, cellName), requestBody.toJSONString(), headers); // Cell??? // 405????? assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, res.getStatusCode()); // ContentType?? Header[] resContentTypeHeaders = res.getResponseHeaders(HttpHeaders.CONTENT_TYPE); assertEquals(1, resContentTypeHeaders.length); // ???JSON assertEquals(MediaType.APPLICATION_JSON, resContentTypeHeaders[0].getValue()); // ?? checkErrorResponse(this.res.bodyAsJson(), "PR405-MC-0001"); } catch (Exception e) { fail(e.getMessage()); } } /** * Cell????ContentType?. */ @SuppressWarnings("unchecked") @Test public final void Cell????ContentType?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, "image/jpeg"); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); res = updateCell(headers, requestBody); // Cell??? // TODO Accept???????? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??conflict?. */ @SuppressWarnings("unchecked") @Test public final void Cell??conflict?() { // Cell???? // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, "image/jpeg"); // DB???????????Cell????409????????????????Cell??? String conflictName = "conflictName" + Long.toString(Calendar.getInstance().getTimeInMillis()); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", conflictName); // Cell? PersoniumResponse beforeres = createCell(headers, requestBody); // 201????? assertEquals(HttpStatus.SC_CREATED, beforeres.getStatusCode()); // ????Cell???Name??? PersoniumResponse resConflict; headers.put(HttpHeaders.IF_MATCH, "*"); resConflict = updateCell(headers, requestBody); this.cellNameToDelete = conflictName; // ?????409?????? assertEquals(HttpStatus.SC_CONFLICT, resConflict.getStatusCode()); assertEquals(MediaType.APPLICATION_JSON, resConflict.getResponseHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue()); assertEquals(0, resConflict.getResponseHeaders(HttpHeaders.ETAG).length); this.checkErrorResponse(resConflict.bodyAsJson(), "PR409-OD-0003"); } /** * Cell??$format?json?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format?json?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); res = updateCellQuery(headers, requestBody, QUERY_FORMAT_JSON); // Cell??? // TODO $format????????? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??$format?atom?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format?atom?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); res = updateCellQuery(headers, requestBody, QUERY_FORMAT_ATOM); // Cell??? // TODO $format????????? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??$format?json, atom?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format?json?atom?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); res = updateCellQuery(headers, requestBody, "$format=test"); // Cell??? // TODO $format????????? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??$format???Accept?json?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format???Accept?json?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??$format???Accept?atom?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format???Accept?atom?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_ATOM_XML); headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_ATOM_XML); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??$format?json, Accept?json?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format?json?Accept?json?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??$format?json, Accept?atom?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format?json?Accept?atom?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_ATOM_XML); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??$format?atom, Accept?json?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format?atom?Accept?json?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??$format?atom, Accept?atom?. */ @SuppressWarnings("unchecked") @Test public final void Cell??$format?atom?Accept?atom?() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.ACCEPT, MediaType.APPLICATION_ATOM_XML); headers.put(HttpHeaders.IF_MATCH, "*"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell?????ID. */ @SuppressWarnings("unchecked") @Test public final void Cell?????ID() { // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "*"); PersoniumRestAdapter rest = new PersoniumRestAdapter(); // ?URL? String cellNameHoge = "hoge" + Long.toString(Calendar.getInstance().getTimeInMillis()); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); String data = requestBody.toJSONString(); try { // res = rest.put(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME, cellNameHoge), data, headers); } catch (Exception e) { fail(e.getMessage()); } // Cell??? // TODO Cell???????????????? assertEquals(HttpStatus.SC_NOT_FOUND, res.getStatusCode()); } /** * Cell??If-Match??. */ @SuppressWarnings("unchecked") @Test public final void Cell??IfMatch??() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, eTag); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); // ?? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_PRECONDITION_FAILED, res.getStatusCode()); } /** * Cell??If-Match??. */ @SuppressWarnings("unchecked") @Test public final void Cell??IfMatch??() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); res = updateCell(headers, requestBody); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_NO_CONTENT, res.getStatusCode()); } /** * Cell??If-Match????. */ @SuppressWarnings("unchecked") @Test public final void Cell??IfMatch????() { // Cell // HashMap<String, String> headers = new HashMap<String, String>(); headers.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); headers.put(HttpHeaders.IF_MATCH, "hoge"); // ? JSONObject requestBody = new JSONObject(); requestBody.put("Name", cellName); res = updateCell(headers, requestBody); // ? res = updateCell(headers, requestBody); // Cell??? assertEquals(HttpStatus.SC_PRECONDITION_FAILED, res.getStatusCode()); } /** * ?. * @param headers * @param requestBody * @return Cell?? */ private static PersoniumResponse createCell(final HashMap<String, String> headers, final JSONObject requestBody) { return createCellQuery(headers, requestBody, null); } /** * ?. * @param headers * @param requestBody * @param query * @return Cell?? */ private static PersoniumResponse createCellQuery(final HashMap<String, String> headers, final JSONObject requestBody, final String query) { PersoniumResponse ret = null; PersoniumRestAdapter rest = new PersoniumRestAdapter(); String data = requestBody.toJSONString(); // ?URL? StringBuilder url = new StringBuilder(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME)); if (query != null) { url.append("?" + query); } try { // ret = rest.post(url.toString(), data, headers); } catch (Exception e) { fail(e.getMessage()); } return ret; } /** * ?. * @param headers * @param requestBody * @return Cell?? */ private PersoniumResponse updateCell(final HashMap<String, String> headers, final JSONObject requestBody) { return updateCellQuery(headers, requestBody, null); } /** * ?. * @param headers * @param requestBody * @param query * @return Cell?? */ private PersoniumResponse updateCellQuery(final HashMap<String, String> headers, final JSONObject requestBody, final String query) { PersoniumResponse ret = null; PersoniumRestAdapter rest = new PersoniumRestAdapter(); String data = requestBody.toJSONString(); // ?URL? StringBuilder url = new StringBuilder(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME, cellName)); if (query != null) { url.append("?" + query); } try { // ret = rest.put(url.toString(), data, headers); } catch (Exception e) { fail(e.getMessage()); } return ret; } /** * ????. * @param headers * @param requestBody * @param cellNameStr ??? * @return Cell?? */ private PersoniumResponse updateCellName(final HashMap<String, String> headers, final JSONObject requestBody, final String cellNameStr) { PersoniumResponse ret = null; PersoniumRestAdapter rest = new PersoniumRestAdapter(); String data = requestBody.toJSONString(); // ?URL? StringBuilder url = new StringBuilder(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME, cellNameStr)); try { // ret = rest.put(url.toString(), data, headers); } catch (Exception e) { fail(e.getMessage()); } return ret; } /** * ID??. * @param id ID */ private void deleteCell(String id) { this.deleteCell(id, true); } /** * ID??. * @param id ID * @param checkStatusCode ? */ private void deleteCell(String id, Boolean checkStatusCode) { // Cell PersoniumRestAdapter rest = new PersoniumRestAdapter(); PersoniumResponse delresponse = null; // HashMap<String, String> requestheaders = new HashMap<String, String>(); requestheaders.put(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); requestheaders.put(HttpHeaders.IF_MATCH, "*"); try { // // ???LOCATION?URL???????jerseyTestFramework????????? delresponse = rest.del(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME, id), requestheaders); } catch (Exception e) { fail(e.getMessage()); } if (checkStatusCode) { assertEquals(HttpStatus.SC_NO_CONTENT, delresponse.getStatusCode()); } } /** * . */ private void deleteCell() { deleteCell(cellNameToDelete); } }