Skip to content

Commit

Permalink
Refactoring (#406)
Browse files Browse the repository at this point in the history
* Initial test setup refactoring

* Final test setup refactoring

* Bump and cleanup dependencies

* Cleanup

* Test helper method for elasticsearch
  • Loading branch information
torleifg authored Jan 10, 2025
1 parent a684d45 commit f0825c7
Show file tree
Hide file tree
Showing 28 changed files with 1,901 additions and 1,499 deletions.
107 changes: 40 additions & 67 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<version>1.0.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.1</version>
<relativePath/>
</parent>

<properties>
Expand Down Expand Up @@ -150,26 +150,29 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito.kotlin</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>5.4.0</version>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>3.9.1</version>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito.kotlin</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
Expand All @@ -181,40 +184,21 @@
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<artifactId>mongodb</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
<groupId>org.wiremock.integrations</groupId>
<artifactId>wiremock-spring-boot</artifactId>
<version>3.4.0</version>
</dependency>


</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
<excludes>
<exclude>banner.txt</exclude>
</excludes>
<filtering>false</filtering>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<targetPath>${project.build.outputDirectory}</targetPath>
<includes>
<include>banner.txt</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
Expand All @@ -225,29 +209,21 @@
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
<configuration>
<jvmTarget>${java.release}</jvmTarget>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<jvmTarget>${java.release}</jvmTarget>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>

<plugin>
Expand Down Expand Up @@ -322,7 +298,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.0</version>
<version>3.5.2</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
<argLine>${surefire.jacoco.args}</argLine>
Expand All @@ -336,7 +312,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.5.0</version>
<version>3.5.2</version>
<configuration>
<argLine>${failsafe.jacoco.args}</argLine>
<groups>contract</groups>
Expand All @@ -346,9 +322,6 @@
</includes>
</configuration>
</plugin>

</plugins>

</build>

</project>
3 changes: 0 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ application.cors.originPatterns: "*"
spring:
config.activate.on-profile: contract-test
security.oauth2.resourceserver.jwt.jwk-set-uri: http://localhost:6000/auth/realms/fdk/protocol/openid-connect/certs
elasticsearch:
username: elastic
password: elasticpwd
application:
historyServiceUri: http://localhost:6000
cors.originPatterns: "*"
126 changes: 126 additions & 0 deletions src/test/kotlin/no/fdk/concept_catalog/ContractTestsBase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package no.fdk.concept_catalog

import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.client.WireMock.*
import no.fdk.concept_catalog.model.Begrep
import no.fdk.concept_catalog.model.ChangeRequest
import no.fdk.concept_catalog.model.CurrentConcept
import no.fdk.concept_catalog.utils.JwkStore
import org.junit.jupiter.api.BeforeEach
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.context.annotation.Import
import org.springframework.data.elasticsearch.core.ElasticsearchOperations
import org.springframework.data.elasticsearch.core.query.DeleteQuery
import org.springframework.data.mongodb.core.MongoOperations
import org.springframework.data.mongodb.core.query.Query
import org.springframework.http.*
import org.springframework.test.context.ActiveProfiles
import org.springframework.web.client.HttpStatusCodeException
import org.wiremock.spring.ConfigureWireMock
import org.wiremock.spring.EnableWireMock

@ActiveProfiles("contract-test")
@Import(TestcontainersConfig::class)
@EnableWireMock(ConfigureWireMock(port = 6000))
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ContractTestsBase {

@LocalServerPort
var port: Int = 0

@Autowired
lateinit var mapper: ObjectMapper

@Autowired
lateinit var mongoOperations: MongoOperations

@Autowired
lateinit var elasticsearchOperations: ElasticsearchOperations

@Autowired
lateinit var testRestTemplate: TestRestTemplate

@BeforeEach
fun setUp() {
stubFor(get(urlPathEqualTo("/auth/realms/fdk/protocol/openid-connect/certs")).willReturn(okJson(JwkStore.get())))

mongoOperations.remove(Query(), Begrep::class.java)
mongoOperations.remove(Query(), ChangeRequest::class.java)

elasticsearchOperations.delete(
DeleteQuery.builder(org.springframework.data.elasticsearch.core.query.Query.findAll()).build(),
CurrentConcept::class.java
)
elasticsearchOperations.indexOps(CurrentConcept::class.java).refresh()
}

fun addToElasticsearchIndex(concept: CurrentConcept) {
elasticsearchOperations.save(concept)
elasticsearchOperations.indexOps(CurrentConcept::class.java).refresh()
}

fun addToElasticsearchIndex(concepts: List<CurrentConcept>) {
elasticsearchOperations.save(concepts)
elasticsearchOperations.indexOps(CurrentConcept::class.java).refresh()
}

fun request(path: String, mediaType: MediaType, httpMethod: HttpMethod): ResponseEntity<String> {
val url = "http://localhost:$port$path"

val httpHeaders = HttpHeaders()
httpHeaders.accept = listOf(mediaType)

val httpEntity: HttpEntity<String> = HttpEntity(httpHeaders)

val response = testRestTemplate.exchange(
url,
httpMethod,
httpEntity,
String::class.java
)

return response
}

fun authorizedRequest(
path: String,
body: String? = null,
token: String? = null,
httpMethod: HttpMethod,
accept: MediaType = MediaType.APPLICATION_JSON
): Map<String, Any?> {
val headers = HttpHeaders()
headers.accept = listOf(accept)
headers.contentType = MediaType.APPLICATION_JSON

token?.let { headers.setBearerAuth(it) }

val httpEntity: HttpEntity<String> = HttpEntity(body, headers)

return try {
val response =
testRestTemplate.exchange("http://localhost:$port$path", httpMethod, httpEntity, String::class.java)

mapOf(
"body" to response.body,
"header" to response.headers,
"status" to response.statusCode.value()
)
} catch (e: HttpStatusCodeException) {
mapOf(
"status" to e.statusCode.value(),
"header" to " ",
"body" to e.responseBodyAsString
)
} catch (e: Exception) {
mapOf(
"status" to e.toString(),
"header" to " ",
"body" to " "
)
}
}
}
24 changes: 24 additions & 0 deletions src/test/kotlin/no/fdk/concept_catalog/TestcontainersConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package no.fdk.concept_catalog

import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.springframework.context.annotation.Bean
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.elasticsearch.ElasticsearchContainer

@TestConfiguration(proxyBeanMethods = false)
class TestcontainersConfig {

@Bean
@ServiceConnection
fun mongoDBContainer(): MongoDBContainer {
return MongoDBContainer("mongo:latest")
}

@Bean
@ServiceConnection
fun elasticsearchContainer(): ElasticsearchContainer {
return ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:8.10.2")
.withEnv("xpack.security.enabled", "false")
}
}
Loading

0 comments on commit f0825c7

Please sign in to comment.