Skip to content

Commit

Permalink
Enable health check, fix Promises and surefire, MainVerticleTest
Browse files Browse the repository at this point in the history
  • Loading branch information
julianladisch committed Oct 12, 2021
1 parent 2b443aa commit f093287
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 116 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildMvn {
doDocker = {
buildJavaDocker {
publishMaster = true
healthChk = false
healthChk = true
}
}
}
Expand Down
37 changes: 26 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</licenses>

<properties>
<vertx.version>3.8.0</vertx.version>
<vertx.version>3.9.9</vertx.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
Expand Down Expand Up @@ -104,7 +104,6 @@
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>3.8.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -141,37 +140,40 @@


<!-- Test dependencies -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.1.1</version>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.jayway.restassured/xml-path -->
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>xml-path</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>


Expand Down Expand Up @@ -282,6 +284,19 @@
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*Suite.java</include>
</includes>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down
48 changes: 17 additions & 31 deletions src/main/java/org/folio/ncip/FolioNcipHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
Expand All @@ -26,9 +25,6 @@
import org.extensiblecatalog.ncip.v2.common.TranslatorFactory;
import org.extensiblecatalog.ncip.v2.service.NCIPInitiationData;
import org.extensiblecatalog.ncip.v2.service.NCIPResponseData;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import io.vertx.core.Future;
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
Expand All @@ -53,25 +49,17 @@ public class FolioNcipHelper {
protected Properties defaultToolkitObjects = new Properties();

public FolioNcipHelper(Promise<Void> promise) {
Future<Void> steps = initToolkitDefaults();
steps.setHandler(ar -> {
if (ar.succeeded()) {
promise.complete();
} else {
promise.fail(ar.cause());
}
});
initToolkitDefaults().onComplete(promise);
}

/*
*
*
* When the module starts, default config. values for the NCIP toolkit are loaded. When
* ncip requests are made, the module will check for updated
* configurations using mod-configuration.
*
*
*/
private Future<Void> initToolkitDefaults() {
Promise<Void> promise = Promise.promise();

try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(Constants.TOOLKIT_PROP_FILE);
Expand All @@ -80,23 +68,21 @@ private Future<Void> initToolkitDefaults() {
properties.load(inputStream);

if (properties.isEmpty()) {
promise.fail("Unable to initialize the default toolkit properties.");
return Future.failedFuture("Unable to initialize the default toolkit properties.");
}

defaultToolkitObjects.put("toolkit", properties);
defaultToolkitObjects.put("servicecontext",
ServiceValidatorFactory.buildServiceValidator(properties).getInitialServiceContext());
defaultToolkitObjects.put("translator", TranslatorFactory.buildTranslator(null, properties));
return promise.future();
return Future.succeededFuture();
} catch (Exception e) {
logger.fatal(Constants.UNABLE_TO_INIT_TOOLKIT);
logger.fatal(e.getLocalizedMessage());
promise.fail(Constants.UNABLE_TO_INIT_TOOLKIT);
return Future.failedFuture(Constants.UNABLE_TO_INIT_TOOLKIT);
}
return promise.future();

}

public InputStream ncipProcess(RoutingContext context) throws Exception {

logger.info("ncip process called...");
Expand Down Expand Up @@ -130,7 +116,7 @@ public InputStream ncipProcess(RoutingContext context) throws Exception {
logger.info("Unable to initialize NCIP properties with mod-configuration.");
logger.info(e.getLocalizedMessage());
}

InputStream stream = new ByteArrayInputStream(context.getBodyAsString().getBytes(StandardCharsets.UTF_8));
NCIPInitiationData initiationData = null;
InputStream responseMsgInputStream = null;
Expand Down Expand Up @@ -166,7 +152,7 @@ public InputStream ncipProcess(RoutingContext context) throws Exception {
/**
* XC NCIP Toolkit properties initialized for each tenant using
* mod-configuration
*
*
* @throws Exception
*
*/
Expand All @@ -189,7 +175,7 @@ public void initToolkit(RoutingContext context) throws Exception {
logger.info("No toolkit configurations found. Using defaults. QUERY:" + configEndpoint);
return;
}

Iterator configsIterator = configs.iterator();
while (configsIterator.hasNext()) {
JsonObject config = (JsonObject) configsIterator.next();
Expand All @@ -216,14 +202,14 @@ public void initToolkit(RoutingContext context) throws Exception {
* initialized for each tenant In the future, properties could be configured in
* settings? They don't typically change frequently...so maybe mod-configuration
* file is fine.
*
*
* @throws Exception
*
*/
public void initNcipProperties(RoutingContext context) throws Exception {

String tenant = context.request().getHeader(Constants.X_OKAPI_TENANT);

// THE NCIP PROPERTY FILE CONTAINS A LIST OF PROPERTIES
// THAT NEED TO BE INITIALIZED
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(Constants.NCIP_PROP_FILE);
Expand Down Expand Up @@ -275,7 +261,7 @@ public void initNcipProperties(RoutingContext context) throws Exception {
"Unable to initialize NCIP properties using mod-configuration." + e.getLocalizedMessage());
}
}

try {
logger.info("=======> initializing address types");
String addressTypesEndpoint = okapiBaseEndpoint + Constants.ADDRESS_TYPES ;
Expand All @@ -293,9 +279,9 @@ public void initNcipProperties(RoutingContext context) throws Exception {
logger.fatal("Unable to initialize address types.");
logger.fatal(e.getLocalizedMessage());
}

ncipProperties.put(tenant, properties);

}


Expand Down Expand Up @@ -343,12 +329,12 @@ public String callApiGet(String uriString, MultiMap okapiHeaders)
HttpEntity entity = response.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
int responseCode = response.getStatusLine().getStatusCode();

logger.info("GET:");
logger.info(uriString);
logger.info(responseCode);
logger.info(responseString);

if (responseCode > 399) {
String responseBody = processErrorResponse(responseString);
throw new Exception(responseBody);
Expand Down
68 changes: 36 additions & 32 deletions src/main/java/org/folio/ncip/MainVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.core.Promise;
import java.io.InputStream;
Expand All @@ -23,61 +25,63 @@ public class MainVerticle extends AbstractVerticle {
private static FolioNcipHelper folioNcipHelper;

@Override
public void start(Promise<Void> promise) throws Exception {

public void start(Promise<Void> startPromise) throws Exception {
final String portStr = System.getProperty(SYS_PORT, DEFAULT_PORT);
final int port = Integer.parseInt(portStr);
logger.info("mod-ncip is using port: " + port);
folioNcipHelper = new FolioNcipHelper(promise);
logger.info("mod-ncip is using port: " + port);

final Promise<Void> helperPromise = Promise.promise();
folioNcipHelper = new FolioNcipHelper(helperPromise);

Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.route(HttpMethod.POST, "/ncip").handler(this::handleNcip);
router.route(HttpMethod.GET, "/ncipconfigcheck").handler(this::ncipConfigCheck);
router.route(HttpMethod.GET, "/admin/health").handler(this::healthCheck);
vertx.createHttpServer().requestHandler(router).listen(port);
final Promise<HttpServer> serverPromise = Promise.promise();
vertx.createHttpServer().requestHandler(router).listen(port, serverPromise);

helperPromise.future()
.compose(x -> serverPromise.future())
.<Void>mapEmpty()
.onComplete(startPromise);
}



protected void ncipConfigCheck(RoutingContext ctx) {
final Promise<Void> promise = Promise.promise();
try {
NcipConfigCheck ncipConfigCheck = new NcipConfigCheck(promise);
ncipConfigCheck.process(ctx);
}
catch(Exception e) {
NcipConfigCheck ncipConfigCheck = new NcipConfigCheck(promise);
promise.future().compose(x -> {
try {
ncipConfigCheck.process(ctx);
} catch (Exception e) {
return Future.failedFuture(e);
}
ctx.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, Constants.TEXT_PLAIN_STRING)
.end(Constants.OK);
return Future.succeededFuture();
}).onFailure(e -> {
logger.error("***************");
logger.error(e.toString());
logger.error(e.getMessage(), e);
ctx.response()
.setStatusCode(500)
.putHeader(HttpHeaders.CONTENT_TYPE, Constants.APP_XML)
.putHeader(HttpHeaders.CONTENT_TYPE, Constants.APP_XML)
.end("<Problem><message>problem processing NCIP request</message><exception>" + e.toString()+ "</exception></Problem>");
}
ctx.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, Constants.TEXT_PLAIN_STRING)
.end(Constants.OK);
});
}

protected void healthCheck(RoutingContext ctx) {
try {
logger.info("healthcheck called");
}
catch(Exception e) {
logger.error("***************");
logger.error(e.toString());
ctx.response()
.setStatusCode(500)
.putHeader(HttpHeaders.CONTENT_TYPE, Constants.APP_XML)
.end("<Problem><message>problem processing NCIP request</message><exception>" + e.toString()+ "</exception></Problem>");
}
logger.info("healthcheck called");
ctx.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, Constants.TEXT_PLAIN_STRING)
.end(Constants.OK);
}



protected void handleNcip(RoutingContext ctx) {

Expand Down Expand Up @@ -108,7 +112,7 @@ protected void handleNcip(RoutingContext ctx) {
System.out.println("The result is: " + res.result());
ctx.response()
.setStatusCode(200)
.putHeader(HttpHeaders.CONTENT_TYPE, Constants.APP_XML)
.putHeader(HttpHeaders.CONTENT_TYPE, Constants.APP_XML)
.end(res.result().toString());
});

Expand Down
Loading

0 comments on commit f093287

Please sign in to comment.