Mercurial > hg > release > thermostat-0.9
changeset 74:61fb3c8f88b8
Refactor Storage to be indepdendent of agent classes.
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Tue, 21 Feb 2012 22:17:37 +0100 |
parents | b024cd568e5f |
children | d7179f3eceb7 503470b769c6 |
files | agent/pom.xml agent/src/main/java/com/redhat/thermostat/agent/Agent.java agent/src/main/java/com/redhat/thermostat/agent/config/StartupConfiguration.java agent/src/main/java/com/redhat/thermostat/agent/storage/AgentInformation.java agent/src/main/java/com/redhat/thermostat/agent/storage/BackendInformation.java agent/src/main/java/com/redhat/thermostat/agent/storage/MongoStorage.java agent/src/main/java/com/redhat/thermostat/agent/storage/Storage.java agent/src/main/java/com/redhat/thermostat/backend/BackendRegistry.java agent/src/test/java/com/redhat/thermostat/agent/AgentTest.java |
diffstat | 9 files changed, 276 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/agent/pom.xml Wed Feb 15 14:18:44 2012 -0500 +++ b/agent/pom.xml Tue Feb 21 22:17:37 2012 +0100 @@ -59,6 +59,12 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>1.9.0</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>com.redhat.thermostat</groupId> <artifactId>thermostat-common</artifactId> <version>${project.version}</version>
--- a/agent/src/main/java/com/redhat/thermostat/agent/Agent.java Wed Feb 15 14:18:44 2012 -0500 +++ b/agent/src/main/java/com/redhat/thermostat/agent/Agent.java Tue Feb 21 22:17:37 2012 +0100 @@ -41,6 +41,8 @@ import com.redhat.thermostat.agent.config.ConfigurationWatcher; import com.redhat.thermostat.agent.config.StartupConfiguration; +import com.redhat.thermostat.agent.storage.AgentInformation; +import com.redhat.thermostat.agent.storage.BackendInformation; import com.redhat.thermostat.agent.storage.Storage; import com.redhat.thermostat.backend.Backend; import com.redhat.thermostat.backend.BackendRegistry; @@ -97,7 +99,7 @@ public synchronized void start() throws LaunchException { if (configWatcherThread == null) { startBackends(); - storage.addAgentInformation(config, backendRegistry); + storage.addAgentInformation(createAgentInformation()); configWatcherThread = new Thread(new ConfigurationWatcher(storage, backendRegistry), "Configuration Watcher"); configWatcherThread.start(); } else { @@ -105,6 +107,19 @@ } } + private AgentInformation createAgentInformation() { + AgentInformation agentInfo = new AgentInformation(); + agentInfo.setStartTime(config.getStartTime()); + for (Backend backend : backendRegistry.getAll()) { + BackendInformation backendInfo = new BackendInformation(); + backendInfo.setName(backend.getName()); + backendInfo.setDescription(backend.getDescription()); + backendInfo.setObserveNewJvm(backend.getObserveNewJvm()); + agentInfo.addBackend(backendInfo); + } + return agentInfo; + } + public synchronized void stop() { if (configWatcherThread != null) { configWatcherThread.interrupt(); // This thread checks for its own interrupted state and ends if interrupted.
--- a/agent/src/main/java/com/redhat/thermostat/agent/config/StartupConfiguration.java Wed Feb 15 14:18:44 2012 -0500 +++ b/agent/src/main/java/com/redhat/thermostat/agent/config/StartupConfiguration.java Tue Feb 21 22:17:37 2012 +0100 @@ -55,7 +55,7 @@ import com.redhat.thermostat.common.LaunchException; import com.redhat.thermostat.common.utils.LoggingUtils; -public final class StartupConfiguration { +public class StartupConfiguration { private static Logger logger = LoggingUtils.getLogger(StartupConfiguration.class);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/main/java/com/redhat/thermostat/agent/storage/AgentInformation.java Tue Feb 21 22:17:37 2012 +0100 @@ -0,0 +1,63 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.storage; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class AgentInformation { + + private long startTime; + private List<BackendInformation> backends = new ArrayList<BackendInformation>(); + + public long getStartTime() { + return startTime; + } + + public void setStartTime(long startTime) { + this.startTime = startTime; + } + + public List<BackendInformation> getBackends() { + return Collections.unmodifiableList(backends); + } + + public void addBackend(BackendInformation backend) { + backends.add(backend); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/main/java/com/redhat/thermostat/agent/storage/BackendInformation.java Tue Feb 21 22:17:37 2012 +0100 @@ -0,0 +1,82 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent.storage; + +import java.util.List; +import java.util.Map; + +public class BackendInformation { + + private String name; + private String description; + private boolean observeNewJvm; + private List<Integer> pids; + private Map<String, String> configuration; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public boolean isObserveNewJvm() { + return observeNewJvm; + } + + public void setObserveNewJvm(boolean observeNewJvm) { + this.observeNewJvm = observeNewJvm; + } + + public List<Integer> getPids() { + return pids; + } + + public Map<String, String> getConfiguration() { + return configuration; + } + +}
--- a/agent/src/main/java/com/redhat/thermostat/agent/storage/MongoStorage.java Wed Feb 15 14:18:44 2012 -0500 +++ b/agent/src/main/java/com/redhat/thermostat/agent/storage/MongoStorage.java Tue Feb 21 22:17:37 2012 +0100 @@ -93,9 +93,9 @@ } @Override - public void addAgentInformation(StartupConfiguration config, BackendRegistry registry) { + public void addAgentInformation(AgentInformation agentInfo) { DBCollection configCollection = db.getCollection(StorageConstants.CATEGORY_AGENT_CONFIG); - DBObject toInsert = createConfigDBObject(config, registry); + DBObject toInsert = createConfigDBObject(agentInfo); /* cast required to disambiguate between putAll(BSONObject) and putAll(Map) */ toInsert.putAll((BSONObject) getAgentDBObject()); configCollection.insert(toInsert, WriteConcern.SAFE); @@ -255,20 +255,20 @@ return coll; } - private DBObject createConfigDBObject(StartupConfiguration config, BackendRegistry registry) { + private DBObject createConfigDBObject(AgentInformation agentInfo) { BasicDBObject result = getAgentDBObject(); - result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_START_TIME, config.getStartTime()); + result.put(StorageConstants.KEY_AGENT_CONFIG_AGENT_START_TIME, agentInfo.getStartTime()); BasicDBObject backends = new BasicDBObject(); - for (Backend backend : registry.getAll()) { + for (BackendInformation backend : agentInfo.getBackends()) { backends.put(backend.getName(), createBackendConfigDBObject(backend)); } result.put(StorageConstants.KEY_AGENT_CONFIG_BACKENDS, backends); return result; } - private DBObject createBackendConfigDBObject(Backend backend) { + private DBObject createBackendConfigDBObject(BackendInformation backend) { BasicDBObject result = new BasicDBObject(); - Map<String, String> configMap = backend.getConfigurationMap(); + Map<String, String> configMap = backend.getConfiguration(); result.append(StorageConstants.KEY_AGENT_CONFIG_BACKEND_NAME, backend.getName()); result.append(StorageConstants.KEY_AGENT_CONFIG_BACKEND_DESC, backend.getDescription()); result.append(StorageConstants.KEY_AGENT_CONFIG_BACKEND_ACTIVE, createBackendActiveDBObject(backend)); @@ -278,9 +278,9 @@ return result; } - private DBObject createBackendActiveDBObject(Backend backend) { + private DBObject createBackendActiveDBObject(BackendInformation backend) { BasicDBObject result = new BasicDBObject(); - result.append(StorageConstants.KEY_AGENT_CONFIG_BACKEND_NEW, backend.getObserveNewJvm()); + result.append(StorageConstants.KEY_AGENT_CONFIG_BACKEND_NEW, backend.isObserveNewJvm()); result.append(StorageConstants.KEY_AGENT_CONFIG_BACKEND_PIDS, new BasicDBList()); // TODO check which processes are already being listened to. return result;
--- a/agent/src/main/java/com/redhat/thermostat/agent/storage/Storage.java Wed Feb 15 14:18:44 2012 -0500 +++ b/agent/src/main/java/com/redhat/thermostat/agent/storage/Storage.java Tue Feb 21 22:17:37 2012 +0100 @@ -56,7 +56,7 @@ public abstract void setAgentId(UUID id); - public abstract void addAgentInformation(StartupConfiguration config, BackendRegistry registry); + public abstract void addAgentInformation(AgentInformation agentInfo); public abstract void removeAgentInformation();
--- a/agent/src/main/java/com/redhat/thermostat/backend/BackendRegistry.java Wed Feb 15 14:18:44 2012 -0500 +++ b/agent/src/main/java/com/redhat/thermostat/backend/BackendRegistry.java Tue Feb 21 22:17:37 2012 +0100 @@ -52,7 +52,7 @@ * A registry for {@link Backend}s. Each {@link Backend} should call * {@link #register(Backend)} to register itself. */ -public final class BackendRegistry { +public class BackendRegistry { private static final Logger logger = LoggingUtils.getLogger(BackendRegistry.class);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/agent/src/test/java/com/redhat/thermostat/agent/AgentTest.java Tue Feb 21 22:17:37 2012 +0100 @@ -0,0 +1,97 @@ +/* + * Copyright 2012 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.agent; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import com.redhat.thermostat.agent.config.StartupConfiguration; +import com.redhat.thermostat.agent.storage.AgentInformation; +import com.redhat.thermostat.agent.storage.BackendInformation; +import com.redhat.thermostat.agent.storage.Storage; +import com.redhat.thermostat.backend.Backend; +import com.redhat.thermostat.backend.BackendRegistry; + +public class AgentTest { + + @Test + public void testStartAgent() throws Exception { + // Setup class under test and test data (config, backendRegistry). + StartupConfiguration config = mock(StartupConfiguration.class); + when(config.getStartTime()).thenReturn(123L); + + Storage storage = mock(Storage.class); + + Backend backend = mock(Backend.class); + when(backend.getName()).thenReturn("testname"); + when(backend.getDescription()).thenReturn("testdesc"); + when(backend.getObserveNewJvm()).thenReturn(true); + when(backend.activate()).thenReturn(true); // TODO: activate() should not return anything and throw exception in error case. + Collection<Backend> backends = new ArrayList<Backend>(); + backends.add(backend); + + BackendRegistry backendRegistry = mock(BackendRegistry.class); + when(backendRegistry.getAll()).thenReturn(backends); + + // Start agent. + Agent agent = new Agent(backendRegistry, config, storage); + agent.start(); + + // Verify that backend has been activated and storage received the agent information. + verify(backend).activate(); + ArgumentCaptor<AgentInformation> argument = ArgumentCaptor.forClass(AgentInformation.class); + verify(storage).addAgentInformation(argument.capture()); + assertEquals(123, argument.getValue().getStartTime()); + List<BackendInformation> backendInfos = argument.getValue().getBackends(); + assertEquals(1, backendInfos.size()); + BackendInformation backend0 = backendInfos.get(0); + assertEquals("testname", backend0.getName()); + assertEquals("testdesc", backend0.getDescription()); + assertEquals(true, backend0.isObserveNewJvm()); + // TODO: We should probably also test getPIDs() and getConfiguration(), but it's not clear to me at this point + // what those should really do (and it looks like they're not implemented yet). + } +}