changeset 2729:9cbc2bcf39c2

Use enum for HTTP method in HttpRequestService Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-July/024358.html
author Miloslav Zezulka <mzezulka@redhat.com>
date Mon, 31 Jul 2017 12:43:10 +0200
parents e80359f05ea7
children 08bb838d3c6c
files agent/core/src/main/java/com/redhat/thermostat/agent/http/HttpRequestService.java agent/core/src/test/java/com/redhat/thermostat/agent/http/HttpRequestServiceTest.java common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOTest.java plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOTest.java plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOTest.java plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImplTest.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/vm-gc/agent/src/main/java/com/redhat/thermostat/vm/gc/agent/internal/models/VmGcStatDAOImpl.java plugins/vm-gc/agent/src/test/java/com/redhat/thermostat/vm/gc/agent/internal/models/VmGcStatDAOImplTest.java plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmMemoryStatDAOImpl.java plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmMemoryStatDAOImplTest.java
diffstat 13 files changed, 52 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/http/HttpRequestService.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/http/HttpRequestService.java	Mon Jul 31 12:43:10 2017 +0200
@@ -67,23 +67,6 @@
 @Service(value = HttpRequestService.class)
 public class HttpRequestService {
     
-    /**
-     * GET request type.
-     */
-    public static final String GET = HttpMethod.GET.asString();
-    /**
-     * PUT request type.
-     */
-    public static final String PUT = HttpMethod.PUT.asString();
-    /**
-     * POST request type.
-     */
-    public static final String POST = HttpMethod.POST.asString();
-    /**
-     * DELETE request type.
-     */
-    public static final String DELETE = HttpMethod.DELETE.asString();
-    
     private static final Logger logger = LoggingUtils.getLogger(HttpRequestService.class);
 
     private static final String KEYCLOAK_TOKEN_SERVICE = "/auth/realms/__REALM__/protocol/openid-connect/token";
@@ -122,19 +105,17 @@
      * Send a HTTP request
      * @param jsonPayload The payload to send, or null if no payload
      * @param uri The complete URI to send to
-     * @param requestType The HTTP request type: GET, PUT, POST or DELETE
+     * @param requestMethod The HTTP request type: GET, PUT, POST or DELETE
      * @return The returned body for GET requests. {@code null} otherwise.
      */
-    public String sendHttpRequest(String jsonPayload, URI uri, String requestType) throws RequestFailedException {
-        // TODO: refactor agent pass around HttpMethod enum instead of string - it's faster and takes less space.
-        HttpMethod requestMethod = HttpMethod.valueOf(requestType);
+    public String sendHttpRequest(String jsonPayload, URI uri, Method requestMethod) throws RequestFailedException {
         // Normalize URI to ensure any duplicate slashes are removed
         uri = uri.normalize();
         Request request = client.newRequest(uri);
         if (jsonPayload != null) {
             request.content(new StringContentProvider(jsonPayload), "application/json");
         }
-        request.method(requestMethod);
+        request.method(requestMethod.getHttpMethod());
 
         try {
             if (agentStartupConfiguration.isKeycloakEnabled()) {
@@ -145,7 +126,7 @@
             if (status != HttpStatus.OK_200) {
                 throw new RequestFailedException(status, "Request to gateway failed. Reason: " + response.getReason());
             }
-            if (requestMethod == HttpMethod.GET) {
+            if (requestMethod == Method.GET) {
                 return response.getContentAsString();
             } else {
                 return null;
@@ -262,4 +243,25 @@
             return reasonStr;
         }
     }
-}
\ No newline at end of file
+
+    /**
+     * HTTP methods for microservice requests.
+     * @author mzezulka
+     */
+    public static enum Method {
+        POST(HttpMethod.POST), 
+        GET(HttpMethod.GET), 
+        DELETE(HttpMethod.DELETE), 
+        PUT(HttpMethod.PUT);
+        
+        private final HttpMethod httpMethod;
+
+        Method(HttpMethod method) {
+            this.httpMethod = method;
+        }
+
+        public HttpMethod getHttpMethod() {
+            return httpMethod;
+        }
+    }
+}
--- a/agent/core/src/test/java/com/redhat/thermostat/agent/http/HttpRequestServiceTest.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/agent/core/src/test/java/com/redhat/thermostat/agent/http/HttpRequestServiceTest.java	Mon Jul 31 12:43:10 2017 +0200
@@ -57,7 +57,6 @@
 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;
@@ -67,9 +66,9 @@
 import com.redhat.thermostat.agent.http.HttpRequestService.HttpClientCreator;
 import com.redhat.thermostat.agent.http.HttpRequestService.RequestFailedException;
 import com.redhat.thermostat.shared.config.SSLConfiguration;
+import org.eclipse.jetty.http.HttpMethod;
 
 public class HttpRequestServiceTest {
-    private static final String POST_METHOD = HttpRequestService.POST;
     private static final URI GATEWAY_URI = URI.create("http://127.0.0.1:30000/test/");
     private static final URI GET_URI = GATEWAY_URI.resolve("?q=foo&l=3");
     private static final String payload = "{}";
@@ -97,7 +96,7 @@
 
         HttpRequestService service = createAndActivateRequestService(configuration);
 
-        service.sendHttpRequest(payload, GATEWAY_URI, POST_METHOD);
+        service.sendHttpRequest(payload, GATEWAY_URI, com.redhat.thermostat.agent.http.HttpRequestService.Method.POST);
 
         verify(configuration).isKeycloakEnabled();
         verifyHttpPostRequest(httpRequest);
@@ -113,7 +112,7 @@
         Request keycloakRequest = mock(Request.class);
         setupKeycloakRequest(keycloakRequest);
 
-        service.sendHttpRequest(payload, GATEWAY_URI, POST_METHOD);
+        service.sendHttpRequest(payload, GATEWAY_URI, com.redhat.thermostat.agent.http.HttpRequestService.Method.POST);
 
         verify(configuration).isKeycloakEnabled();
         verifyHttpPostRequest(httpRequest);
@@ -132,7 +131,7 @@
         Request keycloakRequest = mock(Request.class);
         setupKeycloakRequest(keycloakRequest);
 
-        service.sendHttpRequest(payload, GATEWAY_URI, POST_METHOD);
+        service.sendHttpRequest(payload, GATEWAY_URI, com.redhat.thermostat.agent.http.HttpRequestService.Method.POST);
 
         verify(configuration).isKeycloakEnabled();
         verifyHttpPostRequest(httpRequest);
@@ -141,12 +140,12 @@
 
         verifyKeycloakAcquire(keycloakRequest);
 
-        service.sendHttpRequest(payload, GATEWAY_URI, POST_METHOD);
+        service.sendHttpRequest(payload, GATEWAY_URI, com.redhat.thermostat.agent.http.HttpRequestService.Method.POST);
 
 
         ArgumentCaptor<StringContentProvider> payloadCaptor = ArgumentCaptor.forClass(StringContentProvider.class);
         verify(keycloakRequest, times(2)).content(payloadCaptor.capture(), eq("application/x-www-form-urlencoded"));
-        verify(keycloakRequest, times(2)).method(eq(HttpMethod.valueOf(POST_METHOD)));
+        verify(keycloakRequest, times(2)).method(eq(HttpMethod.POST));
         verify(keycloakRequest, times(2)).send();
 
         String expected = "grant_type=refresh_token&client_id=client&refresh_token=refresh";
@@ -165,14 +164,14 @@
 
         HttpRequestService service = createAndActivateRequestService(configuration);
 
-        String response = service.sendHttpRequest(null, GATEWAY_URI, POST_METHOD);
+        String response = service.sendHttpRequest(null, GATEWAY_URI, com.redhat.thermostat.agent.http.HttpRequestService.Method.POST);
         assertNull(response);
 
         verify(client).newRequest(GATEWAY_URI);
         verify(configuration).isKeycloakEnabled();
 
         verify(httpRequest, times(0)).content(any(StringContentProvider.class), anyString());
-        verify(httpRequest).method(eq(HttpMethod.valueOf(POST_METHOD)));
+        verify(httpRequest).method(eq(HttpMethod.POST));
         verify(httpRequest).send();
     }
     
@@ -192,7 +191,7 @@
         AgentStartupConfiguration configuration = createNoKeycloakConfig();
         HttpRequestService service = new HttpRequestService(creator, configuration);
         service.activate();
-        String content = service.sendHttpRequest(null, GET_URI, HttpRequestService.GET);
+        String content = service.sendHttpRequest(null, GET_URI, com.redhat.thermostat.agent.http.HttpRequestService.Method.GET);
         verify(getClient).newRequest(GET_URI);
         assertEquals(getContent, content);
     }
@@ -210,7 +209,7 @@
         // Add extra slashes to URI
         URI uri = URI.create("http://127.0.0.1:30000//test//?q=bar&l=5");
         URI normalized = URI.create("http://127.0.0.1:30000/test/?q=bar&l=5");
-        String content = service.sendHttpRequest(null, uri, HttpRequestService.GET);
+        String content = service.sendHttpRequest(null, uri, com.redhat.thermostat.agent.http.HttpRequestService.Method.GET);
         verify(getClient).newRequest(normalized);
         assertEquals(getContent, content);
     }
@@ -234,7 +233,7 @@
         AgentStartupConfiguration configuration = createNoKeycloakConfig();
         doThrow(IOException.class).when(request).send();
         HttpRequestService service = createAndActivateRequestService(configuration);
-        service.sendHttpRequest("foo", GATEWAY_URI, HttpRequestService.DELETE /*any valid method*/);
+        service.sendHttpRequest("foo", GATEWAY_URI, com.redhat.thermostat.agent.http.HttpRequestService.Method.DELETE /*any valid method*/);
     }
 
     private AgentStartupConfiguration createNoKeycloakConfig() {
@@ -246,7 +245,7 @@
     private void verifyHttpPostRequest(Request httpRequest) throws InterruptedException, ExecutionException, TimeoutException {
         verify(client).newRequest(GATEWAY_URI);
         verify(httpRequest).content(any(StringContentProvider.class), eq("application/json"));
-        verify(httpRequest).method(eq(HttpMethod.valueOf(POST_METHOD)));
+        verify(httpRequest).method(eq(HttpMethod.POST));
         verify(httpRequest).send();
     }
 
@@ -283,7 +282,7 @@
     private void verifyKeycloakAcquire(Request keycloakRequest) throws InterruptedException, ExecutionException, TimeoutException {
         ArgumentCaptor<StringContentProvider> payloadCaptor = ArgumentCaptor.forClass(StringContentProvider.class);
         verify(keycloakRequest).content(payloadCaptor.capture(), eq("application/x-www-form-urlencoded"));
-        verify(keycloakRequest).method(eq(HttpMethod.valueOf(POST_METHOD)));
+        verify(keycloakRequest).method(eq(HttpMethod.POST));
         verify(keycloakRequest).send();
 
         String expected = "grant_type=password&client_id=client&username=username&password=password";
--- a/common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/common/plugin/src/main/java/com/redhat/thermostat/common/plugin/PluginDAOBase.java	Mon Jul 31 12:43:10 2017 +0200
@@ -59,7 +59,7 @@
 
             final URI gatewayURI = getConfig().getGatewayURL();
             final URI postURI = getPostURI(gatewayURI);
-            httpRequestService.sendHttpRequest(json, postURI, HttpRequestService.POST);
+            httpRequestService.sendHttpRequest(json, postURI, HttpRequestService.Method.POST);
         } catch (IOException | RequestFailedException e) {
             getLogger().log(Level.WARNING, "Failed to send " + obj.getClass().getName() + " to web gateway", e);
         }
--- a/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOTest.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/host-cpu/agent/src/test/java/com/redhat/thermostat/host/cpu/agent/internal/CpuStatDAOTest.java	Mon Jul 31 12:43:10 2017 +0200
@@ -100,7 +100,7 @@
         dao.activate();
         dao.put(info);
 
-        verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, GATEWAY_URI.resolve("systems/" + HOST_NAME), HttpRequestService.POST);
+        verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, GATEWAY_URI.resolve("systems/" + HOST_NAME), HttpRequestService.Method.POST);
     }
 
 }
--- a/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOTest.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/host-memory/agent/src/test/java/com/redhat/thermostat/host/memory/agent/internal/MemoryStatDAOTest.java	Mon Jul 31 12:43:10 2017 +0200
@@ -97,7 +97,7 @@
         dao.activate();
         dao.put(info);
 
-        verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, GATEWAY_URI.resolve("systems/" + HOST_NAME), HttpRequestService.POST);
+        verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, GATEWAY_URI.resolve("systems/" + HOST_NAME), HttpRequestService.Method.POST);
     }
 }
 
--- a/plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOTest.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/host-network/agent/src/test/java/com/redhat/thermostat/host/network/internal/NetworkInfoListDAOTest.java	Mon Jul 31 12:43:10 2017 +0200
@@ -46,7 +46,6 @@
 import java.util.ArrayList;
 import java.util.List;
 
-import org.eclipse.jetty.http.HttpMethod;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -110,7 +109,7 @@
         NetworkInfoList obj = new NetworkInfoList(AGENT_ID, TIMESTAMP, new ArrayList<NetworkInterfaceInfo>());
         dao.put(obj);
 
-        verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, GATEWAY_URI.resolve("systems/" + HOST_NAME), HttpMethod.POST.asString());
+        verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, GATEWAY_URI.resolve("systems/" + HOST_NAME), HttpRequestService.Method.POST);
     }
 }
 
--- a/plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImplTest.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/host-overview/agent/src/test/java/com/redhat/thermostat/host/overview/internal/models/HostInfoDAOImplTest.java	Mon Jul 31 12:43:10 2017 +0200
@@ -102,7 +102,7 @@
         dao.activate();
         
         dao.put(info);
-        verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, PUT_URI, HttpRequestService.POST);
+        verify(httpRequestService, times(1)).sendHttpRequest(SOME_JSON, PUT_URI, HttpRequestService.Method.POST);
     }
 
 }
--- a/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImpl.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImpl.java	Mon Jul 31 12:43:10 2017 +0200
@@ -118,7 +118,7 @@
             // Encode as JSON and send as POST request
             String json = jsonHelper.toJson(Arrays.asList(info));
             URI uri = getAddURI();
-            httpRequestService.sendHttpRequest(json, uri, HttpRequestService.POST);
+            httpRequestService.sendHttpRequest(json, uri, HttpRequestService.Method.POST);
         } catch (IOException | RequestFailedException e) {
            logger.log(Level.WARNING, "Failed to send JVM information to web gateway", e);
         }
@@ -131,7 +131,7 @@
             VmInfoUpdate update = new VmInfoUpdate(timestamp);
             String json = jsonHelper.toJson(update);
             URI uri = getUpdateURI(vmId);
-            httpRequestService.sendHttpRequest(json, uri, HttpRequestService.PUT);
+            httpRequestService.sendHttpRequest(json, uri, HttpRequestService.Method.PUT);
         } catch (IOException | RequestFailedException e) {
            logger.log(Level.WARNING, "Failed to send JVM information update to web gateway", e);
         }
--- a/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImplTest.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/internal/model/VmInfoDAOImplTest.java	Mon Jul 31 12:43:10 2017 +0200
@@ -124,7 +124,7 @@
         dao.putVmInfo(info);
         
         verify(jsonHelper).toJson(eq(Arrays.asList(info)));
-        verify(httpRequestService).sendHttpRequest(SOME_JSON, POST_URI, HttpRequestService.POST);
+        verify(httpRequestService).sendHttpRequest(SOME_JSON, POST_URI, HttpRequestService.Method.POST);
     }
 
     @Test
@@ -140,7 +140,7 @@
         VmInfoUpdate update = updateCaptor.getValue();
         assertEquals(3L, update.getStoppedTime());
                 
-        verify(httpRequestService).sendHttpRequest(SOME_OTHER_JSON, UPDATE_URI, HttpRequestService.PUT);
+        verify(httpRequestService).sendHttpRequest(SOME_OTHER_JSON, UPDATE_URI, HttpRequestService.Method.PUT);
     }
 
 }
--- a/plugins/vm-gc/agent/src/main/java/com/redhat/thermostat/vm/gc/agent/internal/models/VmGcStatDAOImpl.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/vm-gc/agent/src/main/java/com/redhat/thermostat/vm/gc/agent/internal/models/VmGcStatDAOImpl.java	Mon Jul 31 12:43:10 2017 +0200
@@ -101,7 +101,7 @@
     public void putVmGcStat(final VmGcStat stat) {
         try {
             String json = jsonHelper.toJson(Arrays.asList(stat));
-            httpRequestService.sendHttpRequest(json, gatewayURL, HttpRequestService.POST);
+            httpRequestService.sendHttpRequest(json, gatewayURL, HttpRequestService.Method.POST);
         } catch (RequestFailedException | IOException e) {
             logger.log(Level.WARNING, "Failed to send VmGcStat information to web gateway", e);
         }
--- a/plugins/vm-gc/agent/src/test/java/com/redhat/thermostat/vm/gc/agent/internal/models/VmGcStatDAOImplTest.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/vm-gc/agent/src/test/java/com/redhat/thermostat/vm/gc/agent/internal/models/VmGcStatDAOImplTest.java	Mon Jul 31 12:43:10 2017 +0200
@@ -98,7 +98,7 @@
 
         verify(jsonHelper).toJson(eq(Arrays.asList(stat)));
 
-        verify(httpRequestService).sendHttpRequest(JSON, GATEWAY_URI, HttpRequestService.POST);
+        verify(httpRequestService).sendHttpRequest(JSON, GATEWAY_URI, HttpRequestService.Method.POST);
     }
 
 }
--- a/plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmMemoryStatDAOImpl.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/vm-memory/agent/src/main/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmMemoryStatDAOImpl.java	Mon Jul 31 12:43:10 2017 +0200
@@ -93,7 +93,7 @@
     public void putVmMemoryStat(final VmMemoryStat stat) {
         try {
             String json = jsonHelper.toJson(Arrays.asList(stat));
-            httpRequestService.sendHttpRequest(json, gatewayURL, HttpRequestService.POST);
+            httpRequestService.sendHttpRequest(json, gatewayURL, HttpRequestService.Method.POST);
         } catch (RequestFailedException | IOException e) {
             logger.log(Level.WARNING, "Failed to send VmMemoryStat to Web Gateway", e);
         }
--- a/plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmMemoryStatDAOImplTest.java	Mon Jul 31 10:42:26 2017 -0400
+++ b/plugins/vm-memory/agent/src/test/java/com/redhat/thermostat/vm/memory/agent/internal/models/VmMemoryStatDAOImplTest.java	Mon Jul 31 12:43:10 2017 +0200
@@ -123,7 +123,7 @@
         dao.putVmMemoryStat(stat);
 
         verify(jsonHelper).toJson(Arrays.asList(stat));
-        verify(httpRequestService).sendHttpRequest(JSON, GATEWAY_URI, HttpRequestService.POST);
+        verify(httpRequestService).sendHttpRequest(JSON, GATEWAY_URI, HttpRequestService.Method.POST);
     }
     
 }