/** * Called by our {@link DependencyTrackerImpl} when all dependencies are satisfied. */ public void componentStarted(BundleContext context) throws Exception { final File bundleDataArea = context.getDataFile(""); m_agentContext = new AgentContextImpl(bundleDataArea); m_agentContext.setHandler(LoggingHandler.class, new LoggingHandlerImpl(context)); m_agentContext.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(context)); m_agentContext.setHandler(EventsHandler.class, new EventsHandlerImpl(context)); m_agentContext.setHandler(ScheduledExecutorService.class, m_executorService); m_agentContext.setHandler(DownloadHandler.class, new DownloadHandlerImpl(bundleDataArea)); m_agentContext.setHandler(DeploymentHandler.class, new DeploymentHandlerImpl(context, m_packageAdmin)); m_agentContext.setHandler(AgentUpdateHandler.class, new AgentUpdateHandlerImpl(context)); m_agentContext.setHandler(FeedbackHandler.class, new FeedbackHandlerImpl()); IdentificationHandler identificationHandler = (m_identificationHandler != null) ? m_identificationHandler : new IdentificationHandlerImpl(); m_agentContext.setHandler(IdentificationHandler.class, identificationHandler); DiscoveryHandler discoveryHandler = (m_discoveryHandler != null) ? m_discoveryHandler : new DiscoveryHandlerImpl(); m_agentContext.setHandler(DiscoveryHandler.class, discoveryHandler); ConnectionHandler connectionHandler = (m_connectionHandler != null) ? m_connectionHandler : new ConnectionHandlerImpl(); m_agentContext.setHandler(ConnectionHandler.class, connectionHandler); m_agentContext.addComponent(new EventLoggerImpl(context)); // Lastly, inject the (custom) controller for this agent... m_agentContext.setController(m_controller); m_agentContext.start(); m_agentControlRegistration = context.registerService(AgentControl.class.getName(), new AgentControlImpl(m_agentContext), null); }
@Test public void testCurrentVersion() throws Exception { DeploymentHandler deploymentHandler = m_agentContext.getHandler(DeploymentHandler.class); Version current = deploymentHandler.getInstalledVersion(); assertNotNull(current); assertEquals(current, m_version2); }
/** * Called by our {@link DependencyTrackerImpl} when one or more dependencies are no longer satisfied. */ public void componentStopped(BundleContext context) throws Exception { try { if (m_agentControlRegistration != null) { m_agentControlRegistration.unregister(); } } finally { m_agentControlRegistration = null; try { m_agentContext.stop(); } finally { m_agentContext = null; } } }
private void startController(final Object object) { terminateRunningController(); initAgentContextAware(object); // In case of a Runnable, we start a separate thread that executes this task... if (object instanceof Runnable) { ScheduledExecutorService executorService = getHandler(ScheduledExecutorService.class); if (executorService == null || executorService.isShutdown()) { return; } m_future = executorService.submit(new Runnable() { private static final String NAME = "ACE Agent Controller"; @Override public void run() { // Annotate the name of the thread for debugging purposes... Thread.currentThread().setName(NAME); startAgentContextAware(object); ((Runnable) object).run(); stopAgentContextAware(object); } }); } else { // Expect the controller to handle its own execution... startAgentContextAware(object); } }
throw new IllegalStateException("Can not start context. Missing handler: " + handlerIface.getName()); initAgentContextAware(handler); initAgentContextAware(component); startAgentContextAware(handler); startAgentContextAware(component); startController(controller);
@BeforeClass public void setUpAgain() throws Exception { m_agentContextImpl = mockAgentContext(); m_agentContext = m_agentContextImpl; m_agentContextImpl.setHandler(IdentificationHandler.class, new IdentificationHandlerImpl()); m_agentContextImpl.start(); replayTestMocks(); }
private void resetConfigurationHandler(ConfigurationHandler configurationHandler) throws Exception { ConfigurationHandler oldConfigurationHandler = m_agentContextImpl.getHandler(ConfigurationHandler.class); if (oldConfigurationHandler instanceof ComponentBase) { ((ComponentBase) oldConfigurationHandler).stop(); } m_agentContextImpl.setHandler(ConfigurationHandler.class, configurationHandler); m_agentContextImpl.start(); } }
@BeforeClass public void setUp() throws Exception { m_agentContext = mockAgentContext(); m_agentContext.setHandler(DownloadHandler.class, new DownloadHandlerImpl(m_agentContext.getWorkDir())); m_agentContext.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl()); replayTestMocks(); m_agentContext.start(); }
protected AgentContextImpl mockAgentContext(String subDir) throws Exception { if (m_contextDir != null) { cleanDir(m_contextDir); m_contextDir.delete(); } m_contextDir = new File(getWorkDir(), subDir); m_contextDir.mkdirs(); cleanDir(m_contextDir); AgentContextImpl context = new AgentContextImpl(m_contextDir); for (Class<?> handlerClass : AgentContextImpl.KNOWN_HANDLERS) { if (ScheduledExecutorService.class.equals(handlerClass)) { // always inject a proper executor service that simply invokes synchronously... context.setHandler(ScheduledExecutorService.class, new SynchronousExecutorService()); } else { setMockedHandler(context, handlerClass); } } return context; }
protected <T> void setMockedHandler(AgentContextImpl context, Class<T> clazz) { context.setHandler(clazz, addTestMock(clazz)); }
@BeforeClass(alwaysRun = true) public void setUpOnceAgain() throws Exception { m_agentContext = mockAgentContext(); BundleContext bc = mockBundleContext(); m_eventsHandler = new EventsHandlerImpl(bc); m_agentContext.setHandler(EventsHandler.class, m_eventsHandler); m_agentContext.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(bc)); m_agentContext.setHandler(FeedbackHandler.class, new TestFeedbackHandler()); replayTestMocks(); m_agentContext.start(); }
@BeforeClass public void setUpAgain() throws Exception { m_agentContextImpl = mockAgentContext(); replayTestMocks(); BundleContext bc = mockBundleContext(); m_agentContextImpl.setHandler(FeedbackHandler.class, new FeedbackHandlerImpl()); m_agentContextImpl.setHandler(EventsHandler.class, new EventsHandlerImpl(bc)); m_agentContextImpl.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(bc)); m_agentContextImpl.start(); }
@Test public void testAvailableVersions() throws Exception { DeploymentHandler deploymentHandler = m_agentContext.getHandler(DeploymentHandler.class); SortedSet<Version> expected = new TreeSet<>(); expected.add(m_version1); expected.add(m_version2); expected.add(m_version3); SortedSet<Version> available = deploymentHandler.getAvailableVersions(); assertNotNull(available); assertFalse(available.isEmpty()); assertEquals(available, expected); }
@AfterClass public void tearDownAgain() throws Exception { m_agentContextImpl.stop(); verifyTestMocks(); }
@BeforeClass public void setUpOnceAgain() throws Exception { m_webServer = new TestWebServer(PORT, "/", "generated"); m_webServer.start(); m_availableURL1 = new URL("http://localhost:" + PORT); m_availableURL2 = new URL("http://127.0.0.1:" + PORT); m_unavailableURL1 = new URL("http://localhost:19999"); m_unavailableURL2 = new URL("http://127.0.0.1:19999"); BundleContext bc = mockBundleContext(); m_agentContextImpl = mockAgentContext(); m_agentContext = m_agentContextImpl; // Make sure the default server URL is not reachable, as used for this test... m_agentContextImpl.setHandler(DiscoveryHandler.class, new DiscoveryHandlerImpl(m_unavailableURL1.toExternalForm(), true)); m_agentContextImpl.setHandler(EventsHandler.class, new EventsHandlerImpl(bc)); m_agentContextImpl.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(bc)); m_agentContextImpl.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl()); replayTestMocks(); m_agentContextImpl.start(); }
@Test public void testPackageSize() throws Exception { DeploymentHandler deploymentHandler = m_agentContext.getHandler(DeploymentHandler.class); long packageSize = deploymentHandler.getSize(m_version1, true); assertEquals(packageSize, m_remotePackageSize); }
@AfterMethod public void tearDownOnceAgain() throws Exception { m_dummyInputStream.close(); m_agentContext.stop(); verifyTestMocks(); clearTestMocks(); }
@BeforeClass public void setUpOnceAgain() throws Exception { m_basicAuthURL = new URL("http://localhost:" + PORT + "/basicauth"); m_webServer = new TestWebServer(PORT, "/", "generated"); m_webServer.addServlet(new BasicAuthServlet(USERNAME, PASSWORD), "/basicauth/*"); m_webServer.start(); BundleContext bc = mockBundleContext(); m_agentContext = mockAgentContext(); m_agentContext.setHandler(EventsHandler.class, new EventsHandlerImpl(bc)); m_agentContext.setHandler(ConfigurationHandler.class, new ConfigurationHandlerImpl(bc)); m_agentContext.setHandler(ConnectionHandler.class, new ConnectionHandlerImpl()); replayTestMocks(); m_agentContext.start(); }
@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); }
@AfterMethod(alwaysRun = true) public void tearDownAgain(Method method) throws Exception { m_agentContextImpl.stop(); verifyTestMocks(); clearTestMocks(); }