Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests covering controller and data classes #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}