Skip to content

Commit

Permalink
fix error marshalling SCL after automatic scd creation call (#127)
Browse files Browse the repository at this point in the history
[#126] fix importSTDElementsInSCD when copying Compas ICDHeader and add assertIsMarshallable to tests

Signed-off-by: samirromdhani <[email protected]>
Signed-off-by: massifben <[email protected]>
Co-authored-by: samirromdhani <[email protected]>
Co-authored-by: massifben <[email protected]>
  • Loading branch information
samirromdhani and massifben authored Aug 9, 2022
1 parent 19605de commit d58e0cb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller.assertIsMarshallable;

class SclAutomationServiceTest {

Expand All @@ -30,20 +31,25 @@ void init(){
}

@Test
void createSCD() throws Exception {
void createSCD_should_return_generatedSCD() throws Exception {
// Given
SCL ssd = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/scd.xml");
SCL std = SclTestMarshaller.getSCLFromFile("/scd-ied-dtt-com-import-stds/std.xml");
// When
SclRootAdapter expectedSCD = SclAutomationService.createSCD(ssd, headerDTO, Set.of(std));
// Then
assertNotNull(expectedSCD.getCurrentElem().getHeader().getId());
assertNull(expectedSCD.getCurrentElem().getHeader().getHistory());
assertEquals(1, expectedSCD.getCurrentElem().getSubstation().size());
assertEquals(1, expectedSCD.getCurrentElem().getIED().size());
assertNotNull(expectedSCD.getCurrentElem().getDataTypeTemplates());
assertEquals(2, expectedSCD.getCurrentElem().getCommunication().getSubNetwork().size());
assertIsMarshallable(expectedSCD.getCurrentElem());
}

@Test
void createSCD_With_HItem() throws Exception {
// Given
HeaderDTO.HistoryItem historyItem = new HeaderDTO.HistoryItem();
historyItem.setWhat("what");
historyItem.setWho("me");
Expand All @@ -53,14 +59,18 @@ void createSCD_With_HItem() throws Exception {
SCL std1 = SclTestMarshaller.getSCLFromFile("/std_1.xml");
SCL std2 = SclTestMarshaller.getSCLFromFile("/std_2.xml");
SCL std3 = SclTestMarshaller.getSCLFromFile("/std_3.xml");
// When
SclRootAdapter expectedSCD = SclAutomationService.createSCD(ssd, headerDTO, Set.of(std1, std2, std3));
// Then
assertNotNull(expectedSCD.getCurrentElem().getHeader().getId());
assertEquals(1 ,expectedSCD.getCurrentElem().getHeader().getHistory().getHitem().size());
assertEquals(1, expectedSCD.getCurrentElem().getSubstation().size());
assertIsMarshallable(expectedSCD.getCurrentElem());
}

@Test
void createSCD_With_HItems() throws Exception {
// Given
HeaderDTO.HistoryItem historyItem = new HeaderDTO.HistoryItem();
historyItem.setWhat("what");
historyItem.setWho("me");
Expand All @@ -74,17 +84,22 @@ void createSCD_With_HItems() throws Exception {
SCL std1 = SclTestMarshaller.getSCLFromFile("/std_1.xml");
SCL std2 = SclTestMarshaller.getSCLFromFile("/std_2.xml");
SCL std3 = SclTestMarshaller.getSCLFromFile("/std_3.xml");
// When
SclRootAdapter expectedSCD = SclAutomationService.createSCD(ssd, headerDTO,Set.of(std1, std2, std3));
// Then
assertNotNull(expectedSCD.getCurrentElem().getHeader().getId());
assertEquals(1, expectedSCD.getCurrentElem().getHeader().getHistory().getHitem().size());
assertEquals("what", expectedSCD.getCurrentElem().getHeader().getHistory().getHitem().get(0).getWhat());
assertIsMarshallable(expectedSCD.getCurrentElem());
}

@Test
void createSCD_SSD_Without_Substation() throws Exception {
// Given
SCL ssd = SclTestMarshaller.getSCLFromFile("/scd-substation-import-ssd/ssd_without_substations.xml");
// When & Then
assertThrows(ScdException.class,
() -> SclAutomationService.createSCD(ssd, headerDTO, new HashSet<>()) );
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public static SclRootAdapter importSTDElementsInSCD(@NonNull SclRootAdapter scdR
for (Map.Entry<String, TPrivate> entry : mapIEDNameAndPrivate.entrySet()) {
String iedName = entry.getKey();
TPrivate tPrivate = entry.getValue();
String icdSysVerUuid = getCompasICDHeader(tPrivate).map(TCompasICDHeader::getICDSystemVersionUUID).orElseThrow(
String icdSysVerUuid = getCompasICDHeaderValue(tPrivate).map(TCompasICDHeader::getICDSystemVersionUUID).orElseThrow(
() -> new ScdException(ICD_SYSTEM_VERSION_UUID + " is not present in COMPAS-ICDHeader in LNode")
);

Expand Down Expand Up @@ -446,10 +446,10 @@ private static void checkSTDCorrespondanceWithLNodeCompasICDHeader(Map<String, P
}

private static String stdCheckFormatExceptionMessage(TPrivate key) {
return CommonConstants.HEADER_ID + " = " + getCompasICDHeader(key).map(TCompasICDHeader::getHeaderId).orElse(null) +
CommonConstants.HEADER_VERSION + " = " + getCompasICDHeader(key).map(TCompasICDHeader::getHeaderVersion).orElse(null) +
CommonConstants.HEADER_REVISION + " = " + getCompasICDHeader(key).map(TCompasICDHeader::getHeaderRevision).orElse(null) +
"and " + ICD_SYSTEM_VERSION_UUID + " = " + getCompasICDHeader(key).map(TCompasICDHeader::getICDSystemVersionUUID).orElse(null);
return CommonConstants.HEADER_ID + " = " + getCompasICDHeaderValue(key).map(TCompasICDHeader::getHeaderId).orElse(null) +
CommonConstants.HEADER_VERSION + " = " + getCompasICDHeaderValue(key).map(TCompasICDHeader::getHeaderVersion).orElse(null) +
CommonConstants.HEADER_REVISION + " = " + getCompasICDHeaderValue(key).map(TCompasICDHeader::getHeaderRevision).orElse(null) +
"and " + ICD_SYSTEM_VERSION_UUID + " = " + getCompasICDHeaderValue(key).map(TCompasICDHeader::getICDSystemVersionUUID).orElse(null);
}

private static Map<String, TPrivate> createMapIEDNameAndPrivate(SclRootAdapter scdRootAdapter) {
Expand All @@ -460,14 +460,14 @@ private static Map<String, TPrivate> createMapIEDNameAndPrivate(SclRootAdapter s
.map(TLNode::getPrivate).flatMap(Collection::stream)
.filter(tPrivate ->
tPrivate.getType().equals(CommonConstants.COMPAS_ICDHEADER)
&& getCompasICDHeader(tPrivate).isPresent() && getCompasICDHeader(tPrivate).get().getIEDName() != null)
.collect(Collectors.toMap(tPrivate -> getCompasICDHeader(tPrivate).get().getIEDName(), Function.identity()));
&& getCompasICDHeader(tPrivate).isPresent() && getCompasICDHeaderValue(tPrivate).get().getIEDName() != null)
.collect(Collectors.toMap(tPrivate -> getCompasICDHeaderValue(tPrivate).get().getIEDName(), Function.identity()));
}

private static Map<String, Pair<TPrivate, List<SCL>>> createMapICDSystemVersionUuidAndSTDFile(Set<SCL> stds) {
Map<String, Pair<TPrivate, List<SCL>>> stringSCLMap = new HashMap<>();
stds.forEach(std -> std.getIED().forEach(ied -> ied.getPrivate().forEach(tp -> {
getCompasICDHeader(tp).map(TCompasICDHeader::getICDSystemVersionUUID).ifPresent(icdSysVer -> {
getCompasICDHeaderValue(tp).map(TCompasICDHeader::getICDSystemVersionUUID).ifPresent(icdSysVer -> {
Pair<TPrivate, List<SCL>> pair = stringSCLMap.get(icdSysVer);
List<SCL> list = pair != null ? pair.getRight() : new ArrayList<>();
list.add(std);
Expand All @@ -478,9 +478,9 @@ private static Map<String, Pair<TPrivate, List<SCL>>> createMapICDSystemVersionU
}

private static boolean comparePrivateCompasICDHeaders(TPrivate iedPrivate, TPrivate scdPrivate) throws ScdException {
TCompasICDHeader iedCompasICDHeader = getCompasICDHeader(iedPrivate).orElseThrow(
TCompasICDHeader iedCompasICDHeader = getCompasICDHeaderValue(iedPrivate).orElseThrow(
() -> new ScdException(CommonConstants.COMPAS_ICDHEADER + "not found in IED Private "));
TCompasICDHeader scdCompasICDHeader = getCompasICDHeader(scdPrivate).orElseThrow(
TCompasICDHeader scdCompasICDHeader = getCompasICDHeaderValue(scdPrivate).orElseThrow(
() -> new ScdException(CommonConstants.COMPAS_ICDHEADER + "not found in LNode Private "));
return iedCompasICDHeader.getIEDType().equals(scdCompasICDHeader.getIEDType())
&& iedCompasICDHeader.getICDSystemVersionUUID().equals(scdCompasICDHeader.getICDSystemVersionUUID())
Expand All @@ -495,20 +495,21 @@ private static boolean comparePrivateCompasICDHeaders(TPrivate iedPrivate, TPriv
}

private static void copyCompasICDHeaderFromLNodePrivateIntoSTDPrivate(TPrivate stdPrivate, TPrivate lNodePrivate) throws ScdException {
TCompasICDHeader lNodeCompasICDHeader = getCompasICDHeader(lNodePrivate).orElseThrow(
JAXBElement<TCompasICDHeader> lNodeCompasICDHeader = getCompasICDHeader(lNodePrivate).orElseThrow(
() -> new ScdException(CommonConstants.COMPAS_ICDHEADER + "not found in LNode Private "));
stdPrivate.getContent().clear();
stdPrivate.getContent().add(lNodeCompasICDHeader);

}

private static Optional<TCompasICDHeader> getCompasICDHeader(TPrivate tPrivate) {
Optional<JAXBElement<TCompasICDHeader>> tCompasICDHeader = !tPrivate.getType().equals(CommonConstants.COMPAS_ICDHEADER) ? Optional.empty() :
private static Optional<JAXBElement<TCompasICDHeader>> getCompasICDHeader(TPrivate tPrivate) {
return !tPrivate.getType().equals(CommonConstants.COMPAS_ICDHEADER) ? Optional.empty() :
tPrivate.getContent().stream()
.filter(JAXBElement.class::isInstance)
.map(o -> (JAXBElement<TCompasICDHeader>) o)
.findFirst();
return tCompasICDHeader.map(JAXBElement::getValue);
}
private static Optional<TCompasICDHeader> getCompasICDHeaderValue(TPrivate tPrivate) {
return getCompasICDHeader(tPrivate).map(JAXBElement::getValue);
}

public static void removeAllControlBlocksAndDatasetsAndExtRefSrcBindings(final SCL scl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.scl2007b4.model.TPrivate;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.testhelpers.MarshallerWrapper;
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;

import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.*;
import static org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller.assertIsMarshallable;

@Slf4j
class SclRootAdapterTest {
Expand All @@ -34,6 +34,7 @@ void testConstruction() {
assertEquals(SclRootAdapter.RELEASE,sclRootAdapter.get().getSclRelease());
assertEquals(SclRootAdapter.VERSION,sclRootAdapter.get().getSclVersion());
assertEquals(SclRootAdapter.REVISION,sclRootAdapter.get().getSclRevision());
assertIsMarshallable(sclRootAdapter.get().getCurrentElem());

assertThrows(IllegalArgumentException.class, () -> new SclRootAdapter(new SCL()));
}
Expand All @@ -50,10 +51,7 @@ void addIED() throws Exception {
assertDoesNotThrow(() -> sclRootAdapter.addIED(icd1, "IED_NAME1"));
assertThrows(ScdException.class, () -> sclRootAdapter.addIED(icd1, "IED_NAME1"));
assertDoesNotThrow(() -> sclRootAdapter.addIED(icd2, "IED_NAME2"));

MarshallerWrapper marshallerWrapper = SclTestMarshaller.createWrapper();
System.out.println(marshallerWrapper.marshall(sclRootAdapter.getCurrentElem()));

assertIsMarshallable(scd);
}

@Test
Expand All @@ -66,5 +64,6 @@ void addPrivate() throws Exception {
assertTrue(sclRootAdapter.getCurrentElem().getPrivate().isEmpty());
sclRootAdapter.addPrivate(tPrivate);
assertEquals(1, sclRootAdapter.getCurrentElem().getPrivate().size());
assertIsMarshallable(scd);
}
}
}
Loading

0 comments on commit d58e0cb

Please sign in to comment.