Skip to content

Commit

Permalink
Clean up okapi test module (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdickmeiss authored Oct 26, 2020
1 parent b57198a commit 70b72d6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.folio.okapi.sample;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.MultiMap;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpMethod;
Expand All @@ -14,7 +14,6 @@
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Map;
import org.apache.logging.log4j.Logger;
import org.folio.okapi.common.HttpResponse;
import org.folio.okapi.common.ModuleVersionReporter;
Expand Down Expand Up @@ -191,7 +190,7 @@ private void recurseHandle(RoutingContext ctx) {
if (d == null || d.isEmpty()) {
d = "1";
}
String depthstr = d; // must be final
final String depthstr = d;
int depth = Integer.parseInt(depthstr);
if (depth < 0) {
HttpResponse.responseError(ctx, 400, "Bad recursion, can not be negative " + depthstr);
Expand All @@ -204,12 +203,6 @@ private void recurseHandle(RoutingContext ctx) {
ok.get("/recurse?depth=" + depth, res -> {
ok.close();
if (res.succeeded()) {
MultiMap respH = ok.getRespHeaders();
for (Map.Entry<String, String> e : respH.entries()) {
if (e.getKey().startsWith("X-") || e.getKey().startsWith("x-")) {
ctx.response().headers().add(e.getKey(), e.getValue());
}
}
HttpResponse.responseText(ctx, 200);
ctx.response().end(depthstr + " " + res.result());
} else {
Expand Down Expand Up @@ -252,24 +245,23 @@ public void start(Promise<Void> promise) throws IOException {
router.get("/recurse").handler(this::recurseHandle);

HttpServerOptions so = new HttpServerOptions().setHandle100ContinueAutomatically(true);
vertx.createHttpServer(so)
Future<Void> future = vertx.createHttpServer(so)
.requestHandler(router)
.listen(
port,
result -> {
if (result.succeeded()) {
final String pidFile = System.getProperty("pidFile");
if (pidFile != null && !pidFile.isEmpty()) {
final String pid = name.split("@")[0];
try (FileWriter fw = new FileWriter(pidFile)) {
fw.write(pid);
logger.info("Writing {}", pid);
} catch (IOException ex) {
logger.error(ex);
}
}
}
promise.handle(result.mapEmpty());
});
.listen(port)
.compose(result -> {
final String pidFile = System.getProperty("pidFile");
if (pidFile != null && !pidFile.isEmpty()) {
final String pid = name.split("@")[0];
try (FileWriter fw = new FileWriter(pidFile)) {
fw.write(pid);
logger.info("Writing {}", pid);
} catch (IOException ex) {
logger.error(ex);
return Future.failedFuture(ex);
}
}
return Future.succeededFuture();
});
future.onComplete(promise::handle);
}
}
72 changes: 71 additions & 1 deletion okapi-test-module/src/test/java/SampleModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,27 @@ public void testPostXML(TestContext context, OkapiClient cli, Async async) {
}

public void testRecurse(TestContext context, OkapiClient cli, Async async) {
cli.setHeaders(headers);
cli.get("/recurse", res -> {
context.assertTrue(res.succeeded());
context.assertEquals("1 Recursion done", cli.getResponsebody());
testRecurseDepth(context, cli, async);
});
}

public void testRecurseDepth(TestContext context, OkapiClient cli, Async async) {
cli.setHeaders(headers);
cli.get("/recurse?depth=2", res -> {
context.assertTrue(res.succeeded());
context.assertEquals("2 1 Recursion done", cli.getResponsebody());
testRecurseDepthMinus(context, cli, async);
});
}

public void testRecurseDepthMinus(TestContext context, OkapiClient cli, Async async) {
cli.setHeaders(headers);
cli.get("/recurse?depth=-2", res -> {
context.assertTrue(res.failed());
testTenantPost(context, cli, async);
});
}
Expand All @@ -113,16 +130,28 @@ public void testTenantPost(TestContext context, OkapiClient cli, Async async) {
}

public void testTenantPostWithParameters(TestContext context, OkapiClient cli, Async async) {
headers.put(XOkapiHeaders.TOKEN, "dummy-token");
cli.post("/_/tenant", "{\"module_from\": \"m-1.0.0\", \"module_to\":\"m-1.0.1\", "
+ "\"parameters\" : [ {\"key\": \"a\", \"value\" : \"b\"} ] }", res -> {
context.assertTrue(res.succeeded());
context.assertEquals("POST /_/tenant to okapi-test-module for "
+ "tenant my-lib\n",
cli.getResponsebody());
testTenantDelete(context, cli, async);
testTenantPostCheck(context, cli, async);
});
}

public void testTenantPostCheck(TestContext context, OkapiClient cli, Async async) {
headers.put("X-tenant-parameters", "yes");
cli.setHeaders(headers);
cli.get("/testb", res -> {
context.assertTrue(res.succeeded());
context.assertTrue(cli.getResponsebody().startsWith("It works Tenant parameters"),
cli.getResponsebody());
testTenantDelete(context, cli, async);
});
}

public void testTenantDelete(TestContext context, OkapiClient cli, Async async) {
cli.delete("/_/tenant", res -> {
context.assertTrue(res.succeeded());
Expand Down Expand Up @@ -193,6 +222,8 @@ public void testAllHeaders(TestContext context) {
headers.put("X-all-headers", "HBL");
headers.put("X-delay", "2");
headers.put("X-my-header", "my");
headers.put("X-tenant-reqs", "yes");
headers.put("X-tenant-parameters", "yes");
headers.put(XOkapiHeaders.URL, URL);
headers.put(XOkapiHeaders.TENANT, "my-lib");
headers.put(XOkapiHeaders.MATCH_PATH_PATTERN, "/testb");
Expand All @@ -212,6 +243,45 @@ public void testAllHeaders(TestContext context) {
});
}

@Test
public void testNoHeaders(TestContext context) {
Async async = context.async();

HashMap<String, String> headers = new HashMap<>();

headers.put("X-all-headers", "X");
headers.put(XOkapiHeaders.URL, URL);
headers.put(XOkapiHeaders.TENANT, "my-lib");

OkapiClient cli = new OkapiClient(URL, vertx, headers);
cli.enableInfoLog();
cli.get("/testb?q=a", res -> {
context.assertTrue(res.succeeded());
context.assertEquals("It works", cli.getResponsebody());
async.complete();
});
}

@Test
public void testStop(TestContext context) {
Async async = context.async();

HashMap<String, String> headers = new HashMap<>();

headers.put("X-stop-here", "X");
headers.put(XOkapiHeaders.URL, URL);
headers.put(XOkapiHeaders.TENANT, "my-lib");

OkapiClient cli = new OkapiClient(URL, vertx, headers);
cli.enableInfoLog();
cli.get("/testb", res -> {
context.assertTrue(res.succeeded());
context.assertEquals("X", cli.getRespHeaders().get("X-Okapi-Stop"));
context.assertEquals("It works", cli.getResponsebody());
async.complete();
});
}

@Test
public void testUpload(TestContext context) {
int bufSz = 200000;
Expand Down

0 comments on commit 70b72d6

Please sign in to comment.