1414import org .junit .jupiter .api .BeforeEach ;
1515import org .junit .jupiter .api .Test ;
1616import org .junit .jupiter .api .extension .ExtendWith ;
17+ import org .junit .jupiter .params .ParameterizedTest ;
18+ import org .junit .jupiter .params .provider .EnumSource ;
19+ import org .junit .jupiter .params .provider .EnumSource .Mode ;
1720import org .mockito .ArgumentCaptor ;
1821import org .mockito .Captor ;
1922import org .mockito .Mock ;
2225import org .openmuc .jdlms .AccessResultCode ;
2326import org .openmuc .jdlms .AttributeAddress ;
2427import org .openmuc .jdlms .DlmsConnection ;
28+ import org .openmuc .jdlms .GetResult ;
2529import org .openmuc .jdlms .ObisCode ;
2630import org .openmuc .jdlms .SetParameter ;
31+ import org .openmuc .jdlms .datatypes .DataObject ;
2732import org .opensmartgridplatform .adapter .protocol .dlms .domain .commands .utils .ObjectConfigServiceHelper ;
2833import org .opensmartgridplatform .adapter .protocol .dlms .domain .entities .DlmsDevice ;
2934import org .opensmartgridplatform .adapter .protocol .dlms .domain .entities .Protocol ;
@@ -61,6 +66,8 @@ class ClearAlarmRegisterCommandExecutorTest {
6166
6267 @ Mock private MessageMetadata messageMetadata ;
6368
69+ @ Mock private GetResult getResult ;
70+
6471 @ Captor private ArgumentCaptor <SetParameter > setParameterArgumentCaptor ;
6572
6673 private ClearAlarmRegisterCommandExecutor executor ;
@@ -111,7 +118,7 @@ void shouldExecuteForProtocolSmr55() throws ProtocolAdapterException, IOExceptio
111118 }
112119
113120 @ Test
114- void connectionProblemAlarmRegister1 () throws IOException , ProtocolAdapterException {
121+ void connectionProblemAlarmRegister1 () throws IOException {
115122 when (this .dlmsConnection .set (this .setParameterArgumentCaptor .capture ()))
116123 .thenThrow (new IOException ());
117124
@@ -126,7 +133,7 @@ void connectionProblemAlarmRegister1() throws IOException, ProtocolAdapterExcept
126133 }
127134
128135 @ Test
129- void nullResultAlarmRegister1 () throws IOException , ProtocolAdapterException {
136+ void nullResultAlarmRegister1 () throws IOException {
130137 when (this .dlmsConnection .set (this .setParameterArgumentCaptor .capture ())).thenReturn (null );
131138
132139 final DlmsDevice dlmsDevice = new DlmsDevice ("SMR 5.2 device" );
@@ -140,7 +147,7 @@ void nullResultAlarmRegister1() throws IOException, ProtocolAdapterException {
140147 }
141148
142149 @ Test
143- void connectionProblemAlarmRegister2 () throws IOException , ProtocolAdapterException {
150+ void connectionProblemAlarmRegister2 () throws IOException {
144151 when (this .dlmsConnection .set (any (SetParameter .class )))
145152 .thenReturn (AccessResultCode .SUCCESS )
146153 .thenThrow (new IOException ());
@@ -156,7 +163,7 @@ void connectionProblemAlarmRegister2() throws IOException, ProtocolAdapterExcept
156163 }
157164
158165 @ Test
159- void nullResultAlarmRegister2 () throws IOException , ProtocolAdapterException {
166+ void successRegister1AndNullResultAlarmRegister2 () throws IOException {
160167 when (this .dlmsConnection .set (this .setParameterArgumentCaptor .capture ()))
161168 .thenReturn (AccessResultCode .SUCCESS )
162169 .thenReturn (null );
@@ -211,7 +218,7 @@ void resultAlarmRegister2() throws ProtocolAdapterException, IOException {
211218 }
212219
213220 @ Test
214- void connectionProblemAlarmRegister3 () throws IOException , ProtocolAdapterException {
221+ void connectionProblemAlarmRegister3 () throws IOException {
215222 when (this .dlmsConnection .set (any (SetParameter .class )))
216223 .thenReturn (AccessResultCode .SUCCESS )
217224 .thenReturn (AccessResultCode .SUCCESS )
@@ -228,7 +235,7 @@ void connectionProblemAlarmRegister3() throws IOException, ProtocolAdapterExcept
228235 }
229236
230237 @ Test
231- void nullResultAlarmRegister3 () throws IOException , ProtocolAdapterException {
238+ void successRegister1And2AndNullResultAlarmRegister3 () throws IOException {
232239 when (this .dlmsConnection .set (this .setParameterArgumentCaptor .capture ()))
233240 .thenReturn (AccessResultCode .SUCCESS )
234241 .thenReturn (AccessResultCode .SUCCESS )
@@ -286,16 +293,33 @@ void resultAlarmRegister3() throws ProtocolAdapterException, IOException {
286293 assertThat (accessResultCode ).isEqualTo (AccessResultCode .SUCCESS );
287294 }
288295
289- void setupAlarmRegister1 (final DlmsDevice dlmsDevice ) throws ProtocolAdapterException {
296+ @ ParameterizedTest
297+ @ EnumSource (
298+ value = DlmsObjectType .class ,
299+ names = {"ALARM_REGISTER_1" , "ALARM_REGISTER_2" , "ALARM_REGISTER_3" },
300+ mode = Mode .INCLUDE )
301+ void noAlarmPresentInRegister (final DlmsObjectType alarmType )
302+ throws IOException , ProtocolAdapterException {
303+ final DlmsDevice dlmsDevice = new DlmsDevice ("SMR 5.2 device" );
304+ this .setupAlarmRegisterWithNoAlarmPresent (dlmsDevice , alarmType );
305+ final AccessResultCode accessResultCode =
306+ this .executor .execute (this .connectionManager , dlmsDevice , this .dto , this .messageMetadata );
307+ assertThat (accessResultCode ).isEqualTo (AccessResultCode .SUCCESS );
308+ }
309+
310+ void setupAlarmRegister1 (final DlmsDevice dlmsDevice ) throws IOException {
290311
291312 this .mockAlarmCosemObject (
292313 dlmsDevice , OBIS_CODE_ALARM_REGISTER_1 , DlmsObjectType .ALARM_REGISTER_1 .name ());
293314
294315 when (this .connectionManager .getDlmsMessageListener ()).thenReturn (this .dlmsMessageListener );
295316 when (this .connectionManager .getConnection ()).thenReturn (this .dlmsConnection );
317+ when (this .dlmsConnection .get (any (AttributeAddress .class ))).thenReturn (this .getResult );
318+ when (this .getResult .getResultCode ()).thenReturn (AccessResultCode .SUCCESS );
319+ when (this .getResult .getResultData ()).thenReturn (DataObject .newUInteger32Data (1L ));
296320 }
297321
298- void setupAlarmRegister2 (final DlmsDevice dlmsDevice ) throws ProtocolAdapterException {
322+ void setupAlarmRegister2 (final DlmsDevice dlmsDevice ) throws IOException {
299323
300324 this .mockAlarmCosemObject (
301325 dlmsDevice , OBIS_CODE_ALARM_REGISTER_1 , DlmsObjectType .ALARM_REGISTER_1 .name ());
@@ -304,6 +328,9 @@ void setupAlarmRegister2(final DlmsDevice dlmsDevice) throws ProtocolAdapterExce
304328
305329 when (this .connectionManager .getDlmsMessageListener ()).thenReturn (this .dlmsMessageListener );
306330 when (this .connectionManager .getConnection ()).thenReturn (this .dlmsConnection );
331+ when (this .dlmsConnection .get (any (AttributeAddress .class ))).thenReturn (this .getResult );
332+ when (this .getResult .getResultCode ()).thenReturn (AccessResultCode .SUCCESS );
333+ when (this .getResult .getResultData ()).thenReturn (DataObject .newUInteger32Data (1L ));
307334 }
308335
309336 private void mockAlarmCosemObject (
@@ -321,24 +348,43 @@ private void mockAlarmCosemObject(
321348 .thenReturn (Optional .of (attributeAddress ));
322349 }
323350
324- void setupAlarmRegister3 (final DlmsDevice dlmsDevice ) throws ProtocolAdapterException {
351+ void setupAlarmRegister3 (final DlmsDevice dlmsDevice ) throws IOException {
325352 dlmsDevice .setProtocol ("SMR" , "5.5" );
326353
327354 this .mockAlarmCosemObject (
328- dlmsDevice ,
329- OBIS_CODE_ALARM_REGISTER_1 ,
330- org . opensmartgridplatform . dlms . objectconfig . DlmsObjectType .ALARM_REGISTER_1 .name ());
355+ dlmsDevice , OBIS_CODE_ALARM_REGISTER_1 , DlmsObjectType . ALARM_REGISTER_1 . name ());
356+ this . mockAlarmCosemObject (
357+ dlmsDevice , OBIS_CODE_ALARM_REGISTER_2 , DlmsObjectType .ALARM_REGISTER_2 .name ());
331358 this .mockAlarmCosemObject (
332- dlmsDevice ,
333- OBIS_CODE_ALARM_REGISTER_2 ,
334- org .opensmartgridplatform .dlms .objectconfig .DlmsObjectType .ALARM_REGISTER_2 .name ());
359+ dlmsDevice , OBIS_CODE_ALARM_REGISTER_3 , DlmsObjectType .ALARM_REGISTER_3 .name ());
360+
361+ when (this .connectionManager .getDlmsMessageListener ()).thenReturn (this .dlmsMessageListener );
362+ when (this .connectionManager .getConnection ()).thenReturn (this .dlmsConnection );
363+ when (this .dlmsConnection .get (any (AttributeAddress .class ))).thenReturn (this .getResult );
364+ when (this .getResult .getResultCode ()).thenReturn (AccessResultCode .SUCCESS );
365+ when (this .getResult .getResultData ()).thenReturn (DataObject .newUInteger32Data (1L ));
366+ }
367+
368+ void setupAlarmRegisterWithNoAlarmPresent (
369+ final DlmsDevice dlmsDevice , final DlmsObjectType alarmType ) throws IOException {
370+
335371 this .mockAlarmCosemObject (
336- dlmsDevice ,
337- OBIS_CODE_ALARM_REGISTER_3 ,
338- org .opensmartgridplatform .dlms .objectconfig .DlmsObjectType .ALARM_REGISTER_3 .name ());
372+ dlmsDevice , OBIS_CODE_ALARM_REGISTER_1 , DlmsObjectType .ALARM_REGISTER_1 .name ());
373+ if (alarmType == DlmsObjectType .ALARM_REGISTER_2
374+ || alarmType == DlmsObjectType .ALARM_REGISTER_3 ) {
375+ this .mockAlarmCosemObject (
376+ dlmsDevice , OBIS_CODE_ALARM_REGISTER_2 , DlmsObjectType .ALARM_REGISTER_2 .name ());
377+ }
378+ if (alarmType == DlmsObjectType .ALARM_REGISTER_3 ) {
379+ this .mockAlarmCosemObject (
380+ dlmsDevice , OBIS_CODE_ALARM_REGISTER_3 , DlmsObjectType .ALARM_REGISTER_3 .name ());
381+ }
339382
340383 when (this .connectionManager .getDlmsMessageListener ()).thenReturn (this .dlmsMessageListener );
341384 when (this .connectionManager .getConnection ()).thenReturn (this .dlmsConnection );
385+ when (this .dlmsConnection .get (any (AttributeAddress .class ))).thenReturn (this .getResult );
386+ when (this .getResult .getResultCode ()).thenReturn (AccessResultCode .SUCCESS );
387+ when (this .getResult .getResultData ()).thenReturn (DataObject .newUInteger32Data (0L ));
342388 }
343389
344390 void assertForOneRegister (final String protocol , final String protocolVersion )
@@ -353,6 +399,9 @@ void assertForOneRegister(final String protocol, final String protocolVersion)
353399 when (this .connectionManager .getConnection ()).thenReturn (this .dlmsConnection );
354400 when (this .dlmsConnection .set (this .setParameterArgumentCaptor .capture ()))
355401 .thenReturn (AccessResultCode .SUCCESS );
402+ when (this .dlmsConnection .get (any (AttributeAddress .class ))).thenReturn (this .getResult );
403+ when (this .getResult .getResultCode ()).thenReturn (AccessResultCode .SUCCESS );
404+ when (this .getResult .getResultData ()).thenReturn (DataObject .newUInteger32Data (1L ));
356405
357406 this .executor .execute (this .connectionManager , dlmsDevice , this .dto , this .messageMetadata );
358407
@@ -380,6 +429,9 @@ void assertForTwoRegisters(final String protocol, final String protocolVersion)
380429 when (this .connectionManager .getConnection ()).thenReturn (this .dlmsConnection );
381430 when (this .dlmsConnection .set (this .setParameterArgumentCaptor .capture ()))
382431 .thenReturn (AccessResultCode .SUCCESS );
432+ when (this .dlmsConnection .get (any (AttributeAddress .class ))).thenReturn (this .getResult );
433+ when (this .getResult .getResultCode ()).thenReturn (AccessResultCode .SUCCESS );
434+ when (this .getResult .getResultData ()).thenReturn (DataObject .newUInteger32Data (1L ));
383435
384436 this .executor .execute (this .connectionManager , dlmsDevice , this .dto , this .messageMetadata );
385437
@@ -417,6 +469,9 @@ void assertForThreeRegisters(final String protocol, final String protocolVersion
417469 when (this .connectionManager .getConnection ()).thenReturn (this .dlmsConnection );
418470 when (this .dlmsConnection .set (this .setParameterArgumentCaptor .capture ()))
419471 .thenReturn (AccessResultCode .SUCCESS );
472+ when (this .dlmsConnection .get (any (AttributeAddress .class ))).thenReturn (this .getResult );
473+ when (this .getResult .getResultCode ()).thenReturn (AccessResultCode .SUCCESS );
474+ when (this .getResult .getResultData ()).thenReturn (DataObject .newUInteger32Data (1L ));
420475
421476 this .executor .execute (this .connectionManager , dlmsDevice , this .dto , this .messageMetadata );
422477
0 commit comments