Skip to content

Commit

Permalink
test: add unit tests for BranchLogic (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagar-rout authored Oct 11, 2024
1 parent a77ddbb commit e80a4d9
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
123 changes: 123 additions & 0 deletions src/test/java/com/crowdin/logic/BranchLogicTest.java
Original file line number Diff line number Diff line change
@@ -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<GitUtil> gitUtilMock;
private MockedStatic<CrowdinFileUtil> 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);
}
}

0 comments on commit e80a4d9

Please sign in to comment.