Mercurial > hg > thermostat-ng > agent
changeset 2707:00feaa00f469
Update PluginDAOBase to use HttpService
This patch updates the common plugin code to use the new HttpService.
Also it retofits the changes to the host-overview plugin.
Reviewed-by: sgehwolf
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023900.html
author | Simon Tooke <stooke@redhat.com> |
---|---|
date | Thu, 22 Jun 2017 02:36:39 -0400 |
parents | 088eb9e6923c |
children | f88bf3542b62 |
files | common/plugin/pom.xml common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImplTest.java |
diffstat | 4 files changed, 71 insertions(+), 84 deletions(-) [+] |
line wrap: on
line diff
--- a/common/plugin/pom.xml Wed Jun 21 17:46:44 2017 -0400 +++ b/common/plugin/pom.xml Thu Jun 22 02:36:39 2017 -0400 @@ -151,6 +151,11 @@ <version>${project.version}</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>com.redhat.thermostat</groupId> + <artifactId>thermostat-agent-core</artifactId> + <version>${project.version}</version> + </dependency> </dependencies>
--- a/common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java Wed Jun 21 17:46:44 2017 -0400 +++ b/common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java Thu Jun 22 02:36:39 2017 -0400 @@ -37,57 +37,41 @@ package com.redhat.thermostat.common.plugin; import java.io.IOException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; -import org.eclipse.jetty.client.HttpClient; +import com.redhat.thermostat.agent.http.HttpRequestService; 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.HttpHeader; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpStatus; abstract public class PluginDAOBase<Tobj,Tdao> { - private static final String CONTENT_TYPE = "application/json"; - - protected final HttpClient httpClient; - - public PluginDAOBase(HttpClient client) { - this.httpClient = client; - } - protected abstract String toJsonString(Tobj obj) throws IOException; + protected abstract HttpRequestService getHttpRequestService(); protected abstract PluginConfiguration getConfig(); protected abstract String getURL(final String basepath); protected abstract Logger getLogger(); public void put(final Tobj obj) { try { - final String gatewayURL = getConfig().getGatewayURL(); - final String json = toJsonString(obj); - final StringContentProvider provider = new StringContentProvider(json); - final String url = getURL(gatewayURL); - final Request httpRequest = httpClient.newRequest(url); - httpRequest.method(HttpMethod.POST); - httpRequest.content(provider); - httpRequest.header(HttpHeader.CONTENT_TYPE, CONTENT_TYPE); - sendRequest(httpRequest); - } catch (IOException | InterruptedException | TimeoutException | ExecutionException e) { + if (null != getHttpRequestService()) { + String json = toJsonString(obj); + + final String gatewayURL = getConfig().getGatewayURL(); + final String url = getURL(gatewayURL); + final ContentResponse response = getHttpRequestService().sendHttpRequest(json, url, HttpMethod.POST); + final int status = response.getStatus(); + if (status != HttpStatus.OK_200) { + throw new IOException("Gateway returned HTTP status " + String.valueOf(status) + " - " + response.getReason()); + } + + } else { + getLogger().log(Level.WARNING, "Failed to send " + obj.getClass().getName() + " information to web gateway. Http service unavailable."); + } + } catch (Exception e) { getLogger().log(Level.WARNING, "Failed to send " + obj.getClass().getName() + " to web gateway", e); } } - - private void sendRequest(Request httpRequest) - throws InterruptedException, TimeoutException, ExecutionException, IOException { - final ContentResponse resp = httpRequest.send(); - final int status = resp.getStatus(); - if (status != HttpStatus.OK_200) { - throw new IOException("Gateway returned HTTP status " + String.valueOf(status) + " - " + resp.getReason()); - } - } }
--- a/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java Wed Jun 21 17:46:44 2017 -0400 +++ b/plugins/host-overview/agent/src/main/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImpl.java Thu Jun 22 02:36:39 2017 -0400 @@ -41,13 +41,12 @@ import java.util.List; import java.util.logging.Logger; +import com.redhat.thermostat.agent.http.HttpRequestService; import com.redhat.thermostat.common.plugin.SystemID; 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.util.StringContentProvider; import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; import com.redhat.thermostat.common.plugin.PluginConfiguration; @@ -66,36 +65,32 @@ public static final String PLUGIN_ID = "host-overview"; private final JsonHelper jsonHelper; - private final HttpHelper httpHelper; private final ConfigurationCreator configCreator; - - @Reference - private ConfigurationInfoSource configInfoSource; private PluginConfiguration config; @Reference + private ConfigurationInfoSource configurationInfoSource; + + @Reference + private HttpRequestService httpRequestService; + + @Reference private SystemID systemID; public HostInfoDAOImpl() { - this(new HttpClient(), new JsonHelper(new HostInfoTypeAdapter()), new HttpHelper(), - new ConfigurationCreator(), null); + this(new JsonHelper(new HostInfoTypeAdapter()), new ConfigurationCreator()); } - HostInfoDAOImpl(HttpClient client, JsonHelper jh, HttpHelper hh, ConfigurationCreator creator, - ConfigurationInfoSource source) { - super(client); + HostInfoDAOImpl(JsonHelper jh, ConfigurationCreator creator) { this.jsonHelper = jh; - this.httpHelper = hh; this.configCreator = creator; - this.configInfoSource = source; } @Activate void activate() throws Exception { - this.config = configCreator.create(configInfoSource); - httpHelper.startClient(httpClient); + this.config = configCreator.create(configurationInfoSource); } public String getURL(final String base) { @@ -111,6 +106,11 @@ } @Override + protected HttpRequestService getHttpRequestService() { + return httpRequestService; + } + + @Override protected String toJsonString(HostInfo obj) throws IOException { return jsonHelper.toJson(Arrays.asList(obj)); } @@ -125,6 +125,18 @@ this.systemID = systemid; } + protected void bindConfigurationInfoSource(ConfigurationInfoSource cfg) { + this.configurationInfoSource = cfg; + } + + protected void bindHttpRequestService(HttpRequestService httpRequestService) { + this.httpRequestService = httpRequestService; + } + + protected void unbindHttpRequestService(HttpRequestService httpRequestService) { + this.httpRequestService = null; + } + static class JsonHelper { private final HostInfoTypeAdapter typeAdapter; @@ -138,18 +150,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 {
--- a/plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImplTest.java Wed Jun 21 17:46:44 2017 -0400 +++ b/plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImplTest.java Thu Jun 22 02:36:39 2017 -0400 @@ -36,10 +36,12 @@ package com.redhat.thermostat.host.overview.internal.models; +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.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -47,6 +49,7 @@ import java.util.HashMap; import java.util.Map; +import com.redhat.thermostat.agent.http.HttpRequestService; import com.redhat.thermostat.common.plugin.SystemID; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentProvider; @@ -79,30 +82,29 @@ private static final String URL_PROP = "gatewayURL"; private HostInfo info; - private HttpClient httpClient; private HostInfoDAOImpl.JsonHelper jsonHelper; - private HostInfoDAOImpl.HttpHelper httpHelper; - private StringContentProvider contentProvider; - private Request request; - private ContentResponse response; private ConfigurationInfoSource cfiSource; private ConfigurationCreator configCreator; + private SystemID idservice; + private HttpRequestService httpRequestService; @Before public void setup() throws Exception { info = new HostInfo("foo-agent", HOST_NAME, OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL); - - httpHelper = mock(HostInfoDAOImpl.HttpHelper.class); - contentProvider = mock(StringContentProvider.class); - when(httpHelper.createContentProvider(anyString())).thenReturn(contentProvider); - request = mock(Request.class); - httpClient = mock(HttpClient.class); + + Request request = mock(Request.class); + HttpClient httpClient = mock(HttpClient.class); request = mock(Request.class); when(httpClient.newRequest(anyString())).thenReturn(request); - response = mock(ContentResponse.class); + ContentResponse response = mock(ContentResponse.class); when(response.getStatus()).thenReturn(HttpStatus.OK_200); when(request.send()).thenReturn(response); - + + httpRequestService = mock(HttpRequestService.class); + ContentResponse contentResponse = mock(ContentResponse.class); + when(httpRequestService.sendHttpRequest(anyString(), anyString(), any(HttpMethod.class))).thenReturn(contentResponse); + when(contentResponse.getStatus()).thenReturn(HttpStatus.OK_200); + jsonHelper = mock(HostInfoDAOImpl.JsonHelper.class); when(jsonHelper.toJson(anyListOf(HostInfo.class))).thenReturn(SOME_JSON); @@ -112,26 +114,22 @@ when(cfiSource.getConfiguration(anyString(),anyString())).thenReturn(map); configCreator = mock(ConfigurationCreator.class); when(configCreator.create(eq(cfiSource))).thenReturn(new PluginConfiguration(cfiSource, HostInfoDAOImpl.PLUGIN_ID)); + + idservice = mock(SystemID.class); + when(idservice.getSystemID()).thenReturn(HOST_NAME); } @Test public void testPutHostInfo() throws Exception { - HostInfoDAOImpl dao = new HostInfoDAOImpl(httpClient, jsonHelper, httpHelper, configCreator, cfiSource); - SystemID idservice = mock(SystemID.class); - when(idservice.getSystemID()).thenReturn(HOST_NAME); + HostInfoDAOImpl dao = new HostInfoDAOImpl(jsonHelper, configCreator); dao.bindSystemID(idservice); + dao.bindConfigurationInfoSource(cfiSource); + dao.bindHttpRequestService(httpRequestService); dao.activate(); dao.put(info); - - verify(httpClient).newRequest(URL + "/systems/" + HOST_NAME); - verify(request).method(HttpMethod.POST); - verify(jsonHelper).toJson(eq(Arrays.asList(info))); - verify(request).content(Matchers.any(ContentProvider.class)); - verify(request).header(HttpHeader.CONTENT_TYPE, "application/json"); - verify(request).send(); - verify(response).getStatus(); + verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, URL + "/systems/" + HOST_NAME, HttpMethod.POST); } }