-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from weesvc/add-testcontainers
Add testcontainers
- Loading branch information
Showing
11 changed files
with
387 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
services: | ||
|
||
db: | ||
image: postgres:15.3-alpine | ||
ports: | ||
- 5432:5432 | ||
environment: | ||
POSTGRES_USER: postgres | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_DB: defaultdb | ||
restart: always | ||
|
||
adminer: | ||
image: adminer | ||
ports: | ||
- 8180:8080 | ||
restart: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
src/test/java/io/weesvc/springboot/weesvc/WeesvcApplicationTests.java
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
src/test/java/io/weesvc/springboot/weesvc/api/APIContractTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.weesvc.springboot.weesvc.api; | ||
|
||
import io.weesvc.springboot.weesvc.WeesvcApplication; | ||
import io.weesvc.springboot.weesvc.domain.PlacesRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.testcontainers.Testcontainers; | ||
import org.testcontainers.containers.output.WaitingConsumer; | ||
import org.testcontainers.k6.K6Container; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest( | ||
classes = { WeesvcApplication.class, PlacesRestController.class, PlacesRepository.class }, | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, | ||
properties = { "spring.r2dbc.url=r2dbc:tc:postgresql:///testdb?TC_IMAGE_TAG=15.3-alpine" } | ||
) | ||
public class APIContractTests { | ||
|
||
@LocalServerPort | ||
private Integer port; | ||
|
||
@Test | ||
public void validateAPIContract() throws Exception { | ||
|
||
Testcontainers.exposeHostPorts(port); | ||
try ( | ||
K6Container container = new K6Container("grafana/k6:0.49.0") | ||
.withTestScript(MountableFile.forClasspathResource("api-compliance.js")) | ||
.withScriptVar("HOST", "host.testcontainers.internal") | ||
.withScriptVar("PORT", port.toString()) | ||
.withCmdOptions("--no-usage-report") | ||
) { | ||
container.start(); | ||
|
||
WaitingConsumer consumer = new WaitingConsumer(); | ||
container.followOutput(consumer); | ||
|
||
// Wait for test script results to be collected | ||
consumer.waitUntil( | ||
frame -> frame.getUtf8String().contains("iteration_duration"), | ||
1, | ||
TimeUnit.MINUTES | ||
); | ||
|
||
assertThat(container.getLogs()).doesNotContain("thresholds on metrics 'checks' have been crossed"); | ||
// Force a "passing" test to fail to see output results from k6 Test | ||
// assertThat(container.getLogs()).contains("thresholds on metrics 'checks' have been crossed"); | ||
|
||
} | ||
|
||
} | ||
|
||
} |
98 changes: 98 additions & 0 deletions
98
src/test/java/io/weesvc/springboot/weesvc/api/PlacesRestControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package io.weesvc.springboot.weesvc.api; | ||
|
||
import io.weesvc.springboot.weesvc.WeesvcApplication; | ||
import io.weesvc.springboot.weesvc.domain.PlacesRepository; | ||
import io.weesvc.springboot.weesvc.domain.model.Place; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@SpringBootTest( | ||
classes = { WeesvcApplication.class, PlacesRestController.class, PlacesRepository.class }, | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, | ||
properties = { "spring.r2dbc.url=r2dbc:tc:postgresql:///testdb?TC_IMAGE_TAG=15.3-alpine" } | ||
) | ||
public class PlacesRestControllerTests { | ||
|
||
private static final Duration ONE_SECOND_DURATION = Duration.ofSeconds(1); | ||
|
||
@Autowired | ||
PlacesRestController fixture; | ||
|
||
@Test | ||
void getPlaces() { | ||
final List<Place> places = | ||
fixture.getPlaces().collectList().block(ONE_SECOND_DURATION); | ||
assertTrue(places.size() > 0, "Expecting a non-empty list of places"); | ||
} | ||
|
||
@Test | ||
void getPlaceByID() { | ||
final Place place = fixture.getPlaceById(6L).block(ONE_SECOND_DURATION); | ||
assertNotNull(place, "Expecting seeded place with id=6"); | ||
assertEquals(6L, place.getId()); | ||
assertEquals("MIA", place.getName()); | ||
assertEquals("Miami International Airport, FL, USA", place.getDescription()); | ||
assertEquals(25.79516F, place.getLatitude()); | ||
assertEquals(-80.27959F, place.getLongitude()); | ||
assertNotNull(place.getCreatedAt(), "Created date should not be nullable"); | ||
assertNotNull(place.getUpdatedAt(), "Updated date should not be nullable"); | ||
} | ||
|
||
@Test | ||
void createPlace() { | ||
final Place newPlace = Place.builder() | ||
.name("Kerid Crater") | ||
.description("Kerid Crater, Iceland") | ||
.latitude(64.04126F) | ||
.longitude(-20.88530F) | ||
.build(); | ||
|
||
final Place created = fixture.addPlace(newPlace).block(); | ||
assertNotNull(created.getId(), "ID should be autoincremented"); | ||
assertEquals(newPlace.getName(), created.getName()); | ||
assertEquals(newPlace.getDescription(), created.getDescription()); | ||
assertEquals(newPlace.getLatitude(), created.getLatitude()); | ||
assertEquals(newPlace.getLongitude(), created.getLongitude()); | ||
assertNotNull(created.getCreatedAt(), "Created date should not be nullable"); | ||
assertNotNull(created.getUpdatedAt(), "Updated date should not be nullable"); | ||
} | ||
|
||
@Test | ||
void updatePlaceByID() { | ||
final Place original = fixture.getPlaceById(7L).block(ONE_SECOND_DURATION); | ||
assertNotNull(original, "Expecting seeded place with id=7"); | ||
|
||
final Place changes = original.toBuilder() | ||
.name("The Alamo") | ||
.description("The Alamo, San Antonio, TX, USA") | ||
.latitude(29.42590F) | ||
.longitude(-98.48625F) | ||
.build(); | ||
final Place updated = fixture.updatePlaceById(original.getId(), changes).block(ONE_SECOND_DURATION); | ||
assertEquals(changes.getId(), updated.getId()); | ||
assertEquals(changes.getName(), updated.getName()); | ||
assertEquals(changes.getDescription(), updated.getDescription()); | ||
assertEquals(changes.getLatitude(), updated.getLatitude()); | ||
assertEquals(changes.getLongitude(), updated.getLongitude()); | ||
assertEquals(changes.getCreatedAt(), updated.getCreatedAt()); | ||
assertNotEquals(changes.getUpdatedAt(), updated.getUpdatedAt(), "Updated date should be changed"); | ||
} | ||
|
||
@Test | ||
void deletePlaceByID() { | ||
final Long deleteID = 1L; | ||
fixture.deletePlaceById(deleteID).block(ONE_SECOND_DURATION); | ||
|
||
Exception e = assertThrows(RuntimeException.class, () -> { | ||
fixture.getPlaceById(deleteID).block(ONE_SECOND_DURATION); | ||
}); | ||
assertEquals(PlaceNotFoundException.class, e.getCause().getClass()); | ||
} | ||
|
||
} |
Oops, something went wrong.