From 76ce81cb4569b96c55614f1c6f6021642c01b2a8 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Wed, 11 Oct 2023 10:40:31 +0530 Subject: [PATCH 1/4] Fix #653 Added more Unit tests to FileUtils class Improved code coverage from 70% to 95% --- .../crowdin/cli/utils/file/FileUtilsTest.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/crowdin/cli/utils/file/FileUtilsTest.java b/src/test/java/com/crowdin/cli/utils/file/FileUtilsTest.java index 6c5f47414..7c99371e9 100644 --- a/src/test/java/com/crowdin/cli/utils/file/FileUtilsTest.java +++ b/src/test/java/com/crowdin/cli/utils/file/FileUtilsTest.java @@ -6,14 +6,11 @@ import org.junit.jupiter.api.Test; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; public class FileUtilsTest extends WorkWithProjectTestPart { @@ -46,4 +43,30 @@ public void testWriteToFile_2() throws IOException { String result = org.apache.commons.io.FileUtils.readFileToString(file, "UTF-8"); assertEquals(text, result); } + @Test + public void testReadYamlFile_UnexpectedException() { + File yamlFile = new File(tempProject.getBasePath() + "invalid.yaml"); + try (FileOutputStream fos = new FileOutputStream(yamlFile)) { + fos.write("invalid_yaml_content: @#!@".getBytes()); + } catch (IOException e) { + fail("Failed to setup test: " + e.getMessage()); + } + + assertThrows(RuntimeException.class, () -> FileUtils.readYamlFile(yamlFile)); + } + + @Test + public void testReadYamlFile_NullFile() { + assertThrows(NullPointerException.class, () -> FileUtils.readYamlFile(null)); + } + + @Test + public void testReadYamlFile_NonYamlExtension() { + File nonYamlFile = new File(tempProject.getBasePath() + "somefile.txt"); + try { + FileUtils.readYamlFile(nonYamlFile); + } catch (RuntimeException e) { + assertFalse(e.getMessage().equals("error.configuration_file_not_exist")); + } + } } From 34c4120adf76cfb0d43ac1509d30f75a156897f1 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 12 Oct 2023 05:00:53 +0530 Subject: [PATCH 2/4] Fix #658 Added Unit tests for the ListLanguagesAction.java class --- .../actions/ListLanguagesActionTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java diff --git a/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java new file mode 100644 index 000000000..6d085ba47 --- /dev/null +++ b/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java @@ -0,0 +1,74 @@ +package com.crowdin.cli.commands.actions; + +import com.crowdin.cli.BaseCli; +import com.crowdin.cli.client.CrowdinProjectInfo; +import com.crowdin.cli.client.ProjectClient; +import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.properties.ProjectProperties; +import com.crowdin.client.languages.model.Language; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.*; + +public class ListLanguagesActionTest { + + ProjectClient client; + CrowdinProjectInfo projectInfo; + Outputter out; + ProjectProperties properties; + ListLanguagesAction action; + + @BeforeEach + public void setup() { + client = mock(ProjectClient.class); + projectInfo = mock(CrowdinProjectInfo.class); + out = mock(Outputter.class); + properties = mock(ProjectProperties.class); + + when(client.downloadProjectInfo()).thenReturn(projectInfo); + when(projectInfo.isManagerAccess()).thenReturn(true); + } + + @Test + public void testActWithNoManagerAccess() { + when(client.downloadProjectInfo()).thenReturn(projectInfo); + when(projectInfo.isManagerAccess()).thenReturn(false); + + action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, false); + action.act(out, properties, client); + + verify(out, times(2)).println(anyString()); + } + + @Test + public void testActWithPlainViewNoManagerAccess() { + when(client.downloadProjectInfo()).thenReturn(projectInfo); + when(projectInfo.isManagerAccess()).thenReturn(false); + + action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, true); + + assertThrows(RuntimeException.class, () -> action.act(out, properties, client)); + } + + + @Test + public void testActPrintLanguages() { + when(client.downloadProjectInfo()).thenReturn(projectInfo); + when(projectInfo.isManagerAccess()).thenReturn(true); + when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(new Language() {{ + setName("name"); + setTwoLettersCode("code"); + }})); + + action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, false); + action.act(out, properties, client); + + verify(out, times(2)).println(anyString()); + } +} From cbc9472cc1b16a7d4ee95a2c80cab159dd25a904 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 12 Oct 2023 15:56:55 +0530 Subject: [PATCH 3/4] Update ListLanguageActionTest to increase code coverage --- .../crowdin/cli/client/LanguageMapping.java | 4 +- .../actions/ListLanguagesActionTest.java | 108 ++++++++++++++---- 2 files changed, 90 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/crowdin/cli/client/LanguageMapping.java b/src/main/java/com/crowdin/cli/client/LanguageMapping.java index 2227f0164..836346ebf 100644 --- a/src/main/java/com/crowdin/cli/client/LanguageMapping.java +++ b/src/main/java/com/crowdin/cli/client/LanguageMapping.java @@ -7,10 +7,10 @@ import java.util.Map; @ToString -public final class LanguageMapping { +public class LanguageMapping { private final Map> languageMapping; - private LanguageMapping() { + protected LanguageMapping() { this.languageMapping = new HashMap<>(); } diff --git a/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java index 6d085ba47..9c4c8d006 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java @@ -2,6 +2,7 @@ import com.crowdin.cli.BaseCli; import com.crowdin.cli.client.CrowdinProjectInfo; +import com.crowdin.cli.client.LanguageMapping; import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectProperties; @@ -9,9 +10,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.Arrays; import java.util.Collections; -import java.util.List; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.*; @@ -22,7 +21,6 @@ public class ListLanguagesActionTest { CrowdinProjectInfo projectInfo; Outputter out; ProjectProperties properties; - ListLanguagesAction action; @BeforeEach public void setup() { @@ -31,44 +29,114 @@ public void setup() { out = mock(Outputter.class); properties = mock(ProjectProperties.class); + LanguageMapping langMapping = new LanguageMapping() { + @Override + public String getValueOrDefault(String languageId, String placeholder, String defaultValue) { + return defaultValue; + } + }; + when(client.downloadProjectInfo()).thenReturn(projectInfo); when(projectInfo.isManagerAccess()).thenReturn(true); + when(projectInfo.getLanguageMapping()).thenReturn(langMapping); } + @Test public void testActWithNoManagerAccess() { - when(client.downloadProjectInfo()).thenReturn(projectInfo); when(projectInfo.isManagerAccess()).thenReturn(false); + ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, false); + action.act(out, properties, client); + verify(out, times(2)).println(anyString()); + } - action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, false); + @Test + public void testActPrintLanguagesPlainView() { + Language lang = new Language(); + lang.setName("English"); + lang.setId("en"); + lang.setTwoLettersCode("en"); + when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang)); + + ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, true); action.act(out, properties, client); + verify(out).println("en"); + } - verify(out, times(2)).println(anyString()); + @Test + public void testActPrintLanguagesWithThreeLettersCode() { + Language lang = new Language(); + lang.setThreeLettersCode("eng"); + when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang)); + + ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.three_letters_code, false, false); + action.act(out, properties, client); + verify(out).println(contains("eng")); } @Test - public void testActWithPlainViewNoManagerAccess() { - when(client.downloadProjectInfo()).thenReturn(projectInfo); - when(projectInfo.isManagerAccess()).thenReturn(false); + public void testActPrintLanguagesWithAndroidCode() { + Language lang = new Language(); + lang.setAndroidCode("en-rUS"); + when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang)); - action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, true); + ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.android_code, false, false); + action.act(out, properties, client); + verify(out).println(contains("en-rUS")); + } - assertThrows(RuntimeException.class, () -> action.act(out, properties, client)); + @Test + public void testActPrintLanguagesWithOsxCode() { + Language lang = new Language(); + lang.setOsxCode("en"); + when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang)); + + ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.osx_code, false, false); + action.act(out, properties, client); + verify(out).println(contains("en")); } + @Test + public void testActPrintLanguagesWithOsxLocale() { + Language lang = new Language(); + lang.setOsxLocale("en_US"); + when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang)); + + ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.osx_locale, false, false); + action.act(out, properties, client); + verify(out).println(contains("en_US")); + } @Test - public void testActPrintLanguages() { - when(client.downloadProjectInfo()).thenReturn(projectInfo); - when(projectInfo.isManagerAccess()).thenReturn(true); - when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(new Language() {{ - setName("name"); - setTwoLettersCode("code"); - }})); + public void testActPrintLanguagesWithLocale() { + Language lang = new Language(); + lang.setLocale("en_US"); + when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang)); - action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, false); + ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.locale, false, false); action.act(out, properties, client); + verify(out).println(contains("en_US")); + } - verify(out, times(2)).println(anyString()); + @Test + public void testActWithoutManagerAccessPlainView() { + when(projectInfo.isManagerAccess()).thenReturn(false); + + ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, true); + assertThrows(RuntimeException.class, () -> action.act(out, properties, client)); + } + + @Test + public void testActPrintLanguagesWithNullCode() { + Language lang = new Language(); + lang.setName("English"); + lang.setTwoLettersCode("en"); + when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang)); + + ListLanguagesAction action = new ListLanguagesAction(null, false, false); + action.act(out, properties, client); + + verify(out, times(1)).println("✔️ Fetching project info"); + verify(out, times(1)).println("✔️ English @|bold 'en'|@"); } } From 79371949bccfefd6ea85fb5ed0fbcce79a856cb1 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Thu, 12 Oct 2023 16:22:23 +0530 Subject: [PATCH 4/4] Removed testActPrintLanguagesWithNullCode() method --- .../commands/actions/ListLanguagesActionTest.java | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java index 9c4c8d006..d186d47bb 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/ListLanguagesActionTest.java @@ -125,18 +125,4 @@ public void testActWithoutManagerAccessPlainView() { ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, true); assertThrows(RuntimeException.class, () -> action.act(out, properties, client)); } - - @Test - public void testActPrintLanguagesWithNullCode() { - Language lang = new Language(); - lang.setName("English"); - lang.setTwoLettersCode("en"); - when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang)); - - ListLanguagesAction action = new ListLanguagesAction(null, false, false); - action.act(out, properties, client); - - verify(out, times(1)).println("✔️ Fetching project info"); - verify(out, times(1)).println("✔️ English @|bold 'en'|@"); - } }