/** * Establish the connection to the server. * * @exception ConnectException * Throws a ConnectException if there was a problem connecting to the repository. */ private void connect() throws ConnectException { RepositoryConfig repositoryConfig = configurationStorage.getRepositoryConfig(); connection.connect(repositoryConfig.getHost(), repositoryConfig.getPort()); }
/** * {@inheritDoc} */ @Override public final void setRepository(String host, int port) throws StorageException { if ((null == host) || "".equals(host)) { throw new StorageException("Repository host name cannot be null or empty!"); } if (port < 1) { throw new StorageException("Repository port has to be greater than 0!"); } // can not reset repository if (null == repository) { this.repository = new RepositoryConfig(host, port); } if (log.isInfoEnabled()) { log.info("Repository definition added. Host: " + host + " Port: " + port); } }
@Test public void check() throws StorageException { configurationStorage.setRepository("localhost", 1099); String host = configurationStorage.getRepositoryConfig().getHost(); int port = configurationStorage.getRepositoryConfig().getPort(); assertThat(host, is(equalTo("localhost"))); assertThat(port, is(equalTo(1099))); }
@Test public void repositoryOnlySet() throws Exception { Properties properties = System.getProperties(); properties.remove(ConfigurationStorage.AGENT_NAME_PROPERTY); properties.put(ConfigurationStorage.REPOSITORY_PROPERTY, "localhost:8000"); configurationStorage.afterPropertiesSet(); assertThat(configurationStorage.getRepositoryConfig().getHost(), is("localhost")); assertThat(configurationStorage.getRepositoryConfig().getPort(), is(8000)); assertThat(configurationStorage.getAgentName(), is(not(nullValue()))); }
@Test public void repositoryAndAgentNameSet() throws Exception { String agentName = "agentName"; Properties properties = System.getProperties(); properties.put(ConfigurationStorage.REPOSITORY_PROPERTY, "localhost:8000"); properties.put(ConfigurationStorage.AGENT_NAME_PROPERTY, agentName); configurationStorage.afterPropertiesSet(); assertThat(configurationStorage.getRepositoryConfig().getHost(), is("localhost")); assertThat(configurationStorage.getRepositoryConfig().getPort(), is(8000)); assertThat(configurationStorage.getAgentName(), is(agentName)); }
@Test public void resetNotAllowed() throws StorageException { configurationStorage.setRepository("localhost", 1099); configurationStorage.setRepository("localhost1", 1200); String host = configurationStorage.getRepositoryConfig().getHost(); int port = configurationStorage.getRepositoryConfig().getPort(); assertThat(host, is(equalTo("localhost"))); assertThat(port, is(equalTo(1099))); } }
@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); }