1
0
mirror of https://github.com/pcvolkmer/etl-processor.git synced 2025-04-20 17:56:50 +00:00

test: add tests for token requests

This commit is contained in:
Paul-Christian Volkmer 2024-05-06 11:42:11 +02:00
parent 107429fda7
commit 94d7b4c4f0
2 changed files with 69 additions and 7 deletions

View File

@ -26,12 +26,18 @@ import dev.dnpm.etl.processor.monitoring.RestConnectionCheckService
import dev.dnpm.etl.processor.output.MtbFileSender import dev.dnpm.etl.processor.output.MtbFileSender
import dev.dnpm.etl.processor.pseudonym.Generator import dev.dnpm.etl.processor.pseudonym.Generator
import dev.dnpm.etl.processor.services.RequestProcessor import dev.dnpm.etl.processor.services.RequestProcessor
import dev.dnpm.etl.processor.services.TokenRepository import dev.dnpm.etl.processor.services.TokenService
import dev.dnpm.etl.processor.services.TransformationService import dev.dnpm.etl.processor.services.TransformationService
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.ArgumentMatchers.anyString
import org.mockito.junit.jupiter.MockitoExtension import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.boot.test.mock.mockito.MockBean
@ -43,7 +49,9 @@ import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestPropertySource import org.springframework.test.context.TestPropertySource
import org.springframework.test.context.junit.jupiter.SpringExtension import org.springframework.test.context.junit.jupiter.SpringExtension
import org.springframework.test.web.servlet.MockMvc import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.delete
import org.springframework.test.web.servlet.get import org.springframework.test.web.servlet.get
import org.springframework.test.web.servlet.post
import reactor.core.publisher.Sinks import reactor.core.publisher.Sinks
abstract class MockSink : Sinks.Many<Boolean> abstract class MockSink : Sinks.Many<Boolean>
@ -71,23 +79,26 @@ abstract class MockSink : Sinks.Many<Boolean>
MtbFileSender::class, MtbFileSender::class,
RequestProcessor::class, RequestProcessor::class,
TransformationService::class, TransformationService::class,
TokenRepository::class,
GPasConnectionCheckService::class, GPasConnectionCheckService::class,
RestConnectionCheckService::class RestConnectionCheckService::class,
TokenService::class,
) )
class ConfigControllerTest { class ConfigControllerTest {
private lateinit var mockMvc: MockMvc private lateinit var mockMvc: MockMvc
private lateinit var requestProcessor: RequestProcessor private lateinit var requestProcessor: RequestProcessor
private lateinit var tokenService: TokenService
@BeforeEach @BeforeEach
fun setup( fun setup(
@Autowired mockMvc: MockMvc, @Autowired mockMvc: MockMvc,
@Autowired requestProcessor: RequestProcessor @Autowired requestProcessor: RequestProcessor,
@Autowired tokenService: TokenService,
) { ) {
this.mockMvc = mockMvc this.mockMvc = mockMvc
this.requestProcessor = requestProcessor this.requestProcessor = requestProcessor
this.tokenService = tokenService
} }
@Test @Test
@ -113,4 +124,54 @@ class ConfigControllerTest {
} }
} }
@Test
fun testShouldSaveNewToken() {
mockMvc.post("/configs/tokens") {
with(user("admin").roles("ADMIN"))
accept(MediaType.TEXT_HTML)
contentType = MediaType.APPLICATION_FORM_URLENCODED
content = "name=Testtoken"
}.andExpect {
status { is2xxSuccessful() }
}
val captor = argumentCaptor<String>()
verify(tokenService, times(1)).addToken(captor.capture())
assertThat(captor.firstValue).isEqualTo("Testtoken")
}
@Test
fun testShouldNotSaveTokenWithExstingName() {
whenever(tokenService.addToken(anyString())).thenReturn(Result.failure(RuntimeException("Testfailure")))
mockMvc.post("/configs/tokens") {
with(user("admin").roles("ADMIN"))
accept(MediaType.TEXT_HTML)
contentType = MediaType.APPLICATION_FORM_URLENCODED
content = "name=Testtoken"
}.andExpect {
status { is2xxSuccessful() }
}
val captor = argumentCaptor<String>()
verify(tokenService, times(1)).addToken(captor.capture())
assertThat(captor.firstValue).isEqualTo("Testtoken")
}
@Test
fun testShouldDeleteToken() {
mockMvc.delete("/configs/tokens/42") {
with(user("admin").roles("ADMIN"))
accept(MediaType.TEXT_HTML)
}.andExpect {
status { is2xxSuccessful() }
}
val captor = argumentCaptor<Long>()
verify(tokenService, times(1)).deleteToken(captor.capture())
assertThat(captor.firstValue).isEqualTo(42)
}
} }

View File

@ -127,10 +127,11 @@ class ConfigController(
} else { } else {
model.addAttribute("tokensEnabled", true) model.addAttribute("tokensEnabled", true)
val result = tokenService.addToken(name) val result = tokenService.addToken(name)
if (result.isSuccess) { result.onSuccess {
model.addAttribute("newTokenValue", result.getOrDefault("")) model.addAttribute("newTokenValue", it)
model.addAttribute("success", true) model.addAttribute("success", true)
} else { }
result.onFailure {
model.addAttribute("success", false) model.addAttribute("success", false)
} }
model.addAttribute("tokens", tokenService.findAll()) model.addAttribute("tokens", tokenService.findAll())