Skip to content

Commit b2df229

Browse files
Improved formatting of failure messages
Reinstated narrower checking of syspart. validation
1 parent ac7564a commit b2df229

13 files changed

+151
-87
lines changed

src/main/java/edu/ie3/datamodel/exceptions/FailedValidationException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public FailedValidationException(String message) {
1717
super(message);
1818
}
1919

20+
/** @param exceptions List of exceptions, which must not be empty */
2021
public FailedValidationException(List<? extends Exception> exceptions) {
21-
super("Validation failed due to: " + ExceptionUtils.getMessages(exceptions));
22+
super(
23+
"Validation failed due to: \n" + ExceptionUtils.getMessages(exceptions), exceptions.get(0));
2224
}
2325
}

src/main/java/edu/ie3/datamodel/exceptions/InvalidEntityException.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ public class InvalidEntityException extends ValidationException {
1313
private static final long serialVersionUID = 809496087520306374L;
1414

1515
public InvalidEntityException(String faultDescription, UniqueEntity invalidEntity) {
16-
super("Entity is invalid because of: " + faultDescription + " [" + invalidEntity + "]");
16+
super("Entity is invalid because of: \n" + faultDescription + " [" + invalidEntity + "]");
1717
}
1818

1919
public InvalidEntityException(
2020
String faultDescription, Throwable cause, UniqueEntity invalidEntity) {
21-
super("Entity is invalid because of: " + faultDescription + " [" + invalidEntity + "]", cause);
21+
super(
22+
"Entity is invalid because of: \n" + faultDescription + " [" + invalidEntity + "]", cause);
2223
}
2324

2425
public InvalidEntityException(String message, Throwable cause) {

src/main/java/edu/ie3/datamodel/utils/ExceptionUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private ExceptionUtils() {
2121
public static String getMessages(List<? extends Exception> exceptions) {
2222
return exceptions.stream()
2323
.map(Throwable::getMessage)
24-
.reduce("", (a, b) -> a + ", " + b)
25-
.replaceFirst(", ", "");
24+
.reduce("", (a, b) -> a + "\n " + b)
25+
.replaceFirst("\n ", "");
2626
}
2727
}

src/main/java/edu/ie3/datamodel/utils/Try.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ public static <E extends Exception> Try<Void, E> ofVoid(
7272
* @return a {@link Try}
7373
* @param <E> type of exception
7474
*/
75-
public static <E extends Exception> Try<Void, E> ofVoid(boolean failure, E exception) {
75+
public static <E extends Exception> Try<Void, E> ofVoid(
76+
boolean failure, ExceptionSupplier<E> exception) {
7677
if (failure) {
77-
return Failure.ofVoid(exception);
78+
return Failure.ofVoid(exception.get());
7879
} else {
7980
return Success.empty();
8081
}
@@ -418,4 +419,14 @@ public interface TrySupplier<T, E extends Exception> {
418419
public interface VoidSupplier<E extends Exception> {
419420
void get() throws E;
420421
}
422+
423+
/**
424+
* Supplier for exceptions.
425+
*
426+
* @param <E> type of exception that could be thrown
427+
*/
428+
@FunctionalInterface
429+
public interface ExceptionSupplier<E extends Exception> {
430+
E get();
431+
}
421432
}

src/main/java/edu/ie3/datamodel/utils/validation/ConnectorValidationUtils.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,19 @@ private static List<Try<Void, InvalidEntityException>> checkTransformer3W(
259259
transformer3W.getNodeA().getVoltLvl() == transformer3W.getNodeB().getVoltLvl()
260260
|| transformer3W.getNodeA().getVoltLvl() == transformer3W.getNodeC().getVoltLvl()
261261
|| transformer3W.getNodeB().getVoltLvl() == transformer3W.getNodeC().getVoltLvl(),
262-
new InvalidEntityException(
263-
"Transformer connects nodes of the same voltage level", transformer3W)));
262+
() ->
263+
new InvalidEntityException(
264+
"Transformer connects nodes of the same voltage level", transformer3W)));
264265

265266
// Check if transformer connects different subnets
266267
exceptions.add(
267268
Try.ofVoid(
268269
transformer3W.getNodeA().getSubnet() == transformer3W.getNodeB().getSubnet()
269270
|| transformer3W.getNodeA().getSubnet() == transformer3W.getNodeC().getSubnet()
270271
|| transformer3W.getNodeB().getSubnet() == transformer3W.getNodeC().getSubnet(),
271-
new InvalidEntityException(
272-
"Transformer connects nodes in the same subnet", transformer3W)));
272+
() ->
273+
new InvalidEntityException(
274+
"Transformer connects nodes in the same subnet", transformer3W)));
273275

274276
exceptions.add(
275277
Try.ofVoid(
@@ -347,7 +349,9 @@ protected static List<Try<Void, InvalidEntityException>> checkTransformer3WType(
347349
private static Try<Void, InvalidEntityException> checkSwitch(SwitchInput switchInput) {
348350
return Try.ofVoid(
349351
!switchInput.getNodeA().getVoltLvl().equals(switchInput.getNodeB().getVoltLvl()),
350-
new InvalidEntityException("Switch connects two different voltage levels", switchInput));
352+
() ->
353+
new InvalidEntityException(
354+
"Switch connects two different voltage levels", switchInput));
351355
/* Remark: Connecting two different "subnets" is fine, because as of our definition regarding a switchgear in
352356
* "upstream" direction of a transformer, all the nodes, that hare within the switch chain, belong to the lower
353357
* grid, whilst the "real" upper node is within the upper grid */
@@ -393,10 +397,11 @@ protected static Try<Void, ValidationException> checkConnectivity(
393397

394398
return Try.ofVoid(
395399
!inspector.isConnected(),
396-
new InvalidGridException(
397-
"The grid with subnetNo "
398-
+ subGridContainer.getSubnet()
399-
+ " is not connected! Please ensure that all elements are connected correctly!"));
400+
() ->
401+
new InvalidGridException(
402+
"The grid with subnetNo "
403+
+ subGridContainer.getSubnet()
404+
+ " is not connected! Please ensure that all elements are connected correctly!"));
400405
}
401406

402407
/**
@@ -408,9 +413,11 @@ private static Try<Void, InvalidEntityException> connectsDifferentNodes(
408413
ConnectorInput connectorInput) {
409414
return Try.ofVoid(
410415
connectorInput.getNodeA().equals(connectorInput.getNodeB()),
411-
new InvalidEntityException(
412-
connectorInput.getClass().getSimpleName() + " connects the same node, but shouldn't",
413-
connectorInput));
416+
() ->
417+
new InvalidEntityException(
418+
connectorInput.getClass().getSimpleName()
419+
+ " connects the same node, but shouldn't",
420+
connectorInput));
414421
}
415422

416423
/**

src/main/java/edu/ie3/datamodel/utils/validation/GraphicValidationUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ protected static List<Try<Void, InvalidEntityException>> check(GraphicInput grap
4444
exceptions.add(
4545
Try.ofVoid(
4646
graphicInput.getGraphicLayer() == null,
47-
new InvalidEntityException(
48-
"Graphic Layer of graphic element is not defined", graphicInput)));
47+
() ->
48+
new InvalidEntityException(
49+
"Graphic Layer of graphic element is not defined", graphicInput)));
4950

5051
// Further checks for subclasses
5152
if (LineGraphicInput.class.isAssignableFrom(graphicInput.getClass())) {
@@ -67,8 +68,9 @@ private static Try<Void, InvalidEntityException> checkLineGraphicInput(
6768
LineGraphicInput lineGraphicInput) {
6869
return Try.ofVoid(
6970
lineGraphicInput.getPath() == null,
70-
new InvalidEntityException(
71-
"Path of line graphic element is not defined", lineGraphicInput));
71+
() ->
72+
new InvalidEntityException(
73+
"Path of line graphic element is not defined", lineGraphicInput));
7274
}
7375

7476
/**
@@ -82,6 +84,6 @@ private static Try<Void, InvalidEntityException> checkNodeGraphicInput(
8284
NodeGraphicInput nodeGraphicInput) {
8385
return Try.ofVoid(
8486
nodeGraphicInput.getPoint() == null,
85-
new InvalidEntityException("Point of node graphic is not defined", nodeGraphicInput));
87+
() -> new InvalidEntityException("Point of node graphic is not defined", nodeGraphicInput));
8688
}
8789
}

src/main/java/edu/ie3/datamodel/utils/validation/GridContainerValidationUtils.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ private GridContainerValidationUtils() {
6262
exceptions.add(
6363
Try.ofVoid(
6464
exceptionString.isPresent(),
65-
new InvalidGridException(
66-
duplicateUuidsString(gridContainer.getClass().getSimpleName(), exceptionString))));
65+
() ->
66+
new InvalidGridException(
67+
duplicateUuidsString(
68+
gridContainer.getClass().getSimpleName(), exceptionString))));
6769

6870
exceptions.addAll(checkRawGridElements(gridContainer.getRawGrid()));
6971
exceptions.addAll(
@@ -106,9 +108,10 @@ private GridContainerValidationUtils() {
106108
exceptions.add(
107109
Try.ofVoid(
108110
exceptionString.isPresent(),
109-
new InvalidGridException(
110-
duplicateUuidsString(
111-
rawGridElements.getClass().getSimpleName(), exceptionString))));
111+
() ->
112+
new InvalidGridException(
113+
duplicateUuidsString(
114+
rawGridElements.getClass().getSimpleName(), exceptionString))));
112115

113116
/* Checking nodes */
114117
Set<NodeInput> nodes = rawGridElements.getNodes();
@@ -225,9 +228,10 @@ protected static List<Try<Void, UnsafeEntityException>> checkRawGridTypeIds(
225228
exceptions.add(
226229
Try.ofVoid(
227230
exceptionString.isPresent(),
228-
new InvalidGridException(
229-
duplicateUuidsString(
230-
systemParticipants.getClass().getSimpleName(), exceptionString))));
231+
() ->
232+
new InvalidGridException(
233+
duplicateUuidsString(
234+
systemParticipants.getClass().getSimpleName(), exceptionString))));
231235

232236
exceptions.addAll(checkSystemParticipants(systemParticipants.getBmPlants(), nodes));
233237
exceptions.addAll(checkSystemParticipants(systemParticipants.getChpPlants(), nodes));
@@ -318,9 +322,10 @@ protected static List<Try<Void, UnsafeEntityException>> checkSystemParticipantsT
318322
exceptions.add(
319323
Try.ofVoid(
320324
exceptionString.isPresent(),
321-
new InvalidGridException(
322-
duplicateUuidsString(
323-
graphicElements.getClass().getSimpleName(), exceptionString))));
325+
() ->
326+
new InvalidGridException(
327+
duplicateUuidsString(
328+
graphicElements.getClass().getSimpleName(), exceptionString))));
324329

325330
graphicElements
326331
.getNodeGraphics()
@@ -330,7 +335,9 @@ protected static List<Try<Void, UnsafeEntityException>> checkSystemParticipantsT
330335
exceptions.add(
331336
Try.ofVoid(
332337
!nodes.contains(graphic.getNode()),
333-
buildGraphicExceptionMessage(graphic, "node", graphic.getNode().getUuid())));
338+
() ->
339+
buildGraphicExceptionMessage(
340+
graphic, "node", graphic.getNode().getUuid())));
334341
});
335342

336343
graphicElements
@@ -341,7 +348,9 @@ protected static List<Try<Void, UnsafeEntityException>> checkSystemParticipantsT
341348
exceptions.add(
342349
Try.ofVoid(
343350
!lines.contains(graphic.getLine()),
344-
buildGraphicExceptionMessage(graphic, "line", graphic.getLine().getUuid())));
351+
() ->
352+
buildGraphicExceptionMessage(
353+
graphic, "line", graphic.getLine().getUuid())));
345354
});
346355

347356
return exceptions;
@@ -381,11 +390,12 @@ private static Try<Void, InvalidGridException> checkNodeAvailability(
381390

382391
return Try.ofVoid(
383392
available,
384-
new InvalidGridException(
385-
input.getClass().getSimpleName()
386-
+ " "
387-
+ input
388-
+ " is connected to a node that is not in the set of nodes."));
393+
() ->
394+
new InvalidGridException(
395+
input.getClass().getSimpleName()
396+
+ " "
397+
+ input
398+
+ " is connected to a node that is not in the set of nodes."));
389399
}
390400

391401
/**

src/main/java/edu/ie3/datamodel/utils/validation/MeasurementUnitValidationUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ private MeasurementUnitValidationUtils() {
3939
&& !measurementUnit.getQ()
4040
&& !measurementUnit.getVAng()
4141
&& !measurementUnit.getVMag(),
42-
new UnsafeEntityException("Measurement Unit does not measure any values", measurementUnit));
42+
() ->
43+
new UnsafeEntityException(
44+
"Measurement Unit does not measure any values", measurementUnit));
4345
}
4446
}

src/main/java/edu/ie3/datamodel/utils/validation/NodeValidationUtils.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ private NodeValidationUtils() {
5858
node.getvTarget()
5959
.isLessThanOrEqualTo(
6060
Quantities.getQuantity(0, StandardUnits.TARGET_VOLTAGE_MAGNITUDE)),
61-
new InvalidEntityException("Target voltage (p.u.) is not a positive value", node)));
61+
() ->
62+
new InvalidEntityException("Target voltage (p.u.) is not a positive value", node)));
6263
exceptions.add(
6364
Try.ofVoid(
6465
node.getvTarget()
6566
.isGreaterThan(Quantities.getQuantity(2, StandardUnits.TARGET_VOLTAGE_MAGNITUDE)),
66-
new UnsafeEntityException("Target voltage (p.u.) might be too high", node)));
67+
() -> new UnsafeEntityException("Target voltage (p.u.) might be too high", node)));
6768
exceptions.add(
6869
Try.ofVoid(
6970
node.getSubnet() <= 0,
70-
new InvalidEntityException("Subnet can't be zero or negative", node)));
71+
() -> new InvalidEntityException("Subnet can't be zero or negative", node)));
7172
exceptions.add(
7273
Try.ofVoid(
7374
node.getGeoPosition() == null,
74-
new InvalidEntityException("GeoPosition of node is null", node)));
75+
() -> new InvalidEntityException("GeoPosition of node is null", node)));
7576

7677
return exceptions;
7778
}

src/main/java/edu/ie3/datamodel/utils/validation/SystemParticipantValidationUtils.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ protected static List<Try<Void, InvalidEntityException>> check(
5656
exceptions.add(
5757
Try.ofVoid(
5858
systemParticipant.getqCharacteristics() == null,
59-
new InvalidEntityException(
60-
"Reactive power characteristics of system participant is not defined",
61-
systemParticipant)));
59+
() ->
60+
new InvalidEntityException(
61+
"Reactive power characteristics of system participant is not defined",
62+
systemParticipant)));
6263

6364
// Further checks for subclasses
6465
if (BmInput.class.isAssignableFrom(systemParticipant.getClass())) {
@@ -124,8 +125,9 @@ protected static List<Try<Void, InvalidEntityException>> checkType(
124125
(systemParticipantTypeInput.getCapex() == null)
125126
|| (systemParticipantTypeInput.getOpex() == null)
126127
|| (systemParticipantTypeInput.getsRated() == null),
127-
new InvalidEntityException(
128-
"At least one of capex, opex, or sRated is null", systemParticipantTypeInput)));
128+
() ->
129+
new InvalidEntityException(
130+
"At least one of capex, opex, or sRated is null", systemParticipantTypeInput)));
129131

130132
try {
131133
exceptions.add(
@@ -340,7 +342,9 @@ private static List<Try<Void, InvalidEntityException>> checkLoad(LoadInput loadI
340342
exceptions.add(
341343
Try.ofVoid(
342344
loadInput.getLoadProfile() == null,
343-
new InvalidEntityException("No standard load profile defined for load", loadInput)));
345+
() ->
346+
new InvalidEntityException(
347+
"No standard load profile defined for load", loadInput)));
344348

345349
exceptions.addAll(
346350
Try.ofVoid(
@@ -460,9 +464,10 @@ private static List<Try<Void, InvalidEntityException>> checkStorageType(
460464
exceptions.add(
461465
Try.ofVoid(
462466
storageTypeInput.getLifeCycle() < 0,
463-
new InvalidEntityException(
464-
"Permissible amount of life cycles of the storage type must be zero or positive",
465-
storageTypeInput)));
467+
() ->
468+
new InvalidEntityException(
469+
"Permissible amount of life cycles of the storage type must be zero or positive",
470+
storageTypeInput)));
466471

467472
exceptions.addAll(
468473
Try.ofVoid(

0 commit comments

Comments
 (0)