Last active
November 16, 2021 13:50
-
-
Save gbzarelli/f08326cda387cfcc562633ff034c21b7 to your computer and use it in GitHub Desktop.
ContainerStarterTest_part1.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
abstract class DefaultContainerStarterTest { | |
private static final GenericContainer<?> APP; | |
private static final GenericContainer<?> MYSQL_CONTAINER; | |
private static final Network NETWORK = Network.newNetwork(); | |
protected static final WireMockServer MOCK_SERVER; | |
/* Containers are initialized in static block to create only once in test execution */ | |
static { | |
/*Init MockServer*/ | |
MOCK_SERVER = new WireMockServer(wireMockConfig().dynamicPort()); | |
MOCK_SERVER.start(); | |
Testcontainers.exposeHostPorts(MOCK_SERVER.port()); | |
/*Init containers*/ | |
MYSQL_CONTAINER = buildMySqlContainer(); | |
MYSQL_CONTAINER.start(); | |
APP = buildAppContainer(MYSQL_CONTAINER); | |
APP.start(); | |
/*Init RestAssured*/ | |
RestAssured.baseURI = "http://" + APP.getHost(); | |
RestAssured.port = APP.getFirstMappedPort(); | |
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | |
} | |
private static GenericContainer<?> buildMySqlContainer()... | |
private static GenericContainer<?> buildAppContainer(final Startable... dependsOn)... | |
@AfterEach | |
void tearDown()... | |
} |
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
private static GenericContainer<?> buildAppContainer(final Startable... dependsOn) { | |
return new GenericContainer<>("app-test:integration") | |
.dependsOn(dependsOn) | |
.withNetwork(NETWORK) | |
.withEnv("RANDOM_DATA_API_URL", "http://host.testcontainers.internal:" + MOCK_SERVER.port()) | |
.withEnv("MYSQL_USER", "test") | |
.withEnv("MYSQL_PASSWORD", "test") | |
.withEnv("MYSQL_URL", "jdbc:mysql://testdb:" + MySQLContainer.MYSQL_PORT + "/test?autoReconnect=true&useSSL=false") | |
.withExposedPorts(8080) | |
.waitingFor(Wait.forHttp("/health/ready").forStatusCode(200)) | |
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("APP_CONTAINER"))); | |
} |
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
class MessageV1EndpointIT extends DefaultContainerStarterTest { | |
@Test | |
void whenCreateNewScheduleThenReturnTheScheduleWithProtocol() { | |
mockRandomIdNumberToGenerateProtocol(MOCK_SERVER); | |
final var message = "That is the message"; | |
final int id = given() | |
.contentType(ContentType.JSON) | |
.body(buildDefaultMessage(message)) | |
.when() | |
.post("/v1/message/") | |
.then() | |
.statusCode(201) | |
.body("id", notNullValue()) | |
.extract() | |
.path("id"); | |
given() | |
.when() | |
.get("/v1/message/{id}", id) | |
.then() | |
.statusCode(200) | |
.body("protocol", equalTo("000-22-1110")) | |
.body("scheduleDate", notNullValue()) | |
.body("message", equalTo(message)) | |
.body(matchesJsonSchemaInClasspath("jsons/v1/postMessage.json")); | |
} | |
private void mockRandomIdNumberToGenerateProtocol(final WireMockServer mockServer) { | |
mockServer.stubFor(get(urlPathMatching("/api/id_number/random_id_number")) | |
.willReturn(aResponse() | |
.withBodyFile("randomIdNumberResponse.json") | |
.withStatus(200) | |
.withHeader("Content-Type", "application/json"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment