DlmsConnectionHelper { public DlmsConnectionManager createConnectionForDevice(final DlmsDevice device, final DlmsMessageListener messageListener) throws OsgpException { if (device.needsInvocationCounter() && !device.isInvocationCounterInitialized()) { this.invocationCounterManager.initializeInvocationCounter(device); } try { return this.connectionFactory.getConnection(device, messageListener); } catch (final ConnectionException e) { if (device.needsInvocationCounter() && this.indicatesInvocationCounterOutOfSync(e)) { this.resetInvocationCounter(device); } throw e; } } @Autowired  DlmsConnectionHelper(final InvocationCounterManager invocationCounterManager,
            final DlmsConnectionFactory connectionFactory); DlmsConnectionManager createConnectionForDevice(final DlmsDevice device,
            final DlmsMessageListener messageListener);  }
@Test public void createsConnectionForDeviceThatDoesNotNeedInvocationCounter() throws Exception { final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(false).build(); final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener(); final DlmsConnectionManager connectionManager = mock(DlmsConnectionManager.class); when(this.connectionFactory.getConnection(device, listener)).thenReturn(connectionManager); final DlmsConnectionManager result = this.helper.createConnectionForDevice(device, listener); assertThat(result).isSameAs(connectionManager); }
@Test public void initializesInvocationCounterForDeviceThatNeedsInvocationCounterWithInvocationCounterUninitialized() throws Exception { final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true) .withProtocol("SMR") .withInvocationCounter(null) .build(); final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener(); final DlmsConnectionManager connectionManager = mock(DlmsConnectionManager.class); when(this.connectionFactory.getConnection(device, listener)).thenReturn(connectionManager); this.helper.createConnectionForDevice(device, listener); verify(this.invocationCounterManager).initializeInvocationCounter(device); }
@Test public void createsConnectionForDeviceThatNeedsInvocationCounterWithInvocationCounterInitialized() throws Exception { final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true).withInvocationCounter(123L).build(); final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener(); final DlmsConnectionManager connectionManager = mock(DlmsConnectionManager.class); when(this.connectionFactory.getConnection(device, listener)).thenReturn(connectionManager); final DlmsConnectionManager result = this.helper.createConnectionForDevice(device, listener); assertThat(result).isSameAs(connectionManager); verifyNoMoreInteractions(this.invocationCounterManager); }
@Test public void doesNotResetInvocationCounterWhenInvocationCounterIsOutOfSyncForDeviceThatNeedsNoInvocationCounter() throws Exception { final DlmsDevice device = new DlmsDeviceBuilder().withHls5Active(true) .withProtocol("DSMR") .withInvocationCounter(123L) .build(); final DlmsMessageListener listener = new InvocationCountingDlmsMessageListener(); final ConnectionException exception = new ConnectionException( "Error creating connection for device E0051004228715518 with Ip address:62.133.88.34 Port:null " + "UseHdlc:false UseSn:false Message:Socket was closed by remote host."); doThrow(exception).when(this.connectionFactory).getConnection(device, listener); try { this.helper.createConnectionForDevice(device, listener); fail("Expected ConnectionException"); } catch (final ConnectionException e) { } verifyNoMoreInteractions(this.invocationCounterManager); }