Skip to content

Commit

Permalink
Test server for static files
Browse files Browse the repository at this point in the history
  • Loading branch information
haarli committed Nov 21, 2023
1 parent 19fcb23 commit d6197cb
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 171 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package de.mpg.imeji.test.rest.resources.test.integration;

import static de.mpg.imeji.logic.util.ResourceHelper.getStringFromPath;
import static de.mpg.imeji.test.rest.resources.test.integration.MyTestContainerFactory.STATIC_CONTEXT_REST;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.ws.rs.core.Application;

import de.mpg.imeji.logic.security.user.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.StaticHttpHandler;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.AfterClass;
Expand Down Expand Up @@ -42,6 +49,12 @@
*/
public class ImejiTestBase extends JerseyTest {

public static final String STATIC_SERVER_URL = "http://localhost:9999";
public static final String STATIC_CONTEXT_PATH = "/static";
public static final String STATIC_CONTEXT_STORAGE = "src/test/resources/storage";
public static final String STATIC_CONTEXT_REST = "src/test/resources/rest";

private static HttpServer staticServer;
protected static HttpAuthenticationFeature authAsUser = HttpAuthenticationFeature.basic(JenaUtil.TEST_USER_EMAIL, JenaUtil.TEST_USER_PWD);
protected static HttpAuthenticationFeature authAsUser2 =
HttpAuthenticationFeature.basic(JenaUtil.TEST_USER_EMAIL_2, JenaUtil.TEST_USER_PWD);
Expand All @@ -63,28 +76,41 @@ protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);

if (app == null) {
app = new ImejiRestService(null);
}
return app;
return new ImejiRestService(null);
}

@Override
protected DeploymentContext configureDeployment() {
return ServletDeploymentContext.forServlet(new ServletContainer(new ImejiRestService(null))).servletPath("rest").build();
}



@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new MyTestContainerFactory();
return new GrizzlyWebTestContainerFactory();
}



@BeforeClass
public static void setup() throws IOException, URISyntaxException {
ElasticsearchTestUtil.startElasticsearch();
JenaUtil.initJena();
//Start a server for the static content
staticServer = GrizzlyHttpServerFactory.createHttpServer(new URI(STATIC_SERVER_URL));
staticServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler(STATIC_CONTEXT_REST, STATIC_CONTEXT_STORAGE),
STATIC_CONTEXT_PATH);
staticServer.start();

}

@AfterClass
public static void shutdown() throws IOException, URISyntaxException, InterruptedException {
ConcurrencyUtil.waitForImejiThreadsToComplete();
ElasticsearchTestUtil.stopElasticsearch();
JenaUtil.closeJena();
staticServer.shutdownNow();
app = null;
}

Expand All @@ -108,7 +134,7 @@ public static String initCollection() {
CollectionAPIService s = new CollectionAPIService();
try {
collectionTO =
(CollectionTO) RestProcessUtils.buildTOFromJSON(getStringFromPath(STATIC_CONTEXT_REST + "/collection.json"), CollectionTO.class);
(CollectionTO) RestProcessUtils.buildTOFromJSON(getStringFromPath("src/test/resources/rest/collection.json"), CollectionTO.class);

collectionTO = s.create(collectionTO, JenaUtil.testUser);
collectionId = collectionTO.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ItemTestBase extends ImejiTestBase {
private static final Logger LOGGER = LogManager.getLogger(ItemTestBase.class);

public static Item item;
private static final String TARGET_PATH_PREFIX = "/rest/items";
private static final String TARGET_PATH_PREFIX = "/items";

protected static void createItem() throws Exception {
CollectionService cc = new CollectionService();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static de.mpg.imeji.logic.util.ResourceHelper.getStringFromPath;
import static de.mpg.imeji.rest.process.RestProcessUtils.buildJSONFromObject;
import static de.mpg.imeji.rest.process.RestProcessUtils.jsonToPOJO;
import static de.mpg.imeji.test.rest.resources.test.integration.MyTestContainerFactory.STATIC_CONTEXT_REST;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static javax.ws.rs.core.Response.Status.NOT_FOUND;
Expand Down Expand Up @@ -57,7 +56,7 @@ public class CollectionIntegration extends ImejiTestBase {

private static final Logger LOGGER = LoggerFactory.getLogger(CollectionIntegration.class);

private static String pathPrefix = "/rest/collections";
private static String pathPrefix = "/collections";
private static String updateJSON;

@Before
Expand All @@ -67,7 +66,7 @@ public void specificSetup() {

@Test
public void test_1_CreateCollection() throws IOException, ImejiException {
String jsonString = getStringFromPath(STATIC_CONTEXT_REST + "/collection.json");
String jsonString = getStringFromPath("src/test/resources/rest/collection.json");
Response response = target(pathPrefix).register(authAsUser).register(MultiPartFeature.class).request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(jsonString, MediaType.APPLICATION_JSON_TYPE));
assertEquals(response.getStatus(), CREATED.getStatusCode());
Expand All @@ -79,7 +78,7 @@ public void test_1_CreateCollection() throws IOException, ImejiException {

@Test
public void test_1_CreateCollection_5_NoAuth() throws IOException {
String jsonString = getStringFromPath(STATIC_CONTEXT_REST + "/collection.json");
String jsonString = getStringFromPath("src/test/resources/rest/collection.json");
Response response = target(pathPrefix).register(MultiPartFeature.class).request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(jsonString, MediaType.APPLICATION_JSON_TYPE));
assertEquals(UNAUTHORIZED.getStatusCode(), response.getStatus());
Expand Down Expand Up @@ -374,7 +373,7 @@ public void test_6_UpdateCollection() throws IOException, BadRequestException, U

@Test
public void test_6_CreateCollection_1_AdditionalInfos() throws ImejiException, UnsupportedEncodingException, IOException {
String originalJsonString = getStringFromPath(STATIC_CONTEXT_REST + "/collection.json");
String originalJsonString = getStringFromPath("src/test/resources/rest/collection.json");

String jsonString = originalJsonString;
jsonString = jsonString.replace("\"label\": \"Label1\",", "");
Expand Down Expand Up @@ -403,7 +402,7 @@ public void test_6_CreateCollection_1_AdditionalInfos() throws ImejiException, U

@Test
public void test_6_UpdateCollection_1_AdditionalInfos() throws ImejiException, UnsupportedEncodingException, IOException {
String originalJsonString = getStringFromPath(STATIC_CONTEXT_REST + "/collection.json");
String originalJsonString = getStringFromPath("src/test/resources/rest/collection.json");

// Create the collection
Response response = target(pathPrefix).register(authAsUser).register(MultiPartFeature.class).request(MediaType.APPLICATION_JSON_TYPE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static de.mpg.imeji.logic.util.ResourceHelper.getStringFromPath;
import static de.mpg.imeji.logic.util.StorageUtils.calculateChecksum;
import static de.mpg.imeji.rest.process.RestProcessUtils.jsonToPOJO;
import static de.mpg.imeji.test.rest.resources.test.integration.MyTestContainerFactory.STATIC_CONTEXT_PATH;
import static de.mpg.imeji.test.rest.resources.test.integration.MyTestContainerFactory.STATIC_CONTEXT_REST;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.OK;
Expand Down Expand Up @@ -61,7 +59,7 @@ public class ItemIntegration extends ItemTestBase {
private static final Logger LOGGER = LoggerFactory.getLogger(ItemIntegration.class);
private static String itemJSON;
private static String updateJSON;
private static final String PATH_PREFIX = "/rest/items";
private static final String PATH_PREFIX = "/items";
private static final String UPDATED_FILE_NAME = "updated_filename.png";
private static String storedFileURL;

Expand All @@ -70,7 +68,7 @@ public static void specificSetup() throws Exception {
initCollection();
initItem();
itemJSON = getStringFromPath("src/test/resources/rest/itemCreateBasic.json");
updateJSON = getStringFromPath(STATIC_CONTEXT_REST + "/item.json");
updateJSON = getStringFromPath("src/test/resources/rest/item.json");
}

@Test
Expand Down Expand Up @@ -267,7 +265,7 @@ public void createItem_SyntaxInvalidJSONFile() throws Exception {
@Test
public void createItem_WithFile_Fetched() throws IOException {
initCollection();
final String fileURL = target().getUri() + STATIC_CONTEXT_PATH.substring(1) + "/test3.jpg";
final String fileURL = STATIC_SERVER_URL + STATIC_CONTEXT_PATH + "/test3.jpg";

FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.field("json", itemJSON.replace("___COLLECTION_ID___", collectionId).replace("___FETCH_URL___", fileURL)
Expand All @@ -283,7 +281,7 @@ public void createItem_WithFile_Fetched() throws IOException {
@Test
public void createItem_WithFile_Fetched_WithEmptyFileName() throws IOException {
initCollection();
final String fileURL = target().getUri() + STATIC_CONTEXT_PATH.substring(1) + "/test7.jpg";
final String fileURL = STATIC_SERVER_URL + STATIC_CONTEXT_PATH + "/test7.jpg";

FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.field("json",
Expand Down Expand Up @@ -532,7 +530,7 @@ public void test_1_UpdateItem_2_WithFile_Fetched() throws ImejiException, IOExce

initCollection();
initItem();
final String fileURL = target().getUri() + STATIC_CONTEXT_PATH.substring(1) + "/test2.jpg";
final String fileURL = STATIC_SERVER_URL + STATIC_CONTEXT_PATH + "/test2.jpg";

FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.field("json", getStringFromPath(updateJSON).replace("___FILE_NAME___", UPDATED_FILE_NAME).replace("___FETCH_URL___", fileURL)
Expand Down Expand Up @@ -574,7 +572,7 @@ public void test_1_UpdateItem_4_WithFile_Attached_Fetched() throws IOException,

File newFile = ImejiTestResources.getTestPng();

final String fileURL = target().getUri() + STATIC_CONTEXT_PATH.substring(1) + "/test.jpg";
final String fileURL = STATIC_SERVER_URL + STATIC_CONTEXT_PATH + "/test.jpg";

FileDataBodyPart filePart = new FileDataBodyPart("file", newFile);

Expand Down Expand Up @@ -624,7 +622,7 @@ public void test_1_UpdateItem_5_WithFile_Attached_Referenced() throws IOExceptio
@Test
public void test_1_UpdateItem_6_WithFile_Fetched_Referenced() throws IOException, ImejiException {

final String fileURL = target().getUri() + STATIC_CONTEXT_PATH.substring(1) + "/test.jpg";
final String fileURL = STATIC_SERVER_URL + STATIC_CONTEXT_PATH + "/test.jpg";

FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.field("json", getStringFromPath(updateJSON).replace("___FILE_NAME___", UPDATED_FILE_NAME).replace("___FETCH_URL___", fileURL)
Expand Down Expand Up @@ -653,7 +651,7 @@ public void test_1_UpdateItem_7_WithFile_Attached_Fetched_Referenced() throws IO
File newFile = ImejiTestResources.getTest1Jpg();
FileDataBodyPart filePart = new FileDataBodyPart("file", newFile);

final String fileURL = target().getUri() + STATIC_CONTEXT_PATH.substring(1) + "/test1.jpg";
final String fileURL = STATIC_SERVER_URL + STATIC_CONTEXT_PATH + "/test1.jpg";

FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.bodyPart(filePart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class StorageIntegration extends ImejiTestBase {
private static final Logger LOGGER = LoggerFactory.getLogger(StorageIntegration.class);

private final String PATH_PREFIX = "/rest/storage";
private final String PATH_PREFIX = "/storage";

@Test
public void test_1_uploadFormats() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class VersionManagerTest extends ImejiTestBase {

private static String pathPrefixWithVersion = "/rest/vXXX_VERSION_XXX/collections";
private static String pathPrefix = "/rest/collections";
private static String pathPrefix = "/collections";

@Test
public void testDeprecatedVersion() throws IOException {
Expand Down
Loading

0 comments on commit d6197cb

Please sign in to comment.