Skip to content

Commit

Permalink
chore: server test actions
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietzler committed Jul 16, 2024
1 parent 673a035 commit 45b769d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 7 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ jobs:
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "21"

- name: Run checkstyle
uses: dbelyaev/action-checkstyle@v1.14.0
with:
Expand All @@ -67,3 +74,29 @@ jobs:
workdir: ./server
checkstyle_config: ./server/checkstyle.xml
level: error

- name: Run unit tests
run: ./mvnw -Dtest='!de.uftos.e2e.**,!UftosApplicationTests' test

e2e:
name: E2E Tests
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./server

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Java
uses: actions/setup-java@v4
with:
distribution: "temurin"
java-version: "21"

- name: Build container
run: docker compose build api

- name: Run Server e2e tests
run: ./mvnw -Dtest='UftosApplicationTests,de.uftos.e2e.**' test
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dev:
docker compose -f ./docker-compose.yml up
dev-update:
docker compose -f ./docker-compose.yml up --build -V
test:
cd ./server && ./mvnw -Dtest='!de.uftos.e2e.**,!UftosApplicationTests' test
test-stupid:
cd ./server && ./mvnw.cmd -Dtest='!de.uftos.e2e.**,!UftosApplicationTests' test
e2e:
cd ./server && ./mvnw -Dtest='UftosApplicationTests,de.uftos.e2e.**' test
e2e-stupid:
cd ./server && ./mvnw.cmd -Dtest='UftosApplicationTests,de.uftos.e2e.**' test
12 changes: 12 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.8</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
Expand Down
6 changes: 3 additions & 3 deletions server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
spring.application.name=uftos
spring.devtools.livereload.enabled=true
spring.devtools.restart.enabled=true
spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}?stringtype=unspecified
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}
spring.datasource.url=jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/${DB_NAME:uftos}?stringtype=unspecified
spring.datasource.username=${DB_USER:uftos}
spring.datasource.password=${DB_PASSWORD:superSecurePassword}
spring.jpa.hibernate.ddl-auto=update
springdoc.writer-with-order-by-keys=true
springdoc.writer-with-default-pretty-printer=true
Expand Down
48 changes: 44 additions & 4 deletions server/src/test/java/de/uftos/UftosApplicationTests.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
package de.uftos;

import io.restassured.RestAssured;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.TestInstance;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@SpringBootTest
@SuppressWarnings("resource")
@Testcontainers
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class UftosApplicationTests {

@Test
void contextLoads() {
static Network network = Network.newNetwork();

static final GenericContainer<?> postgres = new GenericContainer<>(
DockerImageName.parse("postgres:16.3-alpine3.18"))
.withNetwork(network)
.withNetworkAliases("db")
.withExposedPorts(5432)
.withEnv("POSTGRES_USER", "uftos")
.withEnv("POSTGRES_PASSWORD", "superSecurePassword")
.withEnv("POSTGRES_DB", "uftos");

static final GenericContainer<?> server = new GenericContainer<>("uftos-server-dev")
.dependsOn(postgres)
.withNetwork(network)
.withAccessToHost(true)
.withEnv("DB_HOST", "db")
.withEnv("DB_PORT", "5432")
.withEnv("DB_NAME", "uftos")
.withEnv("DB_USER", "uftos")
.withEnv("DB_PASSWORD", "superSecurePassword")
.withExposedPorts(8080);

@BeforeAll
static void setup() {
postgres.start();
server.start();
RestAssured.port = server.getMappedPort(8080);
RestAssured.baseURI = "http://%s".formatted(server.getHost());
}

// if this doesn't exist surefire will ignore the file
// don't ask me....
@SuppressWarnings("checkstyle:MethodName")
@Test
void _foo() {
}
}
2 changes: 2 additions & 0 deletions server/src/test/java/de/uftos/e2e/StudentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import java.util.Collections;
import java.util.List;
Expand All @@ -29,6 +30,7 @@ class StudentsTest {

@BeforeAll
static void createTestStudents() throws JSONException {
System.out.println(RestAssured.baseURI + ", " + RestAssured.port);
tagId = given().contentType(ContentType.JSON)
.body(generateTagJson(TAG_NAME))
.when()
Expand Down

0 comments on commit 45b769d

Please sign in to comment.