Java tutorial
/** * Personium * Copyright 2014 - 2017 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.jersey.engine.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.io.IOException; import javax.ws.rs.core.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.util.EntityUtils; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; import io.personium.client.DaoException; import io.personium.client.http.PersoniumRequestBuilder; import io.personium.client.http.PersoniumResponse; import io.personium.jersey.engine.test.categories.Integration; /** * ?. */ @RunWith(PersoniumEngineRunner.class) @Category({ Integration.class }) public class ScriptTest extends ScriptTestBase { /** * . */ public ScriptTest() { super("io.personium.engine"); } /** * Cell?CURD. */ @Test public final void cellTest() { callService("cell.js"); } /** * Box?CURD. */ @Test public final void boxTest() { callService("box.js"); } /** * Account?CURD. */ @Test public final void accountTest() { callService("account.js"); } /** * Account??CURD. */ @Test public final void accountValidateTest() { callService("accountValidate.js"); } /** * ServiceSubject?. */ @Test public final void serviceSubjectTest() { callService("serviceSubject.js"); } /** * ChangePassword?CURD. */ @Test public final void changePasswordTest() { callService("changePassword.js"); } /** * ChangeMyPassword?CURD. */ @Test public final void changeMyPasswordTest() { callService("changeMyPassword.js"); } /** * Event?. */ @Test public final void eventTest() { callService("event.js"); } /** * CellLevelEvent?. */ @Test public final void cellLevelEventTest() { callService("cellLevelEvent.js"); } /** * ??. */ @Test public final void cellLevelEventLogTest() { callService("cellLevelEventLog.js"); } /** * Role?CRUD. */ @Test public final void roleTest() { callService("role.js"); } /** * Role(?)?CRUD. */ @Test public final void roleComplexTest() { callService("roleComplex.js"); } /** * Relation?CRUD. */ @Test public final void relationTest() { callService("relation.js"); } /** * Relation(?)?CRUD. */ @Test public final void relationComplexTest() { callService("relationComplex.js"); } /** * ExtRole?CRUD. */ @Test public final void extRoleTest() { callService("extrole.js"); } /** * ExtCell?CRUD. */ @Test public final void extCellTest() { callService("extcell.js"); } /** * MKCOL?. */ @Test public final void mkColTest() { callService("mkcol.js"); } /** * MKODATA?. */ @Test public final void mkOdataTest() { callService("mkodata.js"); } /** * MKSERVICE?. */ @Test public final void mkServiceTest() { callService("mkservice.js"); } /** * asExtCell?. */ @Test public final void asExtCellTest() { callService("asExtCell.js"); } /** * asTransCellAccessToken?. */ @Test public final void asTransCellAccessTokenTest() { callService("asTransCellAccessToken.js"); } /** * asRefreshToken?. */ @Test public final void asRefreshTokenTest() { callService("asRefreshToken.js"); } /** * asRefreshTokenError?. */ @Test public final void asRefreshTokenErrorTest() { callService("asRefreshTokenError.js"); } /** * asSchema?. */ @Test public final void asSchemaTest() { callService("asSchema.js"); } /** * asSchemaByToken?. */ @Test public final void asSchemaByTokenTest() { callService("asSchemaByToken.js"); } /** * asSelf & asClient?. */ @Test public final void asSelfClientTest() { callService("asSelfClient.js"); } /** * linkRelationRole?. */ @Test public final void linkRelationRoleTest() { callService("linkRelationRole.js"); } /** * linkRelationExtCell?. */ @Test public final void linkRelationExtCellTest() { callService("linkRelationExtCell.js"); } /** * linkExtCellRelation?. */ @Test public final void linkExtCellRelationTest() { callService("linkExtCellRelation.js"); } /** * linkExtCellRole?. */ @Test public final void linkExtCellRoleTest() { callService("linkExtCellRole.js"); } /** * linkRoleAccount?. */ @Test public final void linkRoleAccountTest() { callService("linkRoleAccount.js"); } /** * linkRoleExtCell?. */ @Test public final void linkRoleExtCellTest() { callService("linkRoleExtCell.js"); } /** * linkRoleRelation?. */ @Test public final void linkRoleRelationTest() { callService("linkRoleRelation.js"); } /** * linkExtRoleRole?. */ @Test public final void linkExtRoleRoleTest() { callService("linkExtRoleRole.js"); } /** * AssociationEnd?. */ @Test public final void associationEndTest() { callService("associationEnd.js"); } /** * ComplexType?. */ @Test public final void complexTypeTest() { callService("complexType.js"); } /** * Property?. */ @Test public final void propertyTest() { callService("property.js"); } /** * ComplexTypeProperty?. */ @Test public final void complexTypePropertyTest() { callService("complexTypeProperty.js"); } /** * linkAssociationEnd?. */ @Test public final void linkAssociationEndTest() { callService("linkAssociationEnd.js"); } /** * . */ @Test public final void userDataTest() { callService("userDataTest.js"); } /** * ACL. */ @Test public final void aclTest() { callService("acl.js"); } /** * ACL??. */ @Test public final void aclNormalVariationTest() { callService("aclNormalVariation.js"); } /** * ACL??. */ @Test public final void aclErrorVariationTest() { callService("aclErrorVariation.js"); } /** * NavigationPropertyPost?. */ @Test public final void navigationPropertyPostTest() { callService("navigationPropertyPost.js"); } /** * Unit?. */ @Test public final void upgradeUnitTest() { callService("upgradeUnit.js"); } /** * UserData?Query. */ @Test public final void userDetaQueryTest() { callService("userDataQuery.js"); } /** * UserData?Query. */ @Test public final void userDetaQueryExpandTest() { callService("userDataQueryExpand.js"); } /** * Java???????. */ @Test public final void Java???????() { callService("accessDenyJavaAPI.js"); } /** * ?????wrapper?????????. */ @Test public final void ?????wrapper?????????() { callService("cantCallConstructor.js"); } /** * require????????????. */ @Test public final void require????????????() { if (isServiceTest) { // ? Dav?put putScript("requireEvalErrorSub.js", "requireEvalErrorSub.js"); callService("requireEvalError.js"); } if (isServiceTest) { // ?Dav?del try { testSvcCol.del("requireEvalErrorSub.js"); } catch (DaoException e) { fail(e.getMessage()); } } } /** * require??????????????. */ @Test public final void require??????????????() { callService("requireNoFile.js"); } /** * WebDav??????. */ @Test public final void WebDav??????() { String url; String testSrc = "DAOBinaryIO.js"; HttpUriRequest req = null; String reqBody = "reqbodydata------\naaa\nbbb\n?????\n"; try { if (isServiceTest) { // ? Dav?put putScript(testSrc, "test.js"); url = requestUrl(); } else { url = requestUrl(testSrc); } // ? req = new PersoniumRequestBuilder().url(url).method("POST").body(reqBody).token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } HttpResponse objResponse; objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); assertEquals(HttpStatus.SC_OK, dcRes.getStatusCode()); assertEquals(reqBody, dcRes.bodyAsString()); } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ??????. */ @Test public final void ??????() { String url; String testSrc = "DAOBinaryRead.js"; HttpUriRequest req = null; String reqBody = "reqbodydata------\naaa\nbbb\n?????\n"; try { if (isServiceTest) { // ? Dav?put putScript(testSrc, "test.js"); url = requestUrl(); } else { url = requestUrl(testSrc); } // ? req = new PersoniumRequestBuilder().url(url).method("POST").body(reqBody).token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } HttpResponse objResponse; objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); assertEquals(HttpStatus.SC_OK, dcRes.getStatusCode()); assertEquals(reqBody, dcRes.bodyAsString()); } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ???Service?. */ @Test public final void serviceNotFound() { if (isServiceTest) { String url = String.format("%s/%s/%s/%s/test?cell=%s", baseUrl, cellName, boxName, "notfoundsvccol", cellName); try { HttpUriRequest req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } request(req); fail(); } catch (DaoException e) { assertEquals("404", e.getCode()); } } } /** * ????Service. */ @Test public final void serviceEmpty() { String url; HttpUriRequest req = null; try { if (isServiceTest) { // ? Dav?put putScript("empty.js", "test.js"); url = requestUrl(); } else { url = requestUrl("empty.js"); } // ? req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } HttpResponse objResponse; objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); // ???Content-Lengh ??????? int statusCode = objResponse.getStatusLine().getStatusCode(); assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, statusCode); String contentLength = dcRes.getHeader(HttpHeaders.CONTENT_LENGTH); if (contentLength == null || contentLength.length() <= 0) { fail("Content-Lengh header value does not exist"); } } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ????????????. */ @Test public final void serviceJapanese() { String url; String testSrc = "japanese.js"; HttpUriRequest req = null; try { if (isServiceTest) { // ? Dav?put putScript(testSrc, "test.js"); url = requestUrl(); } else { url = requestUrl(testSrc); } // ? req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } HttpResponse objResponse; objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); assertEquals(HttpStatus.SC_OK, dcRes.getStatusCode()); assertEquals("??", dcRes.bodyAsString()); } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ???????????HTTP????????. */ @Test public final void nonSupportedResponseCode() { String url; HttpUriRequest req = null; try { String[][] jsList = {{"nonSupportedResponseCode105.js", "105" }, {"nonSupportedResponseCode301.js", "301" }, {"nonSupportedResponseCode303.js", "303" }, {"nonSupportedResponseCode307.js", "307" } }; for (String[] testSrc : jsList) { // ? Dav?put if (isServiceTest) { putScript(testSrc[0], "test.js"); url = requestUrl(); } else { url = requestUrl(testSrc[0]); } // ? req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } // ??? HttpResponse objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); assertEquals(500, dcRes.getStatusCode()); String expectedMessage = String.format("Server Error : response status illegal type. status: %s", testSrc[1]); assertEquals(expectedMessage, dcRes.bodyAsString()); EntityUtils.consume(objResponse.getEntity()); } } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ???????????HTTP????????(???). */ @Test public final void nonSupportedResponseCodeWithQuery() { String url; HttpUriRequest req = null; try { String[][] jsList = {{"returnResponseCodewithQuery.js", "105" }, {"returnResponseCodewithQuery.js", "301" }, {"returnResponseCodewithQuery.js", "303" }, {"returnResponseCodewithQuery.js", "307" } }; for (String[] testSrc : jsList) { // ? Dav?put if (isServiceTest) { putScript(testSrc[0], "test.js"); url = requestUrl() + "&status=" + testSrc[1]; } else { url = requestUrl(testSrc[0]) + "&status=" + testSrc[1]; } // ? req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } // ??? HttpResponse objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); assertEquals(500, dcRes.getStatusCode()); String expectedMessage = String.format("Server Error : response status illegal type. status: %s", testSrc[1]); assertEquals(expectedMessage, dcRes.bodyAsString()); EntityUtils.consume(objResponse.getEntity()); } } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ??????????????????. */ @Test public final void nonSupportedResponseHeader() { String url; String testSrc = "nonSupportedResponseHeader.js"; HttpUriRequest req = null; try { if (isServiceTest) { // ? Dav?put putScript(testSrc, "test.js"); url = requestUrl(); } else { url = requestUrl(testSrc); } // ? req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } HttpResponse objResponse; objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); assertEquals(200, dcRes.getStatusCode()); assertEquals("header value", dcRes.getHeader("Invalid-custom-header")); assertEquals("??", dcRes.bodyAsString()); } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ?Transfer-Encoding????????. */ @Test public final void nonSupportedResponseHeaderTransferEncoding() { String url; String testSrc = "nonSupportedResponseHeaderTransferEncoding.js"; HttpUriRequest req = null; try { if (isServiceTest) { // ? Dav?put putScript(testSrc, "test.js"); url = requestUrl(); } else { url = requestUrl(testSrc); } // ? req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } HttpResponse objResponse; objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); assertEquals(200, dcRes.getStatusCode()); assertEquals("", dcRes.getHeader("Transfer-Encoding")); assertEquals("??", dcRes.bodyAsString()); } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ?Content-type???????????????. */ @Test public final void unmatchedBodyAndContentType() { String url; String testSrc = "unmatchedBodyAndContentType.js"; HttpUriRequest req = null; try { if (isServiceTest) { // ? Dav?put putScript(testSrc, "test.js"); url = requestUrl(); } else { url = requestUrl(testSrc); } // ? req = new PersoniumRequestBuilder().url(url).method("GET").token(token).build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } HttpResponse objResponse; objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); String bodyAsString = dcRes.bodyAsString(); assertEquals(200, dcRes.getStatusCode()); assertEquals("??", bodyAsString); } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ??Illegal????????. */ @Test public final void ??Illegal? ???????() { // ????? IllegalResponseFixture[] datas = { // ??JSGI????????? new IllegalResponseFixture("Server Error : not NativeObject", "[]"), // ? new IllegalResponseFixture("Server Error : response status illegal type.", "{status:\"hoge\",headers:{},body:[]}"), // ?4? new IllegalResponseFixture("Server Error : response status illegal type. status: 2000", "{status:2000,headers:{},body:[]}"), // ? new IllegalResponseFixture("Server Error : not headers", "{status:200,body:[]}"), // ?????? new IllegalResponseFixture("Server Error : header key format error", "{status:200,headers:{100:\"hoge\"},body:[]}"), // ?????? new IllegalResponseFixture("Server Error : header value format error", "{status:200,headers:{head:1000},body:[]}"), // ?Content-Type????? new IllegalResponseFixture("Server Error : Response header parsing media type.", "{status:200,headers:{\"Content-Type\":\"hoge\"},body:[]}"), // ?Content-Type?charset???? new IllegalResponseFixture("Server Error : response charset illegal type.", "{status:200,headers:{\"Content-Type\":\"plain/text;charset=hoge\"},body:[]}"), // ?? new IllegalResponseFixture("Server Error : response body undefined forEach.", "{status:200,headers:{}}"), // ??forEach? new IllegalResponseFixture("Server Error : response body undefined forEach.", "{status:200,headers:{},body:{}}"), // ???????? new IllegalResponseFixture("Server Error : response body illegal type.", "{status:200,headers:{},body:[1]}") }; String url; String testSrc = "responseIllegal.js"; HttpUriRequest req = null; try { if (isServiceTest) { // ? Dav?put putScript(testSrc, "test.js"); url = requestUrl(); } else { url = requestUrl(testSrc); } for (int i = 0; i < datas.length; i++) { // ? req = new PersoniumRequestBuilder().url(url).method("POST").body(datas[i].requestJson).token(token) .build(); req.setHeader(KEY_HEADER_BASEURL, baseUrl); String version = getVersion(); if (version != null && !(version.equals(""))) { req.setHeader("X-Personium-Version", version); } HttpResponse objResponse; objResponse = httpClient.execute(req); PersoniumResponse dcRes = new PersoniumResponse(objResponse); assertEquals(HttpStatus.SC_INTERNAL_SERVER_ERROR, dcRes.getStatusCode()); assertEquals(datas[i].responseMessage, dcRes.bodyAsString()); } } catch (DaoException e) { fail(e.getMessage()); } catch (ClientProtocolException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } finally { if (isServiceTest) { // ?Dav?del try { testSvcCol.del("test.js"); } catch (DaoException e) { fail(e.getMessage()); } } } } /** * ??????2???. */ @Test public final void ??????2 ???() { callService("cell.js"); callService("cell.js"); } }