From e80a4d93eb5daa94bc892e8115d1ef44e28d9fbd Mon Sep 17 00:00:00 2001 From: Sagar Rout Date: Fri, 11 Oct 2024 08:29:26 +0200 Subject: [PATCH] test: add unit tests for BranchLogic (#134) --- build.gradle | 4 +- .../com/crowdin/logic/BranchLogicTest.java | 123 ++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/crowdin/logic/BranchLogicTest.java diff --git a/build.gradle b/build.gradle index 3f76012..44b7b49 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,9 @@ dependencies { testImplementation 'junit:junit:4.13.1' testImplementation 'org.junit.jupiter:junit-jupiter:5.5.2' testImplementation 'org.hamcrest:hamcrest:2.2' - testImplementation 'org.mockito:mockito-core:2.1.0' + testImplementation 'org.mockito:mockito-core:4.0.0' + testImplementation 'org.mockito:mockito-junit-jupiter:4.0.0' + testImplementation 'org.mockito:mockito-inline:4.0.0' } test { diff --git a/src/test/java/com/crowdin/logic/BranchLogicTest.java b/src/test/java/com/crowdin/logic/BranchLogicTest.java new file mode 100644 index 0000000..fce6b5f --- /dev/null +++ b/src/test/java/com/crowdin/logic/BranchLogicTest.java @@ -0,0 +1,123 @@ +package com.crowdin.logic; + +import com.crowdin.client.BranchInfo; +import com.crowdin.client.Crowdin; +import com.crowdin.client.config.CrowdinConfig; +import com.crowdin.service.CrowdinProjectCacheProvider; +import com.crowdin.util.CrowdinFileUtil; +import com.crowdin.util.GitUtil; +import com.intellij.openapi.project.Project; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Collections; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class BranchLogicTest { + + @Mock + private Crowdin crowdin; + + @Mock + private Project project; + + @Mock + private CrowdinConfig properties; + + @Mock + private CrowdinProjectCacheProvider.CrowdinProjectCache projectCache; + + @InjectMocks + private BranchLogic branchLogic; + + private MockedStatic gitUtilMock; + private MockedStatic crowdinFileUtilMock; + + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + branchLogic = new BranchLogic(crowdin, project, properties); + gitUtilMock = mockStatic(GitUtil.class); + crowdinFileUtilMock = mockStatic(CrowdinFileUtil.class); + } + + @AfterEach + void teardown() { + gitUtilMock.close(); + crowdinFileUtilMock.close(); + } + + @Test + void testAcquireBranchName_UseGitBranch() { + when(properties.isUseGitBranch()).thenReturn(true); + BranchInfo mockBranchInfo = new BranchInfo("feature-branch", "feature-branch"); + when(GitUtil.getCurrentBranch(project)).thenReturn(mockBranchInfo); + when(CrowdinFileUtil.isValidBranchName("feature-branch")).thenReturn(true); + + when(GitUtil.getCurrentBranch(project)).thenReturn(mockBranchInfo); + when(CrowdinFileUtil.isValidBranchName("feature-branch")).thenReturn(true); + + String branchName = branchLogic.acquireBranchName(); + + assertEquals("feature-branch", branchName); + } + + @Test + void testAcquireBranchName_UseConfiguredBranch() { + when(properties.isUseGitBranch()).thenReturn(false); + when(properties.getBranch()).thenReturn("configured-branch"); + when(CrowdinFileUtil.isValidBranchName("configured-branch")).thenReturn(true); + + String branchName = branchLogic.acquireBranchName(); + + assertEquals("configured-branch", branchName); + } + + @Test + void testAcquireBranchName_InvalidBranchName() { + when(properties.isUseGitBranch()).thenReturn(false); + when(properties.getBranch()).thenReturn("invalid/branch"); + when(CrowdinFileUtil.isValidBranchName("invalid/branch")).thenReturn(false); + + RuntimeException exception = assertThrows(RuntimeException.class, () -> branchLogic.acquireBranchName()); + + assertEquals("Branch name can't contain any of the following characters: \\ / : * ? \" < > |", exception.getMessage()); + } + + @Test + void testAcquireBranchName_EmptyBranchName() { + when(properties.isUseGitBranch()).thenReturn(false); + when(properties.getBranch()).thenReturn(""); + when(CrowdinFileUtil.isValidBranchName("")).thenReturn(true); + + String branchName = branchLogic.acquireBranchName(); + + assertEquals("", branchName); + } + + @Test + void testGetBranch_NonExistingBranch_DoNotCreate() { + when(projectCache.getBranches()).thenReturn(Collections.emptyMap()); + when(properties.isUseGitBranch()).thenReturn(false); + when(properties.getBranch()).thenReturn("non-existing-branch"); + when(CrowdinFileUtil.isValidBranchName("non-existing-branch")).thenReturn(true); + + branchLogic.acquireBranchName(); + RuntimeException exception = assertThrows(RuntimeException.class, () -> branchLogic.getBranch(projectCache, false)); + + assertEquals("Branch 'non-existing-branch' does not exists in Crowdin. Try switching to another branch locally", exception.getMessage()); + verify(projectCache).getBranches(); + verifyNoMoreInteractions(crowdin); + } +} \ No newline at end of file