Skip to content

Commit

Permalink
Replaced the JettyTestServer class with MockOaipmhServer based on wir…
Browse files Browse the repository at this point in the history
…emock
  • Loading branch information
aldettinger committed Feb 8, 2021
1 parent e78f48b commit 0f6fbe2
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 187 deletions.
5 changes: 5 additions & 0 deletions integration-tests/oaipmh/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<scope>test</scope>
</dependency>

<!-- The following dependencies guarantee that this module is built after them. You can update them by running `mvn process-resources -Pformat -N` from the source tree root directory -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@
@ApplicationScoped
public class OaipmhRoutes extends RouteBuilder {

@ConfigProperty(name = "cq-oaipmh-its.test1.server.authority")
String test1ServerAuthority;
@ConfigProperty(name = "cq-oaipmh-its.test2.server.authority")
String test2ServerAuthority;
@ConfigProperty(name = "cq-oaipmh-its.test3.server.authority")
String test3ServerAuthority;
@ConfigProperty(name = "cq-oaipmh-its.test4.server.authority")
String test4ServerAuthority;
@ConfigProperty(name = "camel.oaipmh.its.http.server.authority")
String httpServerAuthority;
@ConfigProperty(name = "camel.oaipmh.its.https.server.authority")
String httpsServerAuthority;

public void configure() {

Expand All @@ -44,28 +40,28 @@ public void configure() {
final String listRecordsXpath = "/default:OAI-PMH/default:ListRecords/default:record/default:metadata/oai_dc:dc/dc:title/text()";
final String getRecordXpath = "/default:OAI-PMH/default:GetRecord/default:record/default:metadata/oai_dc:dc/dc:title/text()";

fromF("oaipmh://%s/oai/request?delay=1000&from=2020-06-01T00:00:00Z&initialDelay=1000", test1ServerAuthority)
fromF("oaipmh://%s/oai/request?delay=1000&from=2020-06-01T00:00:00Z&initialDelay=1000", httpServerAuthority)
.split(xpath(listRecordsXpath, ns))
.to("mock:consumerListRecords");

fromF("oaipmh://%s/index.php?page=oai&delay=1000&from=2020-02-01T00:00:00Z&initialDelay=1000", test3ServerAuthority)
fromF("oaipmh://%s/index.php?page=oai&delay=1000&from=2020-02-01T00:00:00Z&initialDelay=1000", httpServerAuthority)
.split(xpath(listRecordsXpath, ns))
.to("mock:consumerListRecordsParticularCase");

final String uriFormat = "oaipmh://%s/oai/request?ssl=true&ignoreSSLWarnings=true&delay=1000&verb=Identify&initialDelay=1000";
fromF(uriFormat, test4ServerAuthority)
fromF(uriFormat, httpsServerAuthority)
.to("mock:consumerIdentifyHttps");

from("direct:producerListRecords")
.setHeader("CamelOaimphFrom", constant("2020-06-01T00:00:00Z"))
.toF("oaipmh://%s/oai/request", test1ServerAuthority)
.toF("oaipmh://%s/oai/request", httpServerAuthority)
.split(body())
.split(xpath(listRecordsXpath, ns))
.to("mock:producerListRecords");

from("direct:producerGetRecord")
.setHeader("CamelOaimphVerb", constant("GetRecord"))
.toF("oaipmh://%s/oai/request", test2ServerAuthority)
.toF("oaipmh://%s/oai/request", httpServerAuthority)
.split(body())
.split(xpath(getRecordXpath, ns))
.to("mock:producerGetRecord");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.camel.quarkus.component.oaipmh.it;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import org.apache.camel.quarkus.test.AvailablePortFinder;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

public final class MockOaipmhServer {

private static Map<String, String> responseCache;
private static final String PASSWORD = "changeit";

private int httpPort;
private int httpsPort;
private WireMockServer server;

private MockOaipmhServer(int httpPort, int httpsPort) {
this.httpPort = httpPort;
this.httpsPort = httpsPort;
}

public static MockOaipmhServer create() {
int httpPort = AvailablePortFinder.getNextAvailable();
int httpsPort = AvailablePortFinder.getNextAvailable();
MockOaipmhServer server = new MockOaipmhServer(httpPort, httpsPort);
return server;
}

/**
* Gets the response cache from the mocked data ZIP file.
*/
private static synchronized Map<String, String> getResponseCache() {
try {
if (responseCache == null) {
HashMap<String, String> cache = new HashMap<String, String>();

ZipInputStream zis = new ZipInputStream(MockOaipmhServer.class.getResourceAsStream("/data.zip"));

ZipEntry entry = zis.getNextEntry();
while (entry != null) {
if (!entry.isDirectory()) {
cache.put(StringUtils.substringAfterLast(entry.getName(), "/"),
IOUtils.toString(zis, StandardCharsets.UTF_8));
}
entry = zis.getNextEntry();
}
responseCache = Collections.unmodifiableMap(cache);
}
} catch (IOException ioex) {
throw new RuntimeException("An issue occured while initializing the OAI-PMH mock server reponse cache", ioex);
}
return responseCache;
}

public void start() {
OaipmhMockTransformer transformer = new OaipmhMockTransformer();
WireMockConfiguration config = wireMockConfig().extensions(transformer);

config.httpsPort(httpsPort);
String keyStorePath = MockOaipmhServer.class.getResource("/jettyKS/localhost.p12").toExternalForm();
config.keystorePath(keyStorePath);
config.keystorePassword(PASSWORD);
config.keyManagerPassword(PASSWORD);

config.port(httpPort);

server = new WireMockServer(config);
server.start();
}

public void stop() {
server.stop();
}

public int getHttpPort() {
return this.httpPort;
}

public int getHttpsPort() {
return this.httpsPort;
}

public static final class OaipmhMockTransformer extends ResponseDefinitionTransformer {

@Override
public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files,
Parameters parameters) {
String sha256Hex = DigestUtils.sha256Hex(request.getUrl());
return new ResponseDefinitionBuilder().withStatus(200).withBody(getResponseCache().get(sha256Hex + ".xml")).build();
}

@Override
public String getName() {
return "oaipmh-mock-transformer";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,28 @@
import java.util.Map;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.apache.camel.quarkus.test.AvailablePortFinder;

public class OaipmhTestResource implements QuarkusTestResourceLifecycleManager {

private OaipmhTestServer test1Server, test2Server, test3Server, test4Server;
private MockOaipmhServer mockOaipmhServer;

@Override
public Map<String, String> start() {
Map<String, String> systemProperties = new HashMap<>();
this.test1Server = initOaipmhTestServer("test1", systemProperties, false);
this.test2Server = initOaipmhTestServer("test2", systemProperties, false);
this.test3Server = initOaipmhTestServer("test3", systemProperties, false);
this.test4Server = initOaipmhTestServer("test4", systemProperties, true);
return systemProperties;
}

private OaipmhTestServer initOaipmhTestServer(String context, Map<String, String> properties, boolean useHttps) {
int availablePort = AvailablePortFinder.getNextAvailable();
OaipmhTestServer server = new OaipmhTestServer(context, availablePort, useHttps);
server.startServer();
properties.put("cq-oaipmh-its." + context + ".server.authority", "localhost:" + availablePort);
return server;
mockOaipmhServer = MockOaipmhServer.create();
mockOaipmhServer.start();

systemProperties.put("camel.oaipmh.its.http.server.authority", "localhost:" + mockOaipmhServer.getHttpPort());
systemProperties.put("camel.oaipmh.its.https.server.authority", "localhost:" + mockOaipmhServer.getHttpsPort());

return systemProperties;
}

@Override
public void stop() {
if (test1Server != null) {
test1Server.stopServer();
}
if (test2Server != null) {
test2Server.stopServer();
}
if (test3Server != null) {
test3Server.stopServer();
}
if (test4Server != null) {
test4Server.stopServer();
if (mockOaipmhServer != null) {
mockOaipmhServer.stop();
}
}
}

0 comments on commit 0f6fbe2

Please sign in to comment.