diff options
author | Sebastian Baltes <Sebastian.Baltes@uni-bayreuth.de> | 2025-05-13 21:54:23 +0200 |
---|---|---|
committer | Sebastian Baltes <Sebastian.Baltes@uni-bayreuth.de> | 2025-05-13 21:54:23 +0200 |
commit | 1f715d85e17d2bf6c8d73df23e12252b993f74ff (patch) | |
tree | 3f430f6588e91c3e47793d7cd4f9957eb5bf4619 /application/src/test/java/de/unibayreuth/se | |
parent | 21fc676c1b5a1d026e1489e9c8fabb80e652a50c (diff) | |
download | se25-assignment03-1f715d85e17d2bf6c8d73df23e12252b993f74ff.tar.gz se25-assignment03-1f715d85e17d2bf6c8d73df23e12252b993f74ff.zip |
Diffstat (limited to 'application/src/test/java/de/unibayreuth/se')
-rw-r--r-- | application/src/test/java/de/unibayreuth/se/campuscoffee/AbstractSystemTest.java | 54 | ||||
-rw-r--r-- | application/src/test/java/de/unibayreuth/se/campuscoffee/CampusCoffeeSystemTests.java | 65 |
2 files changed, 119 insertions, 0 deletions
diff --git a/application/src/test/java/de/unibayreuth/se/campuscoffee/AbstractSystemTest.java b/application/src/test/java/de/unibayreuth/se/campuscoffee/AbstractSystemTest.java new file mode 100644 index 0000000..6ca91ce --- /dev/null +++ b/application/src/test/java/de/unibayreuth/se/campuscoffee/AbstractSystemTest.java @@ -0,0 +1,54 @@ +package de.unibayreuth.se.campuscoffee; + +import de.unibayreuth.se.campuscoffee.domain.ports.PosService; +import io.restassured.RestAssured; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +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.server.LocalServerPort; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.utility.DockerImageName; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles({"test"}) +public abstract class AbstractSystemTest { + static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>( + DockerImageName.parse("postgres:17-alpine")) + .withUsername("postgres") + .withPassword("postgres") + .withDatabaseName("postgres"); + + @BeforeAll + static void beforeAll() { + postgres.start(); + } + + @AfterAll + static void afterAll() { + postgres.stop(); + } + + @DynamicPropertySource + static void configureProperties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", postgres::getJdbcUrl); + registry.add("spring.datasource.username", postgres::getUsername); + registry.add("spring.datasource.password", postgres::getPassword); + } + + @Autowired + protected PosService posService; + + @LocalServerPort + private Integer port; + + @BeforeEach + void setUp() { + RestAssured.baseURI = "http://localhost:" + port; + posService.clear(); + } +}
\ No newline at end of file diff --git a/application/src/test/java/de/unibayreuth/se/campuscoffee/CampusCoffeeSystemTests.java b/application/src/test/java/de/unibayreuth/se/campuscoffee/CampusCoffeeSystemTests.java new file mode 100644 index 0000000..195a36a --- /dev/null +++ b/application/src/test/java/de/unibayreuth/se/campuscoffee/CampusCoffeeSystemTests.java @@ -0,0 +1,65 @@ +package de.unibayreuth.se.campuscoffee; + +import de.unibayreuth.se.campuscoffee.api.dtos.PosDto; +import de.unibayreuth.se.campuscoffee.api.mapper.PosDtoMapper; +import de.unibayreuth.se.campuscoffee.domain.Pos; +import de.unibayreuth.se.campuscoffee.domain.TestFixtures; +import io.restassured.http.ContentType; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.hasSize; + +public class CampusCoffeeSystemTests extends AbstractSystemTest { + + @Autowired + private PosDtoMapper posDtoMapper; + + @Test + void getAllCreatedPos() { + List<Pos> createdPosList = TestFixtures.createPos(posService); + + List<Pos> retrievedPos = given() + .contentType(ContentType.JSON) + .when() + .get("/api/pos") + .then() + .statusCode(200) + .body(".", hasSize(createdPosList.size())) + .and() + .extract().jsonPath().getList("$", PosDto.class) + .stream() + .map(posDtoMapper::toDomain) + .toList(); + + assertThat(retrievedPos) + .usingRecursiveFieldByFieldElementComparatorIgnoringFields("createdAt", "updatedAt") // prevent issues due to differing timestamps after conversions + .containsExactlyInAnyOrderElementsOf(createdPosList); + } + + @Test + void getPosById() { + List<Pos> createdPosList = TestFixtures.createPos(posService); + Pos createdPos = createdPosList.getFirst(); + + Pos retrievedPos = posDtoMapper.toDomain( + given() + .contentType(ContentType.JSON) + .when() + .get("/api/pos/{id}", createdPos.getId()) + .then() + .statusCode(200) + .extract().as(PosDto.class) + ); + + assertThat(retrievedPos) + .usingRecursiveComparison() + .ignoringFields("createdAt", "updatedAt") // prevent issues due to differing timestamps after conversions + .isEqualTo(createdPos); + } + +}
\ No newline at end of file |