Skip to content

Commit

Permalink
Add tests for Tealeaf Service
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatan-ivanov committed Jul 15, 2023
1 parent 3670f1a commit b22c6d1
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tealeaf-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ dependencies {
runtimeOnly 'mysql:mysql-connector-java:latest.release'
runtimeOnly 'org.flywaydb:flyway-core'
runtimeOnly 'org.flywaydb:flyway-mysql'

testImplementation 'org.flywaydb:flyway-core'
}
6 changes: 3 additions & 3 deletions tealeaf-service/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ch.qos.logback:logback-core:1.4.8=compileClasspath,productionRuntimeClasspath,ru
com.fasterxml.jackson.core:jackson-annotations:2.15.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-core:2.15.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-databind:2.15.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.dataformat:jackson-dataformat-toml:2.15.2=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
com.fasterxml.jackson.dataformat:jackson-dataformat-toml:2.15.2=productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.15.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
Expand Down Expand Up @@ -100,7 +100,7 @@ org.antlr:antlr4-runtime:4.10.1=compileClasspath,productionRuntimeClasspath,runt
org.antlr:stringtemplate:3.2.1=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
org.apache.commons:commons-lang3:3.12.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.commons:commons-math:2.2=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
org.apache.commons:commons-text:1.10.0=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
org.apache.commons:commons-text:1.10.0=productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.groovy:groovy-bom:4.0.12=testCompileClasspath,testRuntimeClasspath
org.apache.groovy:groovy-json:4.0.12=testCompileClasspath,testRuntimeClasspath
org.apache.groovy:groovy-xml:4.0.12=testCompileClasspath,testRuntimeClasspath
Expand Down Expand Up @@ -130,7 +130,7 @@ org.checkerframework:checker-qual:3.33.0=productionRuntimeClasspath,runtimeClass
org.codehaus.jettison:jettison:1.4.0=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
org.codehaus.woodstox:stax2-api:4.2.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.eclipse.angus:angus-activation:2.0.1=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
org.flywaydb:flyway-core:9.16.3=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
org.flywaydb:flyway-core:9.16.3=productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.flywaydb:flyway-mysql:9.16.3=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
org.glassfish.jaxb:jaxb-core:4.0.3=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
org.glassfish.jaxb:jaxb-runtime:4.0.3=productionRuntimeClasspath,runtimeClasspath,testRuntimeClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,206 @@
package org.example.teahouse.tealeaf;

import org.example.teahouse.tealeaf.api.CreateTealeafRequest;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

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 static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;
import static org.hamcrest.Matchers.blankOrNullString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(classes = TealeafServiceApplication.class)
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = RANDOM_PORT)
class TealeafServiceApplicationTest {
@Test void contextLoads() {}
@LocalServerPort int port;

@AfterEach
void cleanDb(@Autowired Flyway flyway) {
flyway.clean();
flyway.migrate();
}

@Test
void tealeavesEndpointShouldReturnTealeafResources() {
given()
.port(this.port)
.accept(JSON)
.when()
.get("/tealeaves")
.then()
.statusCode(200)
.body("page.totalElements", equalTo(3))
.body("_embedded.tealeaves.size()", equalTo(3))
.rootPath("_embedded.tealeaves.find { it.name == 'sencha' }")
.body("id", equalTo("6b55663a-1b50-43f1-a3b3-40939e89c4ad"))
.body("name", equalTo("sencha"))
.body("type", equalTo("green"))
.body("suggestedAmount", equalTo("3 g"))
.body("suggestedWaterTemperature", equalTo("75 °C"))
.body("suggestedSteepingTime", equalTo("3 min"))
.detachRootPath("")
.rootPath("_embedded.tealeaves.find { it.name == 'gyokuro' }")
.body("id", equalTo("b00a63a4-9796-4c82-8dd0-5654e872dcf2"))
.body("name", equalTo("gyokuro"))
.body("type", equalTo("green"))
.body("suggestedAmount", equalTo("2 g"))
.body("suggestedWaterTemperature", equalTo("70 °C"))
.body("suggestedSteepingTime", equalTo("2 min"))
.detachRootPath("")
.rootPath("_embedded.tealeaves.find { it.name == 'da hong pao' }")
.body("id", equalTo("98928e21-922f-4bd0-b5e5-275a4328cf7f"))
.body("name", equalTo("da hong pao"))
.body("type", equalTo("black"))
.body("suggestedAmount", equalTo("5 g"))
.body("suggestedWaterTemperature", equalTo("99 °C"))
.body("suggestedSteepingTime", equalTo("3 min"));
}

@ParameterizedTest
@CsvSource({
"6b55663a-1b50-43f1-a3b3-40939e89c4ad,sencha,green,3 g,75 °C,3 min",
"b00a63a4-9796-4c82-8dd0-5654e872dcf2,gyokuro,green,2 g,70 °C,2 min",
"98928e21-922f-4bd0-b5e5-275a4328cf7f,da hong pao,black,5 g,99 °C,3 min"
})
void tealeavesEndpointShouldReturnTealeafResourceById(String id, String name, String type, String amount, String temperature, String time) {
given()
.port(this.port)
.accept(JSON)
.when()
.get("/tealeaves/" + id)
.then()
.statusCode(200)
.body("id", equalTo(id))
.body("name", equalTo(name))
.body("type", equalTo(type))
.body("suggestedAmount", equalTo(amount))
.body("suggestedWaterTemperature", equalTo(temperature))
.body("suggestedSteepingTime", equalTo(time));
}

@ParameterizedTest
@CsvSource({
"6b55663a-1b50-43f1-a3b3-40939e89c4ad,sencha,green,3 g,75 °C,3 min",
"b00a63a4-9796-4c82-8dd0-5654e872dcf2,gyokuro,green,2 g,70 °C,2 min",
"98928e21-922f-4bd0-b5e5-275a4328cf7f,da hong pao,black,5 g,99 °C,3 min"
})
void searchEndpointShouldFindTealeafResources(String id, String name, String type, String amount, String temperature, String time) {
given()
.port(this.port)
.accept(JSON)
.param("name", name)
.when()
.get("/tealeaves/search/findByName")
.then()
.statusCode(200)
.body("id", equalTo(id))
.body("name", equalTo(name))
.body("type", equalTo(type))
.body("suggestedAmount", equalTo(amount))
.body("suggestedWaterTemperature", equalTo(temperature))
.body("suggestedSteepingTime", equalTo(time));
}

@Test
void tealeavesEndpointShouldSupportCreateAndDelete() {
given()
.port(this.port)
.accept(JSON)
.when()
.get("/tealeaves")
.then()
.statusCode(200)
.body("page.totalElements", equalTo(3));

given()
.port(this.port)
.accept(JSON)
.when()
.delete("/tealeaves")
.then()
.statusCode(204);

given()
.port(this.port)
.accept(JSON)
.when()
.get("/tealeaves")
.then()
.statusCode(200)
.body("page.totalElements", equalTo(0));

String id = given()
.port(this.port)
.accept(JSON)
.contentType(JSON)
.body(new CreateTealeafRequest("Ya Shi Xiang", "oolong", "3g", "3 min", "90 °C"))
.when()
.post("/tealeaves")
.then()
.statusCode(201)
.body("id", not(blankOrNullString()))
.body("name", equalTo("Ya Shi Xiang"))
.body("type", equalTo("oolong"))
.body("suggestedAmount", equalTo("3g"))
.body("suggestedWaterTemperature", equalTo("90 °C"))
.body("suggestedSteepingTime", equalTo("3 min"))
.extract().body().path("id");

given()
.port(this.port)
.accept(JSON)
.when()
.get("/tealeaves")
.then()
.statusCode(200)
.body("_embedded.tealeaves.size()", equalTo(1))
.rootPath("_embedded.tealeaves[0]")
.body("id", equalTo(id))
.body("name", equalTo("Ya Shi Xiang"))
.body("type", equalTo("oolong"))
.body("suggestedAmount", equalTo("3g"))
.body("suggestedWaterTemperature", equalTo("90 °C"))
.body("suggestedSteepingTime", equalTo("3 min"));

given()
.port(this.port)
.accept(JSON)
.when()
.get("/tealeaves/" + id)
.then()
.statusCode(200)
.body("id", equalTo(id))
.body("name", equalTo("Ya Shi Xiang"))
.body("type", equalTo("oolong"))
.body("suggestedAmount", equalTo("3g"))
.body("suggestedWaterTemperature", equalTo("90 °C"))
.body("suggestedSteepingTime", equalTo("3 min"));

given()
.port(this.port)
.accept(JSON)
.when()
.delete("/tealeaves/" + id)
.then()
.statusCode(204);

given()
.port(this.port)
.accept(JSON)
.when()
.get("/tealeaves")
.then()
.statusCode(200)
.body("page.totalElements", equalTo(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# spring.output.ansi.enabled=never
spring.flyway.clean-disabled=false

0 comments on commit b22c6d1

Please sign in to comment.