Add school management REST API and architecture checks
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package nl.agilecc.agiledancers;
|
||||
|
||||
import com.tngtech.archunit.core.importer.ImportOption;
|
||||
import com.tngtech.archunit.junit.AnalyzeClasses;
|
||||
import com.tngtech.archunit.junit.ArchTest;
|
||||
import com.tngtech.archunit.lang.ArchRule;
|
||||
|
||||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
|
||||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
|
||||
import static com.tngtech.archunit.library.Architectures.layeredArchitecture;
|
||||
import static com.tngtech.archunit.library.dependencies.SlicesRuleDefinition.slices;
|
||||
|
||||
@AnalyzeClasses(packages = "nl.agilecc.agiledancers", importOptions = ImportOption.DoNotIncludeTests.class)
|
||||
class ArchitectureTest {
|
||||
private static final String ROOT = "nl.agilecc.agiledancers";
|
||||
private static final String API_MODEL = ROOT + ".rest.apimodel";
|
||||
|
||||
// API model classes and records use the Request, Response, View, Dto or DTO suffix.
|
||||
@ArchTest
|
||||
static final ArchRule api_models_reside_in_the_api_model_package = classes()
|
||||
.that().haveNameMatching(".*(Request|Response|View|Dto|DTO)$")
|
||||
.should().resideInAPackage(API_MODEL);
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule rest_records_reside_in_the_api_model_package = classes()
|
||||
.that().resideInAPackage(ROOT + ".rest..")
|
||||
.and().areRecords()
|
||||
.should().resideInAPackage(API_MODEL);
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule api_models_are_public_top_level_types = classes()
|
||||
.that().resideInAPackage(API_MODEL + "..")
|
||||
.should().bePublic()
|
||||
.andShould().beTopLevelClasses()
|
||||
.andShould().haveNameMatching(".*(Request|Response|View|Dto|DTO)$");
|
||||
|
||||
// The existing services depend on concrete repositories. This rule preserves that
|
||||
// arrangement until repository ports and dependency injection are introduced.
|
||||
@ArchTest
|
||||
static final ArchRule layer_dependencies = layeredArchitecture()
|
||||
.consideringOnlyDependenciesInLayers()
|
||||
.layer("Model").definedBy(ROOT + ".model..")
|
||||
.layer("Services").definedBy(ROOT + ".service..")
|
||||
.layer("Repositories").definedBy(ROOT + ".repository..")
|
||||
.optionalLayer("REST").definedBy(ROOT + ".rest..")
|
||||
.optionalLayer("Solver").definedBy(ROOT + ".solver..")
|
||||
.whereLayer("Model").mayNotAccessAnyLayer()
|
||||
.whereLayer("Services").mayOnlyAccessLayers("Model", "Repositories")
|
||||
.whereLayer("Repositories").mayOnlyAccessLayers("Model")
|
||||
.whereLayer("REST").mayOnlyAccessLayers("Services", "Model")
|
||||
.whereLayer("Solver").mayOnlyAccessLayers("Services", "Model");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule production_classes_have_an_architectural_home = classes()
|
||||
.should().resideInAnyPackage(ROOT + ".model..", ROOT + ".service..",
|
||||
ROOT + ".repository..", ROOT + ".rest..", ROOT + ".solver..");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule model_is_independent_of_frameworks = classes()
|
||||
.that().resideInAPackage(ROOT + ".model..")
|
||||
.should().onlyDependOnClassesThat()
|
||||
.resideInAnyPackage(ROOT + ".model..", "java..", "lombok..");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule ortools_is_encapsulated_in_solver = noClasses()
|
||||
.that().resideOutsideOfPackage(ROOT + ".solver..")
|
||||
.should().dependOnClassesThat().resideInAPackage("com.google.ortools..");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule http_api_is_encapsulated_in_rest = noClasses()
|
||||
.that().resideOutsideOfPackage(ROOT + ".rest..")
|
||||
.should().dependOnClassesThat().resideInAnyPackage("jakarta.ws.rs..", "org.jboss.resteasy..");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule packages_are_free_of_cycles = slices()
|
||||
.matching(ROOT + ".(*)..").should().beFreeOfCycles();
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package nl.agilecc.agiledancers;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@QuarkusTest
|
||||
class SchoolManagementResourceTest {
|
||||
private static final String SCHOOLS = "/api/dance-schools";
|
||||
private static final Map<String, Object> DANCER = Map.of("name", "Sam", "birthDate", "2010-05-12");
|
||||
private static final Map<String, Object> GROUP = Map.of("category", "Teens", "type", "REGULAR",
|
||||
"startAge", 12, "endAge", 18, "styles", new String[]{"HIPHOP"});
|
||||
private static final Map<String, Object> SHOW = Map.of("name", "Annual show", "date", "2027-06-12");
|
||||
|
||||
@Test
|
||||
void managesSchoolAndItsResources() {
|
||||
long schoolId = create(SCHOOLS, Map.of("name", "Dance Studio"));
|
||||
String school = SCHOOLS + "/" + schoolId;
|
||||
try {
|
||||
given().get(school).then().statusCode(200).body("name", equalTo("Dance Studio"));
|
||||
given().get(SCHOOLS).then().statusCode(200).body("id", hasItem((int) schoolId));
|
||||
put(school, Map.of("name", "Renamed studio")).statusCode(200).body("name", equalTo("Renamed studio"));
|
||||
|
||||
long dancerId = create(school + "/dancers", DANCER);
|
||||
String dancer = school + "/dancers/" + dancerId;
|
||||
put(dancer, Map.of("name", "Sam updated", "birthDate", "2011-06-13"))
|
||||
.statusCode(200).body("birthDate", equalTo("2011-06-13"));
|
||||
given().get(dancer).then().statusCode(200).body("name", equalTo("Sam updated"));
|
||||
given().get(school + "/dancers").then().statusCode(200).body("id", contains((int) dancerId));
|
||||
|
||||
long groupId = create(school + "/dance-groups", GROUP);
|
||||
String group = school + "/dance-groups/" + groupId;
|
||||
given().put(group + "/dancers/" + dancerId).then().statusCode(204);
|
||||
put(group, Map.of("category", "Selection", "type", "SELECTION", "startAge", 10,
|
||||
"endAge", 20, "styles", new String[]{"JAZZ", "MODERN"}))
|
||||
.statusCode(200).body("category", equalTo("Selection"))
|
||||
.body("dancerIds", contains((int) dancerId)).body("styles", containsInAnyOrder("JAZZ", "MODERN"));
|
||||
given().get(group).then().statusCode(200).body("type", equalTo("SELECTION"));
|
||||
given().get(school + "/dance-groups").then().statusCode(200).body("id", contains((int) groupId));
|
||||
|
||||
long showId = create(school + "/shows", SHOW);
|
||||
String show = school + "/shows/" + showId;
|
||||
given().get(show).then().statusCode(200)
|
||||
.body("dayParts.type", contains("MORNING", "AFTERNOON", "EVENING"))
|
||||
.body("dayParts.danceGroupIds", everyItem(empty()));
|
||||
put(show, Map.of("name", "Summer show", "date", "2027-07-01"))
|
||||
.statusCode(200).body("date", equalTo("2027-07-01"));
|
||||
given().get(school + "/shows").then().statusCode(200).body("id", contains((int) showId));
|
||||
given().get(school).then().statusCode(200).body("dancerIds", contains((int) dancerId))
|
||||
.body("danceGroupIds", contains((int) groupId)).body("showIds", contains((int) showId));
|
||||
|
||||
for (String path : new String[]{dancer, group, show}) {
|
||||
given().delete(path).then().statusCode(204);
|
||||
given().get(path).then().statusCode(404);
|
||||
given().delete(path).then().statusCode(404);
|
||||
}
|
||||
given().get(school).then().body("dancerIds", empty()).body("danceGroupIds", empty()).body("showIds", empty());
|
||||
} finally {
|
||||
given().delete(school).then().statusCode(204);
|
||||
}
|
||||
given().get(school).then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void dancerCanBelongToMultipleGroupsAndDeletingDancerRemovesMemberships() {
|
||||
String school = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "Membership school"));
|
||||
try {
|
||||
long dancerId = create(school + "/dancers", DANCER);
|
||||
String first = school + "/dance-groups/" + create(school + "/dance-groups", GROUP);
|
||||
String second = school + "/dance-groups/" + create(school + "/dance-groups", GROUP);
|
||||
for (String group : new String[]{first, second}) {
|
||||
given().put(group + "/dancers/" + dancerId).then().statusCode(204);
|
||||
given().put(group + "/dancers/" + dancerId).then().statusCode(204);
|
||||
given().get(group).then().body("dancerIds", contains((int) dancerId));
|
||||
}
|
||||
given().delete(first + "/dancers/" + dancerId).then().statusCode(204);
|
||||
given().delete(first + "/dancers/" + dancerId).then().statusCode(204);
|
||||
given().get(first).then().body("dancerIds", empty());
|
||||
given().get(second).then().body("dancerIds", contains((int) dancerId));
|
||||
given().put(first + "/dancers/" + dancerId).then().statusCode(204);
|
||||
given().delete(school + "/dancers/" + dancerId).then().statusCode(204);
|
||||
given().get(first).then().body("dancerIds", empty());
|
||||
given().get(second).then().body("dancerIds", empty());
|
||||
} finally {
|
||||
given().delete(school).then().statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void isolatesSchoolsAndCascadesSchoolDeletion() {
|
||||
String first = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "First"));
|
||||
String second = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "Second"));
|
||||
try {
|
||||
long dancer = create(first + "/dancers", DANCER);
|
||||
long group = create(first + "/dance-groups", GROUP);
|
||||
long show = create(first + "/shows", SHOW);
|
||||
long otherGroup = create(second + "/dance-groups", GROUP);
|
||||
for (String path : new String[]{"/dancers/" + dancer, "/dance-groups/" + group, "/shows/" + show}) {
|
||||
given().get(second + path).then().statusCode(404);
|
||||
given().delete(second + path).then().statusCode(404);
|
||||
}
|
||||
put(second + "/dancers/" + dancer, DANCER).statusCode(404);
|
||||
put(second + "/dance-groups/" + group, GROUP).statusCode(404);
|
||||
put(second + "/shows/" + show, SHOW).statusCode(404);
|
||||
given().put(second + "/dance-groups/" + otherGroup + "/dancers/" + dancer).then().statusCode(404);
|
||||
given().get(second + "/dance-groups/" + otherGroup).then().body("dancerIds", empty());
|
||||
given().delete(first).then().statusCode(204);
|
||||
given().get(first + "/dancers").then().statusCode(404);
|
||||
given().get(first + "/dance-groups").then().statusCode(404);
|
||||
given().get(first + "/shows").then().statusCode(404);
|
||||
given().get(second).then().statusCode(200);
|
||||
} finally {
|
||||
given().delete(first);
|
||||
given().delete(second).then().statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsInvalidInputWithoutChangingStoredData() {
|
||||
given().contentType("application/json").body("{}").post(SCHOOLS).then().statusCode(400);
|
||||
given().contentType("application/json").body("null").post(SCHOOLS).then().statusCode(400);
|
||||
String school = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "Validation school"));
|
||||
try {
|
||||
put(school, Map.of("name", " ")).statusCode(400);
|
||||
given().get(school).then().body("name", equalTo("Validation school"));
|
||||
for (Object body : new Object[]{Map.of("name", "Sam"), Map.of("name", "Sam", "birthDate", "2999-01-01"),
|
||||
Map.of("name", "Sam", "birthDate", "not-a-date")}) {
|
||||
given().contentType("application/json").body(body).post(school + "/dancers").then().statusCode(400);
|
||||
}
|
||||
for (String body : new String[]{"{}", "{\"category\":\"Teens\",\"type\":\"INVALID\"}",
|
||||
"{\"category\":\"Teens\",\"type\":\"REGULAR\",\"startAge\":-1,\"endAge\":18,\"styles\":[]}",
|
||||
"{\"category\":\"Teens\",\"type\":\"REGULAR\",\"startAge\":18,\"endAge\":12,\"styles\":[]}"}) {
|
||||
given().contentType("application/json").body(body).post(school + "/dance-groups").then().statusCode(400);
|
||||
}
|
||||
long group = create(school + "/dance-groups", GROUP);
|
||||
put(school + "/dance-groups/" + group, Map.of("category", "Invalid", "type", "REGULAR",
|
||||
"startAge", 20, "endAge", 10, "styles", new String[]{})).statusCode(400);
|
||||
given().get(school + "/dance-groups/" + group).then().body("category", equalTo("Teens"));
|
||||
given().contentType("application/json").body(Map.of("name", "Show"))
|
||||
.post(school + "/shows").then().statusCode(400);
|
||||
given().get(school + "/dancers").then().body("$", empty());
|
||||
given().get(school + "/shows").then().body("$", empty());
|
||||
given().contentType("application/json").body(DANCER)
|
||||
.post(SCHOOLS + "/9223372036854775807/dancers").then().statusCode(404);
|
||||
} finally {
|
||||
given().delete(school).then().statusCode(204);
|
||||
}
|
||||
}
|
||||
|
||||
private static long create(String path, Object body) {
|
||||
return given().contentType("application/json").body(body).post(path).then()
|
||||
.statusCode(201).header("Location", matchesPattern(".*/[0-9]+$"))
|
||||
.extract().jsonPath().getLong("id");
|
||||
}
|
||||
|
||||
private static ValidatableResponse put(String path, Object body) {
|
||||
return given().contentType("application/json").body(body).put(path).then();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user