Guest User

Untitled

a guest
Dec 10th, 2019
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package be.codefever.eduapp.repository;
  2.  
  3. import be.codefever.eduapp.mockdata.MockData;
  4. import be.codefever.eduapp.models.StudentExamAnswerEntity;
  5. import com.google.gson.reflect.TypeToken;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
  10. import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
  11. import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
  12. import org.springframework.test.context.ActiveProfiles;
  13. import org.springframework.test.context.junit4.SpringRunner;
  14.  
  15. import java.util.List;
  16. import java.util.UUID;
  17. import java.util.stream.Collectors;
  18.  
  19. import static org.hamcrest.CoreMatchers.is;
  20. import static org.hamcrest.Matchers.containsInAnyOrder;
  21. import static org.hamcrest.Matchers.not;
  22. import static org.junit.Assert.assertThat;
  23. import static org.junit.Assert.assertTrue;
  24.  
  25. @RunWith(SpringRunner.class)
  26. @DataJpaTest
  27. @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
  28. @ActiveProfiles("it")
  29. public class StudentExamAnswerRepositoryTest {
  30.  
  31. @Autowired
  32. private TestEntityManager entityManager;
  33.  
  34. @Autowired
  35. private StudentExamAnswerRepository studentExamAnswerRepository;
  36.  
  37. protected static final MockData mockData = new MockData();
  38.  
  39. private final UUID studentId = UUID.fromString("d49515ed-972c-11e9-b935-0a0027000401");
  40. private final UUID studentIdNotFound = UUID.fromString("d49515ed-972c-11e9-b935-0a0027009999");
  41.  
  42. @Test
  43. public void findAllByStudentId() {
  44. //Given
  45. List<StudentExamAnswerEntity> studentExamAnswerEntities = setup_StudentExamAnswerEntityList();
  46. List<StudentExamAnswerEntity> expected = studentExamAnswerEntities.stream().filter(entity -> entity.getStudentId().equals(studentId)).collect(Collectors.toList());
  47. List<StudentExamAnswerEntity> notExpected = studentExamAnswerEntities.stream().filter(entity -> !entity.getStudentId().equals(studentId)).collect(Collectors.toList());
  48.  
  49. //When
  50. List<StudentExamAnswerEntity> result = studentExamAnswerRepository.findAllByStudentId(studentId);
  51.  
  52. //Then
  53. assertThat(result.size(), is(expected.size()));
  54. assertThat(result, containsInAnyOrder(expected.toArray()));
  55. assertThat(result, not(containsInAnyOrder(notExpected.toArray())));
  56. }
  57.  
  58. @Test
  59. public void findAllByStudentId_idNotFound() {
  60. //Given
  61. setup_StudentExamAnswerEntityList();
  62.  
  63. //When
  64. List<StudentExamAnswerEntity> result = studentExamAnswerRepository.findAllByStudentId(studentIdNotFound);
  65.  
  66. //Then
  67. assertTrue(result.isEmpty());
  68. }
  69.  
  70. private List<StudentExamAnswerEntity> setup_StudentExamAnswerEntityList() {
  71. List<StudentExamAnswerEntity> studentExamAnswerEntities = mockData.getFileAsObjectList("studentExamAnswerEntityList.json", new TypeToken<List<StudentExamAnswerEntity>>() {
  72. });
  73. studentExamAnswerEntities.forEach(entity -> entityManager.persist(entity));
  74. return studentExamAnswerEntities;
  75. }
  76. }
RAW Paste Data