Mercurial > hg > thermostat-ng > agent
changeset 2714:cd2f41102214
Port more http client consumers to HttpRequestService.
Reviewed-by: jmatsuok
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023906.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Wed, 28 Jun 2017 16:22:22 +0200 |
parents | 0765cd82bfec |
children | 752a90af8d85 |
files | agent/core/src/main/java/com/redhat/thermostat/agent/http/HttpRequestService.java plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImpl.java plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImplTest.java plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOTest.java |
diffstat | 4 files changed, 164 insertions(+), 257 deletions(-) [+] |
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/http/HttpRequestService.java Fri Jun 23 15:16:13 2017 +0200 +++ b/agent/core/src/main/java/com/redhat/thermostat/agent/http/HttpRequestService.java Wed Jun 28 16:22:22 2017 +0200 @@ -233,7 +233,7 @@ } private RequestFailedException(int responseCode, String reason, Throwable cause) { - super(reason); + super(reason, cause); this.reasonStr = reason; this.responseCode = responseCode; }
--- a/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImpl.java Fri Jun 23 15:16:13 2017 +0200 +++ b/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImpl.java Wed Jun 28 16:22:22 2017 +0200 @@ -37,32 +37,23 @@ package com.redhat.thermostat.jvm.overview.agent.internal.model; import java.io.IOException; -import java.net.URI; import java.util.Arrays; import java.util.Collections; - import java.util.List; import java.util.Set; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; + +import com.redhat.thermostat.agent.http.HttpRequestService; +import com.redhat.thermostat.agent.http.HttpRequestService.RequestFailedException; +import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoTypeAdapter.VmInfoUpdateTypeAdapter; import com.redhat.thermostat.jvm.overview.agent.model.VmId; import com.redhat.thermostat.jvm.overview.agent.model.VmInfo; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Service; -import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.client.HttpContentResponse; -import org.eclipse.jetty.client.HttpRequest; -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 com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.storage.core.AgentId; @Component @@ -74,20 +65,18 @@ private static final String GATEWAY_URL = "http://localhost:26000/api/v100"; // TODO configurable private static final String GATEWAY_PATH = "/vm-info/systems/*/agents/"; private static final String GATEWAY_PATH_JVM_SUFFIX = "/jvms/"; - private static final String CONTENT_TYPE = "application/json"; + + private final JsonHelper jsonHelper; - private final HttpHelper httpHelper; - private final JsonHelper jsonHelper; + @Reference + private HttpRequestService httpRequestService; public VmInfoDAOImpl() throws Exception { - this(new HttpHelper(new HttpClient()), new JsonHelper(new VmInfoTypeAdapter(), new VmInfoUpdateTypeAdapter())); + this(new JsonHelper(new VmInfoTypeAdapter(), new VmInfoUpdateTypeAdapter())); } - VmInfoDAOImpl(HttpHelper httpHelper, JsonHelper jsonHelper) throws Exception { + VmInfoDAOImpl(JsonHelper jsonHelper) throws Exception { this.jsonHelper = jsonHelper; - this.httpHelper = httpHelper; - - this.httpHelper.startClient(); } @Override @@ -102,19 +91,16 @@ @Override public void putVmInfo(final VmInfo info) { - try { - // Encode as JSON and send as POST request - String json = jsonHelper.toJson(Arrays.asList(info)); - StringContentProvider provider = httpHelper.createContentProvider(json); - - String url = getAddURL(info.getAgentId()); - Request httpRequest = httpHelper.newRequest(url); - httpRequest.method(HttpMethod.POST); - httpRequest.content(provider, CONTENT_TYPE); - sendRequest(httpRequest); - } catch (IOException | InterruptedException | TimeoutException | ExecutionException e) { - logger.log(Level.WARNING, "Failed to send JVM information to web gateway", e); - } + // FIXME: Re-enable once /jvms endpoint is being used properly and re-enable ignored + // unit-tests. +// try { +// // Encode as JSON and send as POST request +// String json = jsonHelper.toJson(Arrays.asList(info)); +// String url = getAddURL(info.getAgentId()); +// httpRequestService.sendHttpRequest(json, url, HttpRequestService.POST); +// } catch (IOException | RequestFailedException e) { +// logger.log(Level.WARNING, "Failed to send JVM information to web gateway", e); +// } } @Override @@ -123,26 +109,12 @@ // Encode as JSON and send as PUT request VmInfoUpdate update = new VmInfoUpdate(timestamp); String json = jsonHelper.toJson(update); - StringContentProvider provider = httpHelper.createContentProvider(json); - String url = getUpdateURL(agentId, vmId); - Request httpRequest = httpHelper.newRequest(url); - httpRequest.method(HttpMethod.PUT); - httpRequest.content(provider, CONTENT_TYPE); - sendRequest(httpRequest); - } catch (IOException | InterruptedException | TimeoutException | ExecutionException e) { + httpRequestService.sendHttpRequest(json, url, HttpRequestService.PUT); + } catch (IOException | RequestFailedException e) { logger.log(Level.WARNING, "Failed to send JVM information update to web gateway", e); } } - - 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()); - } - } private String getAddURL(String agentId) { StringBuilder builder = buildURL(agentId); @@ -164,6 +136,15 @@ return builder.toString(); } + protected void bindHttpRequestService(HttpRequestService httpRequestService) { + this.httpRequestService = httpRequestService; + } + + protected void unbindHttpRequestService(HttpRequestService httpRequestService) { + this.httpRequestService = null; + logger.log(Level.INFO, "Unbound HTTP service. Further attempts to store data will fail until bound again."); + } + static class VmInfoUpdate { private final long stoppedTime; @@ -177,7 +158,6 @@ } } - // For testing purposes static class JsonHelper { @@ -198,56 +178,5 @@ } } - - // For testing purposes - static class HttpHelper { - - private final HttpClient httpClient; - - HttpHelper(HttpClient httpClient) { - this.httpClient = httpClient; - } - - void startClient() throws Exception { - httpClient.start(); - } - - StringContentProvider createContentProvider(String content) { - return new StringContentProvider(content); - } - - Request newRequest(String url) { - return new MockRequest(httpClient, URI.create(url)); - } - - } - - // FIXME This class should be removed when the web gateway has a microservice for this DAO - private static class MockRequest extends HttpRequest { - - MockRequest(HttpClient client, URI uri) { - super(client, uri); - } - - @Override - public ContentResponse send() throws InterruptedException, TimeoutException, ExecutionException { - return new MockResponse(); - } - - } - - // FIXME This class should be removed when the web gateway has a microservice for this DAO - private static class MockResponse extends HttpContentResponse { - - MockResponse() { - super(null, null, null); - } - - @Override - public int getStatus() { - return HttpStatus.OK_200; - } - - } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImplTest.java Wed Jun 28 16:22:22 2017 +0200 @@ -0,0 +1,129 @@ +/* + * 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.jvm.overview.agent.internal.model; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyListOf; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import com.redhat.thermostat.agent.http.HttpRequestService; +import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.JsonHelper; +import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.VmInfoUpdate; +import com.redhat.thermostat.jvm.overview.agent.model.VmInfo; + +public class VmInfoDAOImplTest { + + private static final String URL = "http://localhost:26000/api/v100/vm-info/systems/*/agents/foo-agent"; + private static final String UPDATE_URL = URL + "/jvms/vmId"; + private static final String SOME_JSON = "{\"some\" : \"json\"}"; + private static final String SOME_OTHER_JSON = "{\"some\" : {\"other\" : \"json\"}}"; + + private VmInfo info; + private JsonHelper jsonHelper; + private HttpRequestService httpRequestService; + + @Before + public void setUp() throws Exception { + String vmId = "vmId"; + int vmPid = 1; + long startTime = 2L; + long stopTime = Long.MIN_VALUE; + String jVersion = "java 1.0"; + String jHome = "/path/to/jdk/home"; + String mainClass = "Hello.class"; + String commandLine = "World"; + String vmArgs = "-XX=+FastestJITPossible"; + String vmName = "Hotspot"; + String vmInfo = "Some info"; + String vmVersion = "1.0"; + Map<String, String> props = new HashMap<>(); + Map<String, String> env = new HashMap<>(); + String[] libs = new String[0]; + long uid = 2000L; + String username = "myUser"; + info = new VmInfo("foo-agent", vmId, vmPid, startTime, stopTime, jVersion, jHome, + mainClass, commandLine, vmName, vmInfo, vmVersion, vmArgs, + props, env, libs, uid, username); + + httpRequestService = mock(HttpRequestService.class); + jsonHelper = mock(JsonHelper.class); + when(jsonHelper.toJson(anyListOf(VmInfo.class))).thenReturn(SOME_JSON); + when(jsonHelper.toJson(any(VmInfoUpdate.class))).thenReturn(SOME_OTHER_JSON); + } + + @Ignore("Re-enable when proper /jvms endpoint is being used") + @Test + public void testPutVmInfo() throws Exception { + VmInfoDAOImpl dao = new VmInfoDAOImpl(jsonHelper); + dao.bindHttpRequestService(httpRequestService); + dao.putVmInfo(info); + + verify(jsonHelper).toJson(eq(Arrays.asList(info))); + verify(httpRequestService).sendHttpRequest(SOME_JSON, URL, HttpRequestService.POST); + } + + @Ignore("Re-enable when proper /jvms endpoint is being used") + @Test + public void testPutVmStoppedTime() throws Exception { + VmInfoDAOImpl dao = new VmInfoDAOImpl(jsonHelper); + dao.bindHttpRequestService(httpRequestService); + + dao.putVmStoppedTime("foo-agent", "vmId", 3L); + + ArgumentCaptor<VmInfoUpdate> updateCaptor = ArgumentCaptor.forClass(VmInfoUpdate.class); + verify(jsonHelper).toJson(updateCaptor.capture()); + VmInfoUpdate update = updateCaptor.getValue(); + assertEquals(3L, update.getStoppedTime()); + + verify(httpRequestService).sendHttpRequest(SOME_OTHER_JSON, UPDATE_URL, HttpRequestService.PUT); + } + +} +
--- a/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOTest.java Fri Jun 23 15:16:13 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,151 +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.jvm.overview.agent.internal.model; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyListOf; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.HttpHelper; -import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.JsonHelper; -import com.redhat.thermostat.jvm.overview.agent.internal.model.VmInfoDAOImpl.VmInfoUpdate; -import com.redhat.thermostat.jvm.overview.agent.model.VmInfo; -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; - -public class VmInfoDAOTest { - - private static final String URL = "http://localhost:26000/api/v100/vm-info/systems/*/agents/foo-agent"; - private static final String UPDATE_URL = URL + "/jvms/vmId"; - private static final String SOME_JSON = "{\"some\" : \"json\"}"; - private static final String SOME_OTHER_JSON = "{\"some\" : {\"other\" : \"json\"}}"; - private static final String CONTENT_TYPE = "application/json"; - - private VmInfo info; - private JsonHelper jsonHelper; - private HttpHelper httpHelper; - private StringContentProvider contentProvider; - private Request request; - private ContentResponse response; - - @Before - public void setUp() throws Exception { - String vmId = "vmId"; - int vmPid = 1; - long startTime = 2L; - long stopTime = Long.MIN_VALUE; - String jVersion = "java 1.0"; - String jHome = "/path/to/jdk/home"; - String mainClass = "Hello.class"; - String commandLine = "World"; - String vmArgs = "-XX=+FastestJITPossible"; - String vmName = "Hotspot"; - String vmInfo = "Some info"; - String vmVersion = "1.0"; - Map<String, String> props = new HashMap<>(); - Map<String, String> env = new HashMap<>(); - String[] libs = new String[0]; - long uid = 2000L; - String username = "myUser"; - info = new VmInfo("foo-agent", vmId, vmPid, startTime, stopTime, jVersion, jHome, - mainClass, commandLine, vmName, vmInfo, vmVersion, vmArgs, - props, env, libs, uid, username); - - httpHelper = mock(HttpHelper.class); - contentProvider = mock(StringContentProvider.class); - when(httpHelper.createContentProvider(anyString())).thenReturn(contentProvider); - request = mock(Request.class); - when(httpHelper.newRequest(anyString())).thenReturn(request); - response = mock(ContentResponse.class); - when(response.getStatus()).thenReturn(HttpStatus.OK_200); - when(request.send()).thenReturn(response); - - jsonHelper = mock(JsonHelper.class); - when(jsonHelper.toJson(anyListOf(VmInfo.class))).thenReturn(SOME_JSON); - when(jsonHelper.toJson(any(VmInfoUpdate.class))).thenReturn(SOME_OTHER_JSON); - } - - @Test - public void testPutVmInfo() throws Exception { - VmInfoDAO dao = new VmInfoDAOImpl(httpHelper, jsonHelper); - dao.putVmInfo(info); - - verify(httpHelper).newRequest(URL); - verify(request).method(HttpMethod.POST); - verify(jsonHelper).toJson(eq(Arrays.asList(info))); - verify(httpHelper).createContentProvider(SOME_JSON); - verify(request).content(contentProvider, CONTENT_TYPE); - verify(request).send(); - verify(response).getStatus(); - } - - @Test - public void testPutVmStoppedTime() throws Exception { - VmInfoDAO dao = new VmInfoDAOImpl(httpHelper, jsonHelper); - dao.putVmStoppedTime("foo-agent", "vmId", 3L); - - verify(httpHelper).newRequest(UPDATE_URL); - verify(request).method(HttpMethod.PUT); - - ArgumentCaptor<VmInfoUpdate> updateCaptor = ArgumentCaptor.forClass(VmInfoUpdate.class); - verify(jsonHelper).toJson(updateCaptor.capture()); - VmInfoUpdate update = updateCaptor.getValue(); - assertEquals(3L, update.getStoppedTime()); - - verify(httpHelper).createContentProvider(SOME_OTHER_JSON); - verify(request).content(contentProvider, CONTENT_TYPE); - verify(request).send(); - verify(response).getStatus(); - } - -} -