-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4608498
commit 9a490ba
Showing
8 changed files
with
246 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 0 additions & 51 deletions
51
src/test/groovy/com/softeno/template/app/permission/api/PermissionITSpec.groovy
This file was deleted.
Oops, something went wrong.
216 changes: 216 additions & 0 deletions
216
src/test/kotlin/com/softeno/template/app/IntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
package com.softeno.template.app | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import com.github.tomakehurst.wiremock.client.WireMock.* | ||
import com.github.tomakehurst.wiremock.client.WireMock.aResponse | ||
import com.github.tomakehurst.wiremock.client.WireMock.urlMatching | ||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.options | ||
import com.ninjasquad.springmockk.MockkBean | ||
import com.softeno.template.SoftenoMvcJpaApp | ||
import com.softeno.template.app.permission.PermissionFixture.Companion.aPermission | ||
import com.softeno.template.app.permission.PermissionFixture.Companion.aPermissionDto | ||
import com.softeno.template.app.permission.db.PermissionRepository | ||
import io.mockk.every | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.core.Ordered | ||
import org.springframework.core.annotation.Order | ||
import org.springframework.data.domain.PageImpl | ||
import org.springframework.data.domain.Pageable | ||
import org.springframework.kafka.test.context.EmbeddedKafka | ||
import org.springframework.test.web.reactive.server.WebTestClient | ||
import org.springframework.web.reactive.function.BodyInserters | ||
import org.springframework.web.reactive.function.client.WebClient | ||
import org.testcontainers.containers.PostgreSQLContainer | ||
import org.testcontainers.junit.jupiter.Container | ||
import org.testcontainers.junit.jupiter.Testcontainers | ||
|
||
@Testcontainers | ||
@SpringBootTest( | ||
classes = [SoftenoMvcJpaApp::class], | ||
properties = ["spring.profiles.active=integration"], | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT | ||
) | ||
@AutoConfigureWebTestClient(timeout = "6000") | ||
@EnableConfigurationProperties | ||
@EmbeddedKafka(partitions = 1, brokerProperties = ["listeners=PLAINTEXT://localhost:9092", "port=9092"]) | ||
@ConfigurationPropertiesScan("com.softeno") | ||
abstract class BaseIntegrationTest { | ||
|
||
@Autowired | ||
lateinit var permissionRepository: PermissionRepository | ||
|
||
@Autowired | ||
lateinit var webTestClient: WebTestClient | ||
|
||
@Container | ||
var postgreSQLContainer = PostgreSQLContainer("postgres:15.2-alpine") | ||
.withDatabaseName("application") | ||
.withUsername("admin") | ||
.withPassword("admin") | ||
|
||
|
||
@BeforeEach | ||
fun init() { | ||
// ... | ||
} | ||
|
||
@AfterEach | ||
fun cleanup() { | ||
permissionRepository.deleteAll() | ||
} | ||
|
||
} | ||
|
||
class ContextLoadsTest : BaseIntegrationTest() { | ||
|
||
@Test | ||
fun testConnection() { | ||
assertTrue(postgreSQLContainer.isRunning) | ||
} | ||
} | ||
|
||
class PermissionTest : BaseIntegrationTest() { | ||
|
||
@Test | ||
fun shouldReturnEmptyPermissionResponse() { | ||
webTestClient.get().uri("/permissions") | ||
.exchange() | ||
.expectStatus().isOk() | ||
.expectBody().jsonPath("content").isEmpty | ||
} | ||
|
||
@Test | ||
fun shouldRetrievePermission() { | ||
val aPermission = aPermission() | ||
permissionRepository.save(aPermission) | ||
|
||
webTestClient.get().uri("/permissions") | ||
.exchange() | ||
.expectStatus().isOk() | ||
.expectBody() | ||
.jsonPath("content.[0].name").isEqualTo(aPermission.name!!) | ||
.jsonPath("content.[0].description").isEqualTo(aPermission.description!!) | ||
} | ||
|
||
@Test | ||
fun shouldPersistPermission() { | ||
val aPermissionDto = aPermissionDto() | ||
|
||
webTestClient.post().uri("/permissions") | ||
.body(BodyInserters.fromValue(aPermissionDto)) | ||
.exchange() | ||
.expectStatus().isOk | ||
|
||
assertEquals(permissionRepository.findAll().size, 1) | ||
assertEquals(permissionRepository.findAll()[0].name!!, aPermissionDto.name) | ||
assertEquals(permissionRepository.findAll()[0].description!!, aPermissionDto.description) | ||
} | ||
} | ||
|
||
class PermissionTestMockk : BaseIntegrationTest() { | ||
|
||
@MockkBean | ||
@Order(value = Ordered.HIGHEST_PRECEDENCE) | ||
lateinit var permissionRepositoryMock: PermissionRepository | ||
|
||
@BeforeEach | ||
fun initMockkRepository() { | ||
every { permissionRepositoryMock.deleteAll() }.answers { } | ||
} | ||
|
||
@Test | ||
fun shouldPersistAndRetrievePermission() { | ||
val aPermission = aPermission() | ||
|
||
every { permissionRepositoryMock.findAll(any<Pageable>()) }.answers { PageImpl(listOf(aPermission)) } | ||
|
||
webTestClient.get().uri("/permissions") | ||
.exchange() | ||
.expectStatus().isOk() | ||
.expectBody() | ||
.jsonPath("content.[0].name").isEqualTo(aPermission.name!!) | ||
.jsonPath("content.[0].description").isEqualTo(aPermission.description!!) | ||
} | ||
} | ||
|
||
data class SampleResponseDto(val data: String) | ||
|
||
class ExternalControllerTest : BaseIntegrationTest(), ExternalApiAbility { | ||
|
||
@Autowired | ||
private lateinit var webclient: WebClient | ||
|
||
private val wiremock: WireMockServer = WireMockServer(options().port(4500)) | ||
|
||
@BeforeEach | ||
fun `setup wiremock`() { | ||
wiremock.start() | ||
} | ||
|
||
@AfterEach | ||
fun `stop wiremock`() { | ||
wiremock.stop() | ||
wiremock.resetAll() | ||
} | ||
|
||
@Test | ||
fun `mock external service with wiremock`() { | ||
// given | ||
mockGetId(wiremock) | ||
|
||
val expected = SampleResponseDto(data = "1") | ||
|
||
// expect | ||
val response = webclient.get().uri("http://localhost:4500/sample/100") | ||
.retrieve() | ||
.bodyToMono(SampleResponseDto::class.java) | ||
.block() | ||
|
||
assertEquals(expected, response) | ||
} | ||
|
||
@Test | ||
fun `test external controller`() { | ||
// given | ||
mockGetId(wiremock) | ||
|
||
// expect | ||
webTestClient.get().uri("/external/1") | ||
.exchange() | ||
.expectStatus().isOk() | ||
.expectBody() | ||
.jsonPath("data").isEqualTo("1") | ||
} | ||
} | ||
|
||
interface ExternalApiAbility { | ||
|
||
fun mockGetId(wiremock: WireMockServer) { | ||
wiremock.stubFor( | ||
get(urlMatching("/sample/.*")) | ||
.willReturn( | ||
aResponse() | ||
.withStatus(200) | ||
.withHeader("Content-Type", "application/json") | ||
.withBody( | ||
""" | ||
{ | ||
"data": "1" | ||
} | ||
""".trimIndent() | ||
) | ||
) | ||
) | ||
} | ||
} | ||
|
||
|
22 changes: 0 additions & 22 deletions
22
src/test/kotlin/com/softeno/template/app/config/TestRestTemplateConfig.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters