QPID-8259: [Broker-J] Fix generation of json response for operations returning void
authorAlex Rudyy <orudyy@apache.org>
Fri, 21 Dec 2018 14:48:10 +0000 (14:48 +0000)
committerAlex Rudyy <orudyy@apache.org>
Fri, 21 Dec 2018 15:13:01 +0000 (15:13 +0000)
broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
systests/qpid-systests-http-management/src/main/java/org/apache/qpid/tests/http/HttpTestHelper.java
systests/qpid-systests-http-management/src/test/java/org/apache/qpid/tests/http/rest/model/OperationTest.java

index 9a53d11..ed41fcd 100644 (file)
@@ -255,23 +255,18 @@ public class RestServlet extends AbstractServlet
                 content.release();
             }
         }
-        else if (body != null)
+        else
         {
             response.setContentType(APPLICATION_JSON);
-            Object data;
-            if (managementResponse.getType() == ResponseType.MODEL_OBJECT)
+            if (body != null && managementResponse.getType() == ResponseType.MODEL_OBJECT)
             {
-                data = controller.formatConfiguredObject(
+                body = controller.formatConfiguredObject(
                         managementResponse.getBody(),
                         parameters,
                         managementRequest.isSecure()
                         || managementRequest.isConfidentialOperationAllowedOnInsecureChannel());
             }
-            else
-            {
-                data = managementResponse.getBody();
-            }
-            writeJsonResponse(data, request, response);
+            writeJsonResponse(body, request, response);
         }
     }
 
index 04bb288..ef6d9b1 100644 (file)
@@ -28,6 +28,7 @@ import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.security.KeyManagementException;
 import java.security.KeyStore;
 import java.security.KeyStoreException;
@@ -241,7 +242,9 @@ public class HttpTestHelper
             responseCode = connection.getResponseCode();
             Assert.assertEquals(String.format("Unexpected response code from : %s", path), expectedResponseCode, responseCode);
 
-            return new ObjectMapper().readValue(new ByteArrayInputStream(readConnectionInputStream(connection)), valueTypeRef);
+            byte[] data = readConnectionInputStream(connection);
+            LOGGER.debug("Response : {}", new String(data, StandardCharsets.UTF_8));
+            return new ObjectMapper().readValue(new ByteArrayInputStream(data), valueTypeRef);
         }
         finally
         {
@@ -263,7 +266,9 @@ public class HttpTestHelper
             responseCode = connection.getResponseCode();
             Assert.assertEquals(String.format("Unexpected response code from : %s", path), expectedResponseCode, responseCode);
 
-            return new ObjectMapper().readValue(new ByteArrayInputStream(readConnectionInputStream(connection)), valueTypeRef);
+            byte[] buf = readConnectionInputStream(connection);
+            LOGGER.debug("Response data: {}", new String(buf, StandardCharsets.UTF_8));
+            return new ObjectMapper().readValue(new ByteArrayInputStream(buf), valueTypeRef);
         }
         finally
         {
index 4ce88a4..ade744a 100644 (file)
@@ -26,8 +26,11 @@ import static org.apache.qpid.server.management.plugin.servlet.rest.AbstractServ
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.Matchers.greaterThan;
+import static org.hamcrest.Matchers.nullValue;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
 
+import java.io.FileNotFoundException;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -37,6 +40,7 @@ import org.junit.Test;
 
 import org.apache.qpid.tests.http.HttpRequestConfig;
 import org.apache.qpid.tests.http.HttpTestBase;
+import org.apache.qpid.tests.http.HttpTestHelper;
 
 @HttpRequestConfig
 public class OperationTest extends HttpTestBase
@@ -89,4 +93,17 @@ public class OperationTest extends HttpTestBase
     {
         getHelper().submitRequest("virtualhost/notfound", "POST", Collections.emptyMap(), SC_NOT_FOUND);
     }
+
+    @Test
+    public void invokeOperationReturningVoid() throws Exception
+    {
+        final HttpTestHelper brokerHelper = new HttpTestHelper(getBrokerAdmin());
+        final Void response = brokerHelper.postJson("broker/performGC",
+                                                    Collections.emptyMap(),
+                                                    new TypeReference<Void>()
+                                                    {
+                                                    },
+                                                    SC_OK);
+        assertThat(response, is(nullValue()));
+    }
 }