mirror of
https://github.com/sismics/docs.git
synced 2024-11-22 05:57:57 +01:00
cleanup duplicate code
This commit is contained in:
parent
5cdbe9338b
commit
a0e89103af
@ -100,6 +100,12 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jersey.media</groupId>
|
||||||
|
<artifactId>jersey-media-multipart</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<!-- Install test jar -->
|
<!-- Install test jar -->
|
||||||
|
@ -1,13 +1,20 @@
|
|||||||
package com.sismics.docs.rest.util;
|
package com.sismics.docs.rest.util;
|
||||||
|
|
||||||
|
import com.google.common.io.Resources;
|
||||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||||
|
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
|
||||||
|
import org.glassfish.jersey.media.multipart.MultiPartFeature;
|
||||||
|
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
|
||||||
|
|
||||||
import javax.json.JsonObject;
|
import javax.json.JsonObject;
|
||||||
import javax.ws.rs.client.Entity;
|
import javax.ws.rs.client.Entity;
|
||||||
import javax.ws.rs.client.WebTarget;
|
import javax.ws.rs.client.WebTarget;
|
||||||
import javax.ws.rs.core.Form;
|
import javax.ws.rs.core.Form;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.NewCookie;
|
import javax.ws.rs.core.NewCookie;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST client utilities.
|
* REST client utilities.
|
||||||
@ -140,4 +147,19 @@ public class ClientUtil {
|
|||||||
}
|
}
|
||||||
return authToken;
|
return authToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String addFileToDocument(String file, String filename, String token, String documentId) throws IOException {
|
||||||
|
try (InputStream is = Resources.getResource(file).openStream()) {
|
||||||
|
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, filename);
|
||||||
|
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
||||||
|
JsonObject json = resource
|
||||||
|
.register(MultiPartFeature.class)
|
||||||
|
.path("/file").request()
|
||||||
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, token)
|
||||||
|
.put(Entity.entity(multiPart.field("id", documentId).bodyPart(streamDataBodyPart),
|
||||||
|
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
||||||
|
return json.getString("id");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,21 +85,9 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
Assert.assertNotNull(document2Id);
|
Assert.assertNotNull(document2Id);
|
||||||
|
|
||||||
// Add a file
|
// Add a file
|
||||||
String file1Id;
|
String file1Id = clientUtil.addFileToDocument("file/Einstein-Roosevelt-letter.png",
|
||||||
try (InputStream is = Resources.getResource("file/Einstein-Roosevelt-letter.png").openStream()) {
|
"Einstein-Roosevelt-letter.png", document1Token, document1Id);
|
||||||
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "Einstein-Roosevelt-letter.png");
|
|
||||||
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
|
||||||
json = target()
|
|
||||||
.register(MultiPartFeature.class)
|
|
||||||
.path("/file").request()
|
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, document1Token)
|
|
||||||
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
|
||||||
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
|
||||||
file1Id = json.getString("id");
|
|
||||||
Assert.assertNotNull(file1Id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Share this document
|
// Share this document
|
||||||
target().path("/share").request()
|
target().path("/share").request()
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, document1Token)
|
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, document1Token)
|
||||||
@ -147,21 +135,9 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
Assert.assertNotNull(document3Id);
|
Assert.assertNotNull(document3Id);
|
||||||
|
|
||||||
// Add a file
|
// Add a file
|
||||||
String file3Id;
|
clientUtil.addFileToDocument("file/Einstein-Roosevelt-letter.png",
|
||||||
try (InputStream is = Resources.getResource("file/Einstein-Roosevelt-letter.png").openStream()) {
|
"Einstein-Roosevelt-letter.png", document3Token, document3Id);
|
||||||
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "Einstein-Roosevelt-letter.png");
|
|
||||||
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
|
||||||
json = target()
|
|
||||||
.register(MultiPartFeature.class)
|
|
||||||
.path("/file").request()
|
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, document3Token)
|
|
||||||
.put(Entity.entity(multiPart.field("id", document3Id).bodyPart(streamDataBodyPart),
|
|
||||||
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
|
||||||
file3Id = json.getString("id");
|
|
||||||
Assert.assertNotNull(file3Id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// List all documents from document3
|
// List all documents from document3
|
||||||
json = target().path("/document/list")
|
json = target().path("/document/list")
|
||||||
.queryParam("sort_column", 3)
|
.queryParam("sort_column", 3)
|
||||||
@ -392,21 +368,8 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
Assert.assertNotNull(document1Id);
|
Assert.assertNotNull(document1Id);
|
||||||
|
|
||||||
// Add a PDF file
|
// Add a PDF file
|
||||||
String file1Id;
|
String file1Id = clientUtil.addFileToDocument("file/document.odt", "document.odt", documentOdtToken, document1Id);
|
||||||
try (InputStream is = Resources.getResource("file/document.odt").openStream()) {
|
|
||||||
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "document.odt");
|
|
||||||
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
|
||||||
json = target()
|
|
||||||
.register(MultiPartFeature.class)
|
|
||||||
.path("/file").request()
|
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentOdtToken)
|
|
||||||
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
|
||||||
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
|
||||||
file1Id = json.getString("id");
|
|
||||||
Assert.assertNotNull(file1Id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search documents by query in full content
|
// Search documents by query in full content
|
||||||
json = target().path("/document/list")
|
json = target().path("/document/list")
|
||||||
.queryParam("search", "full:ipsum")
|
.queryParam("search", "full:ipsum")
|
||||||
@ -451,21 +414,8 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
Assert.assertNotNull(document1Id);
|
Assert.assertNotNull(document1Id);
|
||||||
|
|
||||||
// Add a PDF file
|
// Add a PDF file
|
||||||
String file1Id;
|
String file1Id = clientUtil.addFileToDocument("file/document.docx", "document.docx", documentDocxToken, document1Id);
|
||||||
try (InputStream is = Resources.getResource("file/document.docx").openStream()) {
|
|
||||||
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "document.docx");
|
|
||||||
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
|
||||||
json = target()
|
|
||||||
.register(MultiPartFeature.class)
|
|
||||||
.path("/file").request()
|
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentDocxToken)
|
|
||||||
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
|
||||||
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
|
||||||
file1Id = json.getString("id");
|
|
||||||
Assert.assertNotNull(file1Id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search documents by query in full content
|
// Search documents by query in full content
|
||||||
json = target().path("/document/list")
|
json = target().path("/document/list")
|
||||||
.queryParam("search", "full:dolor")
|
.queryParam("search", "full:dolor")
|
||||||
@ -510,21 +460,8 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
Assert.assertNotNull(document1Id);
|
Assert.assertNotNull(document1Id);
|
||||||
|
|
||||||
// Add a PDF file
|
// Add a PDF file
|
||||||
String file1Id;
|
String file1Id = clientUtil.addFileToDocument("file/wikipedia.pdf", "wikipedia.pdf", documentPdfToken, document1Id);
|
||||||
try (InputStream is = Resources.getResource("file/wikipedia.pdf").openStream()) {
|
|
||||||
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "wikipedia.pdf");
|
|
||||||
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
|
||||||
json = target()
|
|
||||||
.register(MultiPartFeature.class)
|
|
||||||
.path("/file").request()
|
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentPdfToken)
|
|
||||||
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
|
||||||
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
|
||||||
file1Id = json.getString("id");
|
|
||||||
Assert.assertNotNull(file1Id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search documents by query in full content
|
// Search documents by query in full content
|
||||||
json = target().path("/document/list")
|
json = target().path("/document/list")
|
||||||
.queryParam("search", "full:vrandecic")
|
.queryParam("search", "full:vrandecic")
|
||||||
@ -569,20 +506,7 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
Assert.assertNotNull(document1Id);
|
Assert.assertNotNull(document1Id);
|
||||||
|
|
||||||
// Add a plain text file
|
// Add a plain text file
|
||||||
String file1Id;
|
String file1Id = clientUtil.addFileToDocument("file/document.txt", "document.txt", documentPlainToken, document1Id);
|
||||||
try (InputStream is = Resources.getResource("file/document.txt").openStream()) {
|
|
||||||
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "document.txt");
|
|
||||||
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
|
||||||
json = target()
|
|
||||||
.register(MultiPartFeature.class)
|
|
||||||
.path("/file").request()
|
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentPlainToken)
|
|
||||||
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
|
||||||
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
|
||||||
file1Id = json.getString("id");
|
|
||||||
Assert.assertNotNull(file1Id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search documents by query in full content
|
// Search documents by query in full content
|
||||||
json = target().path("/document/list")
|
json = target().path("/document/list")
|
||||||
@ -628,20 +552,7 @@ public class TestDocumentResource extends BaseJerseyTest {
|
|||||||
Assert.assertNotNull(document1Id);
|
Assert.assertNotNull(document1Id);
|
||||||
|
|
||||||
// Add a video file
|
// Add a video file
|
||||||
String file1Id;
|
String file1Id = clientUtil.addFileToDocument("file/video.webm", "video.webm", documentPlainToken, document1Id);
|
||||||
try (InputStream is = Resources.getResource("file/video.webm").openStream()) {
|
|
||||||
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "video.webm");
|
|
||||||
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
|
|
||||||
json = target()
|
|
||||||
.register(MultiPartFeature.class)
|
|
||||||
.path("/file").request()
|
|
||||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, documentPlainToken)
|
|
||||||
.put(Entity.entity(multiPart.field("id", document1Id).bodyPart(streamDataBodyPart),
|
|
||||||
MediaType.MULTIPART_FORM_DATA_TYPE), JsonObject.class);
|
|
||||||
file1Id = json.getString("id");
|
|
||||||
Assert.assertNotNull(file1Id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search documents by query in full content
|
// Search documents by query in full content
|
||||||
json = target().path("/document/list")
|
json = target().path("/document/list")
|
||||||
|
Loading…
Reference in New Issue
Block a user