Mercurial > hg > thermostat-ng > agent
changeset 2649:7184d65b1aa7
Port VmMemoryStatDAO to communicate with Web Gateway.
Reviewed By: ebaron
Review Thread: http://icedtea.classpath.org/pipermail/thermostat/2017-May/023033.html
line wrap: on
line diff
--- a/distribution/assembly/plugin-assembly.xml Fri May 12 09:53:33 2017 -0400 +++ b/distribution/assembly/plugin-assembly.xml Fri May 12 10:05:08 2017 -0400 @@ -62,7 +62,7 @@ <!-- <include>com.redhat.thermostat:thermostat-vm-heap-analysis-distribution</include>--> <!-- <include>com.redhat.thermostat:thermostat-vm-io-distribution</include>--> <!-- <include>com.redhat.thermostat:thermostat-vm-jmx-distribution</include>--> -<!-- <include>com.redhat.thermostat:thermostat-vm-memory-distribution</include>--> + <include>com.redhat.thermostat:thermostat-vm-memory-distribution</include> <!-- <include>com.redhat.thermostat:thermostat-vm-profiler-distribution</include>--> <!-- <include>com.redhat.thermostat:thermostat-killvm-distribution</include>--> <!-- <include>com.redhat.thermostat:thermostat-vm-numa-distribution</include>-->
--- a/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/VmMemoryStatDAO.java Fri May 12 09:53:33 2017 -0400 +++ b/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/VmMemoryStatDAO.java Fri May 12 10:05:08 2017 -0400 @@ -63,17 +63,6 @@ generationsKey), Arrays.<Key<?>>asList(Key.TIMESTAMP)); - public VmMemoryStat getNewestMemoryStat(VmRef ref); - - public VmMemoryStat getOldestMemoryStat(VmRef ref); - - @Deprecated - public List<VmMemoryStat> getLatestVmMemoryStats(VmRef vm, long since); - - public List<VmMemoryStat> getLatestVmMemoryStats(AgentId agentId, VmId vmId, long since); - - public List<VmMemoryStat> getVmMemoryStats(VmRef vm, long since, long to); - public void putVmMemoryStat(VmMemoryStat stat); }
--- a/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/Activator.java Fri May 12 09:53:33 2017 -0400 +++ b/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/Activator.java Fri May 12 10:05:08 2017 -0400 @@ -50,40 +50,16 @@ import com.redhat.thermostat.vm.memory.common.VmTlabStatDAO; public class Activator implements BundleActivator { - - private ServiceTracker tracker; - private List<ServiceRegistration> registrations = new ArrayList<>(); @Override public void start(BundleContext context) throws Exception { - tracker = new ServiceTracker(context, Storage.class.getName(), null) { - @Override - public Object addingService(ServiceReference reference) { - Storage storage = (Storage) context.getService(reference); - - VmMemoryStatDAO vmMemoryStatDao = new VmMemoryStatDAOImpl(storage); - registrations.add(context.registerService(VmMemoryStatDAO.class.getName(), vmMemoryStatDao, null)); - - VmTlabStatDAO vmTlabStatDao = new VmTlabStatDAOImpl(storage); - registrations.add(context.registerService(VmTlabStatDAO.class.getName(), vmTlabStatDao, null)); - - return super.addingService(reference); - } - - @Override - public void removedService(ServiceReference reference, Object service) { - for (ServiceRegistration reg : registrations) { - reg.unregister(); - } - super.removedService(reference, service); - } - }; - tracker.open(); + VmMemoryStatDAO vmMemoryStatDao = new VmMemoryStatDAOImpl(); + context.registerService(VmMemoryStatDAO.class.getName(), vmMemoryStatDao, null); } @Override public void stop(BundleContext context) throws Exception { - tracker.close(); + // Nothing to do here. } }
--- a/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOImpl.java Fri May 12 09:53:33 2017 -0400 +++ b/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOImpl.java Fri May 12 10:05:08 2017 -0400 @@ -36,105 +36,104 @@ package com.redhat.thermostat.vm.memory.common.internal; +import java.io.IOException; +import java.util.Arrays; import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import java.util.logging.Level; import java.util.logging.Logger; import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.core.PreparedStatement; -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.storage.core.VmBoundaryPojoGetter; -import com.redhat.thermostat.storage.core.VmId; -import com.redhat.thermostat.storage.core.VmLatestPojoListGetter; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.storage.core.VmTimeIntervalPojoListGetter; import com.redhat.thermostat.storage.dao.AbstractDao; -import com.redhat.thermostat.storage.dao.AbstractDaoStatement; import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; import com.redhat.thermostat.vm.memory.common.model.VmMemoryStat; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.client.util.StringContentProvider; +import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.http.HttpStatus; class VmMemoryStatDAOImpl extends AbstractDao implements VmMemoryStatDAO { private static final Logger logger = LoggingUtils.getLogger(VmMemoryStatDAOImpl.class); - // ADD vm-memory-stats SET 'agentId' = ?s , \ - // 'vmId' = ?s , \ - // 'timeStamp' = ?s , \ - // 'metaspaceMaxCapacity' = ?l , \ - // 'metaspaceMinCapacity' = ?l , \ - // 'metaspaceCapacity' = ?l , \ - // 'metaspaceUsed' = ?l , \ - // 'generations' = ?p[ - static final String DESC_ADD_VM_MEMORY_STAT = "ADD " + vmMemoryStatsCategory.getName() + - " SET '" + Key.AGENT_ID.getName() + "' = ?s , " + - "'" + Key.VM_ID.getName() + "' = ?s , " + - "'" + Key.TIMESTAMP.getName() + "' = ?l , " + - "'" + KEY_METASPACE_MAX_CAPACITY.getName() + "' = ?l , " + - "'" + KEY_METASPACE_MIN_CAPACITY.getName() + "' = ?l , " + - "'" + KEY_METASPACE_CAPACITY.getName() + "' = ?l , " + - "'" + KEY_METASPACE_USED.getName() + "' = ?l , " + - "'" + generationsKey.getName() + "' = ?p["; - - private final Storage storage; - private final VmLatestPojoListGetter<VmMemoryStat> latestGetter; - private final VmTimeIntervalPojoListGetter<VmMemoryStat> intervalGetter; - private final VmBoundaryPojoGetter<VmMemoryStat> boundaryGetter; + private final HttpClient client; + private final HttpHelper httpHelper; + private final JsonHelper jsonHelper; - VmMemoryStatDAOImpl(Storage storage) { - this.storage = storage; - storage.registerCategory(vmMemoryStatsCategory); - latestGetter = new VmLatestPojoListGetter<>(storage, vmMemoryStatsCategory); - intervalGetter = new VmTimeIntervalPojoListGetter<>(storage, vmMemoryStatsCategory); - boundaryGetter = new VmBoundaryPojoGetter<>(storage, vmMemoryStatsCategory); + private static final String GATEWAY_URL = "http://localhost:30000"; // TODO configurable + private static final String GATEWAY_PATH = "/jvm-memory/0.0.2/"; + private static final String CONTENT_TYPE = "application/json"; + + VmMemoryStatDAOImpl() throws Exception { + this(new HttpClient(), new HttpHelper(), new JsonHelper(new VmMemoryStatTypeAdapter())); } - @Override - public VmMemoryStat getNewestMemoryStat(VmRef ref) { - return boundaryGetter.getNewestStat(ref); - } + VmMemoryStatDAOImpl(HttpClient client, HttpHelper httpHelper, JsonHelper jsonHelper) throws Exception { + this.client = client; + this.httpHelper = httpHelper; + this.jsonHelper = jsonHelper; - @Override - public VmMemoryStat getOldestMemoryStat(VmRef ref) { - return boundaryGetter.getOldestStat(ref); + this.httpHelper.startClient(this.client); } @Override public void putVmMemoryStat(final VmMemoryStat stat) { - executeStatement(new AbstractDaoStatement<VmMemoryStat>(storage, vmMemoryStatsCategory, DESC_ADD_VM_MEMORY_STAT) { - @Override - public PreparedStatement<VmMemoryStat> customize(PreparedStatement<VmMemoryStat> preparedStatement) { - preparedStatement.setString(0, stat.getAgentId()); - preparedStatement.setString(1, stat.getVmId()); - preparedStatement.setLong(2, stat.getTimeStamp()); - preparedStatement.setLong(3, stat.getMetaspaceMaxCapacity()); - preparedStatement.setLong(4, stat.getMetaspaceMinCapacity()); - preparedStatement.setLong(5, stat.getMetaspaceCapacity()); - preparedStatement.setLong(6, stat.getMetaspaceUsed()); - preparedStatement.setPojoList(7, stat.getGenerations()); - return preparedStatement; - } - }); + try { + String json = jsonHelper.toJson(Arrays.asList(stat)); + StringContentProvider provider = httpHelper.createContentProvider(json); + + String url = GATEWAY_URL + GATEWAY_PATH; + Request httpRequest = client.newRequest(url); + httpRequest.method(HttpMethod.POST); + httpRequest.content(provider, CONTENT_TYPE); + sendRequest(httpRequest); + } catch (Exception e) { + logger.log(Level.WARNING, "Failed to send VmMemoryStat to Web Gateway", e); + } } - @Override - public List<VmMemoryStat> getLatestVmMemoryStats(VmRef ref, long since) { - return latestGetter.getLatest(ref, since); - } - - @Override - public List<VmMemoryStat> getLatestVmMemoryStats(AgentId agentId, VmId vmId, long since) { - return latestGetter.getLatest(agentId, vmId, since); - } - - @Override - public List<VmMemoryStat> getVmMemoryStats(VmRef ref, long since, long to) { - return intervalGetter.getLatest(ref, since, to); + private void sendRequest(Request httpRequest) + throws InterruptedException, TimeoutException, ExecutionException, IOException { + ContentResponse resp = httpRequest.send(); + int status = resp.getStatus(); + if (status != HttpStatus.OK_200) { + throw new IOException("Gateway returned HTTP status " + String.valueOf(status) + " - " + resp.getReason()); + } } @Override protected Logger getLogger() { return logger; } + + // For testing purposes + static class JsonHelper { + + private final VmMemoryStatTypeAdapter adapter; + + public JsonHelper(VmMemoryStatTypeAdapter adapter) { + this.adapter = adapter; + } + + String toJson(List<VmMemoryStat> stats) throws IOException { + return adapter.toJson(stats); + } + } + + // For testing purposes + static class HttpHelper { + + void startClient(HttpClient httpClient) throws Exception { + httpClient.start(); + } + + StringContentProvider createContentProvider(String content) { + return new StringContentProvider(content); + } + + } }
--- a/plugins/vm-memory/common/src/main/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOImplStatementDescriptorRegistration.java Fri May 12 09:53:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,79 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.memory.common.internal; - -import java.util.HashSet; -import java.util.Set; - -import com.redhat.thermostat.storage.core.VmBoundaryPojoGetter; -import com.redhat.thermostat.storage.core.VmLatestPojoListGetter; -import com.redhat.thermostat.storage.core.VmTimeIntervalPojoListGetter; -import com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration; -import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; - -/** - * Registers prepared queries issued by this maven module via - * {@link VmLatestPojoListGetter} and {@link VmMemoryStatDAOImpl}. - * - */ -public class VmMemoryStatDAOImplStatementDescriptorRegistration implements - StatementDescriptorRegistration { - - static final String latestDescriptor = String.format(VmLatestPojoListGetter.VM_LATEST_QUERY_FORMAT, - VmMemoryStatDAO.vmMemoryStatsCategory.getName()); - static final String rangeDescriptor = String.format(VmTimeIntervalPojoListGetter.VM_INTERVAL_QUERY_FORMAT, - VmMemoryStatDAO.vmMemoryStatsCategory.getName()); - static final String latestStatDescriptor = String.format(VmBoundaryPojoGetter.DESC_NEWEST_VM_STAT, - VmMemoryStatDAO.vmMemoryStatsCategory.getName()); - static final String oldestStatDescriptor = String.format(VmBoundaryPojoGetter.DESC_OLDEST_VM_STAT, - VmMemoryStatDAO.vmMemoryStatsCategory.getName()); - - @Override - public Set<String> getStatementDescriptors() { - Set<String> descs = new HashSet<>(5); - descs.add(latestStatDescriptor); - descs.add(oldestStatDescriptor); - descs.add(VmMemoryStatDAOImpl.DESC_ADD_VM_MEMORY_STAT); - - descs.add(latestDescriptor); - descs.add(rangeDescriptor); - - return descs; - } - -} -
--- a/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/ActivatorTest.java Fri May 12 09:53:33 2017 -0400 +++ b/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/ActivatorTest.java Fri May 12 10:05:08 2017 -0400 @@ -47,27 +47,10 @@ import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; public class ActivatorTest { - - @Test - public void verifyActivatorDoesNotRegisterServiceOnMissingDeps() throws Exception { - StubBundleContext context = new StubBundleContext(); - - Activator activator = new Activator(); - - activator.start(context); - - assertEquals(0, context.getAllServices().size()); - assertEquals(1, context.getServiceListeners().size()); - - activator.stop(context); - } @Test public void verifyActivatorRegistersServices() throws Exception { StubBundleContext context = new StubBundleContext(); - Storage storage = mock(Storage.class); - - context.registerService(Storage.class, storage, null); Activator activator = new Activator();
--- a/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOImplStatementDescriptorRegistrationTest.java Fri May 12 09:53:33 2017 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.memory.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - -import java.util.Set; - -import org.junit.Test; - -import com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration; -import com.redhat.thermostat.testutils.ServiceLoaderTest; - -public class VmMemoryStatDAOImplStatementDescriptorRegistrationTest extends ServiceLoaderTest<StatementDescriptorRegistration> { - - public VmMemoryStatDAOImplStatementDescriptorRegistrationTest() { - super(StatementDescriptorRegistration.class, STORAGE_SERVICES + 1 /* tlab */, VmMemoryStatDAOImplStatementDescriptorRegistration.class); - } - - @Test - public void registersAllDescriptors() { - VmMemoryStatDAOImplStatementDescriptorRegistration reg = new VmMemoryStatDAOImplStatementDescriptorRegistration(); - Set<String> descriptors = reg.getStatementDescriptors(); - assertEquals(5, descriptors.size()); - assertFalse("null descriptor not allowed", descriptors.contains(null)); - } - -} -
--- a/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOTest.java Fri May 12 09:53:33 2017 -0400 +++ b/plugins/vm-memory/common/src/test/java/com/redhat/thermostat/vm/memory/common/internal/VmMemoryStatDAOTest.java Fri May 12 10:05:08 2017 -0400 @@ -38,89 +38,64 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.api.ContentResponse; +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.client.util.StringContentProvider; +import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.http.HttpStatus; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.Cursor; -import com.redhat.thermostat.storage.core.DescriptorParsingException; -import com.redhat.thermostat.storage.core.HostRef; import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.core.PreparedStatement; -import com.redhat.thermostat.storage.core.StatementDescriptor; -import com.redhat.thermostat.storage.core.StatementExecutionException; -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.storage.core.VmId; -import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.vm.memory.common.VmMemoryStatDAO; import com.redhat.thermostat.vm.memory.common.model.VmMemoryStat; import com.redhat.thermostat.vm.memory.common.model.VmMemoryStat.Generation; import com.redhat.thermostat.vm.memory.common.model.VmMemoryStat.Space; +import static com.redhat.thermostat.vm.memory.common.internal.VmMemoryStatDAOImpl.HttpHelper; +import static com.redhat.thermostat.vm.memory.common.internal.VmMemoryStatDAOImpl.JsonHelper; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.anyListOf; + public class VmMemoryStatDAOTest { + private HttpClient httpClient; + private HttpHelper httpHelper; + private JsonHelper jsonHelper; + private StringContentProvider contentProvider; + private Request request; + private ContentResponse response; + private static final String JSON = "{\"this\":\"is\",\"test\":\"JSON\"}"; private static final String VM_ID = "0xcafe"; private static final String AGENT_ID = "agent"; - - private Storage storage; - private VmRef vmRef; - private AgentId agentId; - private VmId vmId; - - private PreparedStatement<VmMemoryStat> stmt; - private Cursor<VmMemoryStat> cursor; - - @SuppressWarnings("unchecked") - @Before - public void setUp() throws DescriptorParsingException, StatementExecutionException { - HostRef hostRef = mock(HostRef.class); - when(hostRef.getAgentId()).thenReturn(AGENT_ID); - - vmRef = mock(VmRef.class); - when(vmRef.getHostRef()).thenReturn(hostRef); - when(vmRef.getVmId()).thenReturn(VM_ID); - - agentId = new AgentId(AGENT_ID); - vmId = new VmId(VM_ID); + private static final String CONTENT_TYPE = "application/json"; + private static final String GATEWAY_URL = "http://localhost:30000"; // TODO configurable + private static final String GATEWAY_PATH = "/jvm-memory/0.0.2/"; - storage = mock(Storage.class); - stmt = (PreparedStatement<VmMemoryStat>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - - cursor = (Cursor<VmMemoryStat>) mock(Cursor.class); - when(stmt.executeQuery()).thenReturn(cursor); - - when(cursor.hasNext()).thenReturn(false); - } + @Before + public void setUp() throws Exception { + httpClient = mock(HttpClient.class); + request = mock(Request.class); + when(httpClient.newRequest(anyString())).thenReturn(request); + response = mock(ContentResponse.class); + when(response.getStatus()).thenReturn(HttpStatus.OK_200); + when(request.send()).thenReturn(response); - @SuppressWarnings("unchecked") - private StatementDescriptor<VmMemoryStat> anyDescriptor() { - return (StatementDescriptor<VmMemoryStat>) any(StatementDescriptor.class); - } - - @Test - public void preparedQueryDescriptorsAreSane() { - String addVmMemoryStat = "ADD vm-memory-stats SET 'agentId' = ?s , " + - "'vmId' = ?s , " + - "'timeStamp' = ?l , " + - "'metaspaceMaxCapacity' = ?l , " + - "'metaspaceMinCapacity' = ?l , " + - "'metaspaceCapacity' = ?l , " + - "'metaspaceUsed' = ?l , " + - "'generations' = ?p["; - assertEquals(addVmMemoryStat, VmMemoryStatDAOImpl.DESC_ADD_VM_MEMORY_STAT); + httpHelper = mock(HttpHelper.class); + contentProvider = mock(StringContentProvider.class); + when(httpHelper.createContentProvider(anyString())).thenReturn(contentProvider); + jsonHelper = mock(JsonHelper.class); + when(jsonHelper.toJson(anyListOf(VmMemoryStat.class))).thenReturn(JSON); } @Test @@ -140,63 +115,9 @@ assertEquals(8, keys.size()); } - @Test - public void testGetLatest() throws DescriptorParsingException, StatementExecutionException { - VmMemoryStatDAO impl = new VmMemoryStatDAOImpl(storage); - impl.getNewestMemoryStat(vmRef); - - verify(storage).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, vmRef.getHostRef().getAgentId()); - verify(stmt).setString(1, vmRef.getVmId()); - verify(stmt).executeQuery(); - } - - @Test - public void testVmRefGetLatestSince() throws DescriptorParsingException, StatementExecutionException { - VmMemoryStatDAO impl = new VmMemoryStatDAOImpl(storage); - impl.getLatestVmMemoryStats(vmRef, 123L); - - verify(storage).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, vmRef.getHostRef().getAgentId()); - verify(stmt).setString(1, vmRef.getVmId()); - verify(stmt).setLong(2, 123L); - verify(stmt).executeQuery(); - verifyNoMoreInteractions(stmt); - } - - @Test - public void testGetLatestSince() throws DescriptorParsingException, StatementExecutionException { - VmMemoryStatDAO impl = new VmMemoryStatDAOImpl(storage); - impl.getLatestVmMemoryStats(agentId, vmId, 123L); - - verify(storage).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, agentId.get()); - verify(stmt).setString(1, vmId.get()); - verify(stmt).setLong(2, 123L); - verify(stmt).executeQuery(); - verifyNoMoreInteractions(stmt); - } - - @Test - public void testGetLatestReturnsNullWhenStorageEmpty() throws DescriptorParsingException, StatementExecutionException { - when(cursor.hasNext()).thenReturn(false); - when(cursor.next()).thenReturn(null); - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<VmMemoryStat> stmt = (PreparedStatement<VmMemoryStat>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor); - - VmMemoryStatDAO impl = new VmMemoryStatDAOImpl(storage); - VmMemoryStat latest = impl.getNewestMemoryStat(vmRef); - assertTrue(latest == null); - } - @SuppressWarnings("unchecked") @Test - public void testPutVmMemoryStat() throws DescriptorParsingException, - StatementExecutionException { + public void testPutVmMemoryStat() throws Exception { List<Generation> generations = new ArrayList<Generation>(); @@ -228,31 +149,18 @@ } VmMemoryStat stat = new VmMemoryStat("foo-agent", 1, "vmId", generations.toArray(new Generation[generations.size()]), 2, 3, 4, 5); - - Storage storage = mock(Storage.class); - PreparedStatement<VmMemoryStat> add = mock(PreparedStatement.class); - when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(add); - VmMemoryStatDAO dao = new VmMemoryStatDAOImpl(storage); + VmMemoryStatDAO dao = new VmMemoryStatDAOImpl(httpClient, httpHelper, jsonHelper); dao.putVmMemoryStat(stat); - - @SuppressWarnings("rawtypes") - ArgumentCaptor<StatementDescriptor> captor = ArgumentCaptor.forClass(StatementDescriptor.class); - - verify(storage).prepareStatement(captor.capture()); - StatementDescriptor<?> desc = captor.getValue(); - assertEquals(VmMemoryStatDAOImpl.DESC_ADD_VM_MEMORY_STAT, desc.getDescriptor()); - verify(add).setString(0, stat.getAgentId()); - verify(add).setString(1, stat.getVmId()); - verify(add).setLong(2, stat.getTimeStamp()); - verify(add).setLong(3, stat.getMetaspaceMaxCapacity()); - verify(add).setLong(4, stat.getMetaspaceMinCapacity()); - verify(add).setLong(5, stat.getMetaspaceCapacity()); - verify(add).setLong(6, stat.getMetaspaceUsed()); - verify(add).setPojoList(7, stat.getGenerations()); - verify(add).execute(); - Mockito.verifyNoMoreInteractions(add); + String url = GATEWAY_URL + GATEWAY_PATH; + verify(httpClient).newRequest(url); + verify(request).method(HttpMethod.POST); + verify(jsonHelper).toJson(Arrays.asList(stat)); + verify(httpHelper).createContentProvider(JSON); + verify(request).content(contentProvider, CONTENT_TYPE); + verify(request).send(); + verify(response).getStatus(); } }