connect(); if (!isPlatformRegistered()) { register();
/** * {@inheritDoc} */ @PostConstruct public void afterPropertiesSet() throws Exception { doRegistration(); }
/** * Tests that unregister of platform is executed if connection to the server is established * and registration is performed. */ @Test public void unregisterPlatform() throws Exception { // first simulate connect long fakePlatformId = 3L; when(connection.isConnected()).thenReturn(true); when(configurationStorage.getAgentName()).thenReturn("testAgent"); when(versionService.getVersionAsString()).thenReturn("dummyVersion"); when(connection.register("testAgent", "dummyVersion")).thenReturn(agentConfiguration); when(agentConfiguration.getPlatformId()).thenReturn(fakePlatformId); platformManager.afterPropertiesSet(); platformManager.getPlatformId(); platformManager.unregisterPlatform(); verify(connection, times(1)).unregister(fakePlatformId); verify(connection, times(1)).disconnect(); }
@Test public void retrievePlatformId() throws Exception { long fakePlatformId = 3L; when(connection.isConnected()).thenReturn(true); when(configurationStorage.getAgentName()).thenReturn("testAgent"); when(versionService.getVersionAsString()).thenReturn("dummyVersion"); when(connection.register("testAgent", "dummyVersion")).thenReturn(agentConfiguration); when(agentConfiguration.getPlatformId()).thenReturn(fakePlatformId); platformManager.afterPropertiesSet(); long platformId = platformManager.getPlatformId(); assertThat(platformId, is(equalTo(fakePlatformId))); verify(connection, times(0)).connect(anyString(), anyInt()); }
when(platformManager.getPlatformId()).thenReturn(platformId); verify(platformManager).getPlatformId();
/** * {@inheritDoc} */ @Override @PreDestroy public void unregisterPlatform() { if (connection.isConnected() && isPlatformRegistered()) { try { connection.unregister(platformId); connection.disconnect(); platformId = -1; } catch (Throwable e) { // NOPMD log.warn("Could not un-register the platform."); } } }
/** * Test that unregister will not be called if there is no active connection to the server * and registration is not done at first place. */ @Test public void noUnregisterPlatform() throws Exception { // no unregister if no connection when(connection.isConnected()).thenReturn(false); platformManager.unregisterPlatform(); // no unregister if registration is not done at the first place when(connection.isConnected()).thenReturn(true); platformManager.unregisterPlatform(); verify(connection, times(0)).unregister(anyLong()); }
@Test public void connectAndRetrievePlatformId() throws Exception { String host = "localhost"; int port = 1099; RepositoryConfig repositoryConfig = mock(RepositoryConfig.class); when(repositoryConfig.getHost()).thenReturn(host); when(repositoryConfig.getPort()).thenReturn(port); when(configurationStorage.getRepositoryConfig()).thenReturn(repositoryConfig); when(configurationStorage.getAgentName()).thenReturn("testAgent"); when(versionService.getVersionAsString()).thenReturn("dummyVersion"); long fakePlatformId = 7L; when(connection.isConnected()).thenReturn(false); when(connection.register("testAgent", "dummyVersion")).thenReturn(agentConfiguration); when(agentConfiguration.getPlatformId()).thenReturn(fakePlatformId); platformManager.afterPropertiesSet(); long platformId = platformManager.getPlatformId(); assertThat(platformId, is(equalTo(fakePlatformId))); verify(connection, times(1)).connect(host, port); }
when(platformManager.getPlatformId()).thenReturn(platformId); verify(timer, times(3)).getCurrentTime(); statementHook2.secondAfterBody(coreService, methodId, sensorTypeId, object, parameters, result, false, registeredSensorConfig); verify(platformManager).getPlatformId(); verify(platformManager, times(2)).getPlatformId();