diff --git a/CHANGELOG.md b/CHANGELOG.md index f1a341398..2a22f5bcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed ### Changed +- Changing from comparing strings to comparing uuids in `EntitySource.findFirstEntityByUuid` [#829](https://github.com/ie3-institute/PowerSystemDataModel/issues/829) + ## [4.1.0] - 2023-11-02 diff --git a/src/main/java/edu/ie3/datamodel/io/source/EntitySource.java b/src/main/java/edu/ie3/datamodel/io/source/EntitySource.java index eec826f97..d1cfd5c70 100644 --- a/src/main/java/edu/ie3/datamodel/io/source/EntitySource.java +++ b/src/main/java/edu/ie3/datamodel/io/source/EntitySource.java @@ -71,19 +71,19 @@ protected String safeMapGet(Map map, String key, String mapName) * optional if no matching entity with the provided uuid can be found */ protected Optional findFirstEntityByUuid( - String entityUuid, Collection entities) { + UUID entityUuid, Collection entities) { return entities.stream() .parallel() - .filter(uniqueEntity -> uniqueEntity.getUuid().toString().equalsIgnoreCase(entityUuid)) + .filter(uniqueEntity -> uniqueEntity.getUuid().equals(entityUuid)) .findFirst(); } /** - * Checks if the requested type of an asset can be found in the provided collection of types based - * on the provided fields to values mapping. The provided fields to values mapping needs to have - * one and only one field with key {@link #TYPE} and a corresponding UUID value. If the type can - * be found in the provided collection based on the UUID it is returned wrapped in a {@link - * Success}. Otherwise a {@link Failure} is returned and a warning is logged. + * Checks if the requested type of asset can be found in the provided collection of types based on + * the provided fields to values mapping. The provided fields to values mapping needs to have one + * and only one field with key {@link #TYPE} and a corresponding UUID value. If the type can be + * found in the provided collection based on the UUID it is returned wrapped in a {@link Success}. + * Otherwise a {@link Failure} is returned and a warning is logged. * * @param types a collection of types that should be used for searching * @param fieldsToAttributes the field name to value mapping incl. the key {@link #TYPE} @@ -96,7 +96,7 @@ protected Try getAssetType( Optional assetType = Optional.ofNullable(fieldsToAttributes.get(TYPE)) - .flatMap(typeUuid -> findFirstEntityByUuid(typeUuid, types)); + .flatMap(typeUuid -> findFirstEntityByUuid(UUID.fromString(typeUuid), types)); // if the type is not present we return an empty element and // log a warning @@ -167,10 +167,10 @@ protected TypedConnectorInputEntityData addTypeToE */ protected OperatorInput getFirstOrDefaultOperator( Collection operators, - String operatorUuid, + Optional operatorUuid, String entityClassName, String requestEntityUuid) { - if (operatorUuid == null) { + if (operatorUuid.isEmpty()) { log.warn( "Input source for class '{}' is missing the 'operator' field. " + "This is okay, but you should consider fixing the file by adding the field. " @@ -178,18 +178,16 @@ protected OperatorInput getFirstOrDefaultOperator( entityClassName); return OperatorInput.NO_OPERATOR_ASSIGNED; } else { - return operatorUuid.trim().isEmpty() - ? OperatorInput.NO_OPERATOR_ASSIGNED - : findFirstEntityByUuid(operatorUuid, operators) - .orElseGet( - () -> { - log.debug( - "Cannot find operator with uuid '{}' for element '{}' and uuid '{}'. Defaulting to 'NO OPERATOR ASSIGNED'.", - operatorUuid, - entityClassName, - requestEntityUuid); - return OperatorInput.NO_OPERATOR_ASSIGNED; - }); + return findFirstEntityByUuid(operatorUuid.get(), operators) + .orElseGet( + () -> { + log.debug( + "Cannot find operator with uuid '{}' for element '{}' and uuid '{}'. Defaulting to 'NO OPERATOR ASSIGNED'.", + operatorUuid, + entityClassName, + requestEntityUuid); + return OperatorInput.NO_OPERATOR_ASSIGNED; + }); } } @@ -212,7 +210,7 @@ protected Stream> nodeAssetInputE // get the raw data Map fieldsToAttributes = assetInputEntityData.getFieldsToValues(); // get the node of the entity - String nodeUuid = fieldsToAttributes.get(NODE); + UUID nodeUuid = UUID.fromString(fieldsToAttributes.get(NODE)); Optional node = findFirstEntityByUuid(nodeUuid, nodes); // if the node is not present we return an empty element and @@ -265,7 +263,10 @@ protected AssetInputEntityData assetInputEntityDataStream Collection operators) { // get the operator of the entity - String operatorUuid = fieldsToAttributes.get(OPERATOR); + Optional operatorUuid = + Optional.ofNullable(fieldsToAttributes.get(OPERATOR)) + .filter(s -> !s.isBlank()) + .map(UUID::fromString); OperatorInput operator = getFirstOrDefaultOperator( operators, diff --git a/src/main/java/edu/ie3/datamodel/io/source/GraphicSource.java b/src/main/java/edu/ie3/datamodel/io/source/GraphicSource.java index 7cdc097ca..2ecd52c59 100644 --- a/src/main/java/edu/ie3/datamodel/io/source/GraphicSource.java +++ b/src/main/java/edu/ie3/datamodel/io/source/GraphicSource.java @@ -150,7 +150,7 @@ protected Try buildNodeGraphicEntit Map fieldsToAttributes, Set nodes) { // get the node of the entity - String nodeUuid = fieldsToAttributes.get(NODE); + UUID nodeUuid = UUID.fromString(fieldsToAttributes.get(NODE)); Optional node = findFirstEntityByUuid(nodeUuid, nodes); // if the node is not present we return a failure @@ -197,7 +197,7 @@ protected Try buildLineGraphicEntit Map fieldsToAttributes, Set lines) { // get the node of the entity - String lineUuid = fieldsToAttributes.get("line"); + UUID lineUuid = UUID.fromString(fieldsToAttributes.get("line")); Optional line = findFirstEntityByUuid(lineUuid, lines); // if the node is not present we return an empty element and diff --git a/src/main/java/edu/ie3/datamodel/io/source/RawGridSource.java b/src/main/java/edu/ie3/datamodel/io/source/RawGridSource.java index b06d55d4d..70c228c96 100644 --- a/src/main/java/edu/ie3/datamodel/io/source/RawGridSource.java +++ b/src/main/java/edu/ie3/datamodel/io/source/RawGridSource.java @@ -513,8 +513,8 @@ protected Try buildUntypedConnectorIn Map fieldsToAttributes = assetInputEntityData.getFieldsToValues(); // get the two connector nodes - String nodeAUuid = fieldsToAttributes.get(NODE_A); - String nodeBUuid = fieldsToAttributes.get(NODE_B); + UUID nodeAUuid = UUID.fromString(fieldsToAttributes.get(NODE_A)); + UUID nodeBUuid = UUID.fromString(fieldsToAttributes.get(NODE_B)); Optional nodeA = findFirstEntityByUuid(nodeAUuid, nodes); Optional nodeB = findFirstEntityByUuid(nodeBUuid, nodes); @@ -621,7 +621,7 @@ protected Try addThirdNode( Map fieldsToAttributes = typeEntityData.getFieldsToValues(); // get nodeC of the transformer - String nodeCUuid = fieldsToAttributes.get("nodeC"); + UUID nodeCUuid = UUID.fromString(fieldsToAttributes.get("nodeC")); Optional nodeC = findFirstEntityByUuid(nodeCUuid, nodes); // if nodeC is not present we return a failure diff --git a/src/main/java/edu/ie3/datamodel/io/source/SystemParticipantSource.java b/src/main/java/edu/ie3/datamodel/io/source/SystemParticipantSource.java index 3754362dd..d00f4e37a 100644 --- a/src/main/java/edu/ie3/datamodel/io/source/SystemParticipantSource.java +++ b/src/main/java/edu/ie3/datamodel/io/source/SystemParticipantSource.java @@ -928,12 +928,15 @@ protected Try buildChpEntityData( Optional thermalStorage = Optional.ofNullable(fieldsToAttributes.get(THERMAL_STORAGE)) .flatMap( - thermalStorageUuid -> findFirstEntityByUuid(thermalStorageUuid, thermalStorages)); + thermalStorageUuid -> + findFirstEntityByUuid(UUID.fromString(thermalStorageUuid), thermalStorages)); // get the thermal bus input for this chp unit Optional thermalBus = Optional.ofNullable(fieldsToAttributes.get("thermalBus")) - .flatMap(thermalBusUuid -> findFirstEntityByUuid(thermalBusUuid, thermalBuses)); + .flatMap( + thermalBusUuid -> + findFirstEntityByUuid(UUID.fromString(thermalBusUuid), thermalBuses)); // if the thermal storage or the thermal bus are not present we return an // empty element and log a warning diff --git a/src/test/groovy/edu/ie3/datamodel/io/source/EntitySourceTest.groovy b/src/test/groovy/edu/ie3/datamodel/io/source/EntitySourceTest.groovy index b2881dbdf..4b51be15b 100644 --- a/src/test/groovy/edu/ie3/datamodel/io/source/EntitySourceTest.groovy +++ b/src/test/groovy/edu/ie3/datamodel/io/source/EntitySourceTest.groovy @@ -48,7 +48,7 @@ class EntitySourceTest extends Specification { ) when: - def actual = dummyEntitySource.findFirstEntityByUuid(uuid.toString(), entities) + def actual = dummyEntitySource.findFirstEntityByUuid(uuid, entities) then: actual.present @@ -58,7 +58,7 @@ class EntitySourceTest extends Specification { def "A CsvDataSource should always return an operator. Either the found one (if any) or OperatorInput.NO_OPERATOR_ASSIGNED"() { expect: - dummyEntitySource.getFirstOrDefaultOperator(operators, operatorUuid, entityClassName, requestEntityUuid) == expectedOperator + dummyEntitySource.getFirstOrDefaultOperator(operators, Optional.of(UUID.fromString(operatorUuid)), entityClassName, requestEntityUuid) == expectedOperator where: operatorUuid | operators | entityClassName | requestEntityUuid || expectedOperator