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 com.fujitsu.dc.test.jersey; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.ws.rs.HttpMethod; 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.JSONArray; import org.json.simple.JSONObject; import org.junit.Ignore; import org.junit.runner.RunWith; import org.odata4j.core.ODataConstants; import com.fujitsu.dc.core.DcCoreException; import com.fujitsu.dc.core.model.Cell; import com.fujitsu.dc.test.unit.core.UrlUtils; import com.fujitsu.dc.test.utils.TResponse; import com.sun.jersey.test.framework.WebAppDescriptor; /** * OData??. */ @RunWith(DcRunner.class) @Ignore public class ODataCommon extends AbstractCase { /** * __count??????. */ public static final int COUNT_NONE = -1; /** DcResponse. */ private DcResponse res = null; /** ?URL???. */ public static final String ERROR_RESPONSE_LANG_EN = "en"; /** * . */ protected ODataCommon() { super(); } /** * . * @param value */ public ODataCommon(final String value) { super(value); } /** * . * @param build WebAppDescriptor */ public ODataCommon(WebAppDescriptor build) { super(build); } /** * DcResponse?setter. * @param value */ public void setResponse(DcResponse value) { res = value; } /** * ??url????. * @param url URL * @return ? */ public static DcResponse getOdataResource(String url) { DcResponse dcRes = null; if (url != null) { DcRequest req = DcRequest.get(url).header(HttpHeaders.AUTHORIZATION, AbstractCase.BEARER_MASTER_TOKEN) .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); dcRes = request(req); } return dcRes; } /** * ??url???. * @param url URL * @return ? */ public static DcResponse deleteOdataResource(String url) { DcResponse dcRes = null; if (url != null) { DcRequest req = DcRequest.delete(url) .header(HttpHeaders.AUTHORIZATION, AbstractCase.BEARER_MASTER_TOKEN) .header(HttpHeaders.IF_MATCH, "*"); dcRes = request(req); } return dcRes; } /** * ?????. */ public void cellDelete() { cellDelete(this.res); } /** * ?????. * @param response DcResponse */ public void cellDelete(DcResponse response) { if (response.getStatusCode() == HttpStatus.SC_CREATED) { // ???Cell?ID String cellId = response.getResponseHeaders(HttpHeaders.LOCATION)[0].getValue().split("'")[1]; // Cell?URL? // ???LOCATION?URL???????jerseyTestFramework????????? StringBuilder cellUrl = new StringBuilder(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME, cellId)); // Cell DcResponse resDel = restDelete(cellUrl.toString()); assertEquals(HttpStatus.SC_NO_CONTENT, resDel.getStatusCode()); } } /** * ?????. * @param cellId ???ID * @param checkStatusCode ?? */ public void cellDelete(String cellId, Boolean checkStatusCode) { // Cell?URL? // ???LOCATION?URL???????jerseyTestFramework????????? StringBuilder cellUrl = new StringBuilder(UrlUtils.unitCtl(Cell.EDM_TYPE_NAME, cellId)); // Cell DcResponse resDel = restDelete(cellUrl.toString()); if (checkStatusCode) { assertEquals(HttpStatus.SC_NO_CONTENT, resDel.getStatusCode()); } } /** * ?. * @param cellId ID */ public void cellDelete(String cellId) { this.cellDelete(cellId, true); } /** * ??????. * @param cellIdList ????ID */ public void cellDeleteList(ArrayList<String> cellIdList) { for (int i = 0; i < cellIdList.size(); i++) { cellDelete(cellIdList.get(i)); } } /** * ??. * @param req DcRequest * @param name ??Cell???? */ public void cellNormal(DcRequest req, String name) { req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name); this.res = request(req); checkSuccessResponse(res, MediaType.APPLICATION_JSON_TYPE); } /** * ??(return DcResponse). * @param req DcRequest * @param name ??Cell???? * @return res this.res */ public DcResponse cellNormalResponse(DcRequest req, String name) { req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name); this.res = request(req); checkSuccessResponse(res, MediaType.APPLICATION_JSON_TYPE); return this.res; } /** * ??(return DcResponse). * @param req DcRequest * @param name ??Cell???? * @return res this.res */ public DcResponse domainNormalResponse(DcRequest req, String name) { req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name); req.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); this.res = request(req); checkDomainSuccessResponse(res, MediaType.APPLICATION_JSON_TYPE); return this.res; } /** * ??. * @param req DcRequest * @param name ??Cell???? * @param errKey * @param errValue * @param errSc ? */ public void domainErrResponse(DcRequest req, String name, String errKey, String errValue, int errSc) { req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name).addJsonBody(errKey, errValue); req.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); this.res = request(req); assertEquals(errSc, this.res.getStatusCode()); } /** * Cell????. * @param req DcRequest */ public void cellListNormal(DcRequest req) { req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); this.res = request(req); checkCellListResponse(res, MediaType.APPLICATION_JSON_TYPE); } /** * Cell????(XML). * @param req DcRequest */ public void cellListNormalXml(DcRequest req) { req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); this.res = request(req); String resContentType = res.getFirstHeader(HttpHeaders.CONTENT_TYPE); assertEquals(MediaType.APPLICATION_ATOM_XML, resContentType.split(";")[0]); } /** * Domain????. * @param req DcRequest */ public void domainListNormal(DcRequest req) { req.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON); req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); this.res = request(req); checkDomainListResponse(res, MediaType.APPLICATION_JSON_TYPE); } /** * ????. * @param req DcRequest * @param name ??Cell???? */ public void cellErrorInvalidMethod(DcRequest req, String name) { req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name); this.res = request(req); // Cell???? // 405????? assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, res.getStatusCode()); // ?? checkErrorResponse(res.bodyAsJson(), "PR405-MC-0001"); } /** * ????. * @param req DcRequest */ public void cellErrorInvalidMethod(DcRequest req) { req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); req.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); this.res = request(req); // Cell???? // 405????? assertEquals(HttpStatus.SC_METHOD_NOT_ALLOWED, res.getStatusCode()); } /** * ????. * @param req DcRequest * @param name ??Cell???? */ public void cellConflict(DcRequest req, String name) { // 1 req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name); this.res = request(req); // Cell???? // 201????? assertEquals(HttpStatus.SC_CREATED, res.getStatusCode()); // 2?? DcResponse resConflict = request(req); // Cell???? // 409????? try { assertEquals(HttpStatus.SC_CONFLICT, resConflict.getStatusCode()); // ??? checkErrorResponse(resConflict.bodyAsJson(), DcCoreException.OData.ENTITY_ALREADY_EXISTS.getCode()); } finally { cellDelete(resConflict); } } /** * 2??Cell??2???Cell????????201?????409?????. * @param req DcRequest * @param method HTTP * @param url URL * @param name1 ????Cell???? * @param name2 ????Cell???? */ public void cellCreateResCheck(DcRequest req, String method, String url, String name1, String name2) { // 1 req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name1); this.res = request(req); // Cell???? // 201????? assertEquals(HttpStatus.SC_CREATED, res.getStatusCode()); // 2?? DcRequest req2 = null; if (method.equals(HttpMethod.POST)) { req2 = DcRequest.post(url); } req2.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name2); DcResponse resConflict = request(req2); // Cell???? try { if (name1.equals(name2)) { // 409????? assertEquals(HttpStatus.SC_CONFLICT, resConflict.getStatusCode()); // ??? checkErrorResponse(resConflict.bodyAsJson(), DcCoreException.OData.ENTITY_ALREADY_EXISTS.getCode()); } else { // 201????? assertEquals(HttpStatus.SC_CREATED, resConflict.getStatusCode()); } } finally { cellDelete(resConflict); } } /** * ???. * @param req DcRequest */ public void cellErrorEmptyBody(DcRequest req) { // Cell? req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN); this.res = request(req); // Cell???? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); // ?? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0001"); } /** * Json??????. * @param req DcRequest * @param name Cell?? */ public void cellErrorInvalidJson(DcRequest req, String name) { // Cell? req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addStringBody(name); this.res = request(req); // Cell???? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); // ?? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0001"); } /** * Xml??????. * @param req DcRequest * @param name Cell?? */ public void cellErrorInvalidXml(DcRequest req, String name) { // Cell? req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addStringBody(name); this.res = request(req); // Cell???? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); // ?? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0001"); } /** * ?????????. * @param req DcRequest * @param name Cell?? */ public void cellErrorInvalidField(DcRequest req, String name) { // Cell? String[] key = { "Name", "testKey" }; String[] value = { name, "testValue" }; req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody(key, value); this.res = request(req); // Cell???? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); // ??? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0007"); } /** * Cell???????. * @param req DcRequest * @param name Cell?? * @return ? */ private void cellErrorInvalidName(DcRequest req, String name) { // Cell? req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", name); this.res = request(req); // Cell???? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); } /** * Cell???0???. * @param req DcRequest */ public void cellErrorName0(DcRequest req) { cellErrorInvalidName(req, ""); // ?? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell???129???. * @param req DcRequest */ public void cellErrorName129(DcRequest req) { String name = "01234567890123456789012345678901234567890123456789" + "0123456789012345678901234567890123456789012345678901234567890123456789012345678"; cellErrorInvalidName(req, name); // ?? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell???az?09???_??. * @param req DcRequest */ public void cellErrorNameCharacter(DcRequest req) { String name = ""; cellErrorInvalidName(req, name); // ?? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell???__???. * @param req DcRequest */ public void cellErrorNameUnderbaer(DcRequest req) { String name = "__"; cellErrorInvalidName(req, name); // ?? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * Cell???__ctl???. * @param req DcRequest */ public void cellErrorNameUnderbaerCtl(DcRequest req) { String name = "__ctl"; cellErrorInvalidName(req, name); // ?? checkErrorResponse(res.bodyAsJson(), "PR400-OD-0006"); } /** * ???????. * @param req DcRequest * @param keyName ??? */ public void cellErrorBodyDateCtl(DcRequest req, String keyName) { // Cell? String[] key = { "Name", keyName }; String[] value = { "testCell", "/Date(0)/" }; req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody(key, value); this.res = request(req); // Cell???? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); } /** * ???????. * @param req DcRequest * @param keyName ??? */ public void cellErrorBodyMetadataCtl(DcRequest req, String keyName) { // Cell? String[] key = { "Name", keyName }; String[] value = { "testCell", "test" }; req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody(key, value); this.res = request(req); // Cell???? // 400????? assertEquals(HttpStatus.SC_BAD_REQUEST, res.getStatusCode()); } /** * Cell???Name?1??. * @param req DcRequest. */ public void cellName1(DcRequest req) { String id = "0"; req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", id); this.res = request(req); checkSuccessResponse(res, MediaType.APPLICATION_JSON_TYPE); } /** * Cell???Name?128??. * @param req DcRequest. */ public void cellName128(DcRequest req) { String id = "01234567890123456789012345678901234567890123456789" + "012345678901234567890123456789012345678901234567890123456789012345678901234567"; req.header(HttpHeaders.AUTHORIZATION, BEARER_MASTER_TOKEN).addJsonBody("Name", id); this.res = request(req); checkSuccessResponse(res, MediaType.APPLICATION_JSON_TYPE); } /** * ???.Authorization???. * @param req DcRequest. */ private void cellErrorAccess(DcRequest req) { this.res = request(req); // Cell???? // 401????? assertEquals(HttpStatus.SC_UNAUTHORIZED, res.getStatusCode()); // ?? checkErrorResponse(res.bodyAsJson(), "PR401-AU-0001"); } /** * ???.Authorization?. * @param req DcRequest. */ private void cellErrorAuth(DcRequest req) { this.res = request(req); // Cell???? // 401????? assertEquals(HttpStatus.SC_UNAUTHORIZED, res.getStatusCode()); // ?? checkErrorResponse(res.bodyAsJson(), "PR401-AU-0003"); } /** * ????. * @param req DcRequest */ public void cellErrorAuthNone(DcRequest req) { cellErrorAccess(req); } /** * ?????. * @param req DcRequest */ public void cellErrorAuthInvalid(DcRequest req) { req.header(HttpHeaders.AUTHORIZATION, "test"); cellErrorAuth(req); } /** * ???. * @param response response * @param contentType ContentType */ private void checkSuccessResponse(DcResponse response, MediaType contentType) { // Cell???? // 201????? assertEquals(HttpStatus.SC_CREATED, response.getStatusCode()); // LOCATION? // ??LOCATION??????? // LOCATION???NG?? Header[] resHeaders = response.getResponseHeaders(HttpHeaders.LOCATION); assertEquals(1, resHeaders.length); // DataServiceVersion?? Header[] resDsvHeaders = response.getResponseHeaders(ODataConstants.Headers.DATA_SERVICE_VERSION); assertEquals(1, resDsvHeaders.length); assertEquals("2.0", resDsvHeaders[0].getValue()); // Etag?????? Header[] resEtagHeaders = response.getResponseHeaders(HttpHeaders.ETAG); assertEquals(1, resEtagHeaders.length); if (contentType == MediaType.APPLICATION_JSON_TYPE) { // ContentType?? Header[] resContentTypeHeaders = response.getResponseHeaders(HttpHeaders.CONTENT_TYPE); assertEquals(1, resContentTypeHeaders.length); assertEquals(MediaType.APPLICATION_JSON, resContentTypeHeaders[0].getValue()); // ??Json??? checkCellJson(response.bodyAsJson(), resHeaders[0].getValue()); } else if (contentType == MediaType.APPLICATION_ATOM_XML_TYPE) { // ContentType?? Header[] resContentTypeHeaders = response.getResponseHeaders(HttpHeaders.CONTENT_TYPE); assertEquals(1, resContentTypeHeaders.length); assertEquals(MediaType.APPLICATION_ATOM_XML, resContentTypeHeaders[0].getValue()); // ??? checkCellXML(response.bodyAsXml()); } } /** * ???. * @param response response * @param contentType ContentType */ private void checkDomainSuccessResponse(DcResponse response, MediaType contentType) { // Cell???? // 201????? assertEquals(HttpStatus.SC_CREATED, response.getStatusCode()); // LOCATION? // ??LOCATION??????? // LOCATION???NG?? Header[] resHeaders = response.getResponseHeaders(HttpHeaders.LOCATION); assertEquals(1, resHeaders.length); // DataServiceVersion?? Header[] resDsvHeaders = response.getResponseHeaders(ODataConstants.Headers.DATA_SERVICE_VERSION); assertEquals(1, resDsvHeaders.length); assertEquals("2.0", resDsvHeaders[0].getValue()); // Etag?????? Header[] resEtagHeaders = response.getResponseHeaders(HttpHeaders.ETAG); assertEquals(1, resEtagHeaders.length); if (contentType == MediaType.APPLICATION_JSON_TYPE) { // ContentType?? Header[] resContentTypeHeaders = response.getResponseHeaders(HttpHeaders.CONTENT_TYPE); assertEquals(1, resContentTypeHeaders.length); assertEquals(MediaType.APPLICATION_JSON, resContentTypeHeaders[0].getValue()); // ??Json??? checkDomainJson(response.bodyAsJson(), resHeaders[0].getValue()); } else if (contentType == MediaType.APPLICATION_ATOM_XML_TYPE) { // ContentType?? Header[] resContentTypeHeaders = response.getResponseHeaders(HttpHeaders.CONTENT_TYPE); assertEquals(1, resContentTypeHeaders.length); assertEquals(MediaType.APPLICATION_ATOM_XML, resContentTypeHeaders[0].getValue()); // ??? checkCellXML(response.bodyAsXml()); } } /** * ???. * @param response ? * @param version ?Etag???? */ public static void checkCommonResponseHeader(TResponse response, long version) { checkCommonResponseHeader(response); // Etag?? checkEtag(response, version); } /** * ???. * @param response ? */ public static void checkCommonResponseHeader(TResponse response) { // DataServiceVersion?? response.checkHeader(ODataConstants.Headers.DATA_SERVICE_VERSION, "2.0"); // ContentType?? // response.checkHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); } /** * ???. * @param response ? * @param location ? */ public static void checkCommonResponseHeader(TResponse response, String location) { // LOCATION? response.checkHeader(HttpHeaders.LOCATION, location); checkCommonResponseHeader(response); } /** * ???. * @param json ? * @param uri ? */ public static void checkCommonResponseUri(final JSONObject json, final ArrayList<String> uri) { String value; int count = 0; JSONObject jsonResult; // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); assertEquals(uri.size(), results.size()); for (Object result : results) { jsonResult = (JSONObject) result; JSONObject resMetadata = (JSONObject) jsonResult.get("__metadata"); value = (String) resMetadata.get("uri"); assertEquals(uri.get(count++), value); } } /** * ????uri?????????. * @param json ? * @param uri ???????????uri */ public static void checkResponseUriNotExsists(final JSONObject json, final String uri) { String value; JSONObject jsonResult; // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); for (Object result : results) { jsonResult = (JSONObject) result; JSONObject resMetadata = (JSONObject) jsonResult.get("__metadata"); value = (String) resMetadata.get("uri"); assertFalse(uri.equals(value)); } } /** * Etag??????. * @param response ? * @param version ?Etag???? */ private static void checkEtag(TResponse response, long expectedVersion) { // Etag? String etag = response.getHeader(HttpHeaders.ETAG); long version = getEtagVersion(etag); assertEquals(expectedVersion, version); } /** * Json?????. * @param res ? * @param expectedCode ? * @param expectedMessage ? */ public static final void checkErrorResponseBody(TResponse res, String expectedCode, String expectedMessage) { String code = (String) ((JSONObject) res.bodyAsJson()).get("code"); String message = (String) ((JSONObject) ((JSONObject) res.bodyAsJson()).get("message")).get("value"); assertEquals(expectedCode, code); assertEquals(expectedMessage, message); } /** * Json?????. * @param res ? * @param expectedCode ? * @param expectedMessage ? */ public static final void checkErrorResponseBody(DcResponse res, String expectedCode, String expectedMessage) { JSONObject body = (JSONObject) res.bodyAsJson(); String code = (String) body.get("code"); String message = (String) ((JSONObject) body.get("message")).get("value"); assertEquals(expectedCode, code); assertEquals(expectedMessage, message); } /** * Json???. * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? */ public static final void checkResponseBody(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional) { checkResponseBody(json, locationHeader, type, additional, null, null); } /** * Json???. * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? * @param np NP??? */ public static final void checkResponseBody(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional, Map<String, String> np) { checkResponseBody(json, locationHeader, type, additional, np, null); } /** * Json???. * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? * @param np NP??? * @param etag etag??? */ public static final void checkResponseBody(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional, Map<String, String> np, String etag) { checkResponseBody(json, locationHeader, type, additional, np, etag, null); } /** * Json???. * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? * @param np NP??? * @param etag etag??? * @param published published??? */ public static final void checkResponseBody(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional, Map<String, String> np, String etag, String published) { JSONObject results = (JSONObject) ((JSONObject) json.get("d")).get("results"); checkResults(results, locationHeader, type, additional, np, etag, published); } /** * __count???. * @param json ? * @param count ? */ public static final void checkResponseBodyCount(final JSONObject json, int count) { // __count??? if (count == COUNT_NONE) { // __count?????? assertEquals(null, ((JSONObject) json.get("d")).get("__count")); } else { // __count???? int rescount = Integer.parseInt((String) ((JSONObject) json.get("d")).get("__count")); assertEquals(count, rescount); } } /** * Json???(). * @param json JSONObject * @param uri ?URI?? * @param type ? * @param additional ??? * @param key */ public static final void checkResponseBodyList(final JSONObject json, final Map<String, String> uri, final String type, final Map<String, Map<String, Object>> additional, String key) { checkResponseBodyList(json, uri, type, additional, key, null, null); } /** * Json???(). * @param json JSONObject * @param uri ?URI?? * @param type ? * @param additional ??? * @param key * @param count ?? * @param etag etag */ public static final void checkResponseBodyList(final JSONObject json, final Map<String, String> uri, final String type, final Map<String, Map<String, Object>> additional, String key, int count, Map<String, String> etag) { // __count?? checkResponseBodyCount(json, count); // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); for (Object result : results) { if (etag != null) { // Etag?? checkResponseEtagList(result, etag, key); } String reskey = (String) ((JSONObject) result).get(key); checkResults((JSONObject) result, uri.get(reskey), type, additional.get(reskey)); } } /** * Json???(). * @param json JSONObject * @param uri ?URI?? * @param type ? * @param additional ??? * @param key * @param np NP??? * @param etag etag */ public static final void checkResponseBodyList(final JSONObject json, final Map<String, String> uri, final String type, final Map<String, Map<String, Object>> additional, String key, Map<String, String> np, Map<String, String> etag) { // __count?? checkResponseBodyCount(json, COUNT_NONE); // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); // ??? if (uri == null) { assertEquals(0, results.size()); } else { assertEquals(uri.size(), results.size()); } // results?? for (Object result : results) { if (etag != null) { // Etag?? checkResponseEtagList(result, etag, key); } String reskey = (String) ((JSONObject) result).get(key); checkResults((JSONObject) result, uri.get(reskey), type, additional.get(reskey), np, null); } } /** * Json???(). * @param json JSONObject * @param uri ?URI?? * @param type ? * @param additional ??? * @param key * @param count ?? * @param np NP??? * @param etag etag */ public static final void checkResponseBodyList(final JSONObject json, final Map<String, String> uri, final String type, final Map<String, Map<String, Object>> additional, String key, int count, final Map<String, Map<String, String>> np, Map<String, String> etag) { // __count?? checkResponseBodyCount(json, count); // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); for (Object result : results) { String reskey = (String) ((JSONObject) result).get(key); checkResults((JSONObject) result, uri.get(reskey), type, additional.get(reskey), np.get(reskey), null); } } /** * Json???(). * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? */ public static final void checkResponseBodyList(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional) { // __count?? checkResponseBodyCount(json, COUNT_NONE); // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); for (Object result : results) { checkResults((JSONObject) result, locationHeader, type, additional); } } /** * Json???(). * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? * @param count ?? */ public static final void checkResponseBodyList(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional, int count) { // __count?? checkResponseBodyCount(json, count); // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); for (Object result : results) { checkResults((JSONObject) result, locationHeader, type, additional); } } /** * Json???(). * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? * @param etag etag */ public static final void checkResponseBodyList(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional, final Map<String, String> etag) { // __count?? checkResponseBodyCount(json, COUNT_NONE); // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); for (Object result : results) { checkResults((JSONObject) result, locationHeader, type, additional); } } /** * Etag?(). * @param result result * @param etag etag * @param key key */ private static void checkResponseEtagList(Object result, Map<String, String> etag, String key) { String expectEtag; String resId = (String) ((JSONObject) result).get(key); JSONObject resMetadata = (JSONObject) ((JSONObject) result).get("__metadata"); String resEtag = (String) resMetadata.get("etag"); expectEtag = etag.get(resId); if (expectEtag != null) { assertEquals(expectEtag, resEtag); } else { fail(); } } /** * Json??results?. * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? */ public static final void checkResults(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional) { checkResults(json, locationHeader, type, additional, null, null); } /** * Json??results?. * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? * @param np NP??? * @param etag etag??? */ public static final void checkResults(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional, Map<String, String> np, String etag) { checkResults(json, locationHeader, type, additional, np, etag, null); } /** * Json??results?. * @param json JSONObject * @param locationHeader LOCATION * @param type ? * @param additional ??? * @param np NP??? * @param etag etag??? * @param published published??? */ @SuppressWarnings("unchecked") public static final void checkResults(final JSONObject json, final String locationHeader, final String type, final Map<String, Object> additional, Map<String, String> np, String etag, String published) { String value; // d:__published????????? value = (String) json.get("__published"); assertNotNull(value); assertTrue(validDate(value)); // d:__published?????????? if (published != null) { assertEquals(published, value); } // d:__updated????????? value = (String) json.get("__updated"); assertNotNull(value); assertTrue(validDate(value)); // __metadata? JSONObject metadata = (JSONObject) json.get("__metadata"); // uri? value = (String) metadata.get("uri"); assertNotNull(value); if (locationHeader != null) { // LOCATION????? assertEquals(locationHeader, value); } // etag? value = (String) metadata.get("etag"); if (etag != null) { assertEquals(etag, value); } // type? value = (String) metadata.get("type"); assertNotNull(value); assertEquals(type, value); if (additional != null) { // ?? for (Map.Entry<String, Object> e : additional.entrySet()) { Object jsonValue = json.get(e.getKey()); if (jsonValue instanceof Integer) { jsonValue = ((Integer) jsonValue).longValue(); } if (jsonValue instanceof JSONArray) { JSONArray array = ((JSONArray) jsonValue); for (int i = 0; i < array.size(); i++) { if (array.get(i) instanceof Integer) { array.set(i, ((Integer) array.get(i)).longValue()); } } } Object expected = e.getValue(); if (expected instanceof Integer) { expected = ((Integer) expected).longValue(); } if (expected instanceof JSONArray) { JSONArray array = ((JSONArray) expected); for (int i = 0; i < array.size(); i++) { if (array.get(i) instanceof Integer) { array.set(i, ((Integer) array.get(i)).longValue()); } } } assertEquals(expected, jsonValue); } } if (np != null) { // NP??? for (Map.Entry<String, String> e : np.entrySet()) { JSONObject jsonValue = (JSONObject) json.get(e.getKey()); JSONObject defferedValue = (JSONObject) jsonValue.get("__deferred"); String uriValue = (String) defferedValue.get("uri"); assertEquals(e.getValue(), uriValue); } } } /** * $link???. * @param json ? * @param uri uri? */ public static void checkLinResponseBody(final JSONObject json, final ArrayList<String> uri) { String value; JSONObject jsonResult; // results JSONArray results = (JSONArray) ((JSONObject) json.get("d")).get("results"); assertEquals(uri.size(), results.size()); for (Object result : results) { jsonResult = (JSONObject) result; value = (String) jsonResult.get("uri"); assertTrue("expected uri doesn't contain [" + value + "]", uri.contains(value)); } } /** * Etag???. * @param etag Etag * @return ? */ public static long getEtagVersion(String etag) { // version? Pattern pattern = Pattern.compile("^W/\"([0-9]+)-([0-9]+)\"$"); Matcher m = pattern.matcher(etag); return Long.parseLong(m.replaceAll("$1")); } /** * Etag?Updated?. * @param etag Etag * @return Updated? */ public static long getEtagUpdated(String etag) { // version? Pattern pattern = Pattern.compile("^W/\"([0-9]+)-([0-9]+)\"$"); Matcher m = pattern.matcher(etag); return Long.parseLong(m.replaceAll("$2")); } /** * ??published?. * @param response response * @return published? */ public static String getPublished(TResponse response) { JSONObject results = (JSONObject) ((JSONObject) response.bodyAsJson().get("d")).get("results"); return (String) results.get("__published"); } /** * ??published?. * @param response response * @return published? */ public static String getPublished(DcResponse response) { JSONObject results = (JSONObject) ((JSONObject) response.bodyAsJson().get("d")).get("results"); return (String) results.get("__published"); } /** * ??Etag?. * @param response response * @return Etag? */ public static String getEtag(DcResponse response) { return response.getResponseHeaders(HttpHeaders.ETAG)[0].getValue(); } }