-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSclAutomationServiceIntegrationTest.java
More file actions
177 lines (158 loc) · 8.06 KB
/
Copy pathSclAutomationServiceIntegrationTest.java
File metadata and controls
177 lines (158 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-FileCopyrightText: 2023 RTE FRANCE
//
// SPDX-License-Identifier: Apache-2.0
package org.lfenergy.compas.sct.app;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.lfenergy.compas.scl2007b4.model.LN0;
import org.lfenergy.compas.scl2007b4.model.SCL;
import org.lfenergy.compas.sct.commons.*;
import org.lfenergy.compas.sct.commons.api.ControlBlockEditor;
import org.lfenergy.compas.sct.commons.api.DataTypeTemplateReader;
import org.lfenergy.compas.sct.commons.api.SclEditor;
import org.lfenergy.compas.sct.commons.api.SubstationEditor;
import org.lfenergy.compas.sct.commons.dto.HeaderDTO;
import org.lfenergy.compas.sct.commons.exception.ScdException;
import org.lfenergy.compas.sct.commons.scl.ControlService;
import org.lfenergy.compas.sct.commons.scl.ExtRefService;
import org.lfenergy.compas.sct.commons.scl.SclElementAdapter;
import org.lfenergy.compas.sct.commons.scl.SclRootAdapter;
import org.lfenergy.compas.sct.commons.scl.ldevice.LDeviceAdapter;
import org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.*;
import static org.lfenergy.compas.sct.commons.testhelpers.SclTestMarshaller.assertSclValidateXsd;
class SclAutomationServiceIntegrationTest {
private SclAutomationService sclAutomationService;
private static final IedService iedService = new IedService();
private static final LnService lnService = new LnService();
private static final ExtRefService extRefService = new ExtRefService();
private static final DataTypeTemplateReader dataTypeTemplatesService = new DataTypeTemplatesService() ;
private static final LdeviceService ldeviceService = new LdeviceService(lnService);
private static final SclEditor sclEditor = new SclService(iedService, ldeviceService, lnService, extRefService, dataTypeTemplatesService);
private static final SubstationEditor substationEditor = new SubstationService(new VoltageLevelService());
private static final ControlBlockEditor controlBlockEditor = new ControlBlockEditorService(new ControlService(), new LdeviceService(new LnService()), new ConnectedAPService(), new SubNetworkService(), extRefService);
private HeaderDTO headerDTO;
@BeforeEach
void setUp() {
headerDTO = new HeaderDTO();
headerDTO.setId(UUID.randomUUID());
headerDTO.setRevision("hRevision");
headerDTO.setVersion("hVersion");
sclAutomationService = new SclAutomationService(sclEditor, substationEditor, controlBlockEditor);
}
@Test
void createSCD_should_return_generatedSCD() {
// Given
SCL ssd = SclTestMarshaller.getSCLFromResource("scd-ied-dtt-com-import-stds/scd.xml");
SCL std = SclTestMarshaller.getSCLFromResource("scd-ied-dtt-com-import-stds/std.xml");
// When
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std));
// Then
assertThat(scd.getHeader().getId()).isNotNull();
assertThat(scd.getHeader().getHistory()).isNull();
assertThat(scd.getSubstation()).hasSize(1);
assertThat(scd.getIED()).hasSize(1);
assertThat(scd.getDataTypeTemplates()).isNotNull();
assertThat(scd.getCommunication().getSubNetwork()).hasSize(2);
assertSclValidateXsd(scd);
}
@Test
void createSCD_WithHItem_should_return_generatedSCD() {
// Given
HeaderDTO.HistoryItem historyItem = new HeaderDTO.HistoryItem();
historyItem.setWhat("what");
historyItem.setWho("me");
historyItem.setWhy("because");
headerDTO.getHistoryItems().add(historyItem);
SCL ssd = SclTestMarshaller.getSCLFromResource("scd-substation-import-ssd/ssd.xml");
SCL std1 = SclTestMarshaller.getSCLFromResource("std_1.xml");
SCL std2 = SclTestMarshaller.getSCLFromResource("std_2.xml");
SCL std3 = SclTestMarshaller.getSCLFromResource("std_3.xml");
// When
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std1, std2, std3));
// Then
assertThat(scd.getHeader().getId()).isNotNull();
assertThat(scd.getHeader().getHistory().getHitem()).hasSize(1);
assertThat(scd.getSubstation()).hasSize(1);
assertSclValidateXsd(scd);
}
@Test
void createSCD_WithManyHItem_should_return_generatedSCD() {
// Given
HeaderDTO.HistoryItem historyItem = new HeaderDTO.HistoryItem();
historyItem.setWhat("what");
historyItem.setWho("me");
historyItem.setWhy("because");
HeaderDTO.HistoryItem historyItemBis = new HeaderDTO.HistoryItem();
historyItemBis.setWhat("what Bis");
historyItemBis.setWho("me bis");
historyItemBis.setWhy("because bis");
headerDTO.getHistoryItems().addAll(Arrays.asList(historyItem, historyItemBis));
SCL ssd = SclTestMarshaller.getSCLFromResource("scd-substation-import-ssd/ssd.xml");
SCL std1 = SclTestMarshaller.getSCLFromResource("std_1.xml");
SCL std2 = SclTestMarshaller.getSCLFromResource("std_2.xml");
SCL std3 = SclTestMarshaller.getSCLFromResource("std_3.xml");
// When
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std1, std2, std3));
// Then
assertThat(scd.getHeader().getId()).isNotNull();
assertThat(scd.getHeader().getHistory().getHitem()).hasSize(1);
assertThat(scd.getHeader().getHistory().getHitem().getFirst().getWhat()).isEqualTo("what");
assertSclValidateXsd(scd);
}
@Test
void createSCD_whenSSDWithoutSubstation_shouldThrowException() {
// Given
SCL ssd = SclTestMarshaller.getSCLFromResource("scd-substation-import-ssd/ssd_without_substations.xml");
// When & Then
List<SCL> stdListEmpty = List.of();
assertThatThrownBy(() -> sclAutomationService.createSCD(ssd, headerDTO, stdListEmpty))
.isInstanceOf(ScdException.class);
}
@Test
void createSCD_whenSSDIsNull_shouldThrowException() {
// Given
HeaderDTO.HistoryItem historyItem = new HeaderDTO.HistoryItem();
historyItem.setWhat("what");
historyItem.setWho("me");
historyItem.setWhy("because");
headerDTO.getHistoryItems().add(historyItem);
SCL std1 = SclTestMarshaller.getSCLFromResource("std_1.xml");
List<SCL> stdList = List.of(std1);
// When & Then
assertThatCode(() -> sclAutomationService.createSCD(null, headerDTO, stdList))
.isInstanceOf(NullPointerException.class);
}
@Test
void createSCD_whenheaderDTOIsNull_shouldThrowException() {
// Given
SCL ssd = SclTestMarshaller.getSCLFromResource("scd-substation-import-ssd/ssd.xml");
SCL std1 = SclTestMarshaller.getSCLFromResource("std_1.xml");
List<SCL> stdList = List.of(std1);
// When & Then
assertThatCode(() -> sclAutomationService.createSCD(ssd, null, stdList))
.isInstanceOf(NullPointerException.class);
}
@Test
void createSCD_should_delete_ControlBlocks_DataSet_and_ExtRef_src_attributes() {
// Given
SCL ssd = SclTestMarshaller.getSCLFromResource("scd-ied-dtt-com-import-stds/ssd.xml");
SCL std = SclTestMarshaller.getSCLFromResource("scl-remove-controlBlocks-dataSet-extRefSrc/scl-with-control-blocks.xml");
// When
SCL scd = sclAutomationService.createSCD(ssd, headerDTO, List.of(std));
// Then
LN0 ln0 = new SclRootAdapter(scd).streamIEDAdapters()
.findFirst()
.map(iedAdapter -> iedAdapter.findLDeviceAdapterByLdInst("lDeviceInst1").orElseThrow())
.map(LDeviceAdapter::getLN0Adapter)
.map(SclElementAdapter::getCurrentElem)
.orElseThrow(() -> new RuntimeException("Test shouldn't fail here, please check your XML input file"));
assertThat(ln0.getDataSet()).isEmpty();
assertThat(ln0.getInputs().getExtRef()).hasSize(2);
assertThat(ln0.getInputs().getExtRef().getFirst().isSetSrcLDInst()).isFalse();
assertSclValidateXsd(scd);
}
}