Skip to content

Commit

Permalink
correct for comments of Mathieu
Browse files Browse the repository at this point in the history
  • Loading branch information
thangqp committed Sep 17, 2024
1 parent 8781f3b commit 0177a47
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import lombok.experimental.SuperBuilder;
import org.gridsuite.modification.server.dto.byfilter.AbstractAssignmentInfos;
import org.gridsuite.modification.server.dto.byfilter.DataType;
import org.gridsuite.modification.server.entities.equipment.modification.byfilter.simple.AssignmentEntity;
import org.gridsuite.modification.server.entities.equipment.modification.byfilter.assignment.AssignmentEntity;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.gridsuite.modification.server.dto.byfilter.DataType;
import org.gridsuite.modification.server.entities.equipment.modification.byfilter.simple.AssignmentEntity;
import org.gridsuite.modification.server.entities.equipment.modification.byfilter.assignment.AssignmentEntity;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.gridsuite.modification.server.dto.ModificationInfos;
import org.gridsuite.modification.server.dto.byfilter.assignment.AssignmentInfos;
import org.gridsuite.modification.server.entities.ModificationEntity;
import org.gridsuite.modification.server.entities.equipment.modification.byfilter.simple.AssignmentEntity;
import org.gridsuite.modification.server.entities.equipment.modification.byfilter.assignment.AssignmentEntity;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package org.gridsuite.modification.server.entities.equipment.modification.byfilter.simple;
package org.gridsuite.modification.server.entities.equipment.modification.byfilter.assignment;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ public void apply(Network network, ReportNode subReportNode) {
// collect all filters from all variations
Map<UUID, String> filters = getFilters();

Map<UUID, FilterEquipments> exportFilters =
Map<UUID, FilterEquipments> filterUuidEquipmentsMap =
ModificationUtils.getUuidFilterEquipmentsMap(filterService, network, subReportNode, filters, getModificationInfos().getErrorType());

if (exportFilters != null) {
if (filterUuidEquipmentsMap != null) {
ReportNode subReporter = subReportNode.newReportNode()
.withMessageTemplate(REPORT_KEY_APPLIED_BY_FILTER_MODIFICATIONS, "${" + VALUE_KEY_MODIFICATION_TYPE_LABEL + "}s on ${" + VALUE_KEY_EQUIPMENT_TYPE + "} type")
.withUntypedValue(VALUE_KEY_MODIFICATION_TYPE_LABEL, StringUtils.capitalize(getModificationTypeLabel()))
Expand All @@ -158,7 +158,7 @@ public void apply(Network network, ReportNode subReportNode) {
.withUntypedValue(VALUE_KEY_FILTERS_EACH_ASSIGNMENT, abstractAssignmentInfos.getFilters().stream().map(FilterInfos::getName)
.collect(Collectors.joining(", ")))
.add();
abstractAssignmentInfos.getFilters().forEach(filterInfos -> applyOnFilterEquipments(network, exportFilters, reports, abstractAssignmentInfos, filterInfos));
abstractAssignmentInfos.getFilters().forEach(filterInfos -> applyOnFilterEquipments(network, filterUuidEquipmentsMap, reports, abstractAssignmentInfos, filterInfos));
reports.forEach(report -> insertReportNode(eachAssignmentReporter, report));
});
// reporting
Expand Down Expand Up @@ -328,11 +328,11 @@ private Map<UUID, String> getFilters() {
}

private void applyOnFilterEquipments(Network network,
Map<UUID, FilterEquipments> exportFilters,
Map<UUID, FilterEquipments> filterUuidEquipmentsMap,
List<ReportNode> reports,
AbstractAssignmentInfos abstractAssignmentInfos,
FilterInfos filterInfos) {
FilterEquipments filterEquipments = exportFilters.get(filterInfos.getId());
FilterEquipments filterEquipments = filterUuidEquipmentsMap.get(filterInfos.getId());

if (CollectionUtils.isEmpty(filterEquipments.getIdentifiableAttributes())) {
reports.add(ReportNode.newRootReportNode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected boolean preCheckValue(Identifiable<?> equipment, AbstractAssignmentInf
equipmentNotModifiedCount += 1;
notEditableEquipments.add(equipment.getId());
reports.add(ReportNode.newRootReportNode()
.withMessageTemplate(REPORT_KEY_EQUIPMENT_MODIFIED_ERROR, " Cannot modify equipment ${" + VALUE_KEY_EQUIPMENT_NAME + "} : At least one of the value or referenced field is null")
.withMessageTemplate(REPORT_KEY_EQUIPMENT_MODIFIED_ERROR, " Cannot modify equipment ${" +
VALUE_KEY_EQUIPMENT_NAME + "} : At least one of the value or referenced field is null")
.withUntypedValue(VALUE_KEY_EQUIPMENT_NAME, equipment.getId())
.withSeverity(TypedValue.TRACE_SEVERITY)
.build());
Expand All @@ -79,6 +80,12 @@ protected boolean preCheckValue(Identifiable<?> equipment, AbstractAssignmentInf
if (value2 == 0 && formulaInfos.getOperator() == Operator.DIVISION) {
equipmentNotModifiedCount += 1;
notEditableEquipments.add(equipment.getId());
reports.add(ReportNode.newRootReportNode()
.withMessageTemplate(REPORT_KEY_EQUIPMENT_MODIFIED_ERROR, " Cannot modify equipment ${" +
VALUE_KEY_EQUIPMENT_NAME + "} : The value or referenced field of the second operand in the division operator is zero")
.withUntypedValue(VALUE_KEY_EQUIPMENT_NAME, equipment.getId())
.withSeverity(TypedValue.TRACE_SEVERITY)
.build());
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ protected String getOldValue(Identifiable<?> equipment, AbstractAssignmentInfos

@Override
protected String getNewValue(Identifiable<?> equipment, AbstractAssignmentInfos abstractAssignmentInfos) {
AssignmentInfos<?> simpleModificationInfos = (AssignmentInfos<?>) abstractAssignmentInfos;
if (simpleModificationInfos.getValue() == null) {
throw new NetworkModificationException(MODIFICATION_BY_ASSIGNMENT_ERROR, "There is no value in assignment");
AssignmentInfos<?> assignmentInfos = (AssignmentInfos<?>) abstractAssignmentInfos;
if (assignmentInfos.getValue() == null) {
throw new NetworkModificationException(MODIFICATION_BY_ASSIGNMENT_ERROR, "Missing a value in the assignment");
}
return simpleModificationInfos.getValue().toString();
return assignmentInfos.getValue().toString();
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import org.gridsuite.filter.identifierlistfilter.IdentifierListFilter;
import org.gridsuite.filter.identifierlistfilter.IdentifierListFilterEquipmentAttributes;
import org.gridsuite.filter.utils.EquipmentType;
import org.gridsuite.modification.server.dto.ModificationByAssignmentInfos;
import org.gridsuite.modification.server.dto.FilterInfos;
import org.gridsuite.modification.server.dto.ModificationByAssignmentInfos;
import org.gridsuite.modification.server.dto.NetworkModificationResult;
import org.gridsuite.modification.server.dto.byfilter.DataType;
import org.gridsuite.modification.server.dto.byfilter.assignment.AssignmentInfos;
Expand Down Expand Up @@ -87,43 +87,35 @@ public void testByModificationError() throws Exception {
mockMvc.perform(post(getNetworkModificationUri()).content(mapper.writeValueAsString(null)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());

// Test with empty list of simple modification
checkCreationApplicationStatus(ModificationByAssignmentInfos.builder().equipmentType(getIdentifiableType()).assignmentInfosList(List.of()).build(),
NetworkModificationResult.ApplicationStatus.WITH_ERRORS);
// Test with empty list of assignment
checkCreationApplicationStatus(List.of(), NetworkModificationResult.ApplicationStatus.WITH_ERRORS);

// Test with empty list of filters in simple modification
List<AssignmentInfos<?>> simpleInfosWithNoFilters = getAssignmentInfos().stream().peek(simpleInfos -> simpleInfos.setFilters(List.of())).toList();
checkCreationApplicationStatus(ModificationByAssignmentInfos.builder().equipmentType(getIdentifiableType()).assignmentInfosList(simpleInfosWithNoFilters).build(),
NetworkModificationResult.ApplicationStatus.WITH_ERRORS);
// Test with empty list of filters in assignment
List<AssignmentInfos<?>> assignmentsWithNoFilters = getAssignmentInfos().stream().peek(assignmentInfos -> assignmentInfos.setFilters(List.of())).toList();
checkCreationApplicationStatus(assignmentsWithNoFilters, NetworkModificationResult.ApplicationStatus.WITH_ERRORS);

// Test with editedField = null
AssignmentInfos<?> simpleInfosWithNoEditedField = DoubleAssignmentInfos.builder()
AssignmentInfos<?> assignmentWithNoEditedField = DoubleAssignmentInfos.builder()
.value(50.)
.filters(List.of())
.build();
checkCreationApplicationStatus(ModificationByAssignmentInfos.builder().equipmentType(getIdentifiableType()).assignmentInfosList(List.of(simpleInfosWithNoEditedField)).build(),
NetworkModificationResult.ApplicationStatus.WITH_ERRORS);
checkCreationApplicationStatus(List.of(assignmentWithNoEditedField), NetworkModificationResult.ApplicationStatus.WITH_ERRORS);
}

protected void checkCreateWithWarning(List<AssignmentInfos<?>> simpleInfos, List<IdentifierListFilterEquipmentAttributes> existingEquipmentList) throws Exception {
protected void checkCreateWithWarning(List<AssignmentInfos<?>> assignments, List<IdentifierListFilterEquipmentAttributes> existingEquipmentList) throws Exception {
AbstractFilter filter = getFilterEquipments(FILTER_WITH_ONE_WRONG_ID, existingEquipmentList);

UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/metadata\\?ids=" + FILTER_WITH_ONE_WRONG_ID))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(List.of(filter)))
.withHeader("Content-Type", "application/json"))).getId();

ModificationByAssignmentInfos modificationByAssignmentInfos = ModificationByAssignmentInfos.builder()
.assignmentInfosList(simpleInfos)
.equipmentType(getIdentifiableType())
.build();

checkCreationApplicationStatus(modificationByAssignmentInfos, NetworkModificationResult.ApplicationStatus.WITH_WARNINGS);
checkCreationApplicationStatus(assignments, NetworkModificationResult.ApplicationStatus.WITH_WARNINGS);

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(List.of(FILTER_WITH_ONE_WRONG_ID)), false);
}

protected void checkCreateWithError(List<AssignmentInfos<?>> simpleInfos, List<AbstractFilter> filterEquipments) throws Exception {
protected void checkCreateWithError(List<AssignmentInfos<?>> assignments, List<AbstractFilter> filterEquipments) throws Exception {
String filterIds = filterEquipments.stream()
.map(AbstractFilter::getId)
.map(UUID::toString)
Expand All @@ -134,38 +126,28 @@ protected void checkCreateWithError(List<AssignmentInfos<?>> simpleInfos, List<A
.withBody(mapper.writeValueAsString(filterEquipments))
.withHeader("Content-Type", "application/json"))).getId();

ModificationByAssignmentInfos modificationByAssignmentInfos = ModificationByAssignmentInfos.builder()
.assignmentInfosList(simpleInfos)
.equipmentType(getIdentifiableType())
.build();

checkCreationApplicationStatus(modificationByAssignmentInfos, NetworkModificationResult.ApplicationStatus.WITH_ERRORS);
checkCreationApplicationStatus(assignments, NetworkModificationResult.ApplicationStatus.WITH_ERRORS);

wireMockUtils.verifyGetRequest(stubId,
PATH,
handleQueryParams(filterEquipments.stream().map(AbstractFilter::getId).collect(Collectors.toList())),
handleQueryParams(filterEquipments.stream().map(AbstractFilter::getId).toList()),
false);
}

@Test
public void testModificationWithAllWrongEquipmentIds() throws Exception {
AbstractFilter filter = getFilterEquipments(FILTER_WITH_ALL_WRONG_IDS, List.of());

List<AssignmentInfos<?>> simpleInfosList = getAssignmentInfos().stream()
.peek(simpleInfos -> simpleInfos.setFilters(List.of(new FilterInfos(FILTER_WITH_ALL_WRONG_IDS, "filterWithWrongId"))))
List<AssignmentInfos<?>> assignmentsWithWrongFilter = getAssignmentInfos().stream()
.peek(assignmentInfos -> assignmentInfos.setFilters(List.of(new FilterInfos(FILTER_WITH_ALL_WRONG_IDS, "filterWithWrongId"))))
.toList();

UUID stubId = wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/v1/filters/metadata\\?ids=" + FILTER_WITH_ALL_WRONG_IDS))
.willReturn(WireMock.ok()
.withBody(mapper.writeValueAsString(List.of(filter)))
.withHeader("Content-Type", "application/json"))).getId();

ModificationByAssignmentInfos modificationByAssignmentInfos = ModificationByAssignmentInfos.builder()
.assignmentInfosList(simpleInfosList)
.equipmentType(getIdentifiableType())
.build();

checkCreationApplicationStatus(modificationByAssignmentInfos, NetworkModificationResult.ApplicationStatus.WITH_ERRORS);
checkCreationApplicationStatus(assignmentsWithWrongFilter, NetworkModificationResult.ApplicationStatus.WITH_ERRORS);

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(List.of(FILTER_WITH_ALL_WRONG_IDS)), false);
}
Expand All @@ -181,7 +163,7 @@ public void testCreate() throws Exception {

super.testCreate();

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(filters.stream().map(AbstractFilter::getId).collect(Collectors.toList())), false);
wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(filters.stream().map(AbstractFilter::getId).toList()), false);
}

@Test
Expand All @@ -195,11 +177,15 @@ public void testCopy() throws Exception {

super.testCopy();

wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(filters.stream().map(AbstractFilter::getId).collect(Collectors.toList())), false);
wireMockUtils.verifyGetRequest(stubId, PATH, handleQueryParams(filters.stream().map(AbstractFilter::getId).toList()), false);
}

protected void checkCreationApplicationStatus(ModificationByAssignmentInfos modificationByAssignmentInfos,
protected void checkCreationApplicationStatus(List<? extends AssignmentInfos<?>> assignmentInfos,
NetworkModificationResult.ApplicationStatus applicationStatus) throws Exception {
ModificationByAssignmentInfos modificationByAssignmentInfos = ModificationByAssignmentInfos.builder()
.equipmentType(getIdentifiableType())
.assignmentInfosList(assignmentInfos)
.build();
String modificationToCreateJson = mapper.writeValueAsString(modificationByAssignmentInfos);

MvcResult mvcResult = mockMvc.perform(post(getNetworkModificationUri()).content(modificationToCreateJson).contentType(MediaType.APPLICATION_JSON))
Expand Down Expand Up @@ -259,14 +245,14 @@ String getPath(boolean isRegexPath) {
protected abstract List<AbstractFilter> getTestFilters();

protected List<AssignmentInfos<?>> getAssignmentInfos() {
PropertyAssignmentInfos spySimpleInfos = spy(PropertyAssignmentInfos.builder()
PropertyAssignmentInfos spyAssignmentInfos = spy(PropertyAssignmentInfos.builder()
.editedField(DataType.PROPERTY.name())
.propertyName("propertyName")
.value("propertyValue")
.filters(List.of(filter1))
.build());
doReturn(DataType.PROPERTY).when(spySimpleInfos).getDataType();
return new ArrayList<>(List.of(spySimpleInfos));
doReturn(DataType.PROPERTY).when(spyAssignmentInfos).getDataType();
return new ArrayList<>(List.of(spyAssignmentInfos));
}

protected abstract List<AssignmentInfos<?>> getUpdatedAssignmentInfos();
Expand Down
Loading

0 comments on commit 0177a47

Please sign in to comment.