Skip to content

Commit

Permalink
test: Add Unit tests for the ListLanguagesAction.java class (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sweetdevil144 committed Oct 12, 2023
1 parent f2852d3 commit ae1b0c1
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/cli/client/LanguageMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import java.util.Map;

@ToString
public final class LanguageMapping {
public class LanguageMapping {
private final Map<String, Map<String, String>> languageMapping;

private LanguageMapping() {
protected LanguageMapping() {
this.languageMapping = new HashMap<>();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.crowdin.cli.commands.actions;

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;
import com.crowdin.client.languages.model.Language;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

public class ListLanguagesActionTest {

ProjectClient client;
CrowdinProjectInfo projectInfo;
Outputter out;
ProjectProperties properties;

@BeforeEach
public void setup() {
client = mock(ProjectClient.class);
projectInfo = mock(CrowdinProjectInfo.class);
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(projectInfo.isManagerAccess()).thenReturn(false);
ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.id, false, false);
action.act(out, properties, client);
verify(out, times(2)).println(anyString());
}

@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");
}

@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 testActPrintLanguagesWithAndroidCode() {
Language lang = new Language();
lang.setAndroidCode("en-rUS");
when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang));

ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.android_code, false, false);
action.act(out, properties, client);
verify(out).println(contains("en-rUS"));
}

@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 testActPrintLanguagesWithLocale() {
Language lang = new Language();
lang.setLocale("en_US");
when(projectInfo.getProjectLanguages(true)).thenReturn(Collections.singletonList(lang));

ListLanguagesAction action = new ListLanguagesAction(BaseCli.LanguageCode.locale, false, false);
action.act(out, properties, client);
verify(out).println(contains("en_US"));
}

@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));
}
}

0 comments on commit ae1b0c1

Please sign in to comment.