Mercurial > hg > release > thermostat-1.0
changeset 388:5b3cb3635fca
Implement HeapDAO.getHeapDump().
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-June/001887.html
PR 1032
author | Roman Kennke <rkennke@redhat.com> |
---|---|
date | Tue, 19 Jun 2012 14:38:18 +0200 |
parents | 6ce7d8ac30b9 |
children | cfbc2b43142e |
files | common/core/src/main/java/com/redhat/thermostat/common/dao/HeapDAO.java common/core/src/main/java/com/redhat/thermostat/common/dao/HeapDAOImpl.java common/core/src/main/java/com/redhat/thermostat/common/model/HeapInfo.java common/core/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java common/core/src/main/java/com/redhat/thermostat/common/storage/Storage.java common/core/src/test/java/com/redhat/thermostat/common/dao/HeapDAOTest.java common/core/src/test/java/com/redhat/thermostat/common/model/HeapInfoTest.java common/core/src/test/java/com/redhat/thermostat/common/storage/MongoStorageTest.java |
diffstat | 8 files changed, 67 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/HeapDAO.java Tue Jun 19 13:53:54 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/dao/HeapDAO.java Tue Jun 19 14:38:18 2012 +0200 @@ -36,6 +36,7 @@ package com.redhat.thermostat.common.dao; +import java.io.InputStream; import java.util.Collection; import com.redhat.thermostat.common.model.HeapInfo; @@ -48,8 +49,10 @@ public static final Category heapInfoCategory = new Category("vm-heap-info", Key.AGENT_ID, Key.VM_ID, Key.TIMESTAMP, heapDumpIdKey); - void putHeapInfo(HeapInfo heapInfo); + void putHeapInfo(HeapInfo heapInfo, InputStream heapDump); Collection<HeapInfo> getAllHeapInfo(VmRef vm); + InputStream getHeapDump(HeapInfo heapInfo); + }
--- a/common/core/src/main/java/com/redhat/thermostat/common/dao/HeapDAOImpl.java Tue Jun 19 13:53:54 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/dao/HeapDAOImpl.java Tue Jun 19 14:38:18 2012 +0200 @@ -55,14 +55,13 @@ } @Override - public void putHeapInfo(HeapInfo heapInfo) { + public void putHeapInfo(HeapInfo heapInfo, InputStream heapDumpData) { VmRef vm = heapInfo.getVm(); Chunk chunk = new Chunk(heapInfoCategory, false); chunk.put(Key.AGENT_ID, vm.getAgent().getStringID()); chunk.put(Key.VM_ID, vm.getId()); chunk.put(Key.TIMESTAMP, heapInfo.getTimestamp()); - InputStream heapDumpData = heapInfo.getHeapDump(); String heapDumpId = "heapdump-" + vm.getAgent().getStringID() + "-" + vm.getId() + "-" + heapInfo.getTimestamp(); if (heapDumpData != null) { chunk.put(heapDumpIdKey, heapDumpId); @@ -93,4 +92,9 @@ return info; } + @Override + public InputStream getHeapDump(HeapInfo heapInfo) { + return storage.loadFile(heapInfo.getHeapDumpId()); + } + }
--- a/common/core/src/main/java/com/redhat/thermostat/common/model/HeapInfo.java Tue Jun 19 13:53:54 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/model/HeapInfo.java Tue Jun 19 14:38:18 2012 +0200 @@ -36,7 +36,6 @@ package com.redhat.thermostat.common.model; -import java.io.InputStream; import java.util.Objects; import com.redhat.thermostat.common.dao.VmRef; @@ -45,7 +44,6 @@ private VmRef vm; private long timestamp; - private InputStream heapDump; private String heapDumpId; public HeapInfo(VmRef vm, long timestamp) { @@ -61,14 +59,6 @@ return timestamp; } - public InputStream getHeapDump() { - return heapDump; - } - - public void setHeapDump(InputStream heapDump) { - this.heapDump = heapDump; - } - public void setHeapDumpId(String heapDumpId) { this.heapDumpId = heapDumpId; }
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java Tue Jun 19 13:53:54 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java Tue Jun 19 14:38:18 2012 +0200 @@ -52,6 +52,7 @@ import com.mongodb.DBObject; import com.mongodb.WriteConcern; import com.mongodb.gridfs.GridFS; +import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSInputFile; import com.redhat.thermostat.common.config.StartupConfiguration; import com.redhat.thermostat.common.storage.Connection.ConnectionListener; @@ -393,4 +394,15 @@ GridFSInputFile inputFile = gridFS.createFile(data, filename); inputFile.save(); } + + @Override + public InputStream loadFile(String filename) { + GridFS gridFS = new GridFS(db); + GridFSDBFile file = gridFS.findOne(filename); + if (file == null) { + return null; + } else { + return file.getInputStream(); + } + } }
--- a/common/core/src/main/java/com/redhat/thermostat/common/storage/Storage.java Tue Jun 19 13:53:54 2012 +0200 +++ b/common/core/src/main/java/com/redhat/thermostat/common/storage/Storage.java Tue Jun 19 14:38:18 2012 +0200 @@ -36,7 +36,6 @@ package com.redhat.thermostat.common.storage; -import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.UUID; @@ -88,4 +87,6 @@ public abstract void saveFile(String filename, InputStream data); + public abstract InputStream loadFile(String filename); + }
--- a/common/core/src/test/java/com/redhat/thermostat/common/dao/HeapDAOTest.java Tue Jun 19 13:53:54 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/dao/HeapDAOTest.java Tue Jun 19 14:38:18 2012 +0200 @@ -49,6 +49,7 @@ import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.util.Collection; @@ -69,7 +70,7 @@ private HeapDAO dao; private Storage storage; private HeapInfo heapInfo; - private InputStream dataStream; + private InputStream heapDumpData; @Before public void setUp() { @@ -79,8 +80,7 @@ VmRef vm = new VmRef(host, 123, "test-vm"); heapInfo = new HeapInfo(vm, 12345); byte[] data = new byte[] { 1, 2, 3 }; - dataStream = new ByteArrayInputStream(data); - heapInfo.setHeapDump(dataStream); + heapDumpData = new ByteArrayInputStream(data); // Setup for reading data from DB. Chunk findAllQuery = new Chunk(HeapDAO.heapInfoCategory, false); @@ -100,11 +100,14 @@ when(cursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false); when(cursor.next()).thenReturn(info1).thenReturn(info2).thenReturn(null); when(storage.findAll(findAllQuery)).thenReturn(cursor); + + // Setup for reading heapdump data. + when(storage.loadFile("test")).thenReturn(heapDumpData); } @After public void tearDown() { - dataStream = null; + heapDumpData = null; heapInfo = null; dao = null; storage = null; @@ -125,7 +128,7 @@ @Test public void testPutHeapInfo() { - dao.putHeapInfo(heapInfo); + dao.putHeapInfo(heapInfo, heapDumpData); Chunk expectedChunk = new Chunk(HeapDAO.heapInfoCategory, false); expectedChunk.put(Key.AGENT_ID, "987"); @@ -133,13 +136,12 @@ expectedChunk.put(Key.TIMESTAMP, 12345L); expectedChunk.put(HeapDAO.heapDumpIdKey, "heapdump-987-123-12345"); verify(storage).putChunk(expectedChunk); - verify(storage).saveFile(eq("heapdump-987-123-12345"), same(dataStream)); + verify(storage).saveFile(eq("heapdump-987-123-12345"), same(heapDumpData)); } @Test public void testPutHeapInfoWithoutDump() { - heapInfo.setHeapDump(null); - dao.putHeapInfo(heapInfo); + dao.putHeapInfo(heapInfo, null); Chunk expectedChunk = new Chunk(HeapDAO.heapInfoCategory, false); expectedChunk.put(Key.AGENT_ID, "987"); @@ -163,4 +165,14 @@ assertTrue(heapInfos.contains(info1)); assertTrue(heapInfos.contains(info2)); } + + @Test + public void testGetHeapDump() throws IOException { + heapInfo.setHeapDumpId("test"); + InputStream in = dao.getHeapDump(heapInfo); + assertEquals(1, in.read()); + assertEquals(2, in.read()); + assertEquals(3, in.read()); + assertEquals(-1, in.read()); + } }
--- a/common/core/src/test/java/com/redhat/thermostat/common/model/HeapInfoTest.java Tue Jun 19 13:53:54 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/model/HeapInfoTest.java Tue Jun 19 14:38:18 2012 +0200 @@ -36,11 +36,9 @@ package com.redhat.thermostat.common.model; -import static org.junit.Assert.*; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import org.junit.Before; import org.junit.Test; @@ -67,19 +65,6 @@ } @Test - public void testHeapDump() throws IOException { - assertNull(heapInfo.getHeapDump()); - byte[] test = new byte[]{ 1 , 2 ,3 }; - heapInfo.setHeapDump(new ByteArrayInputStream(test)); - InputStream in = heapInfo.getHeapDump(); - assertNotNull(in); - assertEquals(1, in.read()); - assertEquals(2, in.read()); - assertEquals(3, in.read()); - assertEquals(-1, in.read()); - } - - @Test public void testHeapDumpId() { assertNull(heapInfo.getHeapDumpId()); heapInfo.setHeapDumpId("test");
--- a/common/core/src/test/java/com/redhat/thermostat/common/storage/MongoStorageTest.java Tue Jun 19 13:53:54 2012 +0200 +++ b/common/core/src/test/java/com/redhat/thermostat/common/storage/MongoStorageTest.java Tue Jun 19 14:38:18 2012 +0200 @@ -41,6 +41,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; @@ -71,6 +72,7 @@ import com.mongodb.Mongo; import com.mongodb.MongoURI; import com.mongodb.gridfs.GridFS; +import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSInputFile; import com.redhat.thermostat.common.config.StartupConfiguration; @@ -387,4 +389,22 @@ DBObject val = dbobj.getValue(); assertEquals(new UUID(1, 2).toString(), val.get("agent-id")); } + + @Test + public void verifyLoadFile() throws Exception { + InputStream stream = mock(InputStream.class); + GridFSDBFile file = mock(GridFSDBFile.class); + when(file.getInputStream()).thenReturn(stream); + GridFS gridFS = mock(GridFS.class); + when(gridFS.findOne("test")).thenReturn(file); + PowerMockito.whenNew(GridFS.class).withArguments(any()).thenReturn(gridFS); + PowerMockito.whenNew(Mongo.class).withParameterTypes(MongoURI.class).withArguments(any(MongoURI.class)).thenReturn(m); + MongoStorage storage = makeStorage(); + + InputStream actual = storage.loadFile("test"); + assertSame(stream, actual); + + actual = storage.loadFile("doesnotexist"); + assertNull(actual); + } }