Skip to content

Commit

Permalink
Merge pull request #67 from MTES-MCT/backend-tests
Browse files Browse the repository at this point in the history
Try running backend tests on CI
  • Loading branch information
lwih authored Dec 27, 2023
2 parents d3eb6ad + 25ba451 commit 60a8f76
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build-and-test-backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ jobs:
- name: "Analyse dependencies"
run: make back-check-dependencies

# - name: "Tests"
# run: make back-test
- name: "Tests"
env:
CI: true
run: make back-test

# - name: "Check clean architecture"
# run: make check-clean-archi
Expand Down
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ BACKEND_DIR := backend
BACKEND_CONFIGURATION_FOLDER=$(shell pwd)/infra/configurations/backend/


.PHONY: back-clean-install back-check-dependencies back-test
.PHONY: back-clean-install back-check-dependencies back-test back-verify-ci
back-clean-install:
cd $(BACKEND_DIR) && ./mvnw clean install

back-check-dependencies:
cd $(BACKEND_DIR) && ./mvnw dependency-check:check

back-test:
cd $(BACKEND_DIR) && ./mvnw test -Dspring-boot.run.arguments="--spring.config.additional-location="$(BACKEND_CONFIGURATION_FOLDER)"" \
-Dspring-boot.run.profiles="dev"
cd $(BACKEND_DIR) && ./mvnw test -Pci -Dmaven.main.skip=true

back-verify-ci:
cd $(BACKEND_DIR) && ./mvnw clean verify -Pci





Expand Down
35 changes: 35 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
<artifactId>graphql-java</artifactId>
<version>21.1</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -92,6 +99,12 @@
<artifactId>flyway-core</artifactId>
<version>9.22.3</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down Expand Up @@ -231,4 +244,26 @@
</plugins>
</build>

<profiles>
<profile>
<id>ci</id>
<!-- Configuration specific to CI environment -->
<!-- Disable starting the backend and running migrations -->
<properties>
<flyway.enabled>false</flyway.enabled>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.gouv.gmampa.rapportnav.domain.use_cases.mission.status

import fr.gouv.dgampa.rapportnav.RapportNavApplication
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.action.ActionStatusEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.status.ActionStatusType
import fr.gouv.dgampa.rapportnav.domain.repositories.mission.action.INavActionStatusRepository
Expand All @@ -12,14 +11,30 @@ import org.mockito.BDDMockito.given
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.junit.jupiter.Container
import java.time.ZoneId
import java.time.ZonedDateTime
import java.util.*


@SpringBootTest(classes = [RapportNavApplication::class])
@SpringBootTest(classes = [GetStatusForAction::class])
class GetStatusForActionTests {

companion object {
@Container
val container: PostgreSQLContainer<*> = PostgreSQLContainer<Nothing>("postgres:latest")
.apply {
withDatabaseName("rapportnavdb")
withUsername("postgres")
withPassword("postgres")
}

init {
container.start()
}
}

private var missionId: Int = 1

@MockBean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package fr.gouv.gmampa.rapportnav.infrastructure.database

import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.context.TestPropertySource
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.containers.output.OutputFrame
import org.testcontainers.containers.output.ToStringConsumer
import org.testcontainers.containers.wait.strategy.Wait
import org.testcontainers.junit.jupiter.Testcontainers
import java.time.Duration
import java.time.temporal.ChronoUnit

@Testcontainers
@TestPropertySource("classpath:/application.properties")
@SpringBootTest
abstract class AbstractDBTests {

companion object {
@JvmStatic
val container = PostgreSQLContainer("postgres:15-bookworm")
.apply {
withExposedPorts(5432)
withEnv("POSTGRES_DB", "testdb")
withEnv("POSTGRES_USER", "postgres")
withEnv("POSTGRES_PASSWORD", "postgres")
waitingFor(
Wait.forLogMessage(".*ready to accept connections.*\\s", 2),
)
withStartupTimeout(Duration.of(60L, ChronoUnit.SECONDS))
this.start()
}

@JvmStatic
@DynamicPropertySource
fun properties(registry: DynamicPropertyRegistry) {
registry.add("spring.datasource.url") { getJdbcUrl() }
}

private fun getJdbcUrl(): String {
val toStringConsumer = ToStringConsumer()
container.followOutput(toStringConsumer, OutputFrame.OutputType.STDOUT)

return "jdbc:postgresql://" + container.containerIpAddress + ":" + container.getMappedPort(
PostgreSQLContainer.POSTGRESQL_PORT,
).toString() + "/testdb?user=postgres&password=postgres"
}
}
}

0 comments on commit 60a8f76

Please sign in to comment.