-
Notifications
You must be signed in to change notification settings - Fork 7
/
ServiceRoutesTest.kt
137 lines (124 loc) · 4.52 KB
/
ServiceRoutesTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package blue.mild.covid.vaxx.routes
import blue.mild.covid.vaxx.dao.model.InsuranceCompany
import blue.mild.covid.vaxx.dao.model.UserRole
import blue.mild.covid.vaxx.dao.model.VaccinationBodyPart
import blue.mild.covid.vaxx.dao.repository.PatientRepository
import blue.mild.covid.vaxx.dao.repository.VaccinationRepository
import blue.mild.covid.vaxx.dto.request.IsinJobDtoIn
import blue.mild.covid.vaxx.dto.request.UserRegistrationDtoIn
import blue.mild.covid.vaxx.dto.response.ApplicationInformationDtoOut
import blue.mild.covid.vaxx.dto.response.IsinJobDtoOut
import blue.mild.covid.vaxx.dto.response.SystemStatisticsDtoOut
import blue.mild.covid.vaxx.service.UserService
import blue.mild.covid.vaxx.utils.ServerTestBase
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.server.testing.handleRequest
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.kodein.di.DI
import org.kodein.di.bind
import org.kodein.di.instance
import org.kodein.di.singleton
import java.time.Instant
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import kotlin.test.assertEquals
class ServiceRoutesTest : ServerTestBase() {
private val versionDto = ApplicationInformationDtoOut("test")
// an example of overriding the DI container
override fun overrideDIContainer() = DI {
bind<ApplicationInformationDtoOut>() with singleton { versionDto }
}
@Test
fun `status should respond with ok`() = withTestApplication {
handleRequest(HttpMethod.Get, Routes.status).run {
expectStatus(HttpStatusCode.OK)
}
}
@Test
fun `version should respond with correct version`() = withTestApplication {
handleRequest(HttpMethod.Get, Routes.version).run {
expectStatus(HttpStatusCode.OK)
// expect DTO that was injected directly from this test
assertEquals(versionDto, receive())
}
}
@Test
fun `system analytics should respond with correct data`() = withTestApplication {
val expected = SystemStatisticsDtoOut(0, 0, 0, 0, 0, 0)
handleRequest(HttpMethod.Get, Routes.systemStatistics).run {
expectStatus(HttpStatusCode.OK)
assertEquals(expected, receive())
}
}
@Test
@Suppress("LongMethod")
fun `it should rerun isin job correctly even if patient has no answers`() = withTestApplication {
val patientRepository by closestDI().instance<PatientRepository>()
val vaccinationRepository by closestDI().instance<VaccinationRepository>()
val userService by closestDI().instance<UserService>()
val patientId = runBlocking {
patientRepository.savePatient(
"alice",
"alice",
12345,
"alice",
"1",
"1",
null,
"email",
InsuranceCompany.CPZP,
"indication",
"remoteHost",
mapOf(),
null
)
}
val userId = runBlocking {
userService.registerUser(
UserRegistrationDtoIn(
firstName = "Test",
lastName = "test",
email = "Test",
password = "test",
role = UserRole.ADMIN
)
)
}
runBlocking {
vaccinationRepository.addVaccination(
patientId,
bodyPart = VaccinationBodyPart.BUTTOCK,
vaccinatedOn = Instant.now(),
vaccineSerialNumber = "test",
vaccineExpiration = LocalDate.parse(
"2200-01-01",
DateTimeFormatter.ISO_LOCAL_DATE
),
userPerformingVaccination = userId.id,
doseNumber = 2
)
}
val expected = IsinJobDtoOut(
0, 1, 0, 0,
0, 0, 0,
0, 0, 0
)
val runWhat = IsinJobDtoIn(
exportPatientsInfo = true,
checkVaccinations = true,
exportVaccinationsFirstDose = true,
exportVaccinationsSecondDose = true,
validatePatients = true
)
handleRequest(HttpMethod.Post, Routes.runIsinJob) {
authorize()
jsonBody(runWhat)
}.run {
expectStatus(HttpStatusCode.OK)
val registration = receive<IsinJobDtoOut>()
assertEquals(expected, registration)
}
}
}