From b6afa6d8ba52a5371f970562ca7d6c551b35a650 Mon Sep 17 00:00:00 2001 From: lincmba Date: Thu, 25 Jan 2024 00:00:50 +0300 Subject: [PATCH] Add tests Make requested changes --- efsity/build.gradle.kts | 1 + .../command/PublishFhirResourcesCommand.java | 6 +-- .../command/ValidateFhirResourcesCommand.java | 2 +- .../PublishFhirResourcesCommandTest.java | 49 +++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/efsity/build.gradle.kts b/efsity/build.gradle.kts index fd1661fa..424ead90 100644 --- a/efsity/build.gradle.kts +++ b/efsity/build.gradle.kts @@ -79,6 +79,7 @@ dependencies { testImplementation(kotlin("test")) testImplementation("junit:junit:4.13.2") + testImplementation("org.mockito:mockito-inline:3.12.4") } tasks.withType { options.encoding = deps.versions.project.build.sourceEncoding.get() } diff --git a/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java b/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java index 6b8d81a0..ae9b8981 100644 --- a/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java +++ b/efsity/src/main/java/org/smartregister/command/PublishFhirResourcesCommand.java @@ -79,9 +79,9 @@ public class PublishFhirResourcesCommand implements Runnable{ @CommandLine.Option( names = {"-vr", "--validate-resource"}, description = - "-vr or --validate-resource - (Optional) whether to validate FHIR resources before publishing or not. Optional boolean - default is `true`", + "(Optional) whether to validate FHIR resources before publishing or not. Boolean - default is `true`", required = false) - private String validateResource = "true"; + String validateResource = "true"; @Override public void run() { @@ -166,7 +166,7 @@ void publishResources() throws IOException, ValidationException, GenerationExcep FctUtils.printInfo(String.format("Validating file \u001b[35m%s\u001b[0m", f)); ValidateFhirResourcesCommand.validateFhirResources(f); } else { - FctUtils.printInfo("Publishing Without Validation"); + FctUtils.printInfo(String.format("Publishing \u001b[35m%s\u001b[0m Without Validation", f)); } FctFile inputFile = FctUtils.readFile(f); diff --git a/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java b/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java index 1574ed03..a0e9fb12 100644 --- a/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java +++ b/efsity/src/main/java/org/smartregister/command/ValidateFhirResourcesCommand.java @@ -42,7 +42,7 @@ public class ValidateFhirResourcesCommand implements Runnable { names = {"-s", "--schema"}, description = "configs schema" ) - private static String configSchema; + static String configSchema; @Override public void run() { diff --git a/efsity/src/test/java/org/smartregister/command/PublishFhirResourcesCommandTest.java b/efsity/src/test/java/org/smartregister/command/PublishFhirResourcesCommandTest.java index b276b3f7..d6ee7207 100644 --- a/efsity/src/test/java/org/smartregister/command/PublishFhirResourcesCommandTest.java +++ b/efsity/src/test/java/org/smartregister/command/PublishFhirResourcesCommandTest.java @@ -1,22 +1,29 @@ package org.smartregister.command; +import net.jimblackler.jsonschemafriend.GenerationException; +import net.jimblackler.jsonschemafriend.ValidationException; import org.json.JSONObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import org.mockito.*; import org.smartregister.domain.FctFile; import org.smartregister.util.FctUtils; import java.io.FileWriter; +import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; public class PublishFhirResourcesCommandTest { + @InjectMocks private PublishFhirResourcesCommand publishFhirResourcesCommand; @TempDir @@ -87,4 +94,46 @@ void testBuildResourceObject() throws IOException { assertTrue(resource.get("meta").toString() .contains("{\"tag\":[{\"system\":\"https://smartregister.org/fct-release-version\",\"code\":\"")); } + + @Test + void testPublishResourcesValidationFalse() throws IOException, ValidationException, GenerationException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + try (MockedStatic validateMock = + Mockito.mockStatic(ValidateFhirResourcesCommand.class)) { + validateMock.when(() -> ValidateFhirResourcesCommand.validateFhirResources( + "src/test/resources/raw_questionnaire.json")).thenAnswer(Answers.RETURNS_DEFAULTS); + PublishFhirResourcesCommand mockPublishFhirResourcesCommand = mock(PublishFhirResourcesCommand.class); + mockPublishFhirResourcesCommand.validateResource = "false"; + mockPublishFhirResourcesCommand.accessToken = "testAccessToken"; + mockPublishFhirResourcesCommand.projectFolder = "src/test/resources/raw_questionnaire.json"; + doNothing().when(mockPublishFhirResourcesCommand).postRequest(Mockito.anyString(), Mockito.anyString()); + doCallRealMethod().when(mockPublishFhirResourcesCommand).publishResources(); + mockPublishFhirResourcesCommand.publishResources(); + } + System.setOut(System.out); + String printedOutput = outputStream.toString().trim(); + assertTrue(printedOutput.contains("Without Validation")); + } + + @Test + void testPublishResourcesValidationTrue() throws IOException, ValidationException, GenerationException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + try (MockedStatic validateMock = + Mockito.mockStatic(ValidateFhirResourcesCommand.class)) { + validateMock.when(() -> ValidateFhirResourcesCommand.validateFhirResources( + "src/test/resources/raw_questionnaire.json")).thenAnswer(Answers.RETURNS_DEFAULTS); + PublishFhirResourcesCommand mockPublishFhirResourcesCommand = mock(PublishFhirResourcesCommand.class); + mockPublishFhirResourcesCommand.validateResource = "true"; + mockPublishFhirResourcesCommand.accessToken = "testAccessToken"; + mockPublishFhirResourcesCommand.projectFolder = "src/test/resources/raw_questionnaire.json"; + doNothing().when(mockPublishFhirResourcesCommand).postRequest(Mockito.anyString(), Mockito.anyString()); + doCallRealMethod().when(mockPublishFhirResourcesCommand).publishResources(); + mockPublishFhirResourcesCommand.publishResources(); + } + System.setOut(System.out); + String printedOutput = outputStream.toString().trim(); + assertTrue(printedOutput.contains("Validating file")); + } } \ No newline at end of file