From 73e94494cabafdea3bab57225f0b0a14695bbc5a Mon Sep 17 00:00:00 2001 From: hilary egesa Date: Thu, 16 Jan 2025 19:05:23 +0300 Subject: [PATCH] init tests to all cextract scenerios --- .../command/TranslateCommand.java | 4 +- .../command/TranslateCommandTest.java | 203 ++++++++++++++---- .../src/test/resources/strings_fr.properties | 6 +- 3 files changed, 168 insertions(+), 45 deletions(-) diff --git a/efsity-cli/src/main/java/org/smartregister/command/TranslateCommand.java b/efsity-cli/src/main/java/org/smartregister/command/TranslateCommand.java index 6758b00c..da2a7a32 100644 --- a/efsity-cli/src/main/java/org/smartregister/command/TranslateCommand.java +++ b/efsity-cli/src/main/java/org/smartregister/command/TranslateCommand.java @@ -235,12 +235,12 @@ public void run() { Objects.requireNonNull(inputFilePath, "Input file path cannot be null"); if (inputFilePath.endsWith("configs") || inputFilePath.endsWith("fhir_content")) { - return inputFilePath.getParent().resolve("translation"); + return inputFilePath.resolve("translation"); } if (inputFilePath.toString().endsWith(".json")) { Path parent = inputFilePath.getParent(); if (parent == null || parent.getParent() == null) { - throw new IllegalArgumentException("Invalid file path structure for: " + inputFilePath); + throw new IllegalArgumentException("Invalid file path for: " + inputFilePath); } return parent.getParent().resolve("translation"); } diff --git a/efsity-cli/src/test/java/org/smartregister/command/TranslateCommandTest.java b/efsity-cli/src/test/java/org/smartregister/command/TranslateCommandTest.java index 40b37e1e..fb0fe59c 100644 --- a/efsity-cli/src/test/java/org/smartregister/command/TranslateCommandTest.java +++ b/efsity-cli/src/test/java/org/smartregister/command/TranslateCommandTest.java @@ -18,22 +18,41 @@ import org.smartregister.util.FctUtils; public class TranslateCommandTest { - private Path tempCleanConfigsFolder; private TranslateCommand translateCommand; + // Create temp application directory and add fhir_content and questionnaire dirs + private Path tempAppFolderPath; + private Path fhirContentFolderPath; + private Path rawQuestionnairePath; + private Path tempRawQuestionnaire; + private Path tempAppConfigsFolder; @BeforeEach public void setUp() throws IOException { - tempCleanConfigsFolder = Files.createTempDirectory("temp_clean_configs_folder"); - Path cleanConfigsFolder = Paths.get("src/test/resources/clean_configs_folder"); - FctUtils.copyDirectoryContent(cleanConfigsFolder, tempCleanConfigsFolder); + + + // Create temp application directory and add configs, fhir_content, questionnaire dirs and content + tempAppFolderPath = createTempDirectory("tempApp"); + tempAppConfigsFolder = tempAppFolderPath.resolve("configs"); + Files.createDirectory(tempAppConfigsFolder); + FctUtils.copyDirectoryContent(Paths.get("src/test/resources/clean_configs_folder"), tempAppConfigsFolder); + fhirContentFolderPath = createSubDirectory(tempAppFolderPath, "fhir_content"); + Path questionnairesFolderPath = createSubDirectory(fhirContentFolderPath, "questionnaires"); + rawQuestionnairePath = Paths.get("src/test/resources/raw_questionnaire.json"); + tempRawQuestionnaire = questionnairesFolderPath.resolve("temp_raw_questionnaire.json"); + Files.createFile(tempRawQuestionnaire); + + + Path cleanTestConfigFile = tempAppConfigsFolder.resolve("profile_config.json"); + Files.copy(tempAppConfigsFolder, cleanTestConfigFile, StandardCopyOption.REPLACE_EXISTING); translateCommand = new TranslateCommand(); } @AfterEach - public void tearDown() { - FctUtils.deleteDirectoryRecursively(tempCleanConfigsFolder); + public void tearDown() throws IOException { + deleteDirectory(tempAppConfigsFolder); + deleteDirectory(tempAppFolderPath); } @Test @@ -52,12 +71,11 @@ public void testRunExtractWithInvalidInputPath() { @Test public void testRunExtract() throws IOException { // Assuming you have a valid JSON file for testing - Path rawQuestionnairePath = Paths.get("src/test/resources/raw_questionnaire.json"); - Path tempRawQuestionnaire = Files.createTempFile("temp_raw_questionnaire", ".json"); - Files.copy(rawQuestionnairePath, tempRawQuestionnaire, StandardCopyOption.REPLACE_EXISTING); - Path defaultPropertiesPath = Paths.get("src/test/resources/strings_default.properties"); - Path tempDefaultPropertiesPath = Files.createTempFile("temp_strings_default", ".properties"); + Path tempRawQuestionnaire = createTempFileWithContents( + "temp_raw_questionnaire", ".json", rawQuestionnairePath); + Path tempDefaultPropertiesPath = createTempFile("temp_strings_default", ".properties"); + translateCommand.mode = "extract"; translateCommand.resourceFile = tempRawQuestionnaire.toString(); @@ -66,40 +84,106 @@ public void testRunExtract() throws IOException { assertDoesNotThrow(() -> translateCommand.run()); - Properties existingProperties = - FctUtils.readPropertiesFile(defaultPropertiesPath.toFile().getAbsolutePath()); + compareProperties( + "src/test/resources/strings_default.properties", + tempDefaultPropertiesPath.toString()); + + deleteFile(tempRawQuestionnaire); + deleteFile(tempDefaultPropertiesPath); + } + + @Test + public void testRunExtractFhirContentGivenFolderPathWithoutTranslationFileAndWithoutExtractionType() throws IOException { + + + Files.copy(rawQuestionnairePath, tempRawQuestionnaire, StandardCopyOption.REPLACE_EXISTING); + + Path tempTranslationsPath = fhirContentFolderPath.resolve("translation"); + Path tempDefaultPropertiesPath = tempTranslationsPath.resolve("strings_default.properties"); + + translateCommand.mode = "extract"; + translateCommand.resourceFile = fhirContentFolderPath.toString(); + + assertDoesNotThrow(() -> translateCommand.run()); + + compareProperties( + "src/test/resources/strings_default.properties", + tempDefaultPropertiesPath.toString()); + + deleteFile(tempRawQuestionnaire); + deleteFile(tempDefaultPropertiesPath); + + } + + @Test + public void testRunExtractFhirContentGivenFolderNameWithExtractionTypeWithoutTranslationFile() throws IOException { + + + Files.copy(rawQuestionnairePath, tempRawQuestionnaire, StandardCopyOption.REPLACE_EXISTING); + + Path translationsPath = fhirContentFolderPath.resolve("translation"); + Path tempDefaultPropertiesPath = translationsPath.resolve("strings_default.properties"); + + translateCommand.mode = "extract"; + translateCommand.resourceFile = fhirContentFolderPath.toString(); + translateCommand.extractionType = "fhirContent"; + + assertDoesNotThrow(() -> translateCommand.run()); + + + compareProperties( + "src/test/resources/strings_default.properties", + tempDefaultPropertiesPath.toString()); + + deleteFile(tempRawQuestionnaire); + deleteFile(tempDefaultPropertiesPath); + + } + + + @Test + public void testRunExtractConfigWithCleanConfigsFolderGivenFolderNameWithoutExtractionTypeAndWithoutTranslationFileRunsSuccessfully() throws IOException { - Properties newProperties = - FctUtils.readPropertiesFile(tempDefaultPropertiesPath.toFile().getAbsolutePath()); - // Compare the contents of the two files - assertEquals(existingProperties, newProperties, "File contents are similar."); - // Clean up temporary resources - tempRawQuestionnaire.toFile().delete(); - tempDefaultPropertiesPath.toFile().delete(); + TranslateCommand translateCommandSpy = Mockito.spy(translateCommand); + translateCommandSpy.mode = "extract"; + translateCommandSpy.resourceFile = tempAppConfigsFolder.toString(); + + Path frPropertiesPathOriginal = Paths.get("src/test/resources/strings_fr.properties"); + Path frPropertiesPathTest = + tempAppConfigsFolder.resolve(frPropertiesPathOriginal.getFileName()); + Files.createFile(frPropertiesPathTest); +// Files.copy(frPropertiesPathOriginal, frPropertiesPathTest, StandardCopyOption.REPLACE_EXISTING); + translateCommandSpy.translationFile = frPropertiesPathTest.toString(); + translateCommandSpy.run(); + compareProperties( + frPropertiesPathTest.toString(), frPropertiesPathOriginal.toString()); + + Mockito.verify(translateCommandSpy, Mockito.atLeast(2)) + .copyDirectoryContent(Mockito.any(), Mockito.any()); + Mockito.verify(translateCommandSpy, Mockito.atLeast(1)) + .deleteDirectoryRecursively(Mockito.any()); } @Test - public void testRunExtractConfigWithCleanConfigsFolderRunsSuccessfully() throws IOException { + public void testRunExtractConfigGivenFolderPathAndWithoutTranslationFileAndWithExtractionTypeRunsSuccessfully() throws IOException { + - Path cleanConfigsFolder = tempCleanConfigsFolder; - Path backupFolder = Files.createTempDirectory("temp_back_up_dir"); - FctUtils.copyDirectoryContent(cleanConfigsFolder, backupFolder); TranslateCommand translateCommandSpy = Mockito.spy(translateCommand); translateCommandSpy.mode = "extract"; translateCommandSpy.extractionType = "configs"; - translateCommandSpy.resourceFile = cleanConfigsFolder.toString(); + translateCommandSpy.resourceFile = tempAppConfigsFolder.toString(); Path frPropertiesPathOriginal = Paths.get("src/test/resources/strings_fr.properties"); Path frPropertiesPathTest = - tempCleanConfigsFolder.resolve(frPropertiesPathOriginal.getFileName()); + tempAppConfigsFolder.resolve(frPropertiesPathOriginal.getFileName()); Files.copy(frPropertiesPathOriginal, frPropertiesPathTest, StandardCopyOption.REPLACE_EXISTING); translateCommandSpy.translationFile = frPropertiesPathTest.toString(); translateCommandSpy.run(); Mockito.verify(translateCommandSpy, Mockito.atLeast(2)) - .copyDirectoryContent(Mockito.any(), Mockito.any()); + .copyDirectoryContent(Mockito.any(), Mockito.any()); Mockito.verify(translateCommandSpy, Mockito.atLeast(1)) - .deleteDirectoryRecursively(Mockito.any()); + .deleteDirectoryRecursively(Mockito.any()); } @Test @@ -140,16 +224,59 @@ public void testRunMerge() throws IOException { assertDoesNotThrow(() -> translateCommand.run()); + compareJsonFiles(tempRawQuestionnaire, expectedMergedQuestionnairePath); + deleteFile(tempRawQuestionnaire); + } + + // Helper Methods + private Path createTempDirectory(String name) throws IOException { + return Files.createTempDirectory(name); + } + + private Path createTempDirectoryWithContents(String tempDirName, String sourceDir) throws IOException { + Path tempDir = createTempDirectory(tempDirName); + FctUtils.copyDirectoryContent(Paths.get(sourceDir), tempDir); + return tempDir; + } + + private Path createSubDirectory(Path parent, String name) throws IOException { + Path subDir = parent.resolve(name); + Files.createDirectories(subDir); + return subDir; + } + + private Path createTempFile(String prefix, String suffix) throws IOException { + return Files.createTempFile(prefix, suffix); + } + + private Path createTempFileWithContents(String prefix, String suffix, Path sourceFile) throws IOException { + Path tempFile = createTempFile(prefix, suffix); + Files.copy(sourceFile, tempFile, StandardCopyOption.REPLACE_EXISTING); + return tempFile; + } + + private void deleteDirectory(Path dir) throws IOException { + FctUtils.deleteDirectoryRecursively(dir); + } + + private void deleteFile(Path file) { + try { + Files.deleteIfExists(file); + } catch (IOException e) { + System.err.println("Failed to delete file: " + file + " due to " + e.getMessage()); + } + } + + private void compareProperties(String expectedPath, String actualPath) throws IOException { + Properties expected = FctUtils.readPropertiesFile(expectedPath); + Properties actual = FctUtils.readPropertiesFile(actualPath); + assertEquals(expected, actual, "Properties files do not match."); + } + + private void compareJsonFiles(Path actualPath, Path expectedPath) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); - JsonNode processedRawQuestionnaire = - objectMapper.readTree( - Files.newBufferedReader(tempRawQuestionnaire, StandardCharsets.UTF_8)); - JsonNode mergedQuestionnaire = - objectMapper.readTree( - Files.newBufferedReader(expectedMergedQuestionnairePath, StandardCharsets.UTF_8)); - - // Compare the contents of the two nodes - assertEquals(mergedQuestionnaire, processedRawQuestionnaire, "File merged as expected"); - tempRawQuestionnaire.toFile().delete(); + JsonNode actualJson = objectMapper.readTree(Files.newBufferedReader(actualPath, StandardCharsets.UTF_8)); + JsonNode expectedJson = objectMapper.readTree(Files.newBufferedReader(expectedPath, StandardCharsets.UTF_8)); + assertEquals(expectedJson, actualJson, "JSON files do not match."); } } diff --git a/efsity-cli/src/test/resources/strings_fr.properties b/efsity-cli/src/test/resources/strings_fr.properties index 44a1b58d..6e448549 100644 --- a/efsity-cli/src/test/resources/strings_fr.properties +++ b/efsity-cli/src/test/resources/strings_fr.properties @@ -1,5 +1 @@ -#Thu Nov 02 09:51:17 EAT 2023 -b989b9027fad22ec89690367614e2da6=Raison de l?examen abdominal -f89de0a4cd9317a2987dfc2546e2da5e=Examen abdominal effectué -bafd7322c6e97d25b6299b5d6fe8920b=Non -93cba07454f06a4a960172bbd6e2a435=Oui +690595fc5623820f343974834fee514b=Edit Client Info \ No newline at end of file