Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hajekad3 committed Jan 5, 2023
1 parent c6eb9cb commit 251d493
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package cz.cvut.fit.hajekad3.reservantor;
package cz.cvut.fit.hajekad3.reservantor.Acceptance;

import cz.cvut.fit.hajekad3.reservantor.Api.Controllers.CoachController;
import cz.cvut.fit.hajekad3.reservantor.ApplicationLayer.Implementations.CoachService;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Coach.CoachDto;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Coach.CreateCoachDto;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import static org.mockito.Mockito.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CoachAcceptanceTests {
@Mock
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cz.cvut.fit.hajekad3.reservantor.Helper;

import cz.cvut.fit.hajekad3.reservantor.DomainLayer.Coach;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Coach.CreateCoachDto;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Coach.CoachDto;

import java.util.ArrayList;

public class CoachHelper {
public Coach fillCoach() {
Coach ret = new Coach(fillCoachDto());

return ret;
}

public CoachDto fillCoachDto() {
CoachDto ret = new CoachDto();

ret.setId(1L);
ret.setCostRate(50);
ret.setTrainings(new ArrayList<>());
ret.setPassword("pass");
ret.setEmail("[email protected]");
ret.setFirstName("Jan");
ret.setSecondName("Novak");

return ret;
}

public CreateCoachDto fillCreateCoachDto() {
CreateCoachDto ret = new CreateCoachDto();

ret.setPassword("pass");
ret.setCostRate(50);
ret.setEmail("[email protected]");
ret.setFirstName("Jan");
ret.setSecondName("Novak");

return ret;
}

public Coach fillCoachFromCreateCoachDto() {
Coach ret = fillCoach();

return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cz.cvut.fit.hajekad3.reservantor.Helper;

import cz.cvut.fit.hajekad3.reservantor.DomainLayer.Place;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Place.CreatePlaceDto;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Place.PlaceDto;

import java.util.ArrayList;

public class PlaceHelper {
public Place fillPlace() {
Place ret = new Place(fillPlaceDto());

return ret;
}

public PlaceDto fillPlaceDto() {
PlaceDto ret = new PlaceDto();

ret.setId(1L);
ret.setLatitude(50D);
ret.setLongitude(50D);
ret.setTrainings(new ArrayList<>());

return ret;
}

public CreatePlaceDto fillCreatePlaceDto() {
CreatePlaceDto ret = new CreatePlaceDto();

ret.setLatitude(50D);
ret.setLongitude(50D);

return ret;
}

public Place fillPlaceFromCreatePlaceDto() {
Place ret = fillPlace();

return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cz.cvut.fit.hajekad3.reservantor.Helper;

import cz.cvut.fit.hajekad3.reservantor.DomainLayer.Trainee;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Trainee.CreateTraineeDto;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Trainee.TraineeDto;

import java.util.ArrayList;

public class TraineeHelper {
public Trainee fillTrainee() {
Trainee ret = new Trainee(fillTraineeDto());

return ret;
}

public TraineeDto fillTraineeDto() {
TraineeDto ret = new TraineeDto();

ret.setId(1L);
ret.setSkillCap(50);
ret.setTrainings(new ArrayList<>());
ret.setUsername("user");
ret.setPassword("pass");
ret.setEmail("[email protected]");
ret.setFirstName("Jan");
ret.setSecondName("Novak");

return ret;
}

public CreateTraineeDto fillCreateTraineeDto() {
CreateTraineeDto ret = new CreateTraineeDto();

ret.setSkillCap(50);
ret.setUsername("user");
ret.setPassword("pass");
ret.setEmail("[email protected]");
ret.setFirstName("Jan");
ret.setSecondName("Novak");

return ret;
}

public Trainee fillTraineeFromCreateTraineeDto() {
Trainee ret = fillTrainee();

return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cz.cvut.fit.hajekad3.reservantor.Helper;

import cz.cvut.fit.hajekad3.reservantor.DomainLayer.Training;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Training.CreateTrainingDto;
import cz.cvut.fit.hajekad3.reservantor.InterfaceLayer.Dtos.Training.TrainingDto;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;

public class TrainingHelper {
private Timestamp now() {
Calendar calendar = Calendar.getInstance();
return new Timestamp(calendar.getTimeInMillis());
}

public Training fillTraining() {
Training ret = new Training(fillTrainingDto());

return ret;
}

public TrainingDto fillTrainingDto() {
TrainingDto ret = new TrainingDto();

ret.setId(1L);
ret.setDateOfTraining(now().toString());
ret.setDescription("Blank");
ret.setIdCoach(1L);
ret.setIdPlace(1L);
ret.setParticipatingTraineesIds(new ArrayList<>());

return ret;
}

public CreateTrainingDto fillCreateTrainingDto() {
CreateTrainingDto ret = new CreateTrainingDto();

ret.setDateOfTraining(now().toString());
ret.setDescription("Blank");
ret.setIdCoach(1L);
ret.setIdPlace(1L);

return ret;
}

public Training fillTrainingFromCreateTrainingDto() {
Training ret = fillTraining();

return ret;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cz.cvut.fit.hajekad3.reservantor.Integration;

public class CoachIntegrationTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cz.cvut.fit.hajekad3.reservantor.Unit;

import cz.cvut.fit.hajekad3.reservantor.DomainLayer.Coach;
import cz.cvut.fit.hajekad3.reservantor.DomainLayer.Trainee;
import cz.cvut.fit.hajekad3.reservantor.DomainLayer.Training;
import cz.cvut.fit.hajekad3.reservantor.InfrastructureLayer.Storage.Implementations.CoachRepositoryExtraMethods;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Collections;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CoachUnitTest {
@Mock
private EntityManager entityManager;

@InjectMocks
private CoachRepositoryExtraMethods coachRepositoryExtraMethods;

@Test
public void coachHasTrainedTrainee() {
// Setup
Trainee trainee = new Trainee();
Coach coach = new Coach();
TypedQuery<Training> query = Mockito.mock(TypedQuery.class);
Mockito.when(query.getResultList()).thenReturn(Arrays.asList(new Training()));
Mockito.when(entityManager.createQuery(Mockito.anyString(), Mockito.eq(Training.class)))
.thenReturn(query);

// Execute
boolean result = coachRepositoryExtraMethods.coachTrainedTrainee(trainee, coach);

// Assert
assert(result == true);
}

@Test
public void coachHasNotTrainedTrainee() {
// Setup
Trainee trainee = new Trainee();
Coach coach = new Coach();
TypedQuery<Training> query = Mockito.mock(TypedQuery.class);
Mockito.when(query.getResultList()).thenReturn(Collections.emptyList());
Mockito.when(entityManager.createQuery(Mockito.anyString(), Mockito.eq(Training.class)))
.thenReturn(query);

// Execute
boolean result = coachRepositoryExtraMethods.coachTrainedTrainee(trainee, coach);

// Assert
assert(result == false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package cz.cvut.fit.hajekad3.reservantor.Unit;

public class TraineeUnitTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cz.cvut.fit.hajekad3.reservantor.Unit;

import cz.cvut.fit.hajekad3.reservantor.DomainLayer.Trainee;
import cz.cvut.fit.hajekad3.reservantor.InfrastructureLayer.Storage.Implementations.TraineeRepositoryExtraMethods;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TrainingUnitTest {
@Mock
private EntityManager entityManager;
@InjectMocks
private TraineeRepositoryExtraMethods traineeRepositoryExtraMethods;

@Test
public void testFindMatch() {
// Setup
Trainee trainee = new Trainee();
trainee.setId(1L);
trainee.setSkillCap(5);

Trainee matchingTrainee1 = new Trainee();
matchingTrainee1.setId(2L);
matchingTrainee1.setSkillCap(4);

Trainee matchingTrainee2 = new Trainee();
matchingTrainee2.setId(3L);
matchingTrainee2.setSkillCap(6);

Trainee nonMatchingTrainee = new Trainee();
nonMatchingTrainee.setId(4L);
nonMatchingTrainee.setSkillCap(1);

List<Trainee> expectedResult = Arrays.asList(matchingTrainee1, matchingTrainee2);

String queryString = "SELECT DISTINCT t FROM Trainee t WHERE t.skillCap >= :traineeSkill - :range AND t.skillCap <= :traineeSkill + :range AND t.id != :traineeId";
TypedQuery<Trainee> query = Mockito.mock(TypedQuery.class);
Mockito.when(query.getResultList()).thenReturn(expectedResult);
Mockito.when(entityManager.createQuery(queryString, Trainee.class)).thenReturn(query);
Mockito.when(query.setParameter("traineeId", trainee.getId())).thenReturn(query);
Mockito.when(query.setParameter("traineeSkill", trainee.getSkillCap())).thenReturn(query);
Mockito.when(query.setParameter("range", 5)).thenReturn(query);

// Execute
List<Trainee> result = traineeRepositoryExtraMethods.findMatch(5, trainee);

// Assert
assert(expectedResult == result);
}
}

0 comments on commit 251d493

Please sign in to comment.