66
77import org .junit .jupiter .api .BeforeEach ;
88import org .junit .jupiter .api .Test ;
9+ import org .lfenergy .compas .scl2007b4 .model .LN0 ;
910import org .lfenergy .compas .scl2007b4 .model .SCL ;
1011import org .lfenergy .compas .sct .commons .dto .HeaderDTO ;
1112import org .lfenergy .compas .sct .commons .exception .ScdException ;
13+ import org .lfenergy .compas .sct .commons .scl .SclElementAdapter ;
1214import org .lfenergy .compas .sct .commons .scl .SclRootAdapter ;
15+ import org .lfenergy .compas .sct .commons .scl .ied .LDeviceAdapter ;
1316import org .lfenergy .compas .sct .commons .testhelpers .SclTestMarshaller ;
1417
18+ import java .lang .reflect .Constructor ;
19+ import java .lang .reflect .InvocationTargetException ;
1520import java .util .Arrays ;
1621import java .util .HashSet ;
1722import java .util .Set ;
1823
24+ import static org .assertj .core .api .Assertions .assertThat ;
25+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
1926import static org .junit .jupiter .api .Assertions .*;
2027import static org .lfenergy .compas .sct .commons .testhelpers .SclTestMarshaller .assertIsMarshallable ;
2128
@@ -24,7 +31,7 @@ class SclAutomationServiceTest {
2431 private HeaderDTO headerDTO ;
2532
2633 @ BeforeEach
27- void init (){
34+ void init () {
2835 headerDTO = new HeaderDTO ();
2936 headerDTO .setRevision ("hRevision" );
3037 headerDTO .setVersion ("hVersion" );
@@ -63,7 +70,7 @@ void createSCD_With_HItem() throws Exception {
6370 SclRootAdapter expectedSCD = SclAutomationService .createSCD (ssd , headerDTO , Set .of (std1 , std2 , std3 ));
6471 // Then
6572 assertNotNull (expectedSCD .getCurrentElem ().getHeader ().getId ());
66- assertEquals (1 , expectedSCD .getCurrentElem ().getHeader ().getHistory ().getHitem ().size ());
73+ assertEquals (1 , expectedSCD .getCurrentElem ().getHeader ().getHistory ().getHitem ().size ());
6774 assertEquals (1 , expectedSCD .getCurrentElem ().getSubstation ().size ());
6875 assertIsMarshallable (expectedSCD .getCurrentElem ());
6976 }
@@ -85,7 +92,7 @@ void createSCD_With_HItems() throws Exception {
8592 SCL std2 = SclTestMarshaller .getSCLFromFile ("/std_2.xml" );
8693 SCL std3 = SclTestMarshaller .getSCLFromFile ("/std_3.xml" );
8794 // When
88- SclRootAdapter expectedSCD = SclAutomationService .createSCD (ssd , headerDTO ,Set .of (std1 , std2 , std3 ));
95+ SclRootAdapter expectedSCD = SclAutomationService .createSCD (ssd , headerDTO , Set .of (std1 , std2 , std3 ));
8996 // Then
9097 assertNotNull (expectedSCD .getCurrentElem ().getHeader ().getId ());
9198 assertEquals (1 , expectedSCD .getCurrentElem ().getHeader ().getHistory ().getHitem ().size ());
@@ -98,8 +105,68 @@ void createSCD_SSD_Without_Substation() throws Exception {
98105 // Given
99106 SCL ssd = SclTestMarshaller .getSCLFromFile ("/scd-substation-import-ssd/ssd_without_substations.xml" );
100107 // When & Then
108+ Set <SCL > stdListEmpty = new HashSet <>();
101109 assertThrows (ScdException .class ,
102- () -> SclAutomationService .createSCD (ssd , headerDTO , new HashSet <>()) );
110+ () -> SclAutomationService .createSCD (ssd , headerDTO , stdListEmpty ));
111+ }
112+
113+ @ Test
114+ void createSCD_should_throw_exception_when_null_ssd () throws Exception {
115+ // Given
116+ HeaderDTO .HistoryItem historyItem = new HeaderDTO .HistoryItem ();
117+ historyItem .setWhat ("what" );
118+ historyItem .setWho ("me" );
119+ historyItem .setWhy ("because" );
120+ headerDTO .getHistoryItems ().add (historyItem );
121+ SCL std1 = SclTestMarshaller .getSCLFromFile ("/std_1.xml" );
122+ Set <SCL > stdList = Set .of (std1 );
123+
124+ // When & Then
125+ assertThrows (NullPointerException .class , () -> SclAutomationService .createSCD (null , headerDTO , stdList ));
103126 }
104127
128+ @ Test
129+ void createSCD_should_throw_exception_when_null_headerDTO () throws Exception {
130+ // Given
131+ SCL ssd = SclTestMarshaller .getSCLFromFile ("/scd-substation-import-ssd/ssd.xml" );
132+ SCL std1 = SclTestMarshaller .getSCLFromFile ("/std_1.xml" );
133+ Set <SCL > stdList = Set .of (std1 );
134+
135+ // When & Then
136+ assertThrows (NullPointerException .class , () -> SclAutomationService .createSCD (ssd , null , stdList ));
137+ }
138+
139+ @ Test
140+ void createSCD_should_delete_ControlBlocks_DataSet_and_ExtRef_src_attributes () throws Exception {
141+ // Given
142+ SCL ssd = SclTestMarshaller .getSCLFromFile ("/scd-ied-dtt-com-import-stds/ssd.xml" );
143+ SCL std = SclTestMarshaller .getSCLFromFile ("/scl-remove-controlBlocks-dataSet-extRefSrc/scl-with-control-blocks.xml" );
144+ // When
145+ SclRootAdapter expectedSCD = SclAutomationService .createSCD (ssd , headerDTO , Set .of (std ));
146+ // Then
147+ LN0 ln0 = expectedSCD .streamIEDAdapters ()
148+ .findFirst ()
149+ .map (iedAdapter -> iedAdapter .getLDeviceAdapterByLdInst ("lDeviceInst1" ).orElseThrow ())
150+ .map (LDeviceAdapter ::getLN0Adapter )
151+ .map (SclElementAdapter ::getCurrentElem )
152+ .orElseThrow (() -> new RuntimeException ("Test shouldn't fail here, please check your XML input file" ));
153+
154+ assertThat (ln0 .getDataSet ()).isEmpty ();
155+ assertThat (ln0 .getInputs ().getExtRef ()).hasSize (2 );
156+ assertFalse (ln0 .getInputs ().getExtRef ().get (0 ).isSetSrcLDInst ());
157+ assertIsMarshallable (expectedSCD .getCurrentElem ());
158+ }
159+
160+ @ Test
161+ void class_should_not_be_instantiable () {
162+ // Given
163+ Constructor <?>[] constructors = SclAutomationService .class .getDeclaredConstructors ();
164+ assertThat (constructors ).hasSize (1 );
165+ Constructor <?> constructor = constructors [0 ];
166+ constructor .setAccessible (true );
167+ // When & Then
168+ assertThatThrownBy (constructor ::newInstance )
169+ .isInstanceOf (InvocationTargetException .class )
170+ .getCause ().isInstanceOf (UnsupportedOperationException .class );
171+ }
105172}
0 commit comments