Mercurial > hg > release > thermostat-0.4
changeset 617:a325470a7366
Merge
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Tue, 18 Sep 2012 10:48:11 +0200 |
parents | 838eb2700f6a (diff) 121a91084133 (current diff) |
children | 6a179175bb1b 593c88ea5d0b |
files | |
diffstat | 19 files changed, 443 insertions(+), 414 deletions(-) [+] |
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/AgentInfoConverter.java Wed Sep 12 12:44:35 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* - * 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.common.dao; - -import com.redhat.thermostat.common.model.AgentInformation; -import com.redhat.thermostat.common.storage.Chunk; -import com.redhat.thermostat.common.storage.Key; - -public class AgentInfoConverter { - - public Chunk toChunk(AgentInformation agentInfo) { - Chunk agentChunk = new Chunk(AgentInfoDAO.CATEGORY, true); - - agentChunk.put(Key.AGENT_ID, agentInfo.getAgentId()); - agentChunk.put(AgentInfoDAO.START_TIME_KEY, agentInfo.getStartTime()); - agentChunk.put(AgentInfoDAO.STOP_TIME_KEY, agentInfo.getStopTime()); - agentChunk.put(AgentInfoDAO.ALIVE_KEY, agentInfo.isAlive()); - agentChunk.put(AgentInfoDAO.CONFIG_LISTEN_ADDRESS, agentInfo.getConfigListenAddress()); - - return agentChunk; - } - - public AgentInformation fromChunk(Chunk agentChunk) { - AgentInformation agentInfo = new AgentInformation(); - - agentInfo.setAgentId(agentChunk.get(Key.AGENT_ID)); - agentInfo.setStartTime(agentChunk.get(AgentInfoDAO.START_TIME_KEY)); - agentInfo.setStopTime(agentChunk.get(AgentInfoDAO.STOP_TIME_KEY)); - agentInfo.setAlive(agentChunk.get(AgentInfoDAO.ALIVE_KEY)); - agentInfo.setConfigListenAddress(agentChunk.get(AgentInfoDAO.CONFIG_LISTEN_ADDRESS)); - - return agentInfo; - } -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/AgentInfoDAOImpl.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/dao/AgentInfoDAOImpl.java Tue Sep 18 10:48:11 2012 +0200 @@ -40,17 +40,17 @@ import java.util.List; import com.redhat.thermostat.common.model.AgentInformation; -import com.redhat.thermostat.common.storage.Chunk; import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query; import com.redhat.thermostat.common.storage.Query.Criteria; +import com.redhat.thermostat.common.storage.Remove; import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.common.storage.Update; public class AgentInfoDAOImpl implements AgentInfoDAO { private final Storage storage; - private final AgentInfoConverter converter = new AgentInfoConverter(); public AgentInfoDAOImpl(Storage storage) { this.storage = storage; @@ -108,14 +108,18 @@ @Override public void removeAgentInformation(AgentInformation agentInfo) { - Chunk chunk = new Chunk(CATEGORY, true); - chunk.put(Key.AGENT_ID, agentInfo.getAgentId()); - storage.removeChunk(chunk); + Remove remove = storage.createRemove().from(CATEGORY).where(Key.AGENT_ID, agentInfo.getAgentId()); + storage.removePojo(remove); } @Override public void updateAgentInformation(AgentInformation agentInfo) { - storage.updateChunk(converter.toChunk(agentInfo)); + Update update = storage.createUpdate().from(CATEGORY).where(Key.AGENT_ID, agentInfo.getAgentId()) + .set(START_TIME_KEY, agentInfo.getStartTime()) + .set(STOP_TIME_KEY, agentInfo.getStopTime()) + .set(ALIVE_KEY, agentInfo.isAlive()) + .set(CONFIG_LISTEN_ADDRESS, agentInfo.getConfigListenAddress()); + storage.updatePojo(update); } }
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/BackendInfoConverter.java Wed Sep 12 12:44:35 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -/* - * 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.common.dao; - -import java.util.List; - -import com.redhat.thermostat.common.model.BackendInformation; -import com.redhat.thermostat.common.storage.Chunk; - -public class BackendInfoConverter { - - public Chunk toChunk(BackendInformation backendInfo) { - Chunk chunk = new Chunk(BackendInfoDAO.CATEGORY, false); - - chunk.put(BackendInfoDAO.BACKEND_NAME, backendInfo.getName()); - chunk.put(BackendInfoDAO.BACKEND_DESCRIPTION, backendInfo.getDescription()); - chunk.put(BackendInfoDAO.IS_ACTIVE, backendInfo.isActive()); - List<Integer> pids = backendInfo.getPids(); - chunk.put(BackendInfoDAO.PIDS_TO_MONITOR, pids); - chunk.put(BackendInfoDAO.SHOULD_MONITOR_NEW_PROCESSES, backendInfo.isObserveNewJvm()); - - return chunk; - } - - public BackendInformation fromChunk(Chunk parseFrom) { - if (!parseFrom.getCategory().equals(BackendInfoDAO.CATEGORY)) { - throw new IllegalArgumentException("chunk not a " + BackendInfoDAO.CATEGORY); - } - - BackendInformation info = new BackendInformation(); - - info.setName(parseFrom.get(BackendInfoDAO.BACKEND_NAME)); - info.setDescription(parseFrom.get(BackendInfoDAO.BACKEND_DESCRIPTION)); - info.setActive(parseFrom.get(BackendInfoDAO.IS_ACTIVE)); - info.setPids(parseFrom.get(BackendInfoDAO.PIDS_TO_MONITOR)); - info.setObserveNewJvm(parseFrom.get(BackendInfoDAO.SHOULD_MONITOR_NEW_PROCESSES)); - - return info; - } -}
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/BackendInfoDAOImpl.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/dao/BackendInfoDAOImpl.java Tue Sep 18 10:48:11 2012 +0200 @@ -40,16 +40,15 @@ import java.util.List; import com.redhat.thermostat.common.model.BackendInformation; -import com.redhat.thermostat.common.storage.Chunk; import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query; +import com.redhat.thermostat.common.storage.Query.Criteria; +import com.redhat.thermostat.common.storage.Remove; import com.redhat.thermostat.common.storage.Storage; -import com.redhat.thermostat.common.storage.Query.Criteria; public class BackendInfoDAOImpl implements BackendInfoDAO { - private final BackendInfoConverter converter = new BackendInfoConverter(); private final Storage storage; public BackendInfoDAOImpl(Storage storage) { @@ -79,8 +78,8 @@ @Override public void removeBackendInformation(BackendInformation info) { - Chunk toRemove = converter.toChunk(info); - storage.removeChunk(toRemove); + Remove remove = storage.createRemove().from(CATEGORY).where(BACKEND_NAME, info.getName()); + storage.removePojo(remove); } }
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/VmInfoDAOImpl.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/dao/VmInfoDAOImpl.java Tue Sep 18 10:48:11 2012 +0200 @@ -41,12 +41,12 @@ import java.util.List; import com.redhat.thermostat.common.model.VmInfo; -import com.redhat.thermostat.common.storage.Chunk; import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query; import com.redhat.thermostat.common.storage.Query.Criteria; import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.common.storage.Update; class VmInfoDAOImpl implements VmInfoDAO { @@ -115,13 +115,10 @@ @Override public void putVmStoppedTime(int vmId, long timestamp) { - storage.updateChunk(makeStoppedChunk(vmId, timestamp)); + Update update = storage.createUpdate().from(vmInfoCategory) + .where(Key.VM_ID, vmId) + .set(VmInfoDAO.stopTimeKey, timestamp); + storage.updatePojo(update); } - private Chunk makeStoppedChunk(int vmId, long stopTimeStamp) { - Chunk chunk = new Chunk(VmInfoDAO.vmInfoCategory, false); - chunk.put(Key.VM_ID, vmId); - chunk.put(VmInfoDAO.stopTimeKey, stopTimeStamp); - return chunk; - } }
--- a/common/core/src/main/java/com/redhat/thermostat/common/model/VmInfo.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/model/VmInfo.java Tue Sep 18 10:48:11 2012 +0200 @@ -93,7 +93,7 @@ @Persist public void setVmId(int vmId) { - this.vmPid = vmPid; + this.vmPid = vmId; } @Persist
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/MongoRemove.java Tue Sep 18 10:48:11 2012 +0200 @@ -0,0 +1,66 @@ +/* + * 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.common.storage; + +class MongoRemove implements Remove { + + private Chunk query; + + @Override + public Remove from(Category category) { + if (query != null) { + throw new IllegalStateException(); + } + query = new Chunk(category, false); + return this; + } + + @Override + public <T> Remove where(Key<T> key, T value) { + if (query == null) { + throw new IllegalStateException(); + } + query.put(key, value); + return this; + } + + Chunk getChunk() { + return query; + } + +}
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java Tue Sep 18 10:48:11 2012 +0200 @@ -195,11 +195,19 @@ } @Override - public void updateChunk(Chunk chunk) { + public void updatePojo(Update update) { + assert update instanceof MongoUpdate; + MongoUpdate mongoUpdate = (MongoUpdate) update; + Chunk chunk = mongoUpdate.getChunk(); + updateChunk(chunk); + } + + void updateChunk(Chunk chunk) { Category cat = chunk.getCategory(); DBCollection coll = getCachedCollection(cat.getName()); BasicDBObject toUpdate = new BasicDBObject(); BasicDBObject updateKey = getAgentQueryKeyFromChunkOrGlobalAgent(chunk); + BasicDBObject setObj = null; Map<String, BasicDBObject> nestedParts = new HashMap<String, BasicDBObject>(); Map<String, BasicDBObject> updateKeyNestedParts = new HashMap<String, BasicDBObject>(); for (Key<?> key : cat.getKeys()) { @@ -220,7 +228,11 @@ } updateKeyNested.append(entryParts[1], updateKeyNested); } else { - nested.append(SET_MODIFIER, new BasicDBObject(entryParts[1], chunk.get(key))); + if (setObj == null) { + setObj = new BasicDBObject(); + nested.append(SET_MODIFIER, setObj); + } + setObj.append(entryParts[1], chunk.get(key)); } } } else { @@ -236,7 +248,11 @@ if (isKey) { updateKey.append(mongoKey, value); } else { - toUpdate.append(SET_MODIFIER, new BasicDBObject(mongoKey, value)); + if (setObj == null) { + setObj = new BasicDBObject(); + toUpdate.append(SET_MODIFIER, setObj); + } + setObj.append(mongoKey, value); } } } @@ -256,7 +272,10 @@ } @Override - public void removeChunk(Chunk query) { + public void removePojo(Remove remove) { + assert (remove instanceof MongoRemove); + MongoRemove mongoRemove = (MongoRemove) remove; + Chunk query = mongoRemove.getChunk(); Category category = query.getCategory(); DBCollection coll = getCachedCollection(category.getName()); @@ -315,6 +334,16 @@ } @Override + public Update createUpdate() { + return new MongoUpdate(); + } + + @Override + public Remove createRemove() { + return new MongoRemove(); + } + + @Override public <T extends Pojo> Cursor<T> findAllPojos(Query query, Class<T> resultClass) { MongoQuery mongoQuery = checkAndCastQuery(query); DBCollection coll = getCachedCollection(mongoQuery.getCollectionName()); @@ -385,6 +414,12 @@ @Override public void putPojo(Category category, boolean replace, Pojo pojo) { + Chunk chunk = convertPojoToChunk(category, replace, pojo); + putChunk(chunk); + } + + private Chunk convertPojoToChunk(Category category, boolean replace, + Pojo pojo) { Converter customConverter = converters.get(pojo.getClass()); Chunk chunk; if (customConverter != null) { @@ -392,7 +427,7 @@ } else { chunk = new ChunkAdapter(pojo, category, replace); } - putChunk(chunk); + return chunk; } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/MongoUpdate.java Tue Sep 18 10:48:11 2012 +0200 @@ -0,0 +1,78 @@ +/* + * 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.common.storage; + +// TODO: For now we utilize the Chunk based conversion, and rely on MongoStorage to +// actually resolve the $set fields. Eventually, we want to convert to DBObject +// directly, and take advantage of improved semantics of this class. +class MongoUpdate implements Update { + + private Chunk updateChunk; + + @Override + public Update from(Category category) { + if (updateChunk != null) { + throw new IllegalStateException(); + } + updateChunk = new Chunk(category, false); + return this; + } + + @Override + public <T> Update where(Key<T> key, T value) { + if (updateChunk == null) { + throw new IllegalStateException(); + } + updateChunk.put(key, value); + return this; + } + + @Override + public <T> Update set(Key<T> key, T value) { + if (updateChunk == null) { + throw new IllegalStateException(); + } + updateChunk.put(key, value); + return this; + } + + Chunk getChunk() { + return updateChunk; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/Remove.java Tue Sep 18 10:48:11 2012 +0200 @@ -0,0 +1,47 @@ +/* + * 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.common.storage; + + +public interface Remove { + + Remove from(Category category); + + <T> Remove where(Key<T> key, T value); + +}
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/Storage.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/Storage.java Tue Sep 18 10:48:11 2012 +0200 @@ -61,9 +61,9 @@ public abstract void putPojo(Category category, boolean replace, Pojo pojo); - public abstract void updateChunk(Chunk chunk); + public abstract void updatePojo(Update update); - public abstract void removeChunk(Chunk chunk); + public abstract void removePojo(Remove remove); /** * Drop all data related to the currently running agent. @@ -83,5 +83,6 @@ public abstract InputStream loadFile(String filename); public abstract Query createQuery(); - + public abstract Update createUpdate(); + public abstract Remove createRemove(); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/Update.java Tue Sep 18 10:48:11 2012 +0200 @@ -0,0 +1,45 @@ +/* + * 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.common.storage; + +public interface Update { + + Update from(Category category); + <T> Update where(Key<T> key, T value); + <T> Update set(Key<T> key, T value); +}
--- a/common/core/src/test/java/com/redhat/thermostat/common/dao/AgentInfoConverterTest.java Wed Sep 12 12:44:35 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,100 +0,0 @@ -/* - * 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.common.dao; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import com.redhat.thermostat.common.model.AgentInformation; -import com.redhat.thermostat.common.storage.Chunk; -import com.redhat.thermostat.common.storage.Key; - -public class AgentInfoConverterTest { - - @Test - public void testFromChunk() { - final String AGENT_ID = "12345"; - final boolean ALIVE = true; - final long START_TIME = 1234; - final long STOP_TIME = 5678; - final String CONFIG_ADDRESS = "foobar:666"; - - Chunk agentInfoChunk = new Chunk(AgentInfoDAO.CATEGORY, true); - agentInfoChunk.put(Key.AGENT_ID, AGENT_ID); - agentInfoChunk.put(AgentInfoDAO.ALIVE_KEY, ALIVE); - agentInfoChunk.put(AgentInfoDAO.START_TIME_KEY, START_TIME); - agentInfoChunk.put(AgentInfoDAO.STOP_TIME_KEY, STOP_TIME); - agentInfoChunk.put(AgentInfoDAO.CONFIG_LISTEN_ADDRESS, CONFIG_ADDRESS); - - AgentInfoConverter converter = new AgentInfoConverter(); - AgentInformation info = converter.fromChunk(agentInfoChunk); - - assertEquals(AGENT_ID, info.getAgentId()); - assertEquals(ALIVE, info.isAlive()); - assertEquals(START_TIME, info.getStartTime()); - assertEquals(STOP_TIME, info.getStopTime()); - } - - @Test - public void testToChunk() { - final String AGENT_ID = "12345"; - final boolean ALIVE = true; - final long START_TIME = 1234; - final long STOP_TIME = 5678; - final String CONFIG_ADDRESS = "localhost:666"; - - AgentInformation agentInfo = new AgentInformation(); - agentInfo.setAgentId(AGENT_ID); - agentInfo.setAlive(ALIVE); - agentInfo.setConfigListenAddress(CONFIG_ADDRESS); - agentInfo.setStartTime(START_TIME); - agentInfo.setStopTime(STOP_TIME); - - AgentInfoConverter converter = new AgentInfoConverter(); - Chunk chunk = converter.toChunk(agentInfo); - - assertEquals(AgentInfoDAO.CATEGORY, chunk.getCategory()); - assertEquals(AGENT_ID, chunk.get(Key.AGENT_ID)); - assertEquals(ALIVE, chunk.get(AgentInfoDAO.ALIVE_KEY)); - assertEquals((Long) START_TIME, chunk.get(AgentInfoDAO.START_TIME_KEY)); - assertEquals((Long) STOP_TIME, chunk.get(AgentInfoDAO.STOP_TIME_KEY)); - assertEquals((String) CONFIG_ADDRESS, chunk.get(AgentInfoDAO.CONFIG_LISTEN_ADDRESS)); - - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/dao/AgentInfoDAOTest.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/dao/AgentInfoDAOTest.java Tue Sep 18 10:48:11 2012 +0200 @@ -41,6 +41,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import java.util.Collection; @@ -51,13 +52,15 @@ import org.mockito.ArgumentCaptor; import com.redhat.thermostat.common.model.AgentInformation; -import com.redhat.thermostat.common.model.Pojo; import com.redhat.thermostat.common.storage.Category; import com.redhat.thermostat.common.storage.Chunk; import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query.Criteria; +import com.redhat.thermostat.common.storage.Remove; import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.common.storage.Update; +import com.redhat.thermostat.common.storage.QueryTestHelper; import com.redhat.thermostat.test.MockQuery; public class AgentInfoDAOTest { @@ -202,40 +205,37 @@ @Test public void verifyUpdateAgentInformation() { + + Update mockUpdate = QueryTestHelper.createMockUpdate(); Storage storage = mock(Storage.class); + when(storage.createUpdate()).thenReturn(mockUpdate); AgentInfoDAO dao = new AgentInfoDAOImpl(storage); dao.updateAgentInformation(agentInfo1); - ArgumentCaptor<Chunk> pojoCaptor = ArgumentCaptor.forClass(Chunk.class); - verify(storage).updateChunk(pojoCaptor.capture()); - - Chunk updatedValue = pojoCaptor.getValue(); + verify(mockUpdate).from(AgentInfoDAO.CATEGORY); + verify(mockUpdate).where(Key.AGENT_ID, "1234"); + verify(mockUpdate).set(AgentInfoDAO.START_TIME_KEY, 100L); + verify(mockUpdate).set(AgentInfoDAO.STOP_TIME_KEY, 10L); + verify(mockUpdate).set(AgentInfoDAO.CONFIG_LISTEN_ADDRESS, "foobar:666"); + verify(mockUpdate).set(AgentInfoDAO.ALIVE_KEY, true); + verifyNoMoreInteractions(mockUpdate); + verify(storage).updatePojo(mockUpdate); - Chunk expected = new Chunk(AgentInfoDAO.CATEGORY, true); - expected.put(Key.AGENT_ID, "1234"); - expected.put(AgentInfoDAO.ALIVE_KEY, true); - expected.put(AgentInfoDAO.CONFIG_LISTEN_ADDRESS, "foobar:666"); - expected.put(AgentInfoDAO.START_TIME_KEY, 100L); - expected.put(AgentInfoDAO.STOP_TIME_KEY, 10L); - assertEquals(expected, updatedValue); } @Test public void verifyRemoveAgentInformation() { + Remove mockRemove = QueryTestHelper.createMockRemove(); Storage storage = mock(Storage.class); + when(storage.createRemove()).thenReturn(mockRemove); AgentInfoDAO dao = new AgentInfoDAOImpl(storage); dao.removeAgentInformation(agentInfo1); - ArgumentCaptor<Chunk> queryCaptor = ArgumentCaptor.forClass(Chunk.class); - verify(storage).removeChunk(queryCaptor.capture()); - - Chunk removeQuery = queryCaptor.getValue(); - Chunk expectedQuery = new Chunk(AgentInfoDAO.CATEGORY, true); - expectedQuery.put(Key.AGENT_ID, agentInfo1.getAgentId()); - - assertEquals(expectedQuery, removeQuery); + verify(storage).removePojo(mockRemove); + verify(mockRemove).from(AgentInfoDAO.CATEGORY); + verify(mockRemove).where(Key.AGENT_ID, "1234"); } }
--- a/common/core/src/test/java/com/redhat/thermostat/common/dao/BackendInfoConverterTest.java Wed Sep 12 12:44:35 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -/* - * 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.common.dao; - -import static org.junit.Assert.assertEquals; - -import java.util.Arrays; -import java.util.List; - -import org.junit.Test; - -import com.redhat.thermostat.common.model.BackendInformation; -import com.redhat.thermostat.common.storage.Chunk; - -public class BackendInfoConverterTest { - - @Test - public void testFromChunk() { - final String BACKEND_NAME = "test-backend"; - final String BACKEND_DESC = "test-backend-description-that-may-be-long"; - final Boolean ACTIVE = true; - final List<Integer> TO_MONITOR = Arrays.asList(new Integer[] { -1, 0, 1 }); - final Boolean MONITOR_NEW = false; - - Chunk input = new Chunk(BackendInfoDAO.CATEGORY, true); - input.put(BackendInfoDAO.BACKEND_NAME, BACKEND_NAME); - input.put(BackendInfoDAO.BACKEND_DESCRIPTION, BACKEND_DESC); - input.put(BackendInfoDAO.IS_ACTIVE, ACTIVE); - input.put(BackendInfoDAO.PIDS_TO_MONITOR, TO_MONITOR); - input.put(BackendInfoDAO.SHOULD_MONITOR_NEW_PROCESSES, MONITOR_NEW); - - BackendInfoConverter converter = new BackendInfoConverter(); - - BackendInformation result = converter.fromChunk(input); - - assertEquals(BACKEND_NAME, result.getName()); - assertEquals(BACKEND_DESC, result.getDescription()); - assertEquals(ACTIVE, result.isActive()); - assertEquals(MONITOR_NEW, result.isObserveNewJvm()); - assertEquals(TO_MONITOR, result.getPids()); - } - - @Test - public void testToChunk() { - final String BACKEND_NAME = "test-backend"; - final String BACKEND_DESC = "test-backend-description-that-may-be-long"; - final Boolean ACTIVE = true; - final List<Integer> TO_MONITOR = Arrays.asList(new Integer[] { -1, 0, 1 }); - final Boolean MONITOR_NEW = false; - - BackendInformation backendInfo = new BackendInformation(); - backendInfo.setName(BACKEND_NAME); - backendInfo.setDescription(BACKEND_DESC); - backendInfo.setActive(ACTIVE); - backendInfo.setObserveNewJvm(MONITOR_NEW); - backendInfo.setPids(TO_MONITOR); - - BackendInfoConverter converter = new BackendInfoConverter(); - - Chunk result = converter.toChunk(backendInfo); - - assertEquals(BackendInfoDAO.CATEGORY, result.getCategory()); - - assertEquals(BACKEND_NAME, result.get(BackendInfoDAO.BACKEND_NAME)); - assertEquals(BACKEND_DESC, result.get(BackendInfoDAO.BACKEND_DESCRIPTION)); - assertEquals(ACTIVE, result.get(BackendInfoDAO.IS_ACTIVE)); - assertEquals(TO_MONITOR, result.get(BackendInfoDAO.PIDS_TO_MONITOR)); - assertEquals(MONITOR_NEW, result.get(BackendInfoDAO.SHOULD_MONITOR_NEW_PROCESSES)); - - } - -}
--- a/common/core/src/test/java/com/redhat/thermostat/common/dao/BackendInfoDAOTest.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/dao/BackendInfoDAOTest.java Tue Sep 18 10:48:11 2012 +0200 @@ -40,7 +40,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.util.Arrays; import java.util.Collection; @@ -48,6 +48,7 @@ import org.junit.Before; import org.junit.Test; +import org.mockito.InOrder; import com.redhat.thermostat.common.model.BackendInformation; import com.redhat.thermostat.common.storage.Category; @@ -55,7 +56,9 @@ import com.redhat.thermostat.common.storage.Cursor; import com.redhat.thermostat.common.storage.Key; import com.redhat.thermostat.common.storage.Query.Criteria; +import com.redhat.thermostat.common.storage.Remove; import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.common.storage.QueryTestHelper; import com.redhat.thermostat.test.MockQuery; public class BackendInfoDAOTest { @@ -140,19 +143,17 @@ @Test public void verifyRemoveBackendInformation() { + Remove remove = QueryTestHelper.createMockRemove(); Storage storage = mock(Storage.class); - + when(storage.createRemove()).thenReturn(remove); BackendInfoDAO dao = new BackendInfoDAOImpl(storage); dao.removeBackendInformation(backendInfo1); - Chunk backend1 = new Chunk(BackendInfoDAO.CATEGORY, true); - backend1.put(BackendInfoDAO.BACKEND_NAME, "backend-name"); - backend1.put(BackendInfoDAO.BACKEND_DESCRIPTION, "description"); - backend1.put(BackendInfoDAO.IS_ACTIVE, true); - backend1.put(BackendInfoDAO.SHOULD_MONITOR_NEW_PROCESSES, true); - backend1.put(BackendInfoDAO.PIDS_TO_MONITOR, Arrays.asList(new Integer[] { -1, 0, 1})); - verify(storage).removeChunk(backend1); + verify(storage).removePojo(remove); + InOrder inOrder = inOrder(remove); + inOrder.verify(remove).from(BackendInfoDAO.CATEGORY); + inOrder.verify(remove).where(BackendInfoDAO.BACKEND_NAME, "backend-name"); } }
--- a/common/core/src/test/java/com/redhat/thermostat/common/dao/VmInfoDAOTest.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/dao/VmInfoDAOTest.java Tue Sep 18 10:48:11 2012 +0200 @@ -36,10 +36,13 @@ package com.redhat.thermostat.common.dao; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import java.util.ArrayList; @@ -50,7 +53,6 @@ import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentCaptor; import com.redhat.thermostat.common.model.VmInfo; import com.redhat.thermostat.common.storage.Category; @@ -60,6 +62,8 @@ import com.redhat.thermostat.common.storage.Query; import com.redhat.thermostat.common.storage.Query.Criteria; import com.redhat.thermostat.common.storage.Storage; +import com.redhat.thermostat.common.storage.Update; +import com.redhat.thermostat.common.storage.QueryTestHelper; import com.redhat.thermostat.test.MockQuery; public class VmInfoDAOTest { @@ -279,16 +283,16 @@ @Test public void testPutVmStoppedTime() { + Update mockUpdate = QueryTestHelper.createMockUpdate(); Storage storage = mock(Storage.class); + when(storage.createUpdate()).thenReturn(mockUpdate); VmInfoDAO dao = new VmInfoDAOImpl(storage); dao.putVmStoppedTime(vmId, stopTime); - ArgumentCaptor<Chunk> arg = ArgumentCaptor.forClass(Chunk.class); - verify(storage).updateChunk(arg.capture()); - Chunk chunk = arg.getValue(); - - assertEquals(VmInfoDAO.vmInfoCategory, chunk.getCategory()); - assertEquals((Integer) vmId, chunk.get(Key.VM_ID)); - assertEquals((Long) stopTime, chunk.get(VmInfoDAO.stopTimeKey)); + verify(mockUpdate).from(VmInfoDAO.vmInfoCategory); + verify(mockUpdate).where(Key.VM_ID, 1); + verify(mockUpdate).set(VmInfoDAO.stopTimeKey, 3L); + verifyNoMoreInteractions(mockUpdate); + verify(storage).updatePojo(mockUpdate); } }
--- a/common/core/src/test/java/com/redhat/thermostat/common/storage/MongoStorageTest.java Wed Sep 12 12:44:35 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/storage/MongoStorageTest.java Tue Sep 18 10:48:11 2012 +0200 @@ -444,4 +444,42 @@ actual = storage.loadFile("doesnotexist"); assertNull(actual); } + + @Test + public void verifySimpleUpdate() { + MongoStorage storage = makeStorage(); + Update update = storage.createUpdate().from(testCategory).where(Key.AGENT_ID, "test1").set(key2, "test2"); + storage.updatePojo(update); + + ArgumentCaptor<DBObject> queryCaptor = ArgumentCaptor.forClass(DBObject.class); + ArgumentCaptor<DBObject> valueCaptor = ArgumentCaptor.forClass(DBObject.class); + + verify(testCollection).update(queryCaptor.capture(), valueCaptor.capture()); + DBObject query = queryCaptor.getValue(); + assertTrue(query.containsField(Key.AGENT_ID.getName())); + assertEquals("test1", query.get(Key.AGENT_ID.getName())); + } + + @Test + public void verifyMultiFieldUpdate() { + MongoStorage storage = makeStorage(); + Update update = storage.createUpdate().from(testCategory).where(Key.AGENT_ID, "test1").set(key2, "test2").set(key3, "test3"); + storage.updatePojo(update); + + ArgumentCaptor<DBObject> queryCaptor = ArgumentCaptor.forClass(DBObject.class); + ArgumentCaptor<DBObject> valueCaptor = ArgumentCaptor.forClass(DBObject.class); + + verify(testCollection).update(queryCaptor.capture(), valueCaptor.capture()); + DBObject query = queryCaptor.getValue(); + assertTrue(query.containsField(Key.AGENT_ID.getName())); + assertEquals("test1", query.get(Key.AGENT_ID.getName())); + DBObject value = valueCaptor.getValue(); + assertTrue(value.containsField("$set")); + DBObject values = (DBObject) value.get("$set"); + assertTrue(values.containsField("key2")); + assertEquals("test2", values.get("key2")); + assertTrue(values.containsField("key3")); + assertEquals("test3", values.get("key3")); + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/common/core/src/test/java/com/redhat/thermostat/common/storage/QueryTestHelper.java Tue Sep 18 10:48:11 2012 +0200 @@ -0,0 +1,62 @@ +/* + * 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.common.storage; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class QueryTestHelper { + + @SuppressWarnings("unchecked") + public static Update createMockUpdate() { + Update mockUpdate = mock(Update.class); + when(mockUpdate.from(any(Category.class))).thenReturn(mockUpdate); + when(mockUpdate.where(any(Key.class), any())).thenReturn(mockUpdate); + when(mockUpdate.set(any(Key.class), any())).thenReturn(mockUpdate); + return mockUpdate; + } + + @SuppressWarnings("unchecked") + public static Remove createMockRemove() { + Remove mockRemove = mock(Remove.class); + when(mockRemove.from(any(Category.class))).thenReturn(mockRemove); + when(mockRemove.where(any(Key.class), any())).thenReturn(mockRemove); + return mockRemove; + } +}