Skip to content

Commit

Permalink
feat: connect to catalog-mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsOveTen committed Oct 20, 2023
1 parent 17d5bd9 commit 94c8cb0
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 1 deletion.
22 changes: 22 additions & 0 deletions deploy/staging/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-catalog
labels:
app: service-catalog
spec:
template:
spec:
containers:
- name: service-catalog
env:
- name: MONGO_USERNAME
value: root
- name: MONGO_PASSWORD
valueFrom:
secretKeyRef:
name: catalog-mongodb
key: ROOT_PASSWORD
- name: MONGO_SERVICE
value: staging-catalog-mongodb-headless
3 changes: 3 additions & 0 deletions deploy/staging/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ namespace: staging
resources:
- ../base
- ingress.yaml

patchesStrategicMerge:
- env.yaml
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<dependency>
<groupId>net.logstash.logback</groupId>
Expand All @@ -53,6 +57,10 @@
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>

<!-- Test -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package no.digdir.service_catalog.controller

import no.digdir.service_catalog.model.Service
import no.digdir.service_catalog.mongodb.ServiceRepository
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping

@Controller
@CrossOrigin
@RequestMapping(value = ["/services"])
class ServiceController(private val serviceRepository: ServiceRepository) {

@GetMapping
fun getAllServices(): ResponseEntity<List<Service>> =
ResponseEntity(serviceRepository.findAll(), HttpStatus.OK)

}
9 changes: 9 additions & 0 deletions src/main/kotlin/no/digdir/service_catalog/model/Service.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package no.digdir.service_catalog.model

import org.springframework.data.mongodb.core.mapping.Document

@Document(collection = "services")
data class Service (
val id: String,
val title: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package no.digdir.service_catalog.mongodb

import no.digdir.service_catalog.model.Service
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository

@Repository
interface ServiceRepository : MongoRepository<Service, String?>
3 changes: 2 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ management:
endpoint.health.probes.enabled: true
health:
livenessState.enabled: true
readinessState.enabled: true
readinessState.enabled: true
spring.data.mongodb.uri: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_SERVICE}/serviceCatalog?authSource=admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package no.digdir.service_catalog.integration

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import no.digdir.service_catalog.model.Service
import no.digdir.service_catalog.utils.ApiTestContext
import no.digdir.service_catalog.utils.SERVICES
import no.digdir.service_catalog.utils.apiGet
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus
import org.springframework.test.context.ContextConfiguration

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = [ApiTestContext.Initializer::class])
@Tag("integration")
class GetServices: ApiTestContext() {
private val mapper = jacksonObjectMapper()

@Test
fun `able to get all services`() {
val response = apiGet(port, "/services", null)
Assertions.assertEquals(HttpStatus.OK.value(), response["status"])

val result: List<Service> = mapper.readValue(response["body"] as String)
Assertions.assertEquals(SERVICES, result)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus
import org.springframework.test.context.ContextConfiguration

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = [ApiTestContext.Initializer::class])
@Tag("integration")
class HealthTest: ApiTestContext() {
@Test
Expand Down
35 changes: 35 additions & 0 deletions src/test/kotlin/no/digdir/service_catalog/utils/ApiTestContext.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
package no.digdir.service_catalog.utils

import no.digdir.service_catalog.mongodb.ServiceRepository
import org.junit.jupiter.api.BeforeEach
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.util.TestPropertyValues
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext
import org.testcontainers.containers.GenericContainer
import org.testcontainers.containers.wait.strategy.Wait

abstract class ApiTestContext {
@LocalServerPort
var port: Int = 0

@Autowired
private lateinit var serviceRepository: ServiceRepository
@BeforeEach
fun resetDatabase() {
serviceRepository.deleteAll()
serviceRepository.saveAll(SERVICES)
}

internal class Initializer : ApplicationContextInitializer<ConfigurableApplicationContext> {
override fun initialize(configurableApplicationContext: ConfigurableApplicationContext) {
TestPropertyValues.of(
"spring.data.mongodb.uri=mongodb://$MONGO_USER:$MONGO_PASSWORD@localhost:${mongoContainer.getMappedPort(MONGO_PORT)}/$MONGO_DB_NAME?authSource=admin",
).applyTo(configurableApplicationContext.environment)
}
}

companion object {
val mongoContainer: GenericContainer<*> = GenericContainer("mongo:latest")
.withEnv(MONGO_ENV_VALUES)
.withExposedPorts(MONGO_PORT)
.waitingFor(Wait.forListeningPort())

init {
mongoContainer.start()
}
}
}
19 changes: 19 additions & 0 deletions src/test/kotlin/no/digdir/service_catalog/utils/TestData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package no.digdir.service_catalog.utils

import no.digdir.service_catalog.model.Service
import org.testcontainers.shaded.com.google.common.collect.ImmutableMap

const val MONGO_USER = "testuser"
const val MONGO_PASSWORD = "testpassword"
const val MONGO_PORT = 27017
const val MONGO_DB_NAME = "serviceCatalog"

val MONGO_ENV_VALUES: Map<String, String> = ImmutableMap.of(
"MONGO_INITDB_ROOT_USERNAME", MONGO_USER,
"MONGO_INITDB_ROOT_PASSWORD", MONGO_PASSWORD
)

val SERVICE_0 = Service("0", "title 0")
val SERVICE_1 = Service("1", "title 1")

val SERVICES = listOf(SERVICE_0, SERVICE_1)

0 comments on commit 94c8cb0

Please sign in to comment.