Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Make requested changes
  • Loading branch information
lincmba committed Jan 25, 2024
1 parent 629420e commit b6afa6d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions efsity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dependencies {

testImplementation(kotlin("test"))
testImplementation("junit:junit:4.13.2")
testImplementation("org.mockito:mockito-inline:3.12.4")
}

tasks.withType<JavaCompile> { options.encoding = deps.versions.project.build.sourceEncoding.get() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<ValidateFhirResourcesCommand> 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<ValidateFhirResourcesCommand> 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"));
}
}

0 comments on commit b6afa6d

Please sign in to comment.