Added repository tests and some controller tests.

This commit is contained in:
neviyn 2018-10-03 14:38:01 +01:00
parent d5ded7c12a
commit 76e19e9253
3 changed files with 269 additions and 0 deletions

View File

@ -0,0 +1,85 @@
package uk.co.neviyn.observationdatabase
import junit.framework.TestCase.*
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.junit.MockitoJUnitRunner
import java.util.*
@RunWith(MockitoJUnitRunner::class)
class ControllerTest{
@InjectMocks
lateinit var controller: Controller
@Mock
lateinit var siteRepository: SiteRepository
@Mock
lateinit var tutorRepository: TutorRepository
@Mock
lateinit var observationRepository: ObservationRepository
@Test
fun testGetSites(){
doReturn(listOf(Site(1, "Site A"), Site(2, "Site B"))).`when`(siteRepository).findAll()
val result = controller.getAllSites()
assertNotNull(result)
assertEquals(2, result.size)
assertTrue(result.contains(NameValue("Site A", 1)))
assertTrue(result.contains(NameValue("Site B", 2)))
}
@Test
fun testAddSite(){
val newSite = NewSite("Test")
doReturn(Site(1, "Test")).`when`(siteRepository).save(ArgumentMatchers.any())
val result = controller.addSite(newSite)
verify(siteRepository, times(1)).save(ArgumentMatchers.any())
assertNotNull(result)
assertEquals("Test", result.text)
}
@Test
fun testGetTutorsForSite(){
val site = Site(1, "Site A")
val tutors = listOf(Tutor(1, "Foo", site), Tutor(2, "Bar", site))
site.tutors.addAll(tutors)
doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
val result = controller.getTutorsForSite(1)
assertNotNull(result)
assertEquals(2, result!!.size)
assertTrue(result.contains(NameValue("Foo", 1)))
assertTrue(result.contains(NameValue("Bar", 2)))
}
@Test
fun testGetTutorsForNullSite(){
doReturn(Optional.ofNullable(null)).`when`(siteRepository).findById(1)
val result: List<NameValue>? = controller.getTutorsForSite(1)
assertNull(result)
}
@Test
fun testGetAllTutors(){
doReturn(listOf(Tutor(1, "Foo", Site(1, "X")), Tutor(2, "Bar", Site(2, "A")))).`when`(tutorRepository).findAll()
val result = controller.getAllTutors()
assertNotNull(result)
assertTrue(result.contains(NameValue("Foo", 1)))
assertTrue(result.contains(NameValue("Bar", 2)))
}
@Test
fun testAddTutor(){
val site = Site(1, "X")
doReturn(Optional.of(site)).`when`(siteRepository).findById(1)
doReturn(Tutor(1, "Foo", site)).`when`(tutorRepository).save(ArgumentMatchers.any())
val result = controller.addTutor(NewTutor("Foo", 1))
verify(tutorRepository, times(1)).save(ArgumentMatchers.any())
assertEquals(NameValue("Foo", 1), result)
}
}

View File

@ -0,0 +1,182 @@
package uk.co.neviyn.observationdatabase
import junit.framework.TestCase.*
import org.joda.time.LocalDate
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager
import org.springframework.beans.factory.annotation.Autowired
@RunWith(SpringRunner::class)
@DataJpaTest
class RepositoryTest{
@Autowired
lateinit var entityManager: TestEntityManager
@Autowired
lateinit var observationRepository: ObservationRepository
@Test
fun testFindBySiteAndDateBetween_EmptyRepository(){
val result = observationRepository.findBySiteAndDateBetween(Site(1, "x"), LocalDate.now(), LocalDate.now())
assertTrue(result.isEmpty())
}
@Test
fun testFindBySiteAndWhomAndDateBetween_EmptyRepository(){
val result = observationRepository.findBySiteAndWhomAndDateBetween(Site(1, "x"), "none", LocalDate.now(), LocalDate.now())
assertTrue(result.isEmpty())
}
@Test
fun testFindByTutorsAndDateBetween_EmptyRepository(){
val result = observationRepository.findByTutorsAndDateBetween(Tutor(1, "x", Site(1, "x")), LocalDate.now(), LocalDate.now())
assertTrue(result.isEmpty())
}
@Test
fun testFindByTutorsAndWhomAndDateBetween_EmptyRepository(){
val result = observationRepository.findByTutorsAndWhomAndDateBetween(Tutor(1, "x", Site(1, "x")), "none", LocalDate.now(), LocalDate.now())
assertTrue(result.isEmpty())
}
@Test
fun testFindBySiteAndDateBetween(){
val correctSite = entityManager.persist(Site(name = "Correct"))
val incorrectSite = entityManager.persist(Site(name="Incorrect"))
val tutor = entityManager.persist(Tutor(name = "X", site = correctSite))
val tutor2 = entityManager.persist(Tutor(name = "N", site = incorrectSite))
val tooEarly = Observation(site = correctSite, date = LocalDate.now().minusDays(5),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor))
val tooLate = Observation(site = correctSite, date = LocalDate.now().plusDays(5),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor))
val wrongSite = Observation(site = incorrectSite, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor2))
val justRight = Observation(site = correctSite, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor))
entityManager.persist(tooEarly)
entityManager.persist(tooLate)
entityManager.persist(wrongSite)
entityManager.persist(justRight)
val result = observationRepository.findBySiteAndDateBetween(site = correctSite, startDate = LocalDate.now().minusDays(1), endDate = LocalDate.now().plusDays(1))
assertEquals(1, result.size)
assertEquals(justRight, result.first())
}
@Test
fun testFindBySiteAndWhomAndDateBetween(){
val correctSite = entityManager.persist(Site(name = "Correct"))
val incorrectSite = entityManager.persist(Site(name="Incorrect"))
val tutor = entityManager.persist(Tutor(name = "X", site = correctSite))
val tutor2 = entityManager.persist(Tutor(name = "N", site = incorrectSite))
val tooEarly = Observation(site = correctSite, date = LocalDate.now().minusDays(5),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor))
val tooLate = Observation(site = correctSite, date = LocalDate.now().plusDays(5),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor))
val wrongSite = Observation(site = incorrectSite, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor2))
val justRight = Observation(site = correctSite, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor))
val wrongWhom = Observation(site = correctSite, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G2", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(tutor))
entityManager.persist(tooEarly)
entityManager.persist(tooLate)
entityManager.persist(wrongSite)
entityManager.persist(justRight)
entityManager.persist(wrongWhom)
val result = observationRepository.findBySiteAndWhomAndDateBetween(site = correctSite, startDate = LocalDate.now().minusDays(1), endDate = LocalDate.now().plusDays(1), whom = "G1")
assertEquals(1, result.size)
assertEquals(justRight, result.first())
}
@Test
fun testFindByTutorsAndDateBetween(){
val site = entityManager.persist(Site(name = "Correct"))
val correctTutor = entityManager.persist(Tutor(name = "X", site = site))
val incorrectTutor = entityManager.persist(Tutor(name = "N", site = site))
val tooEarly = Observation(site = site, date = LocalDate.now().minusDays(5),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor))
val tooLate = Observation(site = site, date = LocalDate.now().plusDays(5),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor))
val wrongTutor = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(incorrectTutor))
val justRight = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor))
val justRightMultipleTutors = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor, incorrectTutor))
entityManager.persist(tooEarly)
entityManager.persist(tooLate)
entityManager.persist(wrongTutor)
entityManager.persist(justRight)
entityManager.persist(justRightMultipleTutors)
correctTutor.observations.addAll(listOf(tooEarly, tooLate, justRight, justRightMultipleTutors))
entityManager.persist(correctTutor)
incorrectTutor.observations.addAll(listOf(wrongTutor, justRightMultipleTutors))
entityManager.persist(incorrectTutor)
val result = observationRepository.findByTutorsAndDateBetween(tutor = correctTutor, startDate = LocalDate.now().minusDays(1), endDate = LocalDate.now().plusDays(1))
assertEquals(2, result.size)
assertTrue(result.contains(justRight))
assertTrue(result.contains(justRightMultipleTutors))
assertFalse(result.contains(wrongTutor))
}
@Test
fun testFindByTutorsAndWhomAndDateBetween(){
val site = entityManager.persist(Site(name = "Correct"))
val correctTutor = entityManager.persist(Tutor(name = "X", site = site))
val incorrectTutor = entityManager.persist(Tutor(name = "N", site = site))
val tooEarly = Observation(site = site, date = LocalDate.now().minusDays(5),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor))
val tooLate = Observation(site = site, date = LocalDate.now().plusDays(5),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor))
val wrongTutor = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(incorrectTutor))
val justRight = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor))
val wrongGroup = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G2", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor))
val justRightMultipleTutors = Observation(site = site, date = LocalDate.now(),
type = TrainingType.INITIAL, observed = "1", whom = "G1", monitoring = 5.0,
control = 4.0, conservatism = 3.0, teamwork = 2.0, knowledge = 1.0, entries = listOf(), tutors = setOf(correctTutor, incorrectTutor))
entityManager.persist(tooEarly)
entityManager.persist(tooLate)
entityManager.persist(wrongTutor)
entityManager.persist(justRight)
entityManager.persist(wrongGroup)
entityManager.persist(justRightMultipleTutors)
correctTutor.observations.addAll(listOf(tooEarly, tooLate, justRight, justRightMultipleTutors, wrongGroup))
entityManager.persist(correctTutor)
incorrectTutor.observations.addAll(listOf(wrongTutor, justRightMultipleTutors))
entityManager.persist(incorrectTutor)
val result = observationRepository.findByTutorsAndWhomAndDateBetween(tutor = correctTutor, startDate = LocalDate.now().minusDays(1), endDate = LocalDate.now().plusDays(1), whom = "G1")
assertEquals(2, result.size)
assertTrue(result.contains(justRight))
assertTrue(result.contains(justRightMultipleTutors))
assertFalse(result.contains(wrongTutor))
assertFalse(result.contains(wrongGroup))
}
}

View File

@ -0,0 +1,2 @@
spring.datasource.url=jdbc:h2:mem:db
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true