updated coding standards
All checks were successful
Build, test and publish / verify (push) Successful in 4m43s
All checks were successful
Build, test and publish / verify (push) Successful in 4m43s
This commit is contained in:
@@ -17,8 +17,7 @@ class ArchitectureTest {
|
||||
private static final String API_MODEL = ROOT + ".rest.apimodel";
|
||||
|
||||
// Exclude Lombok builders and compiler-generated enum-switch helper classes.
|
||||
@ArchTest
|
||||
static final ArchRule handwritten_types_are_top_level = classes()
|
||||
private @ArchTest static final ArchRule HANDWRITTEN_TYPES_ARE_TOP_LEVEL = classes()
|
||||
.that()
|
||||
.areNotAnnotatedWith("lombok.Generated")
|
||||
.and()
|
||||
@@ -26,8 +25,7 @@ class ArchitectureTest {
|
||||
.should()
|
||||
.beTopLevelClasses();
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule service_records_reside_in_service_model = classes()
|
||||
private @ArchTest static final ArchRule SERVICE_RECORDS_RESIDE_IN_SERVICE_MODEL = classes()
|
||||
.that()
|
||||
.resideInAPackage(ROOT + ".service..")
|
||||
.and()
|
||||
@@ -35,8 +33,7 @@ class ArchitectureTest {
|
||||
.should()
|
||||
.resideInAPackage(ROOT + ".service.model");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule service_interfaces_reside_in_service_port = classes()
|
||||
private @ArchTest static final ArchRule SERVICE_INTERFACES_RESIDE_IN_SERVICE_PORT = classes()
|
||||
.that()
|
||||
.resideInAPackage(ROOT + ".service..")
|
||||
.and()
|
||||
@@ -44,8 +41,7 @@ class ArchitectureTest {
|
||||
.should()
|
||||
.resideInAPackage(ROOT + ".service.port");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule scheduling_port_uses_domain_types = classes()
|
||||
private @ArchTest static final ArchRule SCHEDULING_PORT_USES_DOMAIN_TYPES = classes()
|
||||
.that()
|
||||
.resideInAPackage(ROOT + ".service.port..")
|
||||
.should()
|
||||
@@ -53,15 +49,13 @@ class ArchitectureTest {
|
||||
.resideInAnyPackage(ROOT + ".service.port..", ROOT + ".model..", "java..");
|
||||
|
||||
// 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()
|
||||
private @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()
|
||||
private @ArchTest static final ArchRule REST_RECORDS_RESIDE_IN_THE_API_MODEL_PACKAGE = classes()
|
||||
.that()
|
||||
.resideInAPackage(ROOT + ".rest..")
|
||||
.and()
|
||||
@@ -69,8 +63,7 @@ class ArchitectureTest {
|
||||
.should()
|
||||
.resideInAPackage(API_MODEL);
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule api_models_are_public_top_level_types = classes()
|
||||
private @ArchTest static final ArchRule API_MODELS_ARE_PUBLIC_TOP_LEVEL_TYPES = classes()
|
||||
.that()
|
||||
.resideInAPackage(API_MODEL + "..")
|
||||
.should()
|
||||
@@ -82,8 +75,7 @@ class ArchitectureTest {
|
||||
|
||||
// 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()
|
||||
private @ArchTest static final ArchRule LAYER_DEPENDENCIES = layeredArchitecture()
|
||||
.consideringOnlyDependenciesInLayers()
|
||||
.layer("Model")
|
||||
.definedBy(ROOT + ".model..")
|
||||
@@ -106,8 +98,7 @@ class ArchitectureTest {
|
||||
.whereLayer("Solver")
|
||||
.mayOnlyAccessLayers("Services", "Model");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule production_classes_have_an_architectural_home = classes()
|
||||
private @ArchTest static final ArchRule PRODUCTION_CLASSES_HAVE_AN_ARCHITECTURAL_HOME = classes()
|
||||
.should()
|
||||
.resideInAnyPackage(
|
||||
ROOT + ".model..",
|
||||
@@ -116,31 +107,27 @@ class ArchitectureTest {
|
||||
ROOT + ".rest..",
|
||||
ROOT + ".solver..");
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule model_is_independent_of_frameworks = classes()
|
||||
private @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()
|
||||
private @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()
|
||||
private @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 =
|
||||
private @ArchTest static final ArchRule PACKAGES_ARE_FREE_OF_CYCLES =
|
||||
slices().matching(ROOT + ".(*)..").should().beFreeOfCycles();
|
||||
}
|
||||
|
||||
@@ -8,8 +8,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
@QuarkusTest
|
||||
class HealthTest {
|
||||
@Test
|
||||
void applicationIsReady() {
|
||||
public @Test void applicationIsReady() {
|
||||
given().when().get("/q/health/ready").then().statusCode(200).body("status", equalTo("UP"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,28 @@
|
||||
package nl.agilecc.agiledancers;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import nl.agilecc.agiledancers.model.*;
|
||||
import java.util.EnumMap;
|
||||
import java.util.IntSummaryStatistics;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import nl.agilecc.agiledancers.model.DanceGroup;
|
||||
import nl.agilecc.agiledancers.model.DayPart;
|
||||
import nl.agilecc.agiledancers.model.Show;
|
||||
import nl.agilecc.agiledancers.model.scheduling.SchedulingInput;
|
||||
import nl.agilecc.agiledancers.model.scheduling.SchedulingResult;
|
||||
import nl.agilecc.agiledancers.model.specifications.*;
|
||||
import nl.agilecc.agiledancers.service.*;
|
||||
import nl.agilecc.agiledancers.service.model.*;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupType;
|
||||
import nl.agilecc.agiledancers.model.specifications.DayPartType;
|
||||
import nl.agilecc.agiledancers.service.ManagementException;
|
||||
import nl.agilecc.agiledancers.service.SchoolManagementService;
|
||||
import nl.agilecc.agiledancers.service.ShowConfiguration;
|
||||
import nl.agilecc.agiledancers.service.model.GroupDetails;
|
||||
import nl.agilecc.agiledancers.service.model.SchoolSnapshot;
|
||||
import nl.agilecc.agiledancers.solver.OrToolsSchedulingEngine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -16,9 +30,9 @@ class SchedulingTest {
|
||||
private final SchoolManagementService service = new SchoolManagementService();
|
||||
private final long school = service.createSchool("Studio").getId();
|
||||
|
||||
private List<DayPart> parts(int n) {
|
||||
private List<DayPart> parts(final int n) {
|
||||
return java.util.stream.IntStream.range(0, n)
|
||||
.mapToObj(i -> DayPart.builder()
|
||||
.mapToObj((final int i) -> DayPart.builder()
|
||||
.date(LocalDate.of(2027, 1, 1).plusDays(i))
|
||||
.type(DayPartType.AFTERNOON)
|
||||
.danceGroups(List.of())
|
||||
@@ -26,12 +40,12 @@ class SchedulingTest {
|
||||
.toList();
|
||||
}
|
||||
|
||||
private DanceGroup group(DanceGroupType type) {
|
||||
return service.createGroup(school, "Group", type, 0, 99, Set.of());
|
||||
private DanceGroup group(final DanceGroupType type) {
|
||||
return service.createGroup(school, new GroupDetails("Group", type, 0, 99, Set.of()));
|
||||
}
|
||||
|
||||
private void run(Show show) {
|
||||
var input = service.beginScheduling(school, show.getId());
|
||||
private void run(final Show show) {
|
||||
final SchedulingInput input = service.beginScheduling(school, show.getId());
|
||||
try {
|
||||
service.finishScheduling(school, input, new OrToolsSchedulingEngine().solve(input));
|
||||
} finally {
|
||||
@@ -39,41 +53,42 @@ class SchedulingTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void exactAppearancesAndOptimalBalanceIncludingEmptyParts() {
|
||||
public @Test void exactAppearancesAndOptimalBalanceIncludingEmptyParts() {
|
||||
group(DanceGroupType.PRE_SCHOOLER);
|
||||
var show = service.createShow(school, "Show", parts(6), null);
|
||||
final Show show = service.createShow(school, "Show", parts(6), null);
|
||||
run(show);
|
||||
var loads = show.getDayParts().stream()
|
||||
.mapToInt(p -> p.getDanceGroups().size())
|
||||
IntSummaryStatistics loads = show.getDayParts().stream()
|
||||
.mapToInt((final DayPart p) -> p.getDanceGroups().size())
|
||||
.summaryStatistics();
|
||||
assertEquals(1, loads.getSum());
|
||||
assertEquals(0, loads.getMin());
|
||||
assertEquals(1, loads.getMax());
|
||||
for (var type : DanceGroupType.values()) group(type);
|
||||
for (final DanceGroupType type : DanceGroupType.values()) {
|
||||
group(type);
|
||||
}
|
||||
run(show);
|
||||
for (var group : service.groups(school))
|
||||
for (final DanceGroup group : service.groups(school)) {
|
||||
assertEquals(
|
||||
show.getAppearances().get(group.getType()).longValue(),
|
||||
show.getDayParts().stream()
|
||||
.filter(p -> p.getDanceGroups().contains(group))
|
||||
.filter((final DayPart p) -> p.getDanceGroups().contains(group))
|
||||
.count());
|
||||
}
|
||||
loads = show.getDayParts().stream()
|
||||
.mapToInt(p -> p.getDanceGroups().size())
|
||||
.mapToInt((final DayPart p) -> p.getDanceGroups().size())
|
||||
.summaryStatistics();
|
||||
assertTrue(loads.getMax() - loads.getMin() <= 1);
|
||||
var snapshot = service.exportSchool(school);
|
||||
final SchoolSnapshot snapshot = service.exportSchool(school);
|
||||
assertEquals(snapshot, SchoolSnapshot.of(snapshot.restore()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultsInitialConfigurationAndPreservationOnEdits() {
|
||||
var show = service.createShow(school, "Show", List.of(), null);
|
||||
public @Test void defaultsInitialConfigurationAndPreservationOnEdits() {
|
||||
final Show show = service.createShow(school, "Show", List.of(), null);
|
||||
assertTrue(show.getAppearances().isEmpty());
|
||||
service.updateShow(school, show.getId(), "Show", parts(3), null);
|
||||
assertEquals(3, show.getAppearances().get(DanceGroupType.PRE_EDUCATION));
|
||||
assertEquals(1, show.getAppearances().get(DanceGroupType.PRE_SCHOOLER));
|
||||
var counts = new EnumMap<>(show.getAppearances());
|
||||
final var counts = new EnumMap<>(show.getAppearances());
|
||||
counts.put(DanceGroupType.PRE_SCHOOLER, 2);
|
||||
service.updateShow(school, show.getId(), "Show", parts(4), counts);
|
||||
assertEquals(counts, show.getAppearances());
|
||||
@@ -85,36 +100,35 @@ class SchedulingTest {
|
||||
assertEquals(counts, show.getAppearances());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsDuplicatePartsMissingCountsAndOutOfRangeCounts() {
|
||||
var duplicate = List.of(parts(1).getFirst(), parts(1).getFirst());
|
||||
public @Test void rejectsDuplicatePartsMissingCountsAndOutOfRangeCounts() {
|
||||
final List<DayPart> duplicate = List.of(parts(1).getFirst(), parts(1).getFirst());
|
||||
assertThrows(ManagementException.class, () -> service.createShow(school, "Show", duplicate, null));
|
||||
assertThrows(ManagementException.class, () -> service.createShow(school, "Show", parts(7), null));
|
||||
assertThrows(ManagementException.class, () -> service.createShow(school, "Show", parts(2), Map.of()));
|
||||
var counts = ShowConfiguration.defaults(2);
|
||||
final Map<DanceGroupType, Integer> counts = ShowConfiguration.defaults(2);
|
||||
counts.put(DanceGroupType.REGULAR, 3);
|
||||
assertThrows(ManagementException.class, () -> service.createShow(school, "Show", parts(2), counts));
|
||||
counts.put(DanceGroupType.REGULAR, 0);
|
||||
assertThrows(ManagementException.class, () -> service.createShow(school, "Show", parts(2), counts));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateRunsStaleResultsAndMalformedResultsPreserveAssignments() {
|
||||
var group = group(DanceGroupType.REGULAR);
|
||||
var show = service.createShow(school, "Show", parts(2), null);
|
||||
public @Test void duplicateRunsStaleResultsAndMalformedResultsPreserveAssignments() {
|
||||
final DanceGroup group = group(DanceGroupType.REGULAR);
|
||||
final Show show = service.createShow(school, "Show", parts(2), null);
|
||||
run(show);
|
||||
var before = show.getDayParts().stream().map(DayPart::getDanceGroups).toList();
|
||||
var input = service.beginScheduling(school, show.getId());
|
||||
final List<List<DanceGroup>> before =
|
||||
show.getDayParts().stream().map(DayPart::getDanceGroups).toList();
|
||||
final SchedulingInput input = service.beginScheduling(school, show.getId());
|
||||
assertThrows(ManagementException.class, () -> service.beginScheduling(school, show.getId()));
|
||||
var result = new OrToolsSchedulingEngine().solve(input);
|
||||
service.updateGroup(school, group.getId(), "Renamed", group.getType(), 0, 99, Set.of());
|
||||
final SchedulingResult result = new OrToolsSchedulingEngine().solve(input);
|
||||
service.updateGroup(school, group.getId(), new GroupDetails("Renamed", group.getType(), 0, 99, Set.of()));
|
||||
assertDoesNotThrow(() -> service.finishScheduling(school, input, result));
|
||||
group(DanceGroupType.PRE_SCHOOLER);
|
||||
assertThrows(ManagementException.class, () -> service.finishScheduling(school, input, result));
|
||||
assertEquals(
|
||||
before, show.getDayParts().stream().map(DayPart::getDanceGroups).toList());
|
||||
service.endScheduling(show.getId());
|
||||
var fresh = service.beginScheduling(school, show.getId());
|
||||
final SchedulingInput fresh = service.beginScheduling(school, show.getId());
|
||||
assertThrows(
|
||||
ManagementException.class,
|
||||
() -> service.finishScheduling(
|
||||
@@ -122,18 +136,17 @@ class SchedulingTest {
|
||||
assertEquals(
|
||||
before, show.getDayParts().stream().map(DayPart::getDanceGroups).toList());
|
||||
// Feasible results are accepted without requiring proof of optimality.
|
||||
var valid = new OrToolsSchedulingEngine().solve(fresh);
|
||||
final SchedulingResult valid = new OrToolsSchedulingEngine().solve(fresh);
|
||||
assertDoesNotThrow(
|
||||
() -> service.finishScheduling(school, fresh, new SchedulingResult(false, valid.assignments())));
|
||||
service.endScheduling(show.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void staleDaypartCountChangesAndDeletedShowsAreRejected() {
|
||||
public @Test void staleDaypartCountChangesAndDeletedShowsAreRejected() {
|
||||
group(DanceGroupType.REGULAR);
|
||||
var show = service.createShow(school, "Show", parts(2), null);
|
||||
var input = service.beginScheduling(school, show.getId());
|
||||
var result = new OrToolsSchedulingEngine().solve(input);
|
||||
final Show show = service.createShow(school, "Show", parts(2), null);
|
||||
final SchedulingInput input = service.beginScheduling(school, show.getId());
|
||||
final SchedulingResult result = new OrToolsSchedulingEngine().solve(input);
|
||||
service.updateShow(school, show.getId(), "Show", parts(3), null);
|
||||
assertThrows(ManagementException.class, () -> service.finishScheduling(school, input, result));
|
||||
service.deleteShow(school, show.getId());
|
||||
@@ -141,13 +154,12 @@ class SchedulingTest {
|
||||
service.endScheduling(show.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void needsGroupsAndDayparts() {
|
||||
var show = service.createShow(school, "Show", parts(1), null);
|
||||
public @Test void needsGroupsAndDayparts() {
|
||||
final Show show = service.createShow(school, "Show", parts(1), null);
|
||||
assertThrows(ManagementException.class, () -> service.beginScheduling(school, show.getId()));
|
||||
group(DanceGroupType.REGULAR);
|
||||
var empty = service.createShow(school, "Empty", List.of(), null);
|
||||
final Show empty = service.createShow(school, "Empty", List.of(), null);
|
||||
assertThrows(ManagementException.class, () -> service.beginScheduling(school, empty.getId()));
|
||||
assertTrue(show.getAppearances().values().stream().allMatch(n -> n == 1));
|
||||
assertTrue(show.getAppearances().values().stream().allMatch((final Integer n) -> n == 1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package nl.agilecc.agiledancers;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.everyItem;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.matchesPattern;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
@@ -17,59 +23,26 @@ class SchoolManagementResourceTest {
|
||||
private static final Map<String, Object> SHOW =
|
||||
Map.of("name", "Annual show", "dayParts", new Object[] {Map.of("date", "2027-06-12", "type", "AFTERNOON")});
|
||||
|
||||
@Test
|
||||
void managesSchoolAndItsResources() {
|
||||
long schoolId = create(SCHOOLS, Map.of("name", "Dance Studio"));
|
||||
String school = SCHOOLS + "/" + schoolId;
|
||||
public @Test void managesSchoolAndItsResources() {
|
||||
final long schoolId = create(SCHOOLS, Map.of("name", "Dance Studio"));
|
||||
final 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;
|
||||
final long dancerId = create(school + "/dancers", DANCER);
|
||||
final 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("AFTERNOON"))
|
||||
.body("dayParts.danceGroupIds", everyItem(empty()));
|
||||
put(show, Map.of("name", "Summer show", "dayParts", new Object[] {
|
||||
Map.of("date", "2027-07-01", "type", "EVENING")
|
||||
}))
|
||||
.statusCode(200)
|
||||
.body("dayParts.date", contains("2027-07-01"));
|
||||
given().get(school + "/shows").then().statusCode(200).body("id", contains((int) showId));
|
||||
final String group = manageGroup(school, dancerId);
|
||||
final long groupId = Long.parseLong(group.substring(group.lastIndexOf('/') + 1));
|
||||
final String show = manageShow(school);
|
||||
final long showId = Long.parseLong(show.substring(show.lastIndexOf('/') + 1));
|
||||
given().get(school)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
@@ -77,7 +50,7 @@ class SchoolManagementResourceTest {
|
||||
.body("danceGroupIds", contains((int) groupId))
|
||||
.body("showIds", contains((int) showId));
|
||||
|
||||
for (String path : new String[] {dancer, group, show}) {
|
||||
for (final 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);
|
||||
@@ -93,14 +66,13 @@ class SchoolManagementResourceTest {
|
||||
given().get(school).then().statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void dancerCanBelongToMultipleGroupsAndDeletingDancerRemovesMemberships() {
|
||||
String school = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "Membership school"));
|
||||
public @Test void dancerCanBelongToMultipleGroupsAndDeletingDancerRemovesMemberships() {
|
||||
final 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}) {
|
||||
final long dancerId = create(school + "/dancers", DANCER);
|
||||
final String first = school + "/dance-groups/" + create(school + "/dance-groups", GROUP);
|
||||
final String second = school + "/dance-groups/" + create(school + "/dance-groups", GROUP);
|
||||
for (final 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));
|
||||
@@ -118,16 +90,15 @@ class SchoolManagementResourceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void isolatesSchoolsAndCascadesSchoolDeletion() {
|
||||
String first = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "First"));
|
||||
String second = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "Second"));
|
||||
public @Test void isolatesSchoolsAndCascadesSchoolDeletion() {
|
||||
final String first = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "First"));
|
||||
final 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}) {
|
||||
final long dancer = create(first + "/dancers", DANCER);
|
||||
final long group = create(first + "/dance-groups", GROUP);
|
||||
final long show = create(first + "/shows", SHOW);
|
||||
final long otherGroup = create(second + "/dance-groups", GROUP);
|
||||
for (final 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);
|
||||
}
|
||||
@@ -149,42 +120,19 @@ class SchoolManagementResourceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsInvalidInputWithoutChangingStoredData() {
|
||||
public @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"));
|
||||
final 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);
|
||||
rejectsInvalidDancersAndGroups(school);
|
||||
final long group = create(school + "/dance-groups", GROUP);
|
||||
put(
|
||||
school + "/dance-groups/" + group,
|
||||
Map.of(
|
||||
@@ -217,25 +165,25 @@ class SchoolManagementResourceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void schedulesAndValidatesTheNewShowConfiguration() {
|
||||
String school = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "Scheduling school"));
|
||||
public @Test void schedulesAndValidatesTheNewShowConfiguration() {
|
||||
final String school = SCHOOLS + "/" + create(SCHOOLS, Map.of("name", "Scheduling school"));
|
||||
try {
|
||||
String show = school + "/shows/" + create(school + "/shows", SHOW);
|
||||
final String show = school + "/shows/" + create(school + "/shows", SHOW);
|
||||
given().post(show + "/schedule").then().statusCode(400);
|
||||
long group = create(school + "/dance-groups", GROUP);
|
||||
final long group = create(school + "/dance-groups", GROUP);
|
||||
given().post(show + "/schedule").then().statusCode(200).body("optimal", equalTo(true));
|
||||
given().get(show).then().body("dayParts[0].danceGroupIds", contains((int) group));
|
||||
String before = given().get(show).asString();
|
||||
String counts = "{\"SHOW_CASE\":1,\"PRE_EDUCATION\":1,\"SELECTION\":1,\"REGULAR\":%s,\"PRE_SCHOOLER\":1}";
|
||||
for (String count : new String[] {"0", "2", "1.5", "null"}) {
|
||||
String body =
|
||||
final String before = given().get(show).asString();
|
||||
final String counts =
|
||||
"{\"SHOW_CASE\":1,\"PRE_EDUCATION\":1,\"SELECTION\":1,\"REGULAR\":%s,\"PRE_SCHOOLER\":1}";
|
||||
for (final String count : new String[] {"0", "2", "1.5", "null"}) {
|
||||
final String body =
|
||||
"{\"name\":\"Show\",\"dayParts\":[{\"date\":\"2027-06-12\",\"type\":\"AFTERNOON\"}],\"appearances\":"
|
||||
+ counts.formatted(count) + "}";
|
||||
put(show, body).statusCode(400);
|
||||
given().get(show).then().body(equalTo(before));
|
||||
}
|
||||
String exported = given().get(school + "/export")
|
||||
final String exported = given().get(school + "/export")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
@@ -256,7 +204,77 @@ class SchoolManagementResourceTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static long create(String path, Object body) {
|
||||
private static String manageGroup(final String school, final long dancerId) {
|
||||
final long groupId = create(school + "/dance-groups", GROUP);
|
||||
final 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));
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
private static String manageShow(final String school) {
|
||||
final long showId = create(school + "/shows", SHOW);
|
||||
final String show = school + "/shows/" + showId;
|
||||
given().get(show)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("dayParts.type", contains("AFTERNOON"))
|
||||
.body("dayParts.danceGroupIds", everyItem(empty()));
|
||||
put(show, Map.of("name", "Summer show", "dayParts", new Object[] {
|
||||
Map.of("date", "2027-07-01", "type", "EVENING")
|
||||
}))
|
||||
.statusCode(200)
|
||||
.body("dayParts.date", contains("2027-07-01"));
|
||||
given().get(school + "/shows").then().statusCode(200).body("id", contains((int) showId));
|
||||
return show;
|
||||
}
|
||||
|
||||
private static void rejectsInvalidDancersAndGroups(final String school) {
|
||||
for (final 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 (final 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);
|
||||
}
|
||||
}
|
||||
|
||||
private static long create(final String path, final Object body) {
|
||||
return given().contentType("application/json")
|
||||
.body(body)
|
||||
.post(path)
|
||||
@@ -268,7 +286,7 @@ class SchoolManagementResourceTest {
|
||||
.getLong("id");
|
||||
}
|
||||
|
||||
private static ValidatableResponse put(String path, Object body) {
|
||||
private static ValidatableResponse put(final String path, final Object body) {
|
||||
return given().contentType("application/json").body(body).put(path).then();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package nl.agilecc.agiledancers;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.emptyOrNullString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -10,8 +13,7 @@ import org.junit.jupiter.api.Test;
|
||||
class SchoolSnapshotResourceTest {
|
||||
private static final String BASE = "/api/dance-schools";
|
||||
|
||||
@Test
|
||||
void downloadPreviewRestoreAndRejectInvalidFiles() {
|
||||
public @Test void downloadPreviewRestoreAndRejectInvalidFiles() {
|
||||
long id = given().contentType("application/json")
|
||||
.body("{\"name\":\"JSON school\"}")
|
||||
.post(BASE)
|
||||
@@ -20,7 +22,7 @@ class SchoolSnapshotResourceTest {
|
||||
.extract()
|
||||
.jsonPath()
|
||||
.getLong("id");
|
||||
String json = given().get(BASE + "/" + id + "/export")
|
||||
final String json = given().get(BASE + "/" + id + "/export")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.header("Content-Disposition", containsString(".json"))
|
||||
@@ -35,21 +37,7 @@ class SchoolSnapshotResourceTest {
|
||||
.statusCode(200)
|
||||
.body("existingId", equalTo((int) id))
|
||||
.body("dancers", equalTo(0));
|
||||
for (String bad : new String[] {
|
||||
"{",
|
||||
"null",
|
||||
json.replace("\"formatVersion\" : 2", "\"formatVersion\" : 1"),
|
||||
json.replace("\"name\" :", "\"unknown\" :"),
|
||||
json + " {}",
|
||||
json.replace("\"dancers\" : [ ]", "\"dancers\" : null")
|
||||
}) {
|
||||
given().contentType("application/json")
|
||||
.body(bad)
|
||||
.post(BASE + "/import?replaceId=" + id)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", not(emptyOrNullString()));
|
||||
}
|
||||
rejectInvalidFiles(id, json);
|
||||
given().get(BASE + "/" + id + "/export").then().body(equalTo(json));
|
||||
given().delete(BASE + "/" + id).then().statusCode(204);
|
||||
id = given().contentType("application/json")
|
||||
@@ -65,4 +53,22 @@ class SchoolSnapshotResourceTest {
|
||||
given().delete(BASE + "/" + id);
|
||||
}
|
||||
}
|
||||
|
||||
private static void rejectInvalidFiles(final long id, final String json) {
|
||||
for (final String bad : new String[] {
|
||||
"{",
|
||||
"null",
|
||||
json.replace("\"formatVersion\" : 2", "\"formatVersion\" : 1"),
|
||||
json.replace("\"name\" :", "\"unknown\" :"),
|
||||
json + " {}",
|
||||
json.replace("\"dancers\" : [ ]", "\"dancers\" : null")
|
||||
}) {
|
||||
given().contentType("application/json")
|
||||
.body(bad)
|
||||
.post(BASE + "/import?replaceId=" + id)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("message", not(emptyOrNullString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,30 @@
|
||||
package nl.agilecc.agiledancers;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import nl.agilecc.agiledancers.model.specifications.*;
|
||||
import nl.agilecc.agiledancers.service.*;
|
||||
import nl.agilecc.agiledancers.service.model.*;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import nl.agilecc.agiledancers.model.DanceGroup;
|
||||
import nl.agilecc.agiledancers.model.DanceSchool;
|
||||
import nl.agilecc.agiledancers.model.Dancer;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupStyle;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupType;
|
||||
import nl.agilecc.agiledancers.model.specifications.DayPartType;
|
||||
import nl.agilecc.agiledancers.service.ManagementException;
|
||||
import nl.agilecc.agiledancers.service.SchoolManagementService;
|
||||
import nl.agilecc.agiledancers.service.ShowConfiguration;
|
||||
import nl.agilecc.agiledancers.service.model.Group;
|
||||
import nl.agilecc.agiledancers.service.model.Part;
|
||||
import nl.agilecc.agiledancers.service.model.Performance;
|
||||
import nl.agilecc.agiledancers.service.model.Person;
|
||||
import nl.agilecc.agiledancers.service.model.SchoolSnapshot;
|
||||
import nl.agilecc.agiledancers.service.model.Trainer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SchoolSnapshotTest {
|
||||
@@ -35,26 +53,27 @@ class SchoolSnapshotTest {
|
||||
ShowConfiguration.defaults(1))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void roundTripAfterRestartPreservesAllFieldsAndSharedRelationships() {
|
||||
var snapshot = complete();
|
||||
var service = new SchoolManagementService();
|
||||
var existing = service.createSchool("Other");
|
||||
public @Test void roundTripAfterRestartPreservesAllFieldsAndSharedRelationships() {
|
||||
final SchoolSnapshot snapshot = complete();
|
||||
final var service = new SchoolManagementService();
|
||||
final DanceSchool existing = service.createSchool("Other");
|
||||
service.createDancer(existing.getId(), "Other dancer", LocalDate.of(2000, 1, 1));
|
||||
var imported = service.importSchool(snapshot, null);
|
||||
final DanceSchool imported = service.importSchool(snapshot, null);
|
||||
assertEquals(2, imported.getDanceGroups().size());
|
||||
var dancer = imported.getDancers().iterator().next();
|
||||
final Dancer dancer = imported.getDancers().iterator().next();
|
||||
imported.getDanceGroups()
|
||||
.forEach(g -> assertSame(dancer, g.getDancers().iterator().next()));
|
||||
.forEach((final DanceGroup g) ->
|
||||
assertSame(dancer, g.getDancers().iterator().next()));
|
||||
assertEquals(SchoolSnapshot.of(snapshot.restore()), service.exportSchool(imported.getId()));
|
||||
var restarted = new SchoolManagementService();
|
||||
var restored = restarted.importSchool(service.exportSchool(imported.getId()), null);
|
||||
final var restarted = new SchoolManagementService();
|
||||
final DanceSchool restored = restarted.importSchool(service.exportSchool(imported.getId()), null);
|
||||
assertEquals(SchoolSnapshot.of(snapshot.restore()), restarted.exportSchool(restored.getId()));
|
||||
// Membership removal must work after remapping hash-based sets.
|
||||
service.deleteDancer(imported.getId(), dancer.getId());
|
||||
imported.getDanceGroups().forEach(g -> assertTrue(g.getDancers().isEmpty()));
|
||||
imported.getDanceGroups()
|
||||
.forEach((final DanceGroup g) -> assertTrue(g.getDancers().isEmpty()));
|
||||
assertEquals(1, existing.getDancers().size());
|
||||
var group = imported.getDanceGroups().iterator().next();
|
||||
final DanceGroup group = imported.getDanceGroups().iterator().next();
|
||||
service.deleteGroup(imported.getId(), group.getId());
|
||||
assertEquals(
|
||||
1,
|
||||
@@ -68,19 +87,18 @@ class SchoolSnapshotTest {
|
||||
assertDoesNotThrow(() -> service.exportSchool(imported.getId()).restore());
|
||||
}
|
||||
|
||||
@Test
|
||||
void previewIsReadOnlyAndReplacementRemovesPreviousConfiguration() {
|
||||
var service = new SchoolManagementService();
|
||||
var snapshot = complete();
|
||||
public @Test void previewIsReadOnlyAndReplacementRemovesPreviousConfiguration() {
|
||||
final var service = new SchoolManagementService();
|
||||
final SchoolSnapshot snapshot = complete();
|
||||
assertNull(service.preview(snapshot).existingId());
|
||||
assertTrue(service.schools().isEmpty());
|
||||
var original = service.importSchool(snapshot, null);
|
||||
final DanceSchool original = service.importSchool(snapshot, null);
|
||||
service.createDancer(original.getId(), "Extra", LocalDate.of(2000, 1, 1));
|
||||
var other = service.createSchool(snapshot.name());
|
||||
final DanceSchool other = service.createSchool(snapshot.name());
|
||||
assertEquals(original.getId(), service.preview(snapshot).existingId());
|
||||
assertThrows(ManagementException.class, () -> service.importSchool(snapshot, null));
|
||||
assertEquals(2, original.getDancers().size());
|
||||
var replaced = service.importSchool(snapshot, original.getId());
|
||||
final DanceSchool replaced = service.importSchool(snapshot, original.getId());
|
||||
assertEquals(SchoolSnapshot.of(snapshot.restore()), service.exportSchool(replaced.getId()));
|
||||
assertSame(other, service.school(other.getId()));
|
||||
service.deleteSchool(replaced.getId());
|
||||
@@ -89,23 +107,21 @@ class SchoolSnapshotTest {
|
||||
service.exportSchool(service.importSchool(snapshot, null).getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unresolvedReferenceLeavesExistingAndOtherSchoolsIntact() {
|
||||
var service = new SchoolManagementService();
|
||||
var valid = complete();
|
||||
var original = service.importSchool(valid, null);
|
||||
var bad = new SchoolSnapshot(
|
||||
public @Test void unresolvedReferenceLeavesExistingAndOtherSchoolsIntact() {
|
||||
final var service = new SchoolManagementService();
|
||||
final SchoolSnapshot valid = complete();
|
||||
final DanceSchool original = service.importSchool(valid, null);
|
||||
final var bad = new SchoolSnapshot(
|
||||
2, valid.key(), valid.name(), List.of(), valid.coaches(), valid.groups(), valid.shows());
|
||||
assertThrows(ManagementException.class, () -> service.preview(bad));
|
||||
assertThrows(ManagementException.class, () -> service.importSchool(bad, original.getId()));
|
||||
assertEquals(SchoolSnapshot.of(valid.restore()), service.exportSchool(original.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptySchoolRoundTrips() {
|
||||
var service = new SchoolManagementService();
|
||||
var empty = service.createSchool("Empty");
|
||||
var snapshot = service.exportSchool(empty.getId());
|
||||
public @Test void emptySchoolRoundTrips() {
|
||||
final var service = new SchoolManagementService();
|
||||
final DanceSchool empty = service.createSchool("Empty");
|
||||
final SchoolSnapshot snapshot = service.exportSchool(empty.getId());
|
||||
service.deleteSchool(empty.getId());
|
||||
assertEquals(
|
||||
SchoolSnapshot.of(snapshot.restore()),
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package nl.agilecc.agiledancers;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import nl.agilecc.agiledancers.model.DanceGroup;
|
||||
import nl.agilecc.agiledancers.model.scheduling.SchedulingResult;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupStyle;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupType;
|
||||
import nl.agilecc.agiledancers.rest.apimodel.GroupRequest;
|
||||
import nl.agilecc.agiledancers.rest.apimodel.ShowRequest;
|
||||
import nl.agilecc.agiledancers.service.DanceGroupService;
|
||||
import nl.agilecc.agiledancers.service.model.Group;
|
||||
import nl.agilecc.agiledancers.service.model.GroupDetails;
|
||||
import nl.agilecc.agiledancers.service.model.Person;
|
||||
import nl.agilecc.agiledancers.service.model.SchoolSnapshot;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SnapshotCollectionsTest {
|
||||
@Test
|
||||
public void snapshotCopiesCallerOwnedCollectionsAndRetainsInvalidInputForValidation() {
|
||||
final List<Person> dancers = new ArrayList<>();
|
||||
final SchoolSnapshot snapshot =
|
||||
new SchoolSnapshot(2, UUID.randomUUID(), "School", dancers, List.of(), List.of(), List.of());
|
||||
dancers.add(new Person("1", "Dancer", null));
|
||||
assertEquals(List.of(), snapshot.dancers());
|
||||
assertThrows(
|
||||
UnsupportedOperationException.class, () -> snapshot.dancers().add(null));
|
||||
final SchoolSnapshot invalid =
|
||||
new SchoolSnapshot(2, UUID.randomUUID(), "School", null, List.of(), List.of(), List.of());
|
||||
assertNull(invalid.dancers());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void schedulingResultCopiesBothCollectionLevelsAndPreservesMalformedSolverResults() {
|
||||
final List<Long> part = new ArrayList<>(List.of(1L));
|
||||
final List<List<Long>> assignments = new ArrayList<>(List.of(part));
|
||||
final SchedulingResult result = new SchedulingResult(true, assignments);
|
||||
part.clear();
|
||||
assignments.clear();
|
||||
assertEquals(List.of(List.of(1L)), result.assignments());
|
||||
assertThrows(
|
||||
UnsupportedOperationException.class,
|
||||
() -> result.assignments().getFirst().clear());
|
||||
assertNull(new SchedulingResult(false, null).assignments());
|
||||
final List<List<Long>> invalid = new ArrayList<>();
|
||||
invalid.add(null);
|
||||
assertNull(new SchedulingResult(false, invalid).assignments().getFirst());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestCollectionsRetainTheirJsonValuesAndCannotBeMutatedThroughAliases() {
|
||||
final Map<DanceGroupType, Integer> counts = new LinkedHashMap<>(Map.of(DanceGroupType.REGULAR, 1));
|
||||
final ShowRequest show = new ShowRequest("Show", List.of(), counts);
|
||||
counts.clear();
|
||||
assertEquals(1, show.appearances().get(DanceGroupType.REGULAR));
|
||||
assertThrows(
|
||||
UnsupportedOperationException.class, () -> show.appearances().clear());
|
||||
final var styles = new LinkedHashSet<>(List.of(DanceGroupStyle.JAZZ));
|
||||
final GroupRequest group = new GroupRequest("Group", DanceGroupType.REGULAR, 0, 99, styles);
|
||||
styles.clear();
|
||||
assertEquals(1, group.styles().size());
|
||||
assertThrows(UnsupportedOperationException.class, () -> group.styles().clear());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupCopiesBothListsWithoutChangingNullableSnapshotValues() {
|
||||
final List<DanceGroupStyle> styles = new ArrayList<>(List.of(DanceGroupStyle.JAZZ));
|
||||
final List<String> dancerIds = new ArrayList<>(List.of("dancer-1"));
|
||||
final Group group = new Group("group-1", "Group", DanceGroupType.REGULAR, 0, 99, styles, dancerIds, null);
|
||||
styles.clear();
|
||||
dancerIds.clear();
|
||||
assertEquals(List.of(DanceGroupStyle.JAZZ), group.styles());
|
||||
assertEquals(List.of("dancer-1"), group.dancerIds());
|
||||
assertThrows(UnsupportedOperationException.class, () -> group.styles().clear());
|
||||
assertThrows(
|
||||
UnsupportedOperationException.class, () -> group.dancerIds().clear());
|
||||
final Group invalid = new Group("group-2", "Group", DanceGroupType.REGULAR, 0, 99, null, null, null);
|
||||
assertNull(invalid.styles());
|
||||
assertNull(invalid.dancerIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupDetailsCopiesTheStylesPassedAcrossTheServiceBoundary() {
|
||||
final var styles = new LinkedHashSet<>(List.of(DanceGroupStyle.JAZZ));
|
||||
final GroupDetails details = new GroupDetails("Group", DanceGroupType.REGULAR, 0, 99, styles);
|
||||
styles.clear();
|
||||
assertEquals(1, details.styles().size());
|
||||
assertThrows(UnsupportedOperationException.class, () -> details.styles().clear());
|
||||
final DanceGroup group = new DanceGroupService().add(details, new LinkedHashSet<>(), null);
|
||||
group.addStyle(DanceGroupStyle.HIPHOP);
|
||||
assertEquals(2, group.getStyles().size());
|
||||
assertEquals(1, details.styles().size());
|
||||
assertNull(new GroupDetails("Group", DanceGroupType.REGULAR, 0, 99, null).styles());
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,36 @@
|
||||
package nl.agilecc.agiledancers.service;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import nl.agilecc.agiledancers.model.*;
|
||||
import nl.agilecc.agiledancers.model.specifications.*;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import nl.agilecc.agiledancers.model.DayPart;
|
||||
import nl.agilecc.agiledancers.model.Show;
|
||||
import nl.agilecc.agiledancers.model.scheduling.SchedulingInput;
|
||||
import nl.agilecc.agiledancers.model.scheduling.SchedulingResult;
|
||||
import nl.agilecc.agiledancers.model.specifications.DanceGroupType;
|
||||
import nl.agilecc.agiledancers.model.specifications.DayPartType;
|
||||
import nl.agilecc.agiledancers.service.model.GroupDetails;
|
||||
import nl.agilecc.agiledancers.service.model.SchoolSnapshot;
|
||||
import nl.agilecc.agiledancers.service.port.SchedulingEngine;
|
||||
import nl.agilecc.agiledancers.solver.OrToolsSchedulingEngine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SchedulingServiceTest {
|
||||
@Test
|
||||
void failedAndNoResultRunsPreserveAssignmentsAndReleaseTheRunGuard() {
|
||||
var schools = new SchoolManagementService();
|
||||
long school = schools.createSchool("School").getId();
|
||||
schools.createGroup(school, "Group", DanceGroupType.REGULAR, 0, 99, Set.of());
|
||||
var show = schools.createShow(
|
||||
public @Test void failedAndNoResultRunsPreserveAssignmentsAndReleaseTheRunGuard() {
|
||||
final var schools = new SchoolManagementService();
|
||||
final long school = schools.createSchool("School").getId();
|
||||
schools.createGroup(school, new GroupDetails("Group", DanceGroupType.REGULAR, 0, 99, Set.of()));
|
||||
final Show show = schools.createShow(
|
||||
school,
|
||||
"Show",
|
||||
List.of(DayPart.builder()
|
||||
@@ -26,34 +39,31 @@ class SchedulingServiceTest {
|
||||
.danceGroups(List.of())
|
||||
.build()),
|
||||
null);
|
||||
var scheduling = new SchedulingService();
|
||||
scheduling.schools = schools;
|
||||
scheduling.engine = new OrToolsSchedulingEngine();
|
||||
final var scheduling = new SchedulingService(schools, new OrToolsSchedulingEngine());
|
||||
scheduling.schedule(school, show.getId());
|
||||
var before = schools.exportSchool(school);
|
||||
for (SchedulingEngine failure : List.<SchedulingEngine>of(
|
||||
input -> {
|
||||
final SchoolSnapshot before = schools.exportSchool(school);
|
||||
for (final SchedulingEngine failure : List.<SchedulingEngine>of(
|
||||
(final SchedulingInput input) -> {
|
||||
throw new IllegalStateException("Native failure");
|
||||
},
|
||||
input -> {
|
||||
(final SchedulingInput input) -> {
|
||||
throw new ManagementException(
|
||||
ManagementExceptionKind.INVALID_INPUT, "No valid result before the limit");
|
||||
},
|
||||
input -> null)) {
|
||||
scheduling.engine = failure;
|
||||
assertThrows(ManagementException.class, () -> scheduling.schedule(school, show.getId()));
|
||||
(final SchedulingInput input) -> null)) {
|
||||
final var failedScheduling = new SchedulingService(schools, failure);
|
||||
assertThrows(ManagementException.class, () -> failedScheduling.schedule(school, show.getId()));
|
||||
assertEquals(before, schools.exportSchool(school));
|
||||
var next = schools.beginScheduling(school, show.getId());
|
||||
final SchedulingInput next = schools.beginScheduling(school, show.getId());
|
||||
schools.endScheduling(next.showId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentRunIsRejectedWhileEditsRemainPossibleAndInvalidateTheFirstResult() throws Exception {
|
||||
var schools = new SchoolManagementService();
|
||||
long school = schools.createSchool("School").getId();
|
||||
schools.createGroup(school, "Group", DanceGroupType.REGULAR, 0, 99, Set.of());
|
||||
var show = schools.createShow(
|
||||
public @Test void concurrentRunIsRejectedWhileEditsRemainPossibleAndInvalidateTheFirstResult() throws Exception {
|
||||
final var schools = new SchoolManagementService();
|
||||
final long school = schools.createSchool("School").getId();
|
||||
schools.createGroup(school, new GroupDetails("Group", DanceGroupType.REGULAR, 0, 99, Set.of()));
|
||||
final Show show = schools.createShow(
|
||||
school,
|
||||
"Show",
|
||||
List.of(DayPart.builder()
|
||||
@@ -62,39 +72,45 @@ class SchedulingServiceTest {
|
||||
.danceGroups(List.of())
|
||||
.build()),
|
||||
null);
|
||||
var scheduling = new SchedulingService();
|
||||
scheduling.schools = schools;
|
||||
var entered = new CountDownLatch(1);
|
||||
var release = new CountDownLatch(1);
|
||||
scheduling.engine = input -> {
|
||||
entered.countDown();
|
||||
try {
|
||||
if (!release.await(10, TimeUnit.SECONDS)) throw new IllegalStateException("Test timeout");
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return new OrToolsSchedulingEngine().solve(input);
|
||||
};
|
||||
try (var executor = Executors.newSingleThreadExecutor()) {
|
||||
var first = executor.submit(() -> scheduling.schedule(school, show.getId()));
|
||||
final var entered = new CountDownLatch(1);
|
||||
final var release = new CountDownLatch(1);
|
||||
final SchedulingEngine engine = blockingEngine(entered, release);
|
||||
final var scheduling = new SchedulingService(schools, engine);
|
||||
try (final java.util.concurrent.ExecutorService executor = Executors.newSingleThreadExecutor()) {
|
||||
final Future<SchedulingResult> first = executor.submit(() -> scheduling.schedule(school, show.getId()));
|
||||
try {
|
||||
assertTrue(entered.await(10, TimeUnit.SECONDS));
|
||||
assertEquals(
|
||||
ManagementExceptionKind.CONFLICT,
|
||||
assertThrows(ManagementException.class, () -> scheduling.schedule(school, show.getId()))
|
||||
.getKind());
|
||||
schools.createGroup(school, "New group", DanceGroupType.REGULAR, 0, 99, Set.of());
|
||||
schools.createGroup(school, new GroupDetails("New group", DanceGroupType.REGULAR, 0, 99, Set.of()));
|
||||
} finally {
|
||||
release.countDown();
|
||||
}
|
||||
var exception = assertThrows(ExecutionException.class, () -> first.get(10, TimeUnit.SECONDS));
|
||||
final ExecutionException exception =
|
||||
assertThrows(ExecutionException.class, () -> first.get(10, TimeUnit.SECONDS));
|
||||
assertInstanceOf(ManagementException.class, exception.getCause());
|
||||
assertTrue(exception.getCause().getMessage().contains("inputs changed"));
|
||||
assertTrue(show.getDayParts().getFirst().getDanceGroups().isEmpty());
|
||||
}
|
||||
scheduling.engine = new OrToolsSchedulingEngine();
|
||||
scheduling.schedule(school, show.getId());
|
||||
new SchedulingService(schools, new OrToolsSchedulingEngine()).schedule(school, show.getId());
|
||||
assertEquals(2, show.getDayParts().getFirst().getDanceGroups().size());
|
||||
}
|
||||
|
||||
private static SchedulingEngine blockingEngine(final CountDownLatch entered, final CountDownLatch release) {
|
||||
final SchedulingEngine engine = (final SchedulingInput input) -> {
|
||||
entered.countDown();
|
||||
try {
|
||||
if (!release.await(10, TimeUnit.SECONDS)) {
|
||||
throw new IllegalStateException("Test timeout");
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
return new OrToolsSchedulingEngine().solve(input);
|
||||
};
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user