+ verify(statisticContext).close();
+ Assert.assertEquals(0, contextsMap.size());
+ }
+
+ private static Map<DeviceContext, StatisticsContext> getContextsMap(StatisticsManagerImpl statisticsManager) throws NoSuchFieldException, IllegalAccessException {
+ // HACK: contexts map for testing shall be accessed in some more civilized way
+ final Field contextsField = StatisticsManagerImpl.class.getDeclaredField("contexts");
+ Assert.assertNotNull(contextsField);
+ contextsField.setAccessible(true);
+ return (Map<DeviceContext, StatisticsContext>) contextsField.get(statisticsManager);
+ }
+
+ @Test
+ public void testGetStatisticsWorkMode() throws Exception {
+ final Future<RpcResult<GetStatisticsWorkModeOutput>> workMode = statisticsManager.getStatisticsWorkMode();
+ Assert.assertTrue(workMode.isDone());
+ Assert.assertTrue(workMode.get().isSuccessful());
+ Assert.assertNotNull(workMode.get().getResult());
+ Assert.assertEquals(StatisticsWorkMode.COLLECTALL, workMode.get().getResult().getMode());
+ }
+
+ /**
+ * switching to {@link StatisticsWorkMode#FULLYDISABLED}; no pollTimeout and no lifecycleRegistry
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testChangeStatisticsWorkMode1() throws Exception {
+ StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
+ when(statisticContext.getPollTimeout()).thenReturn(
+ Optional.<Timeout>absent());
+ when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
+ Collections.<ItemLifeCycleSource>emptyList());
+
+ getContextsMap(statisticsManager).put(mockedDeviceContext, statisticContext);
+
+ final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
+ new ChangeStatisticsWorkModeInputBuilder()
+ .setMode(StatisticsWorkMode.FULLYDISABLED);
+
+ Future<RpcResult<Void>> workMode = statisticsManager
+ .changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
+
+ checkWorkModeChangeOutcome(workMode);
+ Mockito.verify(itemLifeCycleRegistry).getLifeCycleSources();
+ Mockito.verify(statisticContext).getPollTimeout();
+ }
+
+ private static void checkWorkModeChangeOutcome(Future<RpcResult<Void>> workMode) throws InterruptedException, java.util.concurrent.ExecutionException {
+ Assert.assertTrue(workMode.isDone());
+ Assert.assertTrue(workMode.get().isSuccessful());
+ }
+
+
+ /**
+ * switching to {@link StatisticsWorkMode#FULLYDISABLED}; with pollTimeout and lifecycleRegistry
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testChangeStatisticsWorkMode2() throws Exception {
+ Timeout pollTimeout = Mockito.mock(Timeout.class);
+ ItemLifeCycleSource itemLifecycleSource = Mockito.mock(ItemLifeCycleSource.class);
+ StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
+ when(statisticContext.getPollTimeout()).thenReturn(
+ Optional.of(pollTimeout));
+ when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
+ Collections.singletonList(itemLifecycleSource));
+
+ getContextsMap(statisticsManager).put(mockedDeviceContext, statisticContext);
+
+ final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
+ new ChangeStatisticsWorkModeInputBuilder()
+ .setMode(StatisticsWorkMode.FULLYDISABLED);
+
+ Future<RpcResult<Void>> workMode = statisticsManager.changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
+ checkWorkModeChangeOutcome(workMode);
+
+ Mockito.verify(itemLifeCycleRegistry).getLifeCycleSources();
+ Mockito.verify(statisticContext).getPollTimeout();
+ Mockito.verify(pollTimeout).cancel();
+ Mockito.verify(itemLifecycleSource).setItemLifecycleListener(Matchers.<ItemLifecycleListener>any());
+ }
+
+ /**
+ * switching to {@link StatisticsWorkMode#FULLYDISABLED} and back
+ * to {@link StatisticsWorkMode#COLLECTALL}; with lifecycleRegistry and pollTimeout
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testChangeStatisticsWorkMode3() throws Exception {
+ Timeout pollTimeout = Mockito.mock(Timeout.class);
+ ItemLifeCycleSource itemLifecycleSource = Mockito.mock(ItemLifeCycleSource.class);
+ Mockito.doNothing().when(itemLifecycleSource)
+ .setItemLifecycleListener(itemLifeCycleListenerCapt.capture());
+
+ StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
+ when(statisticContext.getPollTimeout()).thenReturn(
+ Optional.of(pollTimeout));
+ when(statisticContext.getItemLifeCycleListener()).thenReturn(
+ Mockito.mock(ItemLifecycleListener.class));
+ when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
+ Collections.singletonList(itemLifecycleSource));
+
+ getContextsMap(statisticsManager).put(mockedDeviceContext, statisticContext);
+
+ final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
+ new ChangeStatisticsWorkModeInputBuilder()
+ .setMode(StatisticsWorkMode.FULLYDISABLED);
+
+ Future<RpcResult<Void>> workMode;
+ workMode = statisticsManager.changeStatisticsWorkMode(
+ changeStatisticsWorkModeInputBld.build());
+ checkWorkModeChangeOutcome(workMode);
+
+ changeStatisticsWorkModeInputBld.setMode(StatisticsWorkMode.COLLECTALL);
+ workMode = statisticsManager.changeStatisticsWorkMode(
+ changeStatisticsWorkModeInputBld.build());
+ checkWorkModeChangeOutcome(workMode);
+
+ Mockito.verify(itemLifeCycleRegistry, Mockito.times(2)).getLifeCycleSources();
+ Mockito.verify(statisticContext).getPollTimeout();
+ Mockito.verify(pollTimeout).cancel();
+
+ final List<ItemLifecycleListener> itemLifeCycleListenerValues = itemLifeCycleListenerCapt.getAllValues();
+ Assert.assertEquals(2, itemLifeCycleListenerValues.size());
+ Assert.assertNotNull(itemLifeCycleListenerValues.get(0));
+ Assert.assertNull(itemLifeCycleListenerValues.get(1));
+ }
+
+ @Test
+ public void testClose() throws Exception {
+ statisticsManager.close();
+ Mockito.verify(serviceControlRegistration).close();
+ }
+
+ @Test
+ public void testCalculateTimerDelay() throws Exception {
+ TimeCounter timeCounter = Mockito.mock(TimeCounter.class);
+ when(timeCounter.getAverageTimeBetweenMarks()).thenReturn(2000L, 4000L);
+
+ statisticsManager.calculateTimerDelay(timeCounter);
+ Assert.assertEquals(3000L, StatisticsManagerImpl.getCurrentTimerDelay());
+ statisticsManager.calculateTimerDelay(timeCounter);
+ Assert.assertEquals(6000L, StatisticsManagerImpl.getCurrentTimerDelay());