From c9c66c9615776fd9d9a628dab4f87d4f446be9e1 Mon Sep 17 00:00:00 2001 From: krunkerista Date: Fri, 28 May 2021 14:28:55 +0200 Subject: [PATCH] Added unit tests covering controller and data classes --- .../controller/TeamControllerTest.java | 70 +++++++++++++++++++ ...JobCompletionNotificationListenerTest.java | 70 +++++++++++++++++++ .../data/MatchDataProcessorTest.java | 52 ++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 src/test/java/io/javabrains/ipldashboard/controller/TeamControllerTest.java create mode 100644 src/test/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListenerTest.java create mode 100644 src/test/java/io/javabrains/ipldashboard/data/MatchDataProcessorTest.java diff --git a/src/test/java/io/javabrains/ipldashboard/controller/TeamControllerTest.java b/src/test/java/io/javabrains/ipldashboard/controller/TeamControllerTest.java new file mode 100644 index 0000000..269c922 --- /dev/null +++ b/src/test/java/io/javabrains/ipldashboard/controller/TeamControllerTest.java @@ -0,0 +1,70 @@ +package io.javabrains.ipldashboard.controller; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.javabrains.ipldashboard.model.Team; +import io.javabrains.ipldashboard.repository.MatchRepository; +import io.javabrains.ipldashboard.repository.TeamRepository; +import java.time.LocalDate; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@ExtendWith(MockitoExtension.class) +@SpringJUnitConfig(TeamController.class) +class TeamControllerTest { + + @Autowired TeamController teamController; + + @MockBean TeamRepository teamRepositoryMock; + @MockBean MatchRepository matchRepositoryMock; + + @Captor ArgumentCaptor stringArgumentCaptor; + + @Test + void testGetAllTeam() { + teamController.getAllTeam(); + verify(teamRepositoryMock).findAll(); + } + + @Test + void testGetTeam(@Mock Team teamMock) { + String expectedTeamName = "test team"; + when(teamRepositoryMock.findByTeamName(expectedTeamName)).thenReturn(teamMock); + + teamController.getTeam(expectedTeamName); + + verify(teamRepositoryMock).findByTeamName(stringArgumentCaptor.capture()); + verify(teamMock).setMatches(anyList()); + + String capturedTeamName = stringArgumentCaptor.getValue(); + assertThat(capturedTeamName).isEqualTo(expectedTeamName); + } + + @Test + void testGetMatchesForTeam() { + String expectedTeamName = "test team"; + int expectedYear = 2020; + + teamController.getMatchesForTeam(expectedTeamName, expectedYear); + + verify(matchRepositoryMock) + .getMatchesByTeamBetweenDates( + stringArgumentCaptor.capture(), + eq(LocalDate.of(expectedYear, 1, 1)), + eq(LocalDate.of(expectedYear + 1, 1, 1))); + + String capturedTeamName = stringArgumentCaptor.getValue(); + assertThat(capturedTeamName).isEqualTo(expectedTeamName); + } +} diff --git a/src/test/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListenerTest.java b/src/test/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListenerTest.java new file mode 100644 index 0000000..0b1119c --- /dev/null +++ b/src/test/java/io/javabrains/ipldashboard/data/JobCompletionNotificationListenerTest.java @@ -0,0 +1,70 @@ +package io.javabrains.ipldashboard.data; + +import static org.assertj.core.api.Assertions.assertThatNoException; +import static org.junit.jupiter.params.provider.Arguments.arguments; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import java.util.stream.Stream; +import javax.persistence.EntityManager; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Answers; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.test.MetaDataInstanceFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@SpringJUnitConfig(JobCompletionNotificationListener.class) +class JobCompletionNotificationListenerTest { + + @Autowired JobCompletionNotificationListener listener; + + @MockBean(answer = Answers.RETURNS_MOCKS) + EntityManager entityManagerMock; + + JobExecution jobExecution; + + private static Stream provideNonCompletedJobStatuses() { + return Stream.of( + arguments(BatchStatus.ABANDONED), + arguments(BatchStatus.FAILED), + arguments(BatchStatus.STARTED), + arguments(BatchStatus.STARTING), + arguments(BatchStatus.STOPPED), + arguments(BatchStatus.STOPPING), + arguments(BatchStatus.UNKNOWN)); + } + + @BeforeEach + void setUp() { + jobExecution = MetaDataInstanceFactory.createJobExecution(); + } + + @ParameterizedTest + @MethodSource("provideNonCompletedJobStatuses") + void testAfterJob_whenJobNotCompleted_thenReturnsWithoutException( + BatchStatus nonCompletedJobStatus) { + jobExecution.setStatus(nonCompletedJobStatus); + + assertThatNoException().isThrownBy(() -> listener.afterJob(jobExecution)); + verify(entityManagerMock, never()).createQuery(anyString(), any()); + } + + @Test + void testAfterJob_whenJobCompleted_thenVerifiesTheResults() { + jobExecution.setStatus(BatchStatus.COMPLETED); + + listener.afterJob(jobExecution); + + verify(entityManagerMock, atLeast(3)).createQuery(anyString(), any()); + } +} diff --git a/src/test/java/io/javabrains/ipldashboard/data/MatchDataProcessorTest.java b/src/test/java/io/javabrains/ipldashboard/data/MatchDataProcessorTest.java new file mode 100644 index 0000000..bf5e88e --- /dev/null +++ b/src/test/java/io/javabrains/ipldashboard/data/MatchDataProcessorTest.java @@ -0,0 +1,52 @@ +package io.javabrains.ipldashboard.data; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.javabrains.ipldashboard.model.Match; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +@SpringJUnitConfig(MatchDataProcessor.class) +class MatchDataProcessorTest { + + private static final String TOSS_WINNER = "test team 1"; + private static final String TOSS_LOSER = "test team 2"; + + static MatchDataProcessor matchDataProcessor; + static MatchInput matchInput; + + @BeforeAll + static void setUp() { + matchDataProcessor = new MatchDataProcessor(); + matchInput = new MatchInput(); + + matchInput.setId("123"); + matchInput.setDate("2020-09-24"); + matchInput.setToss_winner(TOSS_WINNER); + matchInput.setTeam1(TOSS_WINNER); + matchInput.setTeam2(TOSS_LOSER); + } + + @Test + void testProcess_whenTossDecisionIsBat_thenFirstTeamIsTossWinner() throws Exception { + matchInput.setToss_decision("bat"); + + Match match = matchDataProcessor.process(matchInput); + + assert match != null; + assertThat(match.getTeam1()).isEqualTo(TOSS_WINNER); + assertThat(match.getTeam2()).isEqualTo(TOSS_LOSER); + } + + @Test + void testProcess_whenTossDecisionIsField_thenSecondTeamIsTossWinner() throws Exception { + matchInput.setToss_decision("field"); + + Match match = matchDataProcessor.process(matchInput); + + assert match != null; + assertThat(match.getTeam1()).isEqualTo(TOSS_LOSER); + assertThat(match.getTeam2()).isEqualTo(TOSS_WINNER); + } +}