Skip to content

Commit

Permalink
Add update filters in batch to filter client service
Browse files Browse the repository at this point in the history
  • Loading branch information
thangqp committed Jun 11, 2024
1 parent f670c12 commit deed804
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum Type {
MAPPING_NAME_NOT_PROVIDED,
GET_FILTER_ERROR,
CREATE_FILTER_ERROR,
UPDATE_FILTER_ERROR,
DUPLICATE_FILTER_ERROR,
DELETE_FILTER_ERROR,
FILTER_NOT_FOUND
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected ResponseEntity<Object> handleDynamicSimulationException(DynamicMapping
case URI_SYNTAX,
GET_FILTER_ERROR,
CREATE_FILTER_ERROR,
UPDATE_FILTER_ERROR,
DUPLICATE_FILTER_ERROR,
DELETE_FILTER_ERROR
-> ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(exception.getMessage());
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/gridsuite/mapping/server/dto/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ public RuleEntity convertRuleToEntity(MappingEntity parentMapping) {
convertedRule.setEquipmentType(equipmentType);
convertedRule.setMapping(parentMapping);
if (filter != null) {
UUID createdFilterId = UUID.randomUUID();
this.filter.setId(createdFilterId);
convertedRule.setFilterUuid(createdFilterId);
if (filter.getId() != null) {
convertedRule.setFilterUuid(filter.getId());
} else {
UUID createdFilterId = UUID.randomUUID();
this.filter.setId(createdFilterId);
convertedRule.setFilterUuid(createdFilterId);
}
}
return convertedRule;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
public interface FilterClient {
String API_VERSION = "v1";
String FILTER_BASE_END_POINT = "filters";
String FILTER_BATCH_END_POINT = FILTER_BASE_END_POINT + DELIMITER + "batch";
String FILTER_GET_END_POINT = FILTER_BASE_END_POINT + DELIMITER + "metadata";
String FILTER_CREATE_IN_BATCH_END_POINT = FILTER_BASE_END_POINT + DELIMITER + "batch";
String FILTER_DUPLICATE_IN_BATCH_END_POINT = FILTER_BASE_END_POINT + DELIMITER + "batch" + DELIMITER + "duplicate";
String FILTER_CREATE_IN_BATCH_END_POINT = FILTER_BATCH_END_POINT;
String FILTER_UPDATE_IN_BATCH_END_POINT = FILTER_BATCH_END_POINT;
String FILTER_DUPLICATE_IN_BATCH_END_POINT = FILTER_BATCH_END_POINT + DELIMITER + "duplicate";
String FILTER_DELETE_IN_BATCH_END_POINT = FILTER_BASE_END_POINT;

List<ExpertFilter> createFilters(Map<UUID, ExpertFilter> filtersToCreateMap);

List<ExpertFilter> updateFilters(Map<UUID, ExpertFilter> filtersToUpdateMap);

Map<UUID, UUID> duplicateFilters(List<UUID> filterUuids);

void deleteFilters(List<UUID> filterUuids);

List<ExpertFilter> getFilters(List<UUID> filterUuids);

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ public List<ExpertFilter> createFilters(Map<UUID, ExpertFilter> filtersToCreateM
}
}

@Override
public List<ExpertFilter> updateFilters(Map<UUID, ExpertFilter> filtersToUpdateMap) {
if (filtersToUpdateMap == null || filtersToUpdateMap.isEmpty()) {
return Collections.emptyList();
}

String endPointUrl = getEndPointUrl(FILTER_UPDATE_IN_BATCH_END_POINT);

UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(endPointUrl);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<Map<UUID, ExpertFilter>> httpEntity = new HttpEntity<>(filtersToUpdateMap, headers);

// call filter server Rest API
try {
return getRestTemplate().exchange(
uriComponentsBuilder.build().toUriString(),
HttpMethod.PUT,
httpEntity,
new ParameterizedTypeReference<List<ExpertFilter>>() { }).getBody();
} catch (HttpStatusCodeException e) {
throw handleHttpError(e, UPDATE_FILTER_ERROR, getObjectMapper());
}
}

@Override
public Map<UUID, UUID> duplicateFilters(List<UUID> filterUuids) {
if (CollectionUtils.isEmpty(filterUuids)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,33 @@ public InputMapping createMapping(String mappingName, InputMapping mapping) {
throw new DynamicMappingException(MAPPING_NAME_NOT_PROVIDED, "Mapping name not provided");
}

// get all filterUuids used in the mapping to delete at the end if exists
// get all filterUuids used previously in the mapping to infer to update/create/delete filters
List<UUID> filterUuids = ruleRepository.findFilterUuidsByMappingName(mappingName);

// IMPORTANT: filter is enriched with new uuid while converting the whole mapping in cascade
// IMPORTANT: new filter is enriched with new uuid while converting the whole mapping in cascade
// So must do converting before persisting filter in filter-server to ensure that new uuid is provided
MappingEntity mappingToSave = mapping.convertMappingToEntity();

// --- persist all filters appeared in rules in remote filter-server --- //
Map<UUID, ExpertFilter> filtersToCreateMap = mapping.getRules().stream()
// --- update or create filters appeared in rules in remote filter-server --- //
Map<Boolean, Map<UUID, ExpertFilter>> filtersToUpdateOrCreate = mapping.getRules().stream()
.map(rule -> Optional.ofNullable(rule.getFilter()))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toMap(ExpertFilter::getId, filter -> filter));
.collect(Collectors.partitioningBy(
filter -> filterUuids.contains(filter.getId()),
Collectors.toMap(ExpertFilter::getId, filter -> filter)
));

// filters to update
Map<UUID, ExpertFilter> filtersToUpdateMap = filtersToUpdateOrCreate.get(Boolean.TRUE);

// filter to create
Map<UUID, ExpertFilter> filtersToCreateMap = filtersToUpdateOrCreate.get(Boolean.FALSE);

// filter to delete
List<UUID> filterUuidsToDelete = filterUuids.stream().filter(uuid -> !filtersToUpdateMap.containsKey(uuid)).toList();

filterClient.updateFilters(filtersToUpdateMap);
filterClient.createFilters(filtersToCreateMap);

// --- persist in cascade the mapping in local database --- //
Expand All @@ -166,9 +180,9 @@ public InputMapping createMapping(String mappingName, InputMapping mapping) {
}
mappingRepository.save(mappingToSave);

// --- clean old filters in filter-server --- //
if (CollectionUtils.isNotEmpty(filterUuids)) {
filterClient.deleteFilters(filterUuids);
// --- clean filters in filter-server --- //
if (CollectionUtils.isNotEmpty(filterUuidsToDelete)) {
filterClient.deleteFilters(filterUuidsToDelete);
}

return mapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,57 @@ public void testCreateFiltersGivenException() throws JsonProcessingException {
.isEqualTo(ERROR_MESSAGE);
}

public void testUpdateFilters() throws JsonProcessingException {

// prepare filters to update
List<ExpertFilter> filterList = createFilterList();
Map<UUID, ExpertFilter> filtersToUpdate = filterList.stream()
.collect(Collectors.toMap(ExpertFilter::getId, filter -> filter));

// mock response for test case PUT with url - /filters/batch
String endpointUrl = getEndpointUrl(FILTER_UPDATE_IN_BATCH_END_POINT);
wireMockServer.stubFor(WireMock.put(WireMock.urlMatching(endpointUrl))
.withRequestBody(WireMock.equalToJson(objectMapper.writeValueAsString(filtersToUpdate)))
.willReturn(WireMock.ok()
.withBody(objectMapper.writeValueAsString(filterList))
.withHeader("Content-Type", "application/json; charset=utf-8")
));

List<ExpertFilter> updatedFilters = filterClient.updateFilters(filtersToUpdate);

// check result
for (ExpertFilter filter : updatedFilters) {
assertThat(filter).recursivelyEquals(filtersToUpdate.get(filter.getId()));
}

}

@Test
public void testUpdateFiltersGivenException() throws JsonProcessingException {
// prepare filters to update
List<ExpertFilter> filterList = createFilterList();
Map<UUID, ExpertFilter> filtersToUpdate = filterList.stream()
.collect(Collectors.toMap(ExpertFilter::getId, filter -> filter));

// mock response for test case PUT with url - /filters/batch
String endpointUrl = getEndpointUrl(FILTER_UPDATE_IN_BATCH_END_POINT);
wireMockServer.stubFor(WireMock.put(WireMock.urlMatching(endpointUrl))
.withRequestBody(WireMock.equalToJson(objectMapper.writeValueAsString(filtersToUpdate)))
.willReturn(WireMock.serverError()
.withBody(ERROR_MESSAGE_JSON)
));

DynamicMappingException exception = catchThrowableOfType(
() -> filterClient.updateFilters(filtersToUpdate),
DynamicMappingException.class);

// check result
assertThat(exception.getType())
.isEqualTo(UPDATE_FILTER_ERROR);
assertThat(exception.getMessage())
.isEqualTo(ERROR_MESSAGE);
}

@Test
public void testDuplicateFilters() throws JsonProcessingException {

Expand Down

0 comments on commit deed804

Please sign in to comment.