# HG changeset patch # User Severin Gehwolf # Date 1505211540 -7200 # Node ID 6018bc5f92b3620a81b3b1ebdc5d9780514bad56 # Parent 2f33764996f8f3af2d428b2e48ebd05d2d4e7322 Clean-up http-client dependencies. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/024959.html diff -r 2f33764996f8 -r 6018bc5f92b3 agent/core/pom.xml --- a/agent/core/pom.xml Tue Sep 12 08:41:11 2017 -0400 +++ b/agent/core/pom.xml Tue Sep 12 12:19:00 2017 +0200 @@ -110,6 +110,10 @@ com.google.code.gson gson + + org.eclipse.jetty + jetty-client + org.apache.felix diff -r 2f33764996f8 -r 6018bc5f92b3 common/plugin/pom.xml --- a/common/plugin/pom.xml Tue Sep 12 08:41:11 2017 -0400 +++ b/common/plugin/pom.xml Tue Sep 12 12:19:00 2017 +0200 @@ -121,11 +121,6 @@ test - org.eclipse.jetty - jetty-client - ${jetty.version} - - org.apache.felix org.apache.felix.framework diff -r 2f33764996f8 -r 6018bc5f92b3 plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmTlabStatDAOImpl.java --- a/plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmTlabStatDAOImpl.java Tue Sep 12 08:41:11 2017 -0400 +++ b/plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmTlabStatDAOImpl.java Tue Sep 12 12:19:00 2017 +0200 @@ -38,25 +38,23 @@ import java.io.IOException; import java.net.URI; +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.config.experimental.ConfigurationInfoSource; -import com.redhat.thermostat.common.plugin.PluginConfiguration; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.vm.memory.agent.model.VmTlabStat; - import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; -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.HttpStatus; + +import com.redhat.thermostat.agent.http.HttpRequestService; +import com.redhat.thermostat.agent.http.HttpRequestService.Method; +import com.redhat.thermostat.agent.http.RequestFailedException; +import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; +import com.redhat.thermostat.common.plugin.PluginConfiguration; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.vm.memory.agent.model.VmTlabStat; @Component @Service(value = VmTlabStatDAO.class) @@ -64,10 +62,7 @@ private static final Logger logger = LoggingUtils.getLogger(VmTlabStatDAOImpl.class); private static final String PLUGIN_ID = "vm-memory"; - private static final String CONTENT_TYPE = "application/json"; - private final HttpClient client; - private final HttpHelper httpHelper; private final JsonHelper jsonHelper; private final ConfigurationCreator configurationCreator; @@ -75,15 +70,16 @@ @Reference private ConfigurationInfoSource configInfoSource; + + @Reference + private HttpRequestService httpRequestService; public VmTlabStatDAOImpl() throws Exception { - this(new HttpClient(), new HttpHelper(), new JsonHelper(new VmTlabStatTypeAdapter()), new ConfigurationCreator(), null); + this(new JsonHelper(new VmTlabStatTypeAdapter()), new ConfigurationCreator(), null); } - VmTlabStatDAOImpl(HttpClient client, HttpHelper httpHelper, JsonHelper jsonHelper, + VmTlabStatDAOImpl(JsonHelper jsonHelper, ConfigurationCreator configurationCreator, ConfigurationInfoSource configInfoSource) throws Exception { - this.client = client; - this.httpHelper = httpHelper; this.jsonHelper = jsonHelper; this.configurationCreator = configurationCreator; this.configInfoSource = configInfoSource; @@ -93,8 +89,6 @@ void activate() throws Exception { PluginConfiguration config = configurationCreator.create(configInfoSource); this.gatewayURL = config.getGatewayURL(); - - this.httpHelper.startClient(this.client); } @Override @@ -104,24 +98,15 @@ return; // try { // String json = jsonHelper.toJson(Arrays.asList(stat)); -// StringContentProvider provider = httpHelper.createContentProvider(json); -// -// Request httpRequest = client.newRequest(gatewayURL); -// httpRequest.method(HttpMethod.POST); -// httpRequest.content(provider, CONTENT_TYPE); -// sendRequest(httpRequest); -// } catch (Exception e) { +// httpRequestService.sendHttpRequest(json, gatewayURL, Method.POST); +// } catch (RequestFailedException | IOException e) { // logger.log(Level.WARNING, "Failed to send VmTlabStat 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()); - } + + // DS bind method + protected void bindHttpRequestService(HttpRequestService httpRequestService) { + this.httpRequestService = httpRequestService; } protected Logger getLogger() { @@ -142,19 +127,6 @@ } // For testing purposes - static class HttpHelper { - - void startClient(HttpClient httpClient) throws Exception { - httpClient.start(); - } - - StringContentProvider createContentProvider(String content) { - return new StringContentProvider(content); - } - - } - - // For testing purposes static class ConfigurationCreator { PluginConfiguration create(ConfigurationInfoSource source) { diff -r 2f33764996f8 -r 6018bc5f92b3 plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmTlabStatDAOTest.java --- a/plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmTlabStatDAOTest.java Tue Sep 12 08:41:11 2017 -0400 +++ b/plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmTlabStatDAOTest.java Tue Sep 12 12:19:00 2017 +0200 @@ -36,79 +36,61 @@ package com.redhat.thermostat.vm.memory.agent.internal.models; -import static com.redhat.thermostat.vm.memory.agent.internal.models.VmTlabStatDAOImpl.HttpHelper; -import static com.redhat.thermostat.vm.memory.agent.internal.models.VmTlabStatDAOImpl.JsonHelper; import static org.mockito.Matchers.anyListOf; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.mockito.Matchers.eq; import java.net.URI; import java.util.Arrays; -import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; -import com.redhat.thermostat.common.plugin.PluginConfiguration; -import com.redhat.thermostat.vm.memory.agent.model.VmTlabStat; -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.Ignore; import org.junit.Test; +import com.redhat.thermostat.agent.http.HttpRequestService; +import com.redhat.thermostat.agent.http.HttpRequestService.Method; +import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; +import com.redhat.thermostat.common.plugin.PluginConfiguration; +import com.redhat.thermostat.vm.memory.agent.internal.models.VmTlabStatDAOImpl.ConfigurationCreator; +import com.redhat.thermostat.vm.memory.agent.internal.models.VmTlabStatDAOImpl.JsonHelper; +import com.redhat.thermostat.vm.memory.agent.model.VmTlabStat; + public class VmTlabStatDAOTest { private static final String JSON = "{\"this\":\"is\",\"test\":\"JSON\"}"; private static final String VM_ID = "0xcafe"; private static final String AGENT_ID = "agent"; - private static final String CONTENT_TYPE = "application/json"; private static final URI GATEWAY_URI = URI.create("http://example.com/jvm-memory/0.0.2/"); - private HttpClient httpClient; - private HttpHelper httpHelper; private JsonHelper jsonHelper; - private StringContentProvider contentProvider; - private Request request; - private ContentResponse response; private PluginConfiguration config; + private HttpRequestService httpRequestService; + private ConfigurationCreator configCreator; + private ConfigurationInfoSource configInfoSource; @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); - - httpHelper = mock(HttpHelper.class); - contentProvider = mock(StringContentProvider.class); - when(httpHelper.createContentProvider(anyString())).thenReturn(contentProvider); + httpRequestService = mock(HttpRequestService.class); jsonHelper = mock(JsonHelper.class); when(jsonHelper.toJson(anyListOf(VmTlabStat.class))).thenReturn(JSON); config = mock(PluginConfiguration.class); when(config.getGatewayURL()).thenReturn(GATEWAY_URI); + configCreator = mock(ConfigurationCreator.class); + configInfoSource = mock(ConfigurationInfoSource.class); + when(configCreator.create(configInfoSource)).thenReturn(config); } @Test public void testActivation() throws Exception { - PluginConfiguration pluginConfig = mock(PluginConfiguration.class); - when(pluginConfig.getGatewayURL()).thenReturn(GATEWAY_URI); - VmTlabStatDAOImpl.ConfigurationCreator configCreator = mock(VmTlabStatDAOImpl.ConfigurationCreator.class); - ConfigurationInfoSource configInfoSource = mock(ConfigurationInfoSource.class); - when(configCreator.create(configInfoSource)).thenReturn(pluginConfig); - VmTlabStatDAOImpl dao = new VmTlabStatDAOImpl(httpClient, httpHelper, jsonHelper, configCreator, configInfoSource); + VmTlabStatDAOImpl dao = new VmTlabStatDAOImpl(jsonHelper, configCreator, configInfoSource); dao.activate(); - verify(pluginConfig, times(1)).getGatewayURL(); - verify(httpHelper, times(1)).startClient(httpClient); + verify(config, times(1)).getGatewayURL(); } @Test @@ -134,17 +116,12 @@ stat.setTotalFastWaste(678l); stat.setMaxFastWaste(333l); - VmTlabStatDAOImpl.ConfigurationCreator configurationCreator = new VmTlabStatDAOImpl.ConfigurationCreator(); - ConfigurationInfoSource configInfoSource = mock(ConfigurationInfoSource.class); - VmTlabStatDAO dao = new VmTlabStatDAOImpl(httpClient, httpHelper, jsonHelper, configurationCreator, configInfoSource); + VmTlabStatDAOImpl dao = new VmTlabStatDAOImpl(jsonHelper, configCreator, configInfoSource); + dao.bindHttpRequestService(httpRequestService); + dao.activate(); dao.putStat(stat); - verify(httpClient).newRequest(GATEWAY_URI); - 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(); + verify(httpRequestService).sendHttpRequest(eq(JSON), eq(GATEWAY_URI), eq(Method.POST)); } } diff -r 2f33764996f8 -r 6018bc5f92b3 storage/core/pom.xml --- a/storage/core/pom.xml Tue Sep 12 08:41:11 2017 -0400 +++ b/storage/core/pom.xml Tue Sep 12 12:19:00 2017 +0200 @@ -122,10 +122,6 @@ com.google.code.gson gson - - org.eclipse.jetty - jetty-client - javax.servlet