Skip to content

Commit

Permalink
Fix recreating tombstoned identifiable for getOptional
Browse files Browse the repository at this point in the history
Signed-off-by: BOUHOURS Antoine <[email protected]>
  • Loading branch information
antoinebhs committed Jan 3, 2025
1 parent e65c608 commit a216835
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ private static <T, U> Map<T, U> getExternalAttributes(
additionalIdentifiableIdsToExclude.contains(idExtractor.apply(ownerInfo))
);

// Retrieve updated external attributes in partial variant
Map<T, U> externalAttributesUpdatedInPartialVariant = fetchExternalAttributesInVariant.apply(variantNum);
// Retrieve external attributes in partial variant
Map<T, U> partialVariantExternalAttributes = fetchExternalAttributesInVariant.apply(variantNum);

// Combine external attributes from full and partial variant
externalAttributes.putAll(externalAttributesUpdatedInPartialVariant);
externalAttributes.putAll(partialVariantExternalAttributes);

return externalAttributes;
}
Expand All @@ -97,15 +97,15 @@ public static <T, U> Map<T, U> getRegulatingEquipments(
int fullVariantNum,
Supplier<Set<String>> fetchTombstonedRegulatingPointsIds,
Supplier<Set<String>> fetchTombstonedIdentifiableIds,
Supplier<Set<String>> fetchUpdatedRegulatingPointIds,
Supplier<Set<String>> fetchRegulatingPointIdsInVariant,
IntFunction<Map<T, U>> fetchExternalAttributesInVariant,
Function<T, String> idExtractor) {
return getExternalAttributes(
variantNum,
fullVariantNum,
fetchTombstonedRegulatingPointsIds,
fetchTombstonedIdentifiableIds,
fetchUpdatedRegulatingPointIds,
fetchRegulatingPointIdsInVariant,
fetchExternalAttributesInVariant,
idExtractor
);
Expand Down Expand Up @@ -150,7 +150,7 @@ public static <T> List<T> getIdentifiables(
Supplier<Set<String>> fetchTombstonedIdentifiableIds,
IntFunction<List<T>> fetchIdentifiablesInVariant,
Function<T, String> idExtractor,
Supplier<List<String>> fetchUpdatedIdentifiblesIdsInVariant) {
Supplier<List<String>> fetchIdentifiblesIdsInVariant) {
if (NetworkAttributes.isFullVariant(fullVariantNum)) {
// If the variant is full, retrieve identifiables directly
return fetchIdentifiablesInVariant.apply(variantNum);
Expand All @@ -159,24 +159,24 @@ public static <T> List<T> getIdentifiables(
// Retrieve identifiables from the full variant first
List<T> identifiables = fetchIdentifiablesInVariant.apply(fullVariantNum);

// Retrieve updated identifiables in partial variant
List<T> updatedIdentifiables = fetchIdentifiablesInVariant.apply(variantNum);
// Retrieve identifiables in partial variant
List<T> partialVariantIdentifiables = fetchIdentifiablesInVariant.apply(variantNum);

// Retrieve updated ids in partial variant
Set<String> updatedIds = fetchUpdatedIdentifiblesIdsInVariant != null
? new HashSet<>(fetchUpdatedIdentifiblesIdsInVariant.get())
: updatedIdentifiables.stream()
// Retrieve ids in partial variant
Set<String> partialVariantIds = fetchIdentifiblesIdsInVariant != null
? new HashSet<>(fetchIdentifiblesIdsInVariant.get())
: partialVariantIdentifiables.stream()
.map(idExtractor)
.collect(Collectors.toSet());

// Remove any resources that have been updated in the current variant or tombstoned
// Remove any resources that in the partial variant or tombstoned
Set<String> tombstonedIds = fetchTombstonedIdentifiableIds.get();
identifiables.removeIf(resource ->
updatedIds.contains(idExtractor.apply(resource)) || tombstonedIds.contains(idExtractor.apply(resource))
partialVariantIds.contains(idExtractor.apply(resource)) || tombstonedIds.contains(idExtractor.apply(resource))
);

// Combine identifiables from full and partial variant
identifiables.addAll(updatedIdentifiables);
identifiables.addAll(partialVariantIdentifiables);

return identifiables;
}
Expand All @@ -192,18 +192,18 @@ public static <T extends Attributes> Optional<Resource<T>> getOptionalIdentifiab
return fetchIdentifiableInVariant.apply(variantNum);
}

// If the identifiable is tombstoned, return directly
// Retrieve identifiable in partial variant
Optional<Resource<T>> partialVariantIdentifiable = fetchIdentifiableInVariant.apply(variantNum);
if (partialVariantIdentifiable.isPresent()) {
return partialVariantIdentifiable;
}

Set<String> tombstonedIds = fetchTombstonedIdentifiableIds.get();
if (tombstonedIds.contains(identifiableId)) {
// Return empty if the identifiable is marked as tombstoned
return Optional.empty();
}

// Retrieve updated identifiable in partial variant
Optional<Resource<T>> updatedIdentifiable = fetchIdentifiableInVariant.apply(variantNum);
if (updatedIdentifiable.isPresent()) {
return updatedIdentifiable;
}

// Retrieve identifiable from the full variant
return fetchIdentifiableInVariant.apply(fullVariantNum);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,16 @@ void getExternalAttributesFromPartialCloneWithUpdatedIdentifiableWithoutExternal
assertNull(networkStoreRepository.getTemporaryLimits(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, lineId).get(ownerInfoLine));
assertNull(networkStoreRepository.getPermanentLimits(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, lineId).get(ownerInfoLine));
assertNull(networkStoreRepository.getReactiveCapabilityCurvePoints(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, genId).get(ownerInfoGen));
// Set again a tombstone to verify that it does not throw
updateExternalAttributesWithTombstone(1, lineId, genId, twoWTId);
assertNull(networkStoreRepository.getTapChangerSteps(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, twoWTId).get(ownerInfoTwoWT));
assertNull(networkStoreRepository.getTemporaryLimits(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, lineId).get(ownerInfoLine));
assertNull(networkStoreRepository.getPermanentLimits(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, lineId).get(ownerInfoLine));
assertNull(networkStoreRepository.getReactiveCapabilityCurvePoints(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, genId).get(ownerInfoGen));
// Recreate the external attributes and verify that the tombstone is ignored
updateExternalAttributes(1, lineId, genId, twoWTId, loadId);
verifyUpdatedExternalAttributes(lineId, genId, twoWTId, loadId, 1, NETWORK_UUID);
// Set again a tombstone after recreating the external attributes
updateExternalAttributesWithTombstone(1, lineId, genId, twoWTId);
assertNull(networkStoreRepository.getTapChangerSteps(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, twoWTId).get(ownerInfoTwoWT));
assertNull(networkStoreRepository.getTemporaryLimits(NETWORK_UUID, 1, EQUIPMENT_ID_COLUMN, lineId).get(ownerInfoLine));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ void createIdentifiablesWithRecreatedTombstonedIdentifiable() {
assertTrue(getIdentifiableIdsForVariant(NETWORK_UUID, 1).isEmpty());
assertEquals(Set.of(lineId1), getTombstonedIdentifiableIds(NETWORK_UUID, 1));
// Variant 2 (recreated line1 with different attributes)
assertEquals(Optional.of(lineVariant2), networkStoreRepository.getLine(NETWORK_UUID, 2, lineId1));
assertEquals(List.of(), networkStoreRepository.getVoltageLevelLines(NETWORK_UUID, 2, "vl1"));
assertEquals(List.of(lineVariant2), networkStoreRepository.getVoltageLevelLines(NETWORK_UUID, 2, "vl2"));
assertEquals(List.of(lineVariant2), networkStoreRepository.getLines(NETWORK_UUID, 2));
assertEquals(List.of(lineVariant2), getIdentifiablesForVariant(NETWORK_UUID, 2, mappings.getLineMappings()));
assertEquals(List.of(lineId1), getIdentifiableIdsForVariant(NETWORK_UUID, 2));
Expand Down

0 comments on commit a216835

Please sign in to comment.