Code cleanups, mostly runBlocking inlining in tests
This commit is contained in:
parent
8681fa3465
commit
4a78a894a4
10 changed files with 745 additions and 822 deletions
|
@ -10,7 +10,7 @@ import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories
|
||||||
@ConfigurationPropertiesScan
|
@ConfigurationPropertiesScan
|
||||||
@EntityScan("interview.chataccess.dbmodel")
|
@EntityScan("interview.chataccess.dbmodel")
|
||||||
@EnableR2dbcRepositories("interview.chataccess.dbmodel")
|
@EnableR2dbcRepositories("interview.chataccess.dbmodel")
|
||||||
open class ChatAccessApplication
|
class ChatAccessApplication
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
runApplication<ChatAccessApplication>(*args)
|
runApplication<ChatAccessApplication>(*args)
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package interview.chataccess.controller
|
package interview.chataccess.controller
|
||||||
|
|
||||||
|
|
||||||
import interview.chataccess.controller.UtilsRestController.getPrincipal
|
import interview.chataccess.controller.UtilsRestController.getPrincipal
|
||||||
import interview.chataccess.controller.exceptions.ChatSessionNotFoundException
|
import interview.chataccess.controller.exceptions.ChatSessionNotFoundException
|
||||||
import interview.chataccess.controller.exceptions.GenericRestException
|
import interview.chataccess.controller.exceptions.GenericRestException
|
||||||
|
@ -10,6 +9,7 @@ import interview.chataccess.dto.*
|
||||||
import interview.chataccess.service.ChatService
|
import interview.chataccess.service.ChatService
|
||||||
import interview.chataccess.service.MessageService
|
import interview.chataccess.service.MessageService
|
||||||
import interview.chataccess.utils.logger
|
import interview.chataccess.utils.logger
|
||||||
|
import kotlinx.coroutines.CoroutineStart
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
@ -59,7 +59,8 @@ class MessageController(
|
||||||
suspend fun sendMessage(@RequestBody input: ChatMessageSendDTO): Flow<ChatMessagePartialReplyDTO> = coroutineScope {
|
suspend fun sendMessage(@RequestBody input: ChatMessageSendDTO): Flow<ChatMessagePartialReplyDTO> = coroutineScope {
|
||||||
val principal = getPrincipal()
|
val principal = getPrincipal()
|
||||||
|
|
||||||
val sId = async {
|
// Those two coroutines do not need to be LAZY, but it doesn't hurt.
|
||||||
|
val sId = async(start = CoroutineStart.LAZY) {
|
||||||
chatService
|
chatService
|
||||||
.findChatByIdForUsername(input.chatSessionId, principal)
|
.findChatByIdForUsername(input.chatSessionId, principal)
|
||||||
?: throw ChatSessionNotFoundException(
|
?: throw ChatSessionNotFoundException(
|
||||||
|
@ -74,7 +75,7 @@ class MessageController(
|
||||||
// both of the queries asynchronously for all the other cases.
|
// both of the queries asynchronously for all the other cases.
|
||||||
// We take advantage of the coroutines property that error on one of them will cancel
|
// We take advantage of the coroutines property that error on one of them will cancel
|
||||||
// the parent context (when not using supervisor).
|
// the parent context (when not using supervisor).
|
||||||
val pastMessages = async {
|
val pastMessages = async(start = CoroutineStart.LAZY) {
|
||||||
messageService
|
messageService
|
||||||
.findMessagesInChatSessionForUsername(input.chatSessionId, principal)
|
.findMessagesInChatSessionForUsername(input.chatSessionId, principal)
|
||||||
.map { message ->
|
.map { message ->
|
||||||
|
|
|
@ -15,12 +15,12 @@ object UtilsRestController {
|
||||||
.awaitSingle()
|
.awaitSingle()
|
||||||
.authentication.principal
|
.authentication.principal
|
||||||
.let { authPrincipal ->
|
.let { authPrincipal ->
|
||||||
when {
|
when (authPrincipal) {
|
||||||
authPrincipal is User -> {
|
is User -> {
|
||||||
authPrincipal.username
|
authPrincipal.username
|
||||||
}
|
}
|
||||||
|
|
||||||
authPrincipal is Jwt -> {
|
is Jwt -> {
|
||||||
authPrincipal
|
authPrincipal
|
||||||
.claims["email"]
|
.claims["email"]
|
||||||
.toString()
|
.toString()
|
||||||
|
|
|
@ -13,23 +13,18 @@ open class BaseTests {
|
||||||
|
|
||||||
private var internalCounter: Int = 0
|
private var internalCounter: Int = 0
|
||||||
|
|
||||||
fun genChatSession(): ChatSession {
|
fun genChatSession() =
|
||||||
internalCounter++
|
ChatSession.from(
|
||||||
return ChatSession.from(
|
name = (++internalCounter).toString(),
|
||||||
name = internalCounter.toString(),
|
|
||||||
model = TESTS_DEFAULT_MODEL,
|
model = TESTS_DEFAULT_MODEL,
|
||||||
username = TESTS_DEFAULT_USERNAME
|
username = TESTS_DEFAULT_USERNAME
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
fun genChatMessage(chatSession: ChatSession): ChatMessage {
|
fun genChatMessage(chatSession: ChatSession) =
|
||||||
internalCounter++
|
ChatMessage.from(
|
||||||
return ChatMessage.from(
|
|
||||||
chatSession,
|
chatSession,
|
||||||
ChatRoles.USER,
|
ChatRoles.USER,
|
||||||
"Test message ${internalCounter}",
|
"Test message ${(++internalCounter)}",
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
|
@ -34,6 +34,7 @@ class ApplicationStartupEventTests : BaseOllamaClientTest() {
|
||||||
.setBody("{\"status\":\"success\"}")
|
.setBody("{\"status\":\"success\"}")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
ApplicationStartupEvent(appConfig, mockWebClient!!)
|
ApplicationStartupEvent(appConfig, mockWebClient!!)
|
||||||
.loadTestData()
|
.loadTestData()
|
||||||
}
|
}
|
||||||
|
@ -57,7 +58,6 @@ class ApplicationStartupEventTests : BaseOllamaClientTest() {
|
||||||
|
|
||||||
ApplicationStartupEvent(appConfig, mockWebClient!!)
|
ApplicationStartupEvent(appConfig, mockWebClient!!)
|
||||||
.loadTestData()
|
.loadTestData()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,8 +50,7 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: fetch models list")
|
@DisplayName("ChatControllerTest: fetch models list")
|
||||||
fun getModelsList() {
|
fun getModelsList(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
log.debug("Models in the test scenario: {}", appConfig.availableModels)
|
log.debug("Models in the test scenario: {}", appConfig.availableModels)
|
||||||
|
|
||||||
getWebTestClient()
|
getWebTestClient()
|
||||||
|
@ -70,12 +69,10 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
}.toTypedArray())
|
}.toTypedArray())
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: fetch chat sessions list")
|
@DisplayName("ChatControllerTest: fetch chat sessions list")
|
||||||
fun getSessions() {
|
fun getSessions(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
(1..5).map { genChatSession() }
|
(1..5).map { genChatSession() }
|
||||||
.toTypedArray()
|
.toTypedArray()
|
||||||
.also { chatSessions ->
|
.also { chatSessions ->
|
||||||
|
@ -106,12 +103,10 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
.contains(*chatSessions)
|
.contains(*chatSessions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: fetch chat session details")
|
@DisplayName("ChatControllerTest: fetch chat session details")
|
||||||
fun getSessionDetails() {
|
fun getSessionDetails(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
genChatSession()
|
genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
@ -138,12 +133,10 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: fetch non-existent chat session details")
|
@DisplayName("ChatControllerTest: fetch non-existent chat session details")
|
||||||
fun getSessionDetailsNonExist() {
|
fun getSessionDetailsNonExist(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
genChatSession()
|
genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
// We do not store it, i.e., not mock storing, just simulating,
|
// We do not store it, i.e., not mock storing, just simulating,
|
||||||
|
@ -169,13 +162,10 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: delete non-existent chat session")
|
@DisplayName("ChatControllerTest: delete non-existent chat session")
|
||||||
fun deleteChatSessionNotExist() {
|
fun deleteChatSessionNotExist(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
genChatSession()
|
genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
@ -199,12 +189,10 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: delete chat session")
|
@DisplayName("ChatControllerTest: delete chat session")
|
||||||
fun deleteChatSession() {
|
fun deleteChatSession(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
genChatSession()
|
genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
@ -226,12 +214,10 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
.isEqualTo("Deleted")
|
.isEqualTo("Deleted")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: create new chat session with wrong model")
|
@DisplayName("ChatControllerTest: create new chat session with wrong model")
|
||||||
fun createChatSessionWrongModel() {
|
fun createChatSessionWrongModel(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
getWebTestClient()
|
getWebTestClient()
|
||||||
.put()
|
.put()
|
||||||
.uri("/chat/session")
|
.uri("/chat/session")
|
||||||
|
@ -249,12 +235,10 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
RestControllerAdvice.messageForUnsupportedMode(TESTS_DEFAULT_WRONG_MODEL)
|
RestControllerAdvice.messageForUnsupportedMode(TESTS_DEFAULT_WRONG_MODEL)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: create new chat session with empty name")
|
@DisplayName("ChatControllerTest: create new chat session with empty name")
|
||||||
fun createChatSessionEmptyName() {
|
fun createChatSessionEmptyName(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
getWebTestClient()
|
getWebTestClient()
|
||||||
.put()
|
.put()
|
||||||
.uri("/chat/session")
|
.uri("/chat/session")
|
||||||
|
@ -272,12 +256,10 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
RestControllerAdvice.messageForBlankSessionName()
|
RestControllerAdvice.messageForBlankSessionName()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatControllerTest: create new chat session with wrong model")
|
@DisplayName("ChatControllerTest: create new chat session with wrong model")
|
||||||
fun createChatSession() {
|
fun createChatSession(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
// This one looked hideous when written functionally, so semi-imperative it goes.
|
// This one looked hideous when written functionally, so semi-imperative it goes.
|
||||||
|
|
||||||
val chatSession = genChatSession()
|
val chatSession = genChatSession()
|
||||||
|
@ -337,6 +319,4 @@ class ChatControllerTests : BaseControllerTests() {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,8 +47,7 @@ class MessageControllerTests : BaseOllamaClientTest() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageControllerTest: fetch previous non-existent messages")
|
@DisplayName("MessageControllerTest: fetch previous non-existent messages")
|
||||||
fun getPreviousMessagesNotExist() {
|
fun getPreviousMessagesNotExist(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSessionId = Random.nextInt(0, Int.MAX_VALUE)
|
val chatSessionId = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
|
||||||
given(
|
given(
|
||||||
|
@ -67,12 +66,10 @@ class MessageControllerTests : BaseOllamaClientTest() {
|
||||||
RestControllerAdvice.messageForSessionNotFound(chatSessionId)
|
RestControllerAdvice.messageForSessionNotFound(chatSessionId)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageControllerTest: fetch previous non-existent messages")
|
@DisplayName("MessageControllerTest: fetch previous non-existent messages")
|
||||||
fun getPrevious() {
|
fun getPrevious(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSession = genChatSession()
|
val chatSession = genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
@ -107,12 +104,10 @@ class MessageControllerTests : BaseOllamaClientTest() {
|
||||||
.hasSize(chatMessages.size)
|
.hasSize(chatMessages.size)
|
||||||
.contains(*expectedReply)
|
.contains(*expectedReply)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageControllerTest: send message to non-existent chat session")
|
@DisplayName("MessageControllerTest: send message to non-existent chat session")
|
||||||
fun sendMessageChatNotExist() {
|
fun sendMessageChatNotExist(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSessionId = Random.nextInt(0, Int.MAX_VALUE)
|
val chatSessionId = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
|
||||||
given(
|
given(
|
||||||
|
@ -137,12 +132,10 @@ class MessageControllerTests : BaseOllamaClientTest() {
|
||||||
RestControllerAdvice.messageForSessionNotFound(chatSessionId)
|
RestControllerAdvice.messageForSessionNotFound(chatSessionId)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageControllerTest: send chat message (non-streaming reply)")
|
@DisplayName("MessageControllerTest: send chat message (non-streaming reply)")
|
||||||
fun sendChatMessage() {
|
fun sendChatMessage(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSession = genChatSession()
|
val chatSession = genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
@ -221,12 +214,10 @@ class MessageControllerTests : BaseOllamaClientTest() {
|
||||||
.hasSize(1)
|
.hasSize(1)
|
||||||
.contains(*expectedReplies)
|
.contains(*expectedReplies)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageControllerTest: send chat message (streaming reply)")
|
@DisplayName("MessageControllerTest: send chat message (streaming reply)")
|
||||||
fun sendChatMessageStreaming() {
|
fun sendChatMessageStreaming(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSession = genChatSession()
|
val chatSession = genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
@ -313,12 +304,10 @@ class MessageControllerTests : BaseOllamaClientTest() {
|
||||||
.hasSize(expectedReplies.size)
|
.hasSize(expectedReplies.size)
|
||||||
.contains(*expectedReplies)
|
.contains(*expectedReplies)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageControllerTest: Ollama API returns error")
|
@DisplayName("MessageControllerTest: Ollama API returns error")
|
||||||
fun sendChatMessageButGot4xx() {
|
fun sendChatMessageButGot4xx(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSession = genChatSession()
|
val chatSession = genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
@ -360,12 +349,10 @@ class MessageControllerTests : BaseOllamaClientTest() {
|
||||||
it.startsWith(RestControllerAdvice.messagePrefixForGenericRestException())
|
it.startsWith(RestControllerAdvice.messagePrefixForGenericRestException())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageControllerTest: send chat message (streaming reply with error in middle)")
|
@DisplayName("MessageControllerTest: send chat message (streaming reply with error in middle)")
|
||||||
fun sendChatMessageStreamingError() {
|
fun sendChatMessageStreamingError(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSession = genChatSession()
|
val chatSession = genChatSession()
|
||||||
.apply {
|
.apply {
|
||||||
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
this.id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
|
@ -418,4 +405,4 @@ class MessageControllerTests : BaseOllamaClientTest() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -10,10 +10,7 @@ import kotlinx.coroutines.runBlocking
|
||||||
import org.junit.jupiter.api.DisplayName
|
import org.junit.jupiter.api.DisplayName
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.assertThrows
|
import org.junit.jupiter.api.assertThrows
|
||||||
import org.junit.jupiter.api.extension.ExtendWith
|
|
||||||
import org.mockito.Mock
|
import org.mockito.Mock
|
||||||
import org.mockito.Mockito.`when`
|
|
||||||
import org.mockito.junit.jupiter.MockitoExtension
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
|
||||||
import org.springframework.context.annotation.Import
|
import org.springframework.context.annotation.Import
|
||||||
|
@ -27,7 +24,6 @@ import org.springframework.security.web.server.WebFilterChainProxy
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient
|
import org.springframework.test.web.reactive.server.WebTestClient
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
import reactor.core.publisher.Mono
|
|
||||||
|
|
||||||
@Import(TestApplicationConfig::class, SecurityConfig::class)
|
@Import(TestApplicationConfig::class, SecurityConfig::class)
|
||||||
@WebFluxTest(RestSecurityTests.Http200Controller::class)
|
@WebFluxTest(RestSecurityTests.Http200Controller::class)
|
||||||
|
@ -41,14 +37,8 @@ class RestSecurityTests : BaseControllerTests() {
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
class Http200Controller {
|
class Http200Controller {
|
||||||
|
|
||||||
val log = logger()
|
val log = logger()
|
||||||
|
|
||||||
@GetMapping("/")
|
|
||||||
suspend fun getOK(): Mono<String> {
|
|
||||||
return Mono.just("OK")
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/user")
|
@GetMapping("/user")
|
||||||
suspend fun getUsername(): String = getPrincipal().also { log.debug("Principal: ${it} ") }
|
suspend fun getUsername(): String = getPrincipal().also { log.debug("Principal: ${it} ") }
|
||||||
}
|
}
|
||||||
|
@ -71,8 +61,7 @@ class RestSecurityTests : BaseControllerTests() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Authentication required")
|
@DisplayName("Authentication required")
|
||||||
fun getModelsList() {
|
fun getModelsList(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
getWebTestClient()
|
getWebTestClient()
|
||||||
.get()
|
.get()
|
||||||
.uri("/")
|
.uri("/")
|
||||||
|
@ -81,14 +70,12 @@ class RestSecurityTests : BaseControllerTests() {
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.is4xxClientError
|
.is4xxClientError
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("getPrincipal works for mock users")
|
@DisplayName("getPrincipal works for mock users")
|
||||||
@WithMockUser(username = TESTS_DEFAULT_USERNAME)
|
@WithMockUser(username = TESTS_DEFAULT_USERNAME)
|
||||||
fun getPrincipalForMockUser() {
|
fun getPrincipalForMockUser(): Unit = runBlocking {
|
||||||
// This one is just to make sure other tests with @WithMockUser do behave.
|
// This one is just to make sure other tests with @WithMockUser do behave.
|
||||||
runBlocking {
|
|
||||||
getWebTestClient()
|
getWebTestClient()
|
||||||
.get()
|
.get()
|
||||||
.uri("/user")
|
.uri("/user")
|
||||||
|
@ -97,12 +84,10 @@ class RestSecurityTests : BaseControllerTests() {
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.is2xxSuccessful
|
.is2xxSuccessful
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("getPrincipal works for mocked OAuth2")
|
@DisplayName("getPrincipal works for mocked OAuth2")
|
||||||
fun getPrincipalForMockOAuth2() {
|
fun getPrincipalForMockOAuth2(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
getWebTestClient()
|
getWebTestClient()
|
||||||
.mutateWith(
|
.mutateWith(
|
||||||
SecurityMockServerConfigurers.mockJwt().jwt { jwt ->
|
SecurityMockServerConfigurers.mockJwt().jwt { jwt ->
|
||||||
|
@ -118,16 +103,13 @@ class RestSecurityTests : BaseControllerTests() {
|
||||||
.expectBody(String::class.java)
|
.expectBody(String::class.java)
|
||||||
.isEqualTo(TESTS_DEFAULT_USERNAME)
|
.isEqualTo(TESTS_DEFAULT_USERNAME)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private val auth: Authentication? = null
|
private val auth: Authentication? = null
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("getPrincipal reacts to non-supported authentication context")
|
@DisplayName("getPrincipal reacts to non-supported authentication context")
|
||||||
fun getPrincipalWithUnsupportedContext() {
|
fun getPrincipalWithUnsupportedContext(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
assertThrows<IllegalStateException> {
|
assertThrows<IllegalStateException> {
|
||||||
SecurityContextHolder
|
SecurityContextHolder
|
||||||
.getContext()
|
.getContext()
|
||||||
|
@ -137,4 +119,3 @@ class RestSecurityTests : BaseControllerTests() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
|
@ -45,8 +45,7 @@ class ChatServiceTests : BaseServiceTests() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatServiceTest: create chat session")
|
@DisplayName("ChatServiceTest: create chat session")
|
||||||
fun createChatSession() {
|
fun createChatSession(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
genChatSession()
|
genChatSession()
|
||||||
.let { chatSession ->
|
.let { chatSession ->
|
||||||
chatService
|
chatService
|
||||||
|
@ -59,12 +58,10 @@ class ChatServiceTests : BaseServiceTests() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatServiceTest: delete chat session")
|
@DisplayName("ChatServiceTest: delete chat session")
|
||||||
fun deleteChatSession() {
|
fun deleteChatSession(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
chatService
|
chatService
|
||||||
.save(genChatSession())
|
.save(genChatSession())
|
||||||
.let { chatSession ->
|
.let { chatSession ->
|
||||||
|
@ -75,23 +72,19 @@ class ChatServiceTests : BaseServiceTests() {
|
||||||
assertNull(chatRepository.findById(chatSession.id!!))
|
assertNull(chatRepository.findById(chatSession.id!!))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatServiceTest: delete non-existent chat session")
|
@DisplayName("ChatServiceTest: delete non-existent chat session")
|
||||||
fun deleteChatSessionNotExist() {
|
fun deleteChatSessionNotExist(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val id = Random.nextInt(0, Int.MAX_VALUE)
|
val id = Random.nextInt(0, Int.MAX_VALUE)
|
||||||
assertFalse(
|
assertFalse(
|
||||||
chatService.delete(id, TESTS_DEFAULT_USERNAME)
|
chatService.delete(id, TESTS_DEFAULT_USERNAME)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatServiceTest: fetching chat sessions by id and username")
|
@DisplayName("ChatServiceTest: fetching chat sessions by id and username")
|
||||||
fun findOneByIdAndUsername() {
|
fun findOneByIdAndUsername(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
chatRepository.saveAll(
|
chatRepository.saveAll(
|
||||||
(1..3).map { genChatSession() }
|
(1..3).map { genChatSession() }
|
||||||
).onEach { chatSession ->
|
).onEach { chatSession ->
|
||||||
|
@ -109,12 +102,10 @@ class ChatServiceTests : BaseServiceTests() {
|
||||||
?: fail("findChatByIdForUsername returned null when shouldn't")
|
?: fail("findChatByIdForUsername returned null when shouldn't")
|
||||||
}.lastOrNull()
|
}.lastOrNull()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatServiceTest: trying to fetch another user chat session")
|
@DisplayName("ChatServiceTest: trying to fetch another user chat session")
|
||||||
fun findOneByIdAndUsernameButDifferentUser() {
|
fun findOneByIdAndUsernameButDifferentUser(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
chatService
|
chatService
|
||||||
.save(genChatSession())
|
.save(genChatSession())
|
||||||
.let { chatSession ->
|
.let { chatSession ->
|
||||||
|
@ -124,12 +115,10 @@ class ChatServiceTests : BaseServiceTests() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatServiceTest: trying to fetch non existing chat session")
|
@DisplayName("ChatServiceTest: trying to fetch non existing chat session")
|
||||||
fun findOneByIdAndUsernameButNonExisting() {
|
fun findOneByIdAndUsernameButNonExisting(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
chatService
|
chatService
|
||||||
.save(genChatSession())
|
.save(genChatSession())
|
||||||
.let { chatSession ->
|
.let { chatSession ->
|
||||||
|
@ -139,12 +128,10 @@ class ChatServiceTests : BaseServiceTests() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("ChatServiceTest: checking the order of queried chat sessions for username")
|
@DisplayName("ChatServiceTest: checking the order of queried chat sessions for username")
|
||||||
fun findAllByUsernameAndCheckOrder() {
|
fun findAllByUsernameAndCheckOrder(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
// Order should be reversed from the order of inserting into database. Last first.
|
// Order should be reversed from the order of inserting into database. Last first.
|
||||||
(1..5).map { genChatSession() }
|
(1..5).map { genChatSession() }
|
||||||
.also { csList ->
|
.also { csList ->
|
||||||
|
@ -173,5 +160,3 @@ class ChatServiceTests : BaseServiceTests() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
|
@ -42,8 +42,7 @@ class MessageServiceTests : BaseServiceTests() {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageServiceTest: create message")
|
@DisplayName("MessageServiceTest: create message")
|
||||||
fun createMessage() {
|
fun createMessage(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
genChatSession()
|
genChatSession()
|
||||||
.let { chatSession ->
|
.let { chatSession ->
|
||||||
genChatMessage(
|
genChatMessage(
|
||||||
|
@ -61,12 +60,10 @@ class MessageServiceTests : BaseServiceTests() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageServiceTest: find all previous messages and verify order")
|
@DisplayName("MessageServiceTest: find all previous messages and verify order")
|
||||||
fun findAllByChatSessionIdAndUsername() {
|
fun findAllByChatSessionIdAndUsername(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSession = genChatSession()
|
val chatSession = genChatSession()
|
||||||
.let { chatSession ->
|
.let { chatSession ->
|
||||||
chatService
|
chatService
|
||||||
|
@ -100,12 +97,10 @@ class MessageServiceTests : BaseServiceTests() {
|
||||||
awaitComplete()
|
awaitComplete()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("MessageServiceTest: whether we can retrieve messages from another user's chat session")
|
@DisplayName("MessageServiceTest: whether we can retrieve messages from another user's chat session")
|
||||||
fun findAllForAnotherUser() {
|
fun findAllForAnotherUser(): Unit = runBlocking {
|
||||||
runBlocking {
|
|
||||||
val chatSession = genChatSession()
|
val chatSession = genChatSession()
|
||||||
.let { chatSession ->
|
.let { chatSession ->
|
||||||
chatService
|
chatService
|
||||||
|
@ -129,4 +124,3 @@ class MessageServiceTests : BaseServiceTests() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
Loading…
Reference in a new issue