From 89250de244695c5d4b13c9c88659e718cf32775e Mon Sep 17 00:00:00 2001 From: markiantorno Date: Fri, 17 Apr 2020 20:17:12 -0400 Subject: [PATCH] Some cleanup and tests before first commit. --- org.hl7.fhir.validation/pom.xml | 1 - .../org/hl7/fhir/validation/Validator.java | 2 +- .../fhir/validation/cli/RestEndpoints.java | 5 +- .../hl7/fhir/validation/cli/ValidatorGui.java | 10 ++-- .../cli/controller/CliContextController.java | 19 ++++--- .../cli/controller/ValidationController.java | 21 ++++++++ .../fhir/validation/cli/model/CliContext.java | 54 ++++++++++++++++--- .../cli/services/ValidationService.java | 2 +- .../validation/cli/utils/SnomedVersion.java | 20 +++---- .../hl7/fhir/validation/cli/BaseRestTest.java | 36 +++++++++++++ .../fhir/validation/cli/ValidatorGuiTest.java | 4 +- .../cli/controller/HttpGetContextTest.java | 47 ++++++++++++++++ .../cli/controller/HttpPutContextTest.java | 24 +++++++++ .../org.mockito.plugins.MockMaker | 1 + 14 files changed, 212 insertions(+), 34 deletions(-) create mode 100644 org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/BaseRestTest.java create mode 100644 org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/controller/HttpGetContextTest.java create mode 100644 org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/controller/HttpPutContextTest.java create mode 100644 org.hl7.fhir.validation/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/org.hl7.fhir.validation/pom.xml b/org.hl7.fhir.validation/pom.xml index 98728ef76..ea27cfbdd 100644 --- a/org.hl7.fhir.validation/pom.xml +++ b/org.hl7.fhir.validation/pom.xml @@ -197,5 +197,4 @@ - diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Validator.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Validator.java index ba3b97b3d..8abac8da5 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Validator.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/Validator.java @@ -135,7 +135,7 @@ public class Validator { String v = Common.getVersion(args); String definitions = VersionUtilities.packageForVersion(v) + "#" + v; ValidationEngine validationEngine = Common.getValidationEngine(v, definitions, cliContext.getTxLog()); - ValidatorGui.start(cliContext, validationEngine); + ValidatorGui.start(cliContext, validationEngine, true); } else if (Params.hasParam(args, Params.TEST)) { Common.runValidationEngineTests(); } else if (args.length == 0 || Params.hasParam(args, Params.HELP) || Params.hasParam(args, "?") || Params.hasParam(args, "-?") || Params.hasParam(args, "/?")) { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/RestEndpoints.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/RestEndpoints.java index b3999fc0a..8b71d7c81 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/RestEndpoints.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/RestEndpoints.java @@ -1,6 +1,5 @@ package org.hl7.fhir.validation.cli; -import com.fasterxml.jackson.databind.ObjectMapper; import io.javalin.Javalin; import org.hl7.fhir.validation.ValidationEngine; import org.hl7.fhir.validation.cli.controller.CliContextController; @@ -22,8 +21,8 @@ public class RestEndpoints { app.get("/home", myUIController.renderLandingPage); - app.get("/context", myCliContextController.getCurrentCliContext); - app.post("/context", myCliContextController.setCurrentCliContext); + app.get("/context", myCliContextController::handleGetCurrentCliContext); + app.post("/context", myCliContextController::handleSetCurrentCliContext); app.post("/validate", myValidationController.handleValidationRequest); } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/ValidatorGui.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/ValidatorGui.java index 93a5f09e8..3030d0e2e 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/ValidatorGui.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/ValidatorGui.java @@ -18,6 +18,8 @@ public class ValidatorGui { private static final String WEB_APP_FILE_LOCATION = "/public"; private static Javalin app; + private ValidatorGui(){} + /** * N.B. this entry point, is only for testing. Please start from command line using the argument {@code -gui} for * actual use. @@ -27,15 +29,17 @@ public class ValidatorGui { String v = Common.getVersion(args); String definitions = VersionUtilities.packageForVersion(v) + "#" + v; ValidationEngine validationEngine = Common.getValidationEngine(v, definitions, cliContext.getTxLog()); - start(new CliContext(), validationEngine); + start(new CliContext(), validationEngine, false); } - public static void start(CliContext currentContext, ValidationEngine validationEngine) { + public static void start(CliContext currentContext, ValidationEngine validationEngine, boolean bootBrowser) { app = Javalin.create(); new RestEndpoints().initRestEndpoints(app, currentContext, validationEngine); app.config.addStaticFiles(WEB_APP_FILE_LOCATION); app.start(GUI_FRONTEND_PORT); - openBrowser(); + if (bootBrowser) { + openBrowser(); + } } public static void openBrowser() { diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/controller/CliContextController.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/controller/CliContextController.java index 1835a9f6b..afd3e4478 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/controller/CliContextController.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/controller/CliContextController.java @@ -1,27 +1,30 @@ package org.hl7.fhir.validation.cli.controller; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import io.javalin.http.Handler; +import io.javalin.http.Context; import org.apache.http.HttpStatus; import org.hl7.fhir.validation.cli.model.CliContext; +import org.jetbrains.annotations.NotNull; public class CliContextController { + private final String JSON_MIME_TYPE = "application/json"; + private CliContext myCliContext; public CliContextController(CliContext cliContext) { this.myCliContext = cliContext; } - public Handler getCurrentCliContext = ctx -> { + public void handleGetCurrentCliContext(@NotNull Context ctx) throws JsonProcessingException { ObjectMapper Obj = new ObjectMapper(); String jsonStr = Obj.writeValueAsString(myCliContext); - ctx.result(jsonStr); - }; + ctx.result(jsonStr).contentType(JSON_MIME_TYPE).status(HttpStatus.SC_OK); + } - public Handler setCurrentCliContext = ctx -> { + public void handleSetCurrentCliContext(@NotNull Context ctx) { myCliContext = ctx.bodyAsClass(CliContext.class); - ctx.status(HttpStatus.SC_CREATED); - }; - + ctx.status(HttpStatus.SC_OK); + } } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/controller/ValidationController.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/controller/ValidationController.java index c290415a2..12cb2d4e1 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/controller/ValidationController.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/controller/ValidationController.java @@ -1,7 +1,9 @@ package org.hl7.fhir.validation.cli.controller; import com.fasterxml.jackson.databind.ObjectMapper; +import io.javalin.http.Context; import io.javalin.http.Handler; +import org.apache.http.HttpStatus; import org.hl7.fhir.validation.ValidationEngine; import org.hl7.fhir.validation.cli.model.ValidationRequest; import org.hl7.fhir.validation.cli.model.ValidationResponse; @@ -15,6 +17,25 @@ public class ValidationController { this.myValidationEngine = validationEngine; } + public void handleValidationRequest(Context ctx) { + ValidationRequest request = ctx.bodyAsClass(ValidationRequest.class); + ValidationResponse response = null; + try { + response = ValidationService.validateSources(request, myValidationEngine); + ObjectMapper Obj = new ObjectMapper(); + /* + * TODO + * Write file contents to temp files to pass to validator instead of creating our own endpoint. + * Create File => new temp file + * Use Option => DeleteOnShutdown + */ + String jsonStr = Obj.writeValueAsString(response); + ctx.status(200).json(jsonStr); + } catch (Exception e) { + ctx.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).result(e.getLocalizedMessage()); + } + } + public Handler handleValidationRequest = ctx -> { ValidationRequest request = ctx.bodyAsClass(ValidationRequest.class); ValidationResponse response = ValidationService.validateSources(request, myValidationEngine); diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java index 81335261c..1e910eae7 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/model/CliContext.java @@ -60,7 +60,7 @@ public class CliContext { private Validator.EngineMode mode = Validator.EngineMode.VALIDATION; @JsonProperty("locale") - private Locale locale = null; + private String locale = Locale.ENGLISH.getDisplayLanguage(); @JsonProperty("locations") private Map locations = new HashMap(); @@ -172,21 +172,21 @@ public class CliContext { @JsonProperty("locale") public String getLanguageCode() { - return this.locale.getDisplayLanguage(); + return locale; } public Locale getLocale() { - return locale; + return Locale.forLanguageTag(this.locale); } @JsonProperty("locale") public CliContext setLocale(String languageString) { - this.locale = new Locale(languageString); + this.locale = languageString; return this; } public CliContext setLocale(Locale locale) { - this.locale = locale; + this.locale = locale.getDisplayLanguage(); return this; } @@ -332,14 +332,18 @@ public class CliContext { return this; } + public SnomedVersion getSnomedCT() { + return SnomedVersion.getFromCode(snomedCT); + } + @JsonProperty("snomedCT") - public String getSnomedCT() { + public String getSnomedCTCode() { return snomedCT; } @JsonProperty("snomedCT") public CliContext setSnomedCT(String snomedCT) { - this.snomedCT = SnomedVersion.resolveSnomedCTCode(snomedCT); + this.snomedCT = snomedCT; return this; } @@ -405,4 +409,40 @@ public class CliContext { ", assumeValidRestReferences=" + assumeValidRestReferences + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CliContext that = (CliContext) o; + return doNative == that.doNative && + anyExtensionsAllowed == that.anyExtensionsAllowed && + hintAboutNonMustSupport == that.hintAboutNonMustSupport && + recursive == that.recursive && + doDebug == that.doDebug && + assumeValidRestReferences == that.assumeValidRestReferences && + canDoNative == that.canDoNative && + Objects.equals(map, that.map) && + Objects.equals(output, that.output) && + Objects.equals(txServer, that.txServer) && + Objects.equals(sv, that.sv) && + Objects.equals(txLog, that.txLog) && + Objects.equals(mapLog, that.mapLog) && + Objects.equals(lang, that.lang) && + Objects.equals(fhirpath, that.fhirpath) && + Objects.equals(snomedCT, that.snomedCT) && + Objects.equals(targetVer, that.targetVer) && + Objects.equals(igs, that.igs) && + Objects.equals(questionnaires, that.questionnaires) && + Objects.equals(profiles, that.profiles) && + Objects.equals(sources, that.sources) && + mode == that.mode && + Objects.equals(locale, that.locale) && + Objects.equals(locations, that.locations); + } + + @Override + public int hashCode() { + return Objects.hash(doNative, anyExtensionsAllowed, hintAboutNonMustSupport, recursive, doDebug, assumeValidRestReferences, canDoNative, map, output, txServer, sv, txLog, mapLog, lang, fhirpath, snomedCT, targetVer, igs, questionnaires, profiles, sources, mode, locale, locations); + } } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java index 67ab7dbf0..953a524be 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java @@ -178,7 +178,7 @@ public class ValidationService { validator.setAnyExtensionsAllowed(cliContext.isAnyExtensionsAllowed()); validator.setLanguage(cliContext.getLang()); validator.setLocale(cliContext.getLocale()); - validator.setSnomedExtension(cliContext.getSnomedCT()); + validator.setSnomedExtension(cliContext.getSnomedCTCode()); validator.setAssumeValidRestReferences(cliContext.isAssumeValidRestReferences()); return validator; } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/SnomedVersion.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/SnomedVersion.java index 11cae4b20..3be055e5f 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/SnomedVersion.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/utils/SnomedVersion.java @@ -1,6 +1,8 @@ package org.hl7.fhir.validation.cli.utils; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; public enum SnomedVersion { @@ -32,15 +34,15 @@ public enum SnomedVersion { return code; } - public static String resolveSnomedCTCode(String s) { - String foundCode; - Optional opt = Arrays.stream(values()) - .filter(v -> v.lang.equals(s)) - .findFirst(); - if (opt.isPresent()) { - return opt.get().code; - } else { - throw new Error("Snomed edition '" + s + "' not known"); + public static SnomedVersion getFromCode(String code) { + return lookup.get(code); + } + + private static final Map lookup = new HashMap<>(); + + static { + for (SnomedVersion s : SnomedVersion.values()) { + lookup.put(s.getCode(), s); } } } diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/BaseRestTest.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/BaseRestTest.java new file mode 100644 index 000000000..cd3a88874 --- /dev/null +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/BaseRestTest.java @@ -0,0 +1,36 @@ +package org.hl7.fhir.validation.cli; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.HttpResponse; +import org.apache.http.util.EntityUtils; +import org.hl7.fhir.validation.cli.model.CliContext; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; + +import java.io.IOException; + +public abstract class BaseRestTest { + + protected final String JSON_MIME_TYPE = "application/json"; + + @BeforeAll + public static void startServer() { + ValidatorGui.start(new CliContext(), null, false); + } + + @AfterAll + public static void stopServer() { + ValidatorGui.stop(); + } + + public static T retrieveResourceFromResponse(HttpResponse response, Class clazz) + throws IOException { + + String jsonFromResponse = EntityUtils.toString(response.getEntity()); + ObjectMapper mapper = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return mapper.readValue(jsonFromResponse, clazz); + } + +} diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/ValidatorGuiTest.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/ValidatorGuiTest.java index b9b5e3f4d..b8e88e779 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/ValidatorGuiTest.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/ValidatorGuiTest.java @@ -3,6 +3,7 @@ package org.hl7.fhir.validation.cli; import io.github.bonigarcia.wdm.WebDriverManager; import org.hl7.fhir.validation.cli.model.CliContext; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; @@ -15,8 +16,9 @@ class ValidatorGuiTest { private final String HTML_TITLE_TAG = "FHIR HL7 Resrouce Validator GUI"; @Test + @DisplayName("Page boots correctly, and displays index.html") public void UI_contains_correct_heading() throws IOException { - ValidatorGui.start(new CliContext(), null); + ValidatorGui.start(new CliContext(), null, false); WebDriverManager.chromedriver().setup(); ChromeOptions options = new ChromeOptions(); options.addArguments("--headless"); diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/controller/HttpGetContextTest.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/controller/HttpGetContextTest.java new file mode 100644 index 000000000..ac10cd47a --- /dev/null +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/controller/HttpGetContextTest.java @@ -0,0 +1,47 @@ +package org.hl7.fhir.validation.cli.controller; + +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.client.HttpClientBuilder; +import org.hl7.fhir.validation.cli.BaseRestTest; +import org.hl7.fhir.validation.cli.model.CliContext; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +class HttpGetContextTest extends BaseRestTest { + + private final String GET_CONTEXT_URL = "http://localhost:8080/context"; + + @Test + @DisplayName("Testing status code on get context endpoint.") + public void testStatus() throws IOException { + HttpUriRequest request = new HttpGet(GET_CONTEXT_URL); + HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); + Assertions.assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK); + } + + @Test + @DisplayName("Testing media type on get context endpoint.") + public void testMediaType() throws IOException { + HttpUriRequest request = new HttpGet(GET_CONTEXT_URL); + HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); + String mimeType = ContentType.getOrDefault(httpResponse.getEntity()).getMimeType(); + Assertions.assertEquals(JSON_MIME_TYPE, mimeType ); + } + + @Test + @DisplayName("Testing status code on get context endpoint.") + public void testJSONPayload() throws IOException { + HttpUriRequest request = new HttpGet(GET_CONTEXT_URL); + HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); + CliContext resource = retrieveResourceFromResponse(httpResponse, CliContext.class); + Assertions.assertEquals(new CliContext(), resource); + } + +} \ No newline at end of file diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/controller/HttpPutContextTest.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/controller/HttpPutContextTest.java new file mode 100644 index 000000000..5c967191e --- /dev/null +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/controller/HttpPutContextTest.java @@ -0,0 +1,24 @@ +package org.hl7.fhir.validation.cli.controller; + +import io.javalin.http.Context; + +import org.junit.jupiter.api.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +class HttpPutContextTest { + + public CliContextController myCliContextController; + + public HttpPutContextTest() { + this.myCliContextController = new CliContextController(null); + } + + @Test + void handleSetCurrentCliContext() { + Context context = mock(Context.class); + this.myCliContextController.handleSetCurrentCliContext(context); + verify(context).status(200); + } +} \ No newline at end of file diff --git a/org.hl7.fhir.validation/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/org.hl7.fhir.validation/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 000000000..ca6ee9cea --- /dev/null +++ b/org.hl7.fhir.validation/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline \ No newline at end of file