Skip to content

Commit

Permalink
Feature/web spring (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
jactor-rises authored May 20, 2024
2 parents 6ffdd71 + 1715e98 commit 3fd8c8f
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 161 deletions.
5 changes: 2 additions & 3 deletions web/src/test/kotlin/com/github/jactor/web/JactorWebTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package com.github.jactor.web

import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import com.github.jactor.web.controller.AboutController
import com.github.jactor.web.controller.HomeController
import com.github.jactor.web.controller.UserController
import com.github.jactor.web.test.AbstractNoDirtySpringContextTest
import assertk.assertThat
import assertk.assertions.isNotNull

@SpringBootTest(classes = [JactorWeb::class])
internal class JactorWebTest {
internal class JactorWebTest: AbstractNoDirtySpringContextTest() {

@Autowired
private val homeController: HomeController? = null
Expand Down
51 changes: 20 additions & 31 deletions web/src/test/kotlin/com/github/jactor/web/RequestManagerTest.kt
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
package com.github.jactor.web

import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import com.ninjasquad.springmockk.MockkBean
import com.github.jactor.web.test.AbstractNoDirtySpringContextTest
import assertk.assertAll
import assertk.assertThat
import assertk.assertions.isEqualTo
import io.mockk.every
import jakarta.servlet.http.HttpServletRequest

@SpringBootTest
internal class RequestManagerTest {

@MockkBean
private lateinit var httpServletRequestMock: HttpServletRequest

@Value("\${server.servlet.context-path}")
private lateinit var contextPath: String

internal class RequestManagerTest: AbstractNoDirtySpringContextTest() {
@Test
fun `should fetch currentUrl and attach it to the model`() {
every { httpServletRequestMock.requestURI } returns "$contextPath/user"
every { httpServletRequestMock.queryString } returns "choose=jactor"
every { httpServletRequestMockk.requestURI } returns "$contextPath/user"
every { httpServletRequestMockk.queryString } returns "choose=jactor"

assertThat(RequestManager(contextPath, httpServletRequestMock).fetchCurrentUrl())
assertThat(RequestManager(contextPath, httpServletRequestMockk).fetchCurrentUrl())
.isEqualTo("/user?choose=jactor")
}

@Test
fun `should not add query string to currentUrl if query string is blank`() {
every { httpServletRequestMock.requestURI } returns "$contextPath/user"
every { httpServletRequestMock.queryString } returns ""
every { httpServletRequestMockk.requestURI } returns "$contextPath/user"
every { httpServletRequestMockk.queryString } returns ""

assertThat(RequestManager(contextPath, httpServletRequestMock).fetchCurrentUrl()).isEqualTo("/user")
assertThat(RequestManager(contextPath, httpServletRequestMockk).fetchCurrentUrl()).isEqualTo("/user")
}

@Test
fun `should not add parameter called lang`() {
every { httpServletRequestMock.requestURI } returns "$contextPath/home"
every { httpServletRequestMock.queryString } returns "lang=en"
every { httpServletRequestMockk.requestURI } returns "$contextPath/home"
every { httpServletRequestMockk.queryString } returns "lang=en"

val languageParam = RequestManager(contextPath, httpServletRequestMock).fetchCurrentUrl()
val languageParam = RequestManager(contextPath, httpServletRequestMockk).fetchCurrentUrl()

every { httpServletRequestMock.requestURI } returns "$contextPath/user"
every { httpServletRequestMock.queryString } returns "lang=no&choose=tip"
every { httpServletRequestMockk.requestURI } returns "$contextPath/user"
every { httpServletRequestMockk.queryString } returns "lang=no&choose=tip"

val langAndOtherParam = RequestManager(contextPath, httpServletRequestMock).fetchCurrentUrl()
val langAndOtherParam = RequestManager(contextPath, httpServletRequestMockk).fetchCurrentUrl()

assertAll {
assertThat(languageParam).isEqualTo("/home")
Expand All @@ -56,17 +45,17 @@ internal class RequestManagerTest {

@Test
fun `should not add context-path to current url`() {
every { httpServletRequestMock.requestURI } returns "$contextPath/home"
every { httpServletRequestMock.queryString } returns null
every { httpServletRequestMockk.requestURI } returns "$contextPath/home"
every { httpServletRequestMockk.queryString } returns null

assertThat(RequestManager(contextPath, httpServletRequestMock).fetchCurrentUrl()).isEqualTo("/home")
assertThat(RequestManager(contextPath, httpServletRequestMockk).fetchCurrentUrl()).isEqualTo("/home")
}

@Test
fun `should not add centext-path to the view name`() {
every { httpServletRequestMock.requestURI } returns "$contextPath/someView"
every { httpServletRequestMock.queryString } returns null
every { httpServletRequestMockk.requestURI } returns "$contextPath/someView"
every { httpServletRequestMockk.queryString } returns null

assertThat(RequestManager(contextPath, httpServletRequestMock).fetchChosenView()).isEqualTo("someView")
assertThat(RequestManager(contextPath, httpServletRequestMockk).fetchChosenView()).isEqualTo("someView")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,15 @@ import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.http.ResponseEntity
import org.springframework.web.client.RestClientException
import org.springframework.web.client.RestTemplate
import com.github.jactor.web.test.AbstractNoDirtySpringContextTest
import assertk.assertAll
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
internal class UserConsumerIntegrationTest {

@Autowired
private lateinit var restTemplate: RestTemplate

@Autowired
private lateinit var testRestTemplate: TestRestTemplate
internal class UserConsumerIntegrationTest : AbstractNoDirtySpringContextTest() {

@Autowired
@Qualifier("userConsumer")
Expand All @@ -37,16 +26,14 @@ internal class UserConsumerIntegrationTest {

@BeforeEach
fun `assume jactor-persistence is running`() {
lateinit var response: ResponseEntity<String>

try {
response = testRestTemplate.getForEntity("$baseUrl/actuator/health", String::class.java)
} catch (e: RestClientException) {
assumeTrue(false)
}

assumeTrue(response.statusCode.is2xxSuccessful, response.statusCode.toString())
assumeTrue(/* assumption = */ response.body?.contains("UP") ?: false, /* message = */ response.body)
runCatching {
val response: ResponseEntity<String> = testRestTemplate.getForEntity(
"$baseUrl/actuator/health", String::class.java
)

assumeTrue(response.statusCode.is2xxSuccessful, response.statusCode.toString())
assumeTrue(/* assumption = */ response.body?.contains("UP") ?: false, /* message = */ response.body)
}.onFailure { assumeTrue(false) }
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,22 @@
package com.github.jactor.web.controller

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.servlet.view.InternalResourceViewResolver
import com.github.jactor.web.i18n.MyMessages
import com.github.jactor.web.model.HomePageModel
import com.github.jactor.web.test.AbstractSpringMockMvcTest
import assertk.assertAll
import assertk.assertThat
import assertk.assertions.hasSize
import assertk.assertions.isNotNull
import junit.framework.AssertionFailedError

@SpringBootTest
internal class HomeControllerTest {
private lateinit var mockMvc: MockMvc

@Autowired
private lateinit var myMessages: MyMessages

@Value("\${spring.mvc.view.prefix}")
private lateinit var prefix: String

@Value("\${spring.mvc.view.suffix}")
private lateinit var suffix: String

@BeforeEach
fun `mock mvc with view resolver`() {
val internalResourceViewResolver = InternalResourceViewResolver()

internalResourceViewResolver.setPrefix(prefix)
internalResourceViewResolver.setSuffix(suffix)

mockMvc = MockMvcBuilders.standaloneSetup(HomeController(myMessages))
.setViewResolvers(internalResourceViewResolver)
.build()
internal class HomeControllerTest : AbstractSpringMockMvcTest() {
override val initMockMvc: (InternalResourceViewResolver) -> MockMvc = {
MockMvcBuilders.standaloneSetup(HomeController(myMessages)).setViewResolvers(it).build()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,32 @@
package com.github.jactor.web.controller

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.servlet.view.InternalResourceViewResolver
import com.github.jactor.shared.dto.UserDto
import com.github.jactor.web.consumer.UserConsumer
import com.github.jactor.web.menu.MenuFacade
import com.github.jactor.web.menu.MenuItem
import com.ninjasquad.springmockk.MockkBean
import com.github.jactor.web.test.AbstractSpringMockMvcTest
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import io.mockk.every
import io.mockk.verify

@SpringBootTest
internal class UserControllerTest {
internal class UserControllerTest : AbstractSpringMockMvcTest() {
override val initMockMvc: (InternalResourceViewResolver) -> MockMvc = {
MockMvcBuilders.standaloneSetup(UserController(userConsumerMock, menuFacade, contextPath))
.setViewResolvers(it).build()
}

companion object {
private const val REQUEST_USER = "choose"
private const val USER_ENDPOINT = "/user"
private const val USER_JACTOR = "jactor"
}

private lateinit var mockMvc: MockMvc

@MockkBean
@Qualifier("userConsumer")
private lateinit var userConsumerMock: UserConsumer

@Autowired
private lateinit var menuFacade: MenuFacade

@Value("\${server.servlet.context-path}")
private lateinit var contextPath: String

@Value("\${spring.mvc.view.prefix}")
private lateinit var prefix: String

@Value("\${spring.mvc.view.suffix}")
private lateinit var suffix: String

@BeforeEach
fun `mock mvc with view resolver`() {
val internalResourceViewResolver = InternalResourceViewResolver()

internalResourceViewResolver.setPrefix(prefix)
internalResourceViewResolver.setSuffix(suffix)

mockMvc = MockMvcBuilders.standaloneSetup(UserController(userConsumerMock, menuFacade, contextPath))
.setViewResolvers(internalResourceViewResolver)
.build()
}

@Test
fun `should not fetch user by username if the username is missing from the request`() {
every { userConsumerMock.findAllUsernames() } returns emptyList()
Expand Down
Loading

0 comments on commit 3fd8c8f

Please sign in to comment.