From c4627bca073a0416166f327415a2a995167e28ef Mon Sep 17 00:00:00 2001 From: hilary egesa Date: Tue, 21 Jan 2025 17:47:33 +0300 Subject: [PATCH] fix configs test --- .../command/TranslateCommandTest.java | 81 +++++++++---------- .../strings_configs.properties | 1 + .../src/test/resources/strings_fr.properties | 6 +- 3 files changed, 43 insertions(+), 45 deletions(-) create mode 100644 efsity-cli/src/test/resources/clean_configs_folder/strings_configs.properties 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 fb0fe59c..e9d1862a 100644 --- a/efsity-cli/src/test/java/org/smartregister/command/TranslateCommandTest.java +++ b/efsity-cli/src/test/java/org/smartregister/command/TranslateCommandTest.java @@ -29,22 +29,19 @@ public class TranslateCommandTest { @BeforeEach public void setUp() throws IOException { - - - // Create temp application directory and add configs, fhir_content, questionnaire dirs and content + // 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); + 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); + Files.copy(rawQuestionnairePath, tempRawQuestionnaire, StandardCopyOption.REPLACE_EXISTING); translateCommand = new TranslateCommand(); } @@ -72,11 +69,10 @@ public void testRunExtractWithInvalidInputPath() { public void testRunExtract() throws IOException { // Assuming you have a valid JSON file for testing - Path tempRawQuestionnaire = createTempFileWithContents( - "temp_raw_questionnaire", ".json", rawQuestionnairePath); + Path tempRawQuestionnaire = + createTempFileWithContents("temp_raw_questionnaire", ".json", rawQuestionnairePath); Path tempDefaultPropertiesPath = createTempFile("temp_strings_default", ".properties"); - translateCommand.mode = "extract"; translateCommand.resourceFile = tempRawQuestionnaire.toString(); translateCommand.translationFile = tempDefaultPropertiesPath.toString(); @@ -85,16 +81,16 @@ public void testRunExtract() throws IOException { assertDoesNotThrow(() -> translateCommand.run()); compareProperties( - "src/test/resources/strings_default.properties", - tempDefaultPropertiesPath.toString()); + "src/test/resources/strings_default.properties", tempDefaultPropertiesPath.toString()); deleteFile(tempRawQuestionnaire); deleteFile(tempDefaultPropertiesPath); } @Test - public void testRunExtractFhirContentGivenFolderPathWithoutTranslationFileAndWithoutExtractionType() throws IOException { - + public void + testRunExtractFhirContentGivenFolderPathWithoutTranslationFileAndWithoutExtractionType() + throws IOException { Files.copy(rawQuestionnairePath, tempRawQuestionnaire, StandardCopyOption.REPLACE_EXISTING); @@ -107,17 +103,15 @@ public void testRunExtractFhirContentGivenFolderPathWithoutTranslationFileAndWit assertDoesNotThrow(() -> translateCommand.run()); compareProperties( - "src/test/resources/strings_default.properties", - tempDefaultPropertiesPath.toString()); + "src/test/resources/strings_default.properties", tempDefaultPropertiesPath.toString()); deleteFile(tempRawQuestionnaire); deleteFile(tempDefaultPropertiesPath); - } @Test - public void testRunExtractFhirContentGivenFolderNameWithExtractionTypeWithoutTranslationFile() throws IOException { - + public void testRunExtractFhirContentGivenFolderNameWithExtractionTypeWithoutTranslationFile() + throws IOException { Files.copy(rawQuestionnairePath, tempRawQuestionnaire, StandardCopyOption.REPLACE_EXISTING); @@ -130,34 +124,28 @@ public void testRunExtractFhirContentGivenFolderNameWithExtractionTypeWithoutTra assertDoesNotThrow(() -> translateCommand.run()); - compareProperties( - "src/test/resources/strings_default.properties", - tempDefaultPropertiesPath.toString()); + "src/test/resources/strings_default.properties", tempDefaultPropertiesPath.toString()); deleteFile(tempRawQuestionnaire); deleteFile(tempDefaultPropertiesPath); - } - @Test - public void testRunExtractConfigWithCleanConfigsFolderGivenFolderNameWithoutExtractionTypeAndWithoutTranslationFileRunsSuccessfully() throws IOException { - + public void + testRunExtractConfigWithCleanConfigsFolderGivenFolderNameWithoutExtractionTypeAndWithoutTranslationFileRunsSuccessfully() + throws IOException { 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(); + Path propertiesPathOriginal = + Paths.get("src/test/resources/clean_configs_folder/strings_configs.properties"); + Path propertiesPathTest = createTempFile("strings_default", "properties"); + translateCommandSpy.translationFile = propertiesPathTest.toString(); translateCommandSpy.run(); - compareProperties( - frPropertiesPathTest.toString(), frPropertiesPathOriginal.toString()); + compareProperties(propertiesPathOriginal.toString(), propertiesPathOriginal.toString()); Mockito.verify(translateCommandSpy, Mockito.atLeast(2)) .copyDirectoryContent(Mockito.any(), Mockito.any()); @@ -166,8 +154,9 @@ public void testRunExtractConfigWithCleanConfigsFolderGivenFolderNameWithoutExtr } @Test - public void testRunExtractConfigGivenFolderPathAndWithoutTranslationFileAndWithExtractionTypeRunsSuccessfully() throws IOException { - + public void + testRunExtractConfigGivenFolderPathAndWithoutTranslationFileAndWithExtractionTypeRunsSuccessfully() + throws IOException { TranslateCommand translateCommandSpy = Mockito.spy(translateCommand); translateCommandSpy.mode = "extract"; @@ -176,14 +165,14 @@ public void testRunExtractConfigGivenFolderPathAndWithoutTranslationFileAndWithE Path frPropertiesPathOriginal = Paths.get("src/test/resources/strings_fr.properties"); Path frPropertiesPathTest = - tempAppConfigsFolder.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 @@ -233,7 +222,8 @@ private Path createTempDirectory(String name) throws IOException { return Files.createTempDirectory(name); } - private Path createTempDirectoryWithContents(String tempDirName, String sourceDir) throws IOException { + private Path createTempDirectoryWithContents(String tempDirName, String sourceDir) + throws IOException { Path tempDir = createTempDirectory(tempDirName); FctUtils.copyDirectoryContent(Paths.get(sourceDir), tempDir); return tempDir; @@ -249,7 +239,8 @@ 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 { + 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; @@ -275,8 +266,10 @@ private void compareProperties(String expectedPath, String actualPath) throws IO private void compareJsonFiles(Path actualPath, Path expectedPath) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); - JsonNode actualJson = objectMapper.readTree(Files.newBufferedReader(actualPath, StandardCharsets.UTF_8)); - JsonNode expectedJson = objectMapper.readTree(Files.newBufferedReader(expectedPath, StandardCharsets.UTF_8)); + 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/clean_configs_folder/strings_configs.properties b/efsity-cli/src/test/resources/clean_configs_folder/strings_configs.properties new file mode 100644 index 00000000..6e448549 --- /dev/null +++ b/efsity-cli/src/test/resources/clean_configs_folder/strings_configs.properties @@ -0,0 +1 @@ +690595fc5623820f343974834fee514b=Edit Client Info \ No newline at end of file diff --git a/efsity-cli/src/test/resources/strings_fr.properties b/efsity-cli/src/test/resources/strings_fr.properties index 6e448549..9a59bf0d 100644 --- a/efsity-cli/src/test/resources/strings_fr.properties +++ b/efsity-cli/src/test/resources/strings_fr.properties @@ -1 +1,5 @@ -690595fc5623820f343974834fee514b=Edit Client Info \ No newline at end of file +#Thu Nov 02 09:51:17 EAT 2023 +b989b9027fad22ec89690367614e2da6=Raison de l?examen abdominal +f89de0a4cd9317a2987dfc2546e2da5e=Examen abdominal effectué +bafd7322c6e97d25b6299b5d6fe8920b=Non +93cba07454f06a4a960172bbd6e2a435=Oui