Skip to content

Commit 087b77f

Browse files
committed
Trying to pass integration tests
1 parent 8fa436a commit 087b77f

File tree

9 files changed

+139
-141
lines changed

9 files changed

+139
-141
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ ext {
8181
jacksonKotlinVersion = '2.15.4'
8282
dateTimeVersion = '0.6.1'
8383
mapstructVersion = '1.6.3'
84-
ktorVersion = '3.0.3'
84+
ktorVersion = '2.3.0'
8585
coroutinesVersion = '1.10.1'
8686
}
8787

@@ -154,8 +154,8 @@ dependencies {
154154
implementation "org.mapstruct:mapstruct:$mapstructVersion"
155155
kapt("org.springframework.boot:spring-boot-configuration-processor")
156156
kapt "org.mapstruct:mapstruct-processor:$mapstructVersion"
157-
// implementation("io.ktor:ktor-client-core:$ktorVersion")
158-
// implementation "io.ktor:ktor-client-cio:$ktorVersion"
157+
implementation("io.ktor:ktor-client-core:$ktorVersion")
158+
implementation "io.ktor:ktor-client-cio:$ktorVersion"
159159
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
160160
testImplementation 'org.mockito.kotlin:mockito-kotlin:3.2.0'
161161
// testImplementation 'org.mockito:mockito-inline:5.2.0'

src/integrationTest/java/org/radarbase/appserver/auth/NotificationEndpointAuthTest.kt

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class NotificationEndpointAuthTest {
114114
FcmNotifications::class.java
115115
)
116116

117-
Assertions.assertEquals(HttpStatus.UNAUTHORIZED, notificationDtoResponseEntity.getStatusCode())
117+
Assertions.assertEquals(HttpStatus.UNAUTHORIZED, notificationDtoResponseEntity.statusCode)
118118
}
119119

120120
@Test
@@ -134,7 +134,7 @@ class NotificationEndpointAuthTest {
134134
FcmNotifications::class.java
135135
)
136136

137-
Assertions.assertEquals(HttpStatus.UNAUTHORIZED, notificationDtoResponseEntity.getStatusCode())
137+
Assertions.assertEquals(HttpStatus.UNAUTHORIZED, notificationDtoResponseEntity.statusCode)
138138
}
139139

140140
@Test
@@ -163,84 +163,84 @@ class NotificationEndpointAuthTest {
163163
}
164164
}
165165

166-
// @Test
167-
// @Order(1)
168-
// fun createNotificationForUser() {
169-
// val notificationDtoHttpEntity =
170-
// HttpEntity<FcmNotificationDto?>(fcmNotificationDto, AUTH_HEADER)
171-
//
172-
// val notificationDtoResponseEntity: ResponseEntity<FcmNotificationDto?> =
173-
// restTemplate.exchange<FcmNotificationDto?>(
174-
// createURLWithPort(
175-
// port,
176-
// (ProjectEndpointAuthTest.PROJECT_PATH
177-
// + UserEndpointAuthTest.DEFAULT_PROJECT
178-
// + UserEndpointAuthTest.USER_PATH
179-
// + DEFAULT_USER
180-
// + NOTIFICATION_PATH)
181-
// ),
182-
// HttpMethod.POST,
183-
// notificationDtoHttpEntity,
184-
// FcmNotificationDto::class.java
185-
// )
186-
//
187-
// Assertions.assertEquals(HttpStatus.CREATED, notificationDtoResponseEntity.getStatusCode())
188-
// }
189-
190-
// @Test
191-
// @Order(2)
192-
// fun createBatchNotificationsForUser() {
193-
// fcmNotificationDto.title = "new title"
194-
// fcmNotificationDto.fcmMessageId = "xxxyyyy"
195-
// val notificationDtoHttpEntity =
196-
// HttpEntity<FcmNotifications?>(
197-
// FcmNotifications()
198-
// .withNotifications(
199-
// listOf<FcmNotificationDto>(fcmNotificationDto)
200-
// ),
201-
// AUTH_HEADER
202-
// )
203-
//
204-
// val notificationDtoResponseEntity: ResponseEntity<FcmNotifications?> =
205-
// restTemplate.exchange<FcmNotifications?>(
206-
// createURLWithPort(
207-
// port,
208-
// (ProjectEndpointAuthTest.PROJECT_PATH
209-
// + UserEndpointAuthTest.DEFAULT_PROJECT
210-
// + UserEndpointAuthTest.USER_PATH
211-
// + DEFAULT_USER
212-
// + NOTIFICATION_PATH
213-
// + "/batch")
214-
// ),
215-
// HttpMethod.POST,
216-
// notificationDtoHttpEntity,
217-
// FcmNotifications::class.java
218-
// )
219-
//
220-
// Assertions.assertEquals(HttpStatus.OK, notificationDtoResponseEntity.getStatusCode())
221-
// }
222-
223-
// @Test
224-
// fun viewNotificationsForUser() {
225-
// val notificationDtoHttpEntity = HttpEntity<FcmNotifications?>(null, AUTH_HEADER)
226-
//
227-
// val notificationDtoResponseEntity: ResponseEntity<FcmNotifications?> =
228-
// restTemplate.exchange<FcmNotifications?>(
229-
// createURLWithPort(
230-
// port,
231-
// (ProjectEndpointAuthTest.PROJECT_PATH
232-
// + UserEndpointAuthTest.DEFAULT_PROJECT
233-
// + UserEndpointAuthTest.USER_PATH
234-
// + DEFAULT_USER
235-
// + NOTIFICATION_PATH)
236-
// ),
237-
// HttpMethod.GET,
238-
// notificationDtoHttpEntity,
239-
// FcmNotifications::class.java
240-
// )
241-
//
242-
// Assertions.assertEquals(HttpStatus.OK, notificationDtoResponseEntity.getStatusCode())
243-
// }
166+
@Test
167+
@Order(1)
168+
fun createNotificationForUser() {
169+
val notificationDtoHttpEntity =
170+
HttpEntity<FcmNotificationDto?>(fcmNotificationDto, AUTH_HEADER)
171+
172+
val notificationDtoResponseEntity: ResponseEntity<FcmNotificationDto?> =
173+
restTemplate.exchange<FcmNotificationDto?>(
174+
createURLWithPort(
175+
port,
176+
(ProjectEndpointAuthTest.PROJECT_PATH
177+
+ UserEndpointAuthTest.DEFAULT_PROJECT
178+
+ UserEndpointAuthTest.USER_PATH
179+
+ DEFAULT_USER
180+
+ NOTIFICATION_PATH)
181+
),
182+
HttpMethod.POST,
183+
notificationDtoHttpEntity,
184+
FcmNotificationDto::class.java
185+
)
186+
187+
Assertions.assertEquals(HttpStatus.CREATED, notificationDtoResponseEntity.statusCode)
188+
}
189+
190+
@Test
191+
@Order(2)
192+
fun createBatchNotificationsForUser() {
193+
fcmNotificationDto.title = "new title"
194+
fcmNotificationDto.fcmMessageId = "xxxyyyy"
195+
val notificationDtoHttpEntity =
196+
HttpEntity<FcmNotifications?>(
197+
FcmNotifications()
198+
.withNotifications(
199+
listOf<FcmNotificationDto>(fcmNotificationDto)
200+
),
201+
AUTH_HEADER
202+
)
203+
204+
val notificationDtoResponseEntity: ResponseEntity<FcmNotifications?> =
205+
restTemplate.exchange<FcmNotifications?>(
206+
createURLWithPort(
207+
port,
208+
(ProjectEndpointAuthTest.PROJECT_PATH
209+
+ UserEndpointAuthTest.DEFAULT_PROJECT
210+
+ UserEndpointAuthTest.USER_PATH
211+
+ DEFAULT_USER
212+
+ NOTIFICATION_PATH
213+
+ "/batch")
214+
),
215+
HttpMethod.POST,
216+
notificationDtoHttpEntity,
217+
FcmNotifications::class.java
218+
)
219+
220+
Assertions.assertEquals(HttpStatus.OK, notificationDtoResponseEntity.statusCode)
221+
}
222+
223+
@Test
224+
fun viewNotificationsForUser() {
225+
val notificationDtoHttpEntity = HttpEntity<FcmNotifications?>(null, AUTH_HEADER)
226+
227+
val notificationDtoResponseEntity: ResponseEntity<FcmNotifications?> =
228+
restTemplate.exchange<FcmNotifications?>(
229+
createURLWithPort(
230+
port,
231+
(ProjectEndpointAuthTest.PROJECT_PATH
232+
+ UserEndpointAuthTest.DEFAULT_PROJECT
233+
+ UserEndpointAuthTest.USER_PATH
234+
+ DEFAULT_USER
235+
+ NOTIFICATION_PATH)
236+
),
237+
HttpMethod.GET,
238+
notificationDtoHttpEntity,
239+
FcmNotifications::class.java
240+
)
241+
242+
Assertions.assertEquals(HttpStatus.OK, notificationDtoResponseEntity.statusCode)
243+
}
244244

245245
@Test
246246
fun viewNotificationsForProject() {
@@ -259,7 +259,7 @@ class NotificationEndpointAuthTest {
259259
FcmNotifications::class.java
260260
)
261261

262-
Assertions.assertEquals(HttpStatus.OK, notificationDtoResponseEntity.getStatusCode())
262+
Assertions.assertEquals(HttpStatus.OK, notificationDtoResponseEntity.statusCode)
263263
}
264264

265265
@Test
@@ -281,7 +281,7 @@ class NotificationEndpointAuthTest {
281281
FcmNotifications::class.java
282282
)
283283

284-
Assertions.assertEquals(HttpStatus.FORBIDDEN, notificationDtoResponseEntity.getStatusCode())
284+
Assertions.assertEquals(HttpStatus.FORBIDDEN, notificationDtoResponseEntity.statusCode)
285285
}
286286

287287
@Test
@@ -298,7 +298,7 @@ class NotificationEndpointAuthTest {
298298
FcmNotifications::class.java
299299
)
300300

301-
Assertions.assertEquals(HttpStatus.UNAUTHORIZED, notificationDtoResponseEntity.getStatusCode())
301+
Assertions.assertEquals(HttpStatus.UNAUTHORIZED, notificationDtoResponseEntity.statusCode)
302302
}
303303

304304
companion object {

src/main/java/org/radarbase/appserver/controller/ProtocolEndpoint.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ProtocolEndpoint (private val protocolGenerator: ProtocolGenerator) {
6161
fun getProtocolUsingProjectIdAndSubjectId(
6262
@PathVariable @Valid projectId: String, @PathVariable @Valid subjectId: String
6363
): Protocol {
64-
return this.protocolGenerator.getProtocolForSubject(subjectId)
64+
return this.protocolGenerator.getProtocolForSubject(subjectId) ?: Protocol()
6565
}
6666

6767
@GetMapping(

src/main/java/org/radarbase/appserver/service/GithubClient.kt

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121

2222
package org.radarbase.appserver.service
2323

24-
//import io.ktor.client.*
25-
//import io.ktor.client.call.*
26-
//import io.ktor.client.engine.cio.*
27-
//import io.ktor.client.plugins.defaultRequest
28-
//import io.ktor.client.request.*
29-
//import io.ktor.http.*
24+
import io.ktor.client.*
25+
import io.ktor.client.call.*
26+
import io.ktor.client.engine.cio.*
27+
import io.ktor.client.plugins.defaultRequest
28+
import io.ktor.client.request.*
29+
import io.ktor.http.*
3030
import kotlinx.coroutines.runBlocking
3131
import org.slf4j.LoggerFactory
3232
import org.springframework.beans.factory.annotation.Value
@@ -68,16 +68,16 @@ class GithubClient(
6868
* This client sets default request headers, specifies request timeout, and
6969
* enables automatic redirection following.
7070
*/
71-
// @Transient
72-
// private val client = HttpClient(CIO) {
73-
// defaultRequest {
74-
// header(HttpHeaders.Accept, GITHUB_API_ACCEPT_HEADER)
75-
// }
76-
// engine {
77-
// requestTimeout = httpTimeout * 1000L
78-
// }
79-
// followRedirects = true
80-
// }
71+
@Transient
72+
private val client = HttpClient(CIO) {
73+
defaultRequest {
74+
header(HttpHeaders.Accept, GITHUB_API_ACCEPT_HEADER)
75+
}
76+
engine {
77+
requestTimeout = httpTimeout * 1000L
78+
}
79+
followRedirects = true
80+
}
8181

8282
/**
8383
* Retrieves the content from a specified GitHub URL. This method attempts to make an authenticated request
@@ -88,32 +88,32 @@ class GithubClient(
8888
* @return The content retrieved from the specified GitHub URL as a String.
8989
* @throws ResponseStatusException If the response indicates an error or the content size is too large.
9090
*/
91-
// fun getGithubContent(url: String, authenticated: Boolean = true): String = runBlocking {
92-
// val request = client.prepareGet(getValidGithubUri(url)) {
93-
// if (authenticated && authorizationHeader.isNotEmpty()) {
94-
// header(HttpHeaders.Authorization, authorizationHeader)
95-
// }
96-
// }
97-
// val response = request.execute()
98-
// if (response.status.value in 200..299) {
99-
// val contentLengthHeader = response.headers[HttpHeaders.ContentLength]?.toIntOrNull()
100-
// contentLengthHeader?.let { checkContentLength(it) }
101-
//
102-
// val contentStream: ByteArray = response.body<ByteArray>()
103-
// checkContentLength(contentStream.size)
104-
// return@runBlocking String(contentStream)
105-
// } else if (response.status.value == 401 && authenticated) {
106-
// logger.warn("Unauthorized access to Github content from URL {}, retrying..", url)
107-
// return@runBlocking getGithubContent(url, false)
108-
// } else {
109-
// logger.error("Error getting Github content from URL {} : {}", url, response)
110-
// throw ResponseStatusException(
111-
// HttpStatus.valueOf(response.status.value), "Github content could not be retrieved"
112-
// )
113-
// }
114-
// }
91+
fun getGithubContent(url: String, authenticated: Boolean = true): String = runBlocking {
92+
val request = client.prepareGet(getValidGithubUri(url)) {
93+
if (authenticated && authorizationHeader.isNotEmpty()) {
94+
header(HttpHeaders.Authorization, authorizationHeader)
95+
}
96+
}
97+
val response = request.execute()
98+
if (response.status.value in 200..299) {
99+
val contentLengthHeader = response.headers[HttpHeaders.ContentLength]?.toIntOrNull()
100+
contentLengthHeader?.let { checkContentLength(it) }
101+
102+
val contentStream: ByteArray = response.body<ByteArray>()
103+
checkContentLength(contentStream.size)
104+
return@runBlocking String(contentStream)
105+
} else if (response.status.value == 401 && authenticated) {
106+
logger.warn("Unauthorized access to Github content from URL {}, retrying..", url)
107+
return@runBlocking getGithubContent(url, false)
108+
} else {
109+
logger.error("Error getting Github content from URL {} : {}", url, response)
110+
throw ResponseStatusException(
111+
HttpStatus.valueOf(response.status.value), "Github content could not be retrieved"
112+
)
113+
}
114+
}
115115

116-
fun getGithubContent(url: String): String = getGithubContent(url)
116+
fun getGithubContent(url: String): String = getGithubContent(url, true)
117117

118118
/**
119119
* Validates the given content length against a predefined maximum content length.

src/main/java/org/radarbase/appserver/service/QuestionnaireScheduleService.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,10 @@ class QuestionnaireScheduleService(
119119
@Transactional
120120
fun generateScheduleForUser(user: User): Schedule {
121121
val subjectId: String? = user.subjectId
122-
123122
checkNotNull(subjectId) { "Subject ID cannot be null in questionnaire scheduler service." }
124123

125124
val protocol: Protocol? = protocolGenerator.getProtocolForSubject(subjectId)
126125
val newSchedule: Schedule
127-
128126
if (protocol == null) {
129127
newSchedule = Schedule()
130128
} else {
@@ -158,10 +156,10 @@ class QuestionnaireScheduleService(
158156
): Schedule {
159157

160158
val user: User = subjectAndProjectExistsElseThrow(subjectId, projectId)
161-
val protocol: Protocol = protocolGenerator.getProtocolForSubject(subjectId)
159+
val protocol: Protocol? = protocolGenerator.getProtocolForSubject(subjectId)
162160

163161
checkInvalidDetails<NotFoundException>(
164-
{ !protocol.hasAssessment(assessment.name) },
162+
{ protocol == null || !protocol.hasAssessment(assessment.name) },
165163
{ "Assessment not found in protocol. Add assessment to protocol first" }
166164
)
167165

src/main/java/org/radarbase/appserver/service/questionnaire/protocol/DefaultProtocolGenerator.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ class DefaultProtocolGenerator(
118118
* @throws IOException If an error occurs while retrieving protocols or handling cached values.
119119
*/
120120
@Throws(IOException::class)
121-
private fun forceGetProtocolForSubject(subjectId: String): Protocol {
121+
private fun forceGetProtocolForSubject(subjectId: String): Protocol? {
122122
try {
123-
return cachedProtocolMap.get(true)[subjectId]!!
123+
return cachedProtocolMap.get(true)[subjectId]
124124
} catch (ex: IOException) {
125125
logger.warn("Cannot retrieve Protocols, using cached values if available.", ex)
126126
return cachedProtocolMap.getCachedMap()[subjectId]!!
@@ -142,7 +142,7 @@ class DefaultProtocolGenerator(
142142
* @return The protocol corresponding to the given subject.
143143
* @throws IOException If an error occurs during protocol retrieval and no cached value is available.
144144
*/
145-
override fun getProtocolForSubject(subjectId: String): Protocol {
145+
override fun getProtocolForSubject(subjectId: String): Protocol? {
146146
try {
147147
val protocol = cachedProtocolMap[subjectId]
148148
if (protocol == null) {

src/main/java/org/radarbase/appserver/service/questionnaire/protocol/ProtocolGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ interface ProtocolGenerator {
5353
* @param subjectId The identifier of the subject for which the protocol is to be retrieved.
5454
* @return The protocol corresponding to the specified subject.
5555
*/
56-
fun getProtocolForSubject(subjectId: String): Protocol
56+
fun getProtocolForSubject(subjectId: String): Protocol?
5757
}

0 commit comments

Comments
 (0)