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.box.dav.file; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.ws.rs.core.MediaType; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; import com.fujitsu.dc.core.DcCoreException; import com.fujitsu.dc.test.categories.Integration; import com.fujitsu.dc.test.categories.Regression; import com.fujitsu.dc.test.categories.Unit; import com.fujitsu.dc.test.jersey.AbstractCase; import com.fujitsu.dc.test.jersey.DcRequest; import com.fujitsu.dc.test.jersey.DcResponse; import com.fujitsu.dc.test.jersey.DcRunner; import com.fujitsu.dc.test.jersey.ODataCommon; import com.fujitsu.dc.test.unit.core.UrlUtils; import com.fujitsu.dc.test.utils.DavResourceUtils; import com.fujitsu.dc.test.utils.TResponse; import com.sun.jersey.test.framework.JerseyTest; import static org.fest.assertions.Assertions.assertThat; import static org.junit.Assert.assertEquals; /** * ServiceSource??MOVE????. <br /> * ?????????????????? * <ul> * <li></li> * <li></li> * <li></li> * </ul> * ????????WebDAV???????????????. * @see com.fujitsu.dc.test.jersey.box.dav.file.MoveFileTest */ @RunWith(DcRunner.class) @Category({ Unit.class, Integration.class, Regression.class }) public class MoveServiceSourceHeaderValidateTest extends JerseyTest { private static final String TOKEN = AbstractCase.MASTER_TOKEN_NAME; private static final String CELL_NAME = "testcell1"; private static final String BOX_NAME = "box1"; private static final String FILE_NAME = "file1.txt"; private static final String FILE_BODY = "testFileBody"; /** * . */ public MoveServiceSourceHeaderValidateTest() { super("com.fujitsu.dc.core.rs"); } /** * ServiceSource?MOVE?Destination?????400????. */ @Test public final void ServiceSource?MOVE?Destination?????400 ????() { final String svcCol = "serviceCol"; try { // ? DavResourceUtils.createServiceCollection(AbstractCase.BEARER_MASTER_TOKEN, HttpStatus.SC_CREATED, CELL_NAME, BOX_NAME, svcCol); DavResourceUtils.createWebDavFile(TOKEN, CELL_NAME, BOX_NAME + "/" + svcCol + "/__src/" + FILE_NAME, FILE_BODY, MediaType.TEXT_PLAIN, HttpStatus.SC_CREATED); // File? String url = UrlUtils.box(CELL_NAME, BOX_NAME, svcCol, "__src", FILE_NAME); DcRequest req = DcRequest.move(url); req.header(HttpHeaders.AUTHORIZATION, AbstractCase.BEARER_MASTER_TOKEN); // DcResponse response = AbstractCase.request(req); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); DcCoreException expectedException = DcCoreException.Dav.REQUIRED_REQUEST_HEADER_NOT_EXIST .params(HttpHeaders.DESTINATION); ODataCommon.checkErrorResponseBody(response, expectedException.getCode(), expectedException.getMessage()); } finally { DavResourceUtils.deleteWebDavFile(CELL_NAME, TOKEN, BOX_NAME, svcCol + "/__src/" + FILE_NAME); DavResourceUtils.deleteCollection(CELL_NAME, BOX_NAME, svcCol, TOKEN, -1); } } /** * ServiceSource?MOVE?Overwrite?????????????400????. */ @Test public final void ServiceSource?MOVE?Overwrite?????????????400 ????() { final String svcCol = "serviceCol"; final String destFileName = "destFile.txt"; final String destination = UrlUtils.box(CELL_NAME, BOX_NAME, destFileName); try { // ? DavResourceUtils.createServiceCollection(AbstractCase.BEARER_MASTER_TOKEN, HttpStatus.SC_CREATED, CELL_NAME, BOX_NAME, svcCol); DavResourceUtils.createWebDavFile(TOKEN, CELL_NAME, BOX_NAME + "/" + svcCol + "/__src/" + FILE_NAME, FILE_BODY, MediaType.TEXT_PLAIN, HttpStatus.SC_CREATED); // File? String url = UrlUtils.box(CELL_NAME, BOX_NAME, svcCol, "__src", FILE_NAME); DcRequest req = DcRequest.move(url); req.header(HttpHeaders.AUTHORIZATION, AbstractCase.BEARER_MASTER_TOKEN); req.header(HttpHeaders.DESTINATION, destination); req.header(HttpHeaders.OVERWRITE, "Y"); // DcResponse response = AbstractCase.request(req); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.SC_BAD_REQUEST); DcCoreException expectedException = DcCoreException.Dav.INVALID_REQUEST_HEADER.params( HttpHeaders.OVERWRITE, "Y"); ODataCommon.checkErrorResponseBody(response, expectedException.getCode(), expectedException.getMessage()); // ????? DavResourceUtils .getWebDav(CELL_NAME, TOKEN, BOX_NAME, svcCol + "/__src/" + FILE_NAME, HttpStatus.SC_OK); // ??????? DavResourceUtils.getWebDav(CELL_NAME, TOKEN, BOX_NAME, destFileName, HttpStatus.SC_NOT_FOUND); } finally { DavResourceUtils.deleteWebDavFile(CELL_NAME, TOKEN, BOX_NAME, svcCol + "/__src/" + FILE_NAME); DavResourceUtils.deleteCollection(CELL_NAME, BOX_NAME, svcCol, TOKEN, -1); } } /** * ServiceSource?MOVE?Depth?infinity?????400????. */ @Test public final void ServiceSource?MOVE?Depth?infinity?????400 ????() { final String svcCol = "serviceCol"; final String depth = "1"; final String destFileName = "destFile.txt"; final String destination = UrlUtils.box(CELL_NAME, BOX_NAME, destFileName); try { // ? DavResourceUtils.createServiceCollection(AbstractCase.BEARER_MASTER_TOKEN, HttpStatus.SC_CREATED, CELL_NAME, BOX_NAME, svcCol); DavResourceUtils.createWebDavFile(TOKEN, CELL_NAME, BOX_NAME + "/" + svcCol + "/__src/" + FILE_NAME, FILE_BODY, MediaType.TEXT_PLAIN, HttpStatus.SC_CREATED); // File? String url = UrlUtils.box(CELL_NAME, BOX_NAME, svcCol, "__src", FILE_NAME); DcRequest req = DcRequest.move(url); req.header(HttpHeaders.AUTHORIZATION, AbstractCase.BEARER_MASTER_TOKEN); req.header(HttpHeaders.DESTINATION, destination); req.header(HttpHeaders.DEPTH, depth); // DcResponse response = AbstractCase.request(req); assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode()); DcCoreException expectedException = DcCoreException.Dav.INVALID_DEPTH_HEADER.params(depth); ODataCommon.checkErrorResponseBody(response, expectedException.getCode(), expectedException.getMessage()); // ????? DavResourceUtils.getWebDav(CELL_NAME, TOKEN, BOX_NAME, svcCol + "/__src/" + FILE_NAME, HttpStatus.SC_OK); // ??????? DavResourceUtils.getWebDav(CELL_NAME, TOKEN, BOX_NAME, destFileName, HttpStatus.SC_NOT_FOUND); } finally { DavResourceUtils.deleteWebDavFile(CELL_NAME, TOKEN, BOX_NAME, svcCol + "/__src/" + FILE_NAME); DavResourceUtils.deleteCollection(CELL_NAME, BOX_NAME, svcCol, TOKEN, -1); } } /** * ServiceSource?MOVE?If_Match?????????412????. */ @Test public final void ServiceSource?MOVE?If_Match???? ?????412 ????() { final String svcCol = "serviceCol"; final String depth = "infinity"; final String destFileName = "destFile.txt"; final String destination = UrlUtils.box(CELL_NAME, BOX_NAME, destFileName); try { // ? DavResourceUtils.createServiceCollection(AbstractCase.BEARER_MASTER_TOKEN, HttpStatus.SC_CREATED, CELL_NAME, BOX_NAME, svcCol); DavResourceUtils.createWebDavFile(TOKEN, CELL_NAME, BOX_NAME + "/" + svcCol + "/__src/" + FILE_NAME, FILE_BODY, MediaType.TEXT_PLAIN, HttpStatus.SC_CREATED); // ??Etag?? TResponse res = DavResourceUtils.getWebDav(CELL_NAME, TOKEN, BOX_NAME, svcCol + "/__src/" + FILE_NAME, HttpStatus.SC_OK); String etag = res.getHeader(HttpHeaders.ETAG); // File? String url = UrlUtils.box(CELL_NAME, BOX_NAME, svcCol, "__src", FILE_NAME); DcRequest req = DcRequest.move(url); req.header(HttpHeaders.AUTHORIZATION, AbstractCase.BEARER_MASTER_TOKEN); req.header(HttpHeaders.DESTINATION, destination); req.header(HttpHeaders.DEPTH, depth); req.header(HttpHeaders.OVERWRITE, "T"); req.header(HttpHeaders.IF_MATCH, etag + "dummy"); // DcResponse response = AbstractCase.request(req); assertEquals(HttpStatus.SC_PRECONDITION_FAILED, response.getStatusCode()); DcCoreException expectedException = DcCoreException.Dav.ETAG_NOT_MATCH; ODataCommon.checkErrorResponseBody(response, expectedException.getCode(), expectedException.getMessage()); // ????? DavResourceUtils.getWebDav(CELL_NAME, TOKEN, BOX_NAME, svcCol + "/__src/" + FILE_NAME, HttpStatus.SC_OK); // ??????? DavResourceUtils.getWebDav(CELL_NAME, TOKEN, BOX_NAME, destFileName, HttpStatus.SC_NOT_FOUND); } finally { DavResourceUtils.deleteWebDavFile(CELL_NAME, TOKEN, BOX_NAME, svcCol + "/__src/" + FILE_NAME); DavResourceUtils.deleteCollection(CELL_NAME, BOX_NAME, svcCol, TOKEN, -1); } } /** * Etag???. * @param etag Etag * @return ? */ public static long getEtagVersion(String etag) { // version? Pattern pattern = Pattern.compile("^\"([0-9]+)-([0-9]+)\"$"); Matcher m = pattern.matcher(etag); return Long.parseLong(m.replaceAll("$1")); } }