@Override public String getAgentId() { return getConfigurationHandler().get(CONFIG_IDENTIFICATION_AGENTID, CONFIG_DEFAULT_AGENTID); } }
@Override protected void onInit() throws Exception { getEventsHandler().addListener(this); // The controller is started *after* all other components, causing it to miss the initial configuration-update // event, hence we need to get the configuration ourselves for the first time... ConfigurationHandler config = getConfigurationHandler(); m_updateStreaming.set(config.getBoolean(CONFIG_CONTROLLER_STREAMING, m_updateStreaming.get())); m_fixPackage.set(config.getBoolean(CONFIG_CONTROLLER_FIXPACKAGES, m_fixPackage.get())); m_interval.set(config.getLong(CONFIG_CONTROLLER_SYNCINTERVAL, m_interval.get())); m_syncDelay.set(config.getLong(CONFIG_CONTROLLER_SYNCDELAY, m_syncDelay.get())); m_maxRetries.set(config.getLong(CONFIG_CONTROLLER_RETRIES, m_maxRetries.get())); logDebug("Config initialized: update: %s, fixPkg: %s, syncDelay: %d, syncInterval: %d, maxRetries: %d", m_updateStreaming.get(), m_fixPackage.get(), m_syncDelay.get(), m_interval.get(), m_maxRetries.get()); }
protected void configureAgent(ConfigurationHandler handler, String... configuration) { Map<String, String> config = new HashMap<>(); for (int i = 0; i < configuration.length; i += 2) { config.put(configuration[i], configuration[i + 1]); } handler.putAll(config); }
@Override protected void onInit() throws Exception { String urls = getConfigurationHandler().get(CONFIG_DISCOVERY_SERVERURLS, mergeUrls(m_defaultServerURLs)); boolean checkServerURLs = getConfigurationHandler().getBoolean(CONFIG_DISCOVERY_CHECKING, DEFAULT_CHECK_SERVER_URLS); m_defaultServerURLs = m_serverURLs = splitUrls(urls); // ACE-520 - checking URLs only makes sense if there is more than one URL available... m_defaultCheckURLs = m_checkURLs = checkServerURLs && m_defaultServerURLs.size() > 1; getEventsHandler().addListener(this); }
@Test public void testConfigBooleanProps() throws Exception { ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl(m_context); resetConfigurationHandler(configurationHandler); configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); configureAgent(configurationHandler, "boolean1", "true", "boolean2", "false"); assertEquals(configurationHandler.getBoolean("boolean1", false), true); assertEquals(configurationHandler.getBoolean("boolean2", true), false); assertEquals(configurationHandler.getBoolean("booleanX", true), true); assertEquals(configurationHandler.getBoolean("booleanY", false), false); }
@Test public void testConfigLongProps() throws Exception { ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl(m_context); resetConfigurationHandler(configurationHandler); configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); configureAgent(configurationHandler, "long1", "42", "long2", "4"); assertEquals(configurationHandler.getLong("long1", 0l), 42); assertEquals(configurationHandler.getLong("long2", 0l), 4l); assertEquals(configurationHandler.getLong("longX", 42l), 42l); }
assertEquals(configurationHandler.get(systemKey1, "default1"), "value1"); assertEquals(configurationHandler.get(systemKey2, "default2"), "value2"); assertEquals(configurationHandler.get(systemKey1, "qux"), "qux"); assertEquals(configurationHandler.get(systemKey2, "quu"), "quu"); assertEquals(configurationHandler.get(systemKey1, "qux"), "newvalue1"); assertEquals(configurationHandler.get(systemKey2, "quu"), "newvalue2"); assertEquals(configurationHandler.get(systemKey1, "qqq"), "newvalue1"); assertEquals(configurationHandler.get(systemKey2, "qqq"), "newvalue2");
@Test public void testBasicAuthOK() throws Exception { Map<String, String> props = new HashMap<>(); props.put(AgentConstants.CONFIG_CONNECTION_AUTHTYPE, Types.BASIC.name()); props.put(AgentConstants.CONFIG_CONNECTION_USERNAME, USERNAME); props.put(AgentConstants.CONFIG_CONNECTION_PASSWORD, PASSWORD); ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); configurationHandler.putAll(props); ConnectionHandler connectionHandler = m_agentContext.getHandler(ConnectionHandler.class); HttpURLConnection connection = (HttpURLConnection) connectionHandler.getConnection(m_basicAuthURL); assertEquals(connection.getResponseCode(), HttpServletResponse.SC_OK); } }
@Test public void testConfigClean() throws Exception { ConfigurationHandler configurationHandler = new ConfigurationHandlerImpl(m_context); resetConfigurationHandler(configurationHandler); configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); assertEquals(configurationHandler.get("key1", "default1"), "default1"); // should be persisted configurationHandler = new ConfigurationHandlerImpl(m_context); resetConfigurationHandler(configurationHandler); configurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); assertEquals(configurationHandler.get("key1", "default1"), "default1"); }
@Test public void testBasicAuthFORBIDDEN() throws Exception { Map<String, String> props = new HashMap<>(); props.put(AgentConstants.CONFIG_CONNECTION_AUTHTYPE, Types.NONE.name()); ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); configurationHandler.putAll(props); ConnectionHandler connectionHandler = m_agentContext.getHandler(ConnectionHandler.class); HttpURLConnection connection = (HttpURLConnection) connectionHandler.getConnection(m_basicAuthURL); assertEquals(connection.getResponseCode(), HttpServletResponse.SC_FORBIDDEN); }
@Test public void testEmptyIdentification() throws Exception { ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); reset(configurationHandler); expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class))) .andReturn(null).once(); replay(configurationHandler); IdentificationHandler identificationHandler = m_agentContext.getHandler(IdentificationHandler.class); assertNull(identificationHandler.getAgentId()); } }
@Test public void testUpdatedIdentification() throws Exception { ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); reset(configurationHandler); expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class))) .andReturn("qqq").once(); expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class))) .andReturn("yyy").once(); replay(configurationHandler); IdentificationHandler identificationHandler = m_agentContext.getHandler(IdentificationHandler.class); assertEquals(identificationHandler.getAgentId(), "qqq"); assertEquals(identificationHandler.getAgentId(), "yyy"); }
@Test public void testNoIdentification() throws Exception { ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); reset(configurationHandler); expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class))) .andReturn(null).once(); replay(configurationHandler); IdentificationHandler identificationHandler = m_agentContext.getHandler(IdentificationHandler.class); assertNull(identificationHandler.getAgentId()); }
@Test public void testAvailableIdentification() throws Exception { ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); reset(configurationHandler); expect(configurationHandler.get(eq(AgentConstants.CONFIG_IDENTIFICATION_AGENTID), anyObject(String.class))) .andReturn("qqq").once(); replay(configurationHandler); IdentificationHandler identificationHandler = m_agentContext.getHandler(IdentificationHandler.class); assertEquals(identificationHandler.getAgentId(), "qqq"); }
expect(configurationHandler.get(notNull(String.class), anyObject(String.class))).andReturn(null).anyTimes();