This is an automated email from the ASF dual-hosted git repository.
tmaret pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-core.git
The following commit(s) were added to refs/heads/master by this push:
new 57abab2 SLING-7229 - DistributionAgentServlet throws NPE upon test
action
57abab2 is described below
commit 57abab27db7adfc3bcd06083e090c83f8e82e10b
Author: tmaret <[email protected]>
AuthorDate: Fri Nov 3 13:53:34 2017 +0100
SLING-7229 - DistributionAgentServlet throws NPE upon test action
---
.../distribution/servlet/ServletJsonUtils.java | 55 ++++++++++-------
.../distribution/servlet/ServletJsonUtilsTest.java | 69 ++++++++++++++++++++++
2 files changed, 104 insertions(+), 20 deletions(-)
diff --git
a/src/main/java/org/apache/sling/distribution/servlet/ServletJsonUtils.java
b/src/main/java/org/apache/sling/distribution/servlet/ServletJsonUtils.java
index 0aac3e4..4c4b925 100644
--- a/src/main/java/org/apache/sling/distribution/servlet/ServletJsonUtils.java
+++ b/src/main/java/org/apache/sling/distribution/servlet/ServletJsonUtils.java
@@ -43,15 +43,6 @@ class ServletJsonUtils {
private final static Logger log =
LoggerFactory.getLogger(ServletJsonUtils.class);
public static void writeJson(SlingHttpServletResponse response,
DistributionResponse distributionResponse) throws IOException {
- JsonObjectBuilder json = Json.createObjectBuilder();
- try {
- json.add("success", distributionResponse.isSuccessful());
- json.add("state", distributionResponse.getState().name());
- json.add("message", distributionResponse.getMessage());
-
- } catch (JsonException e) {
- log.error("Cannot write json", e);
- }
switch (distributionResponse.getState()) {
case DISTRIBUTED:
@@ -67,14 +58,46 @@ class ServletJsonUtils {
// TODO
break;
}
- append(json.build(), response.getWriter());
+ JsonObject body = buildBody(distributionResponse);
+ append(body, response.getWriter());
}
public static void writeJson(SlingHttpServletResponse response, int
status, String message,
@Nullable Map<String, String> kv) throws
IOException {
+
+ response.setStatus(status);
+ JsonObject body = buildBody(message, kv);
+ append(body, response.getWriter());
+ }
+
+ private static void append(JsonObject json, Writer writer) throws
IOException {
+ StringWriter buffer = new StringWriter();
+ Json.createWriter(buffer).writeObject(json);
+ writer.append(buffer.toString());
+ }
+
+ protected static JsonObject buildBody(DistributionResponse
distributionResponse) {
JsonObjectBuilder json = Json.createObjectBuilder();
try {
- json.add("message", message);
+ json.add("success", distributionResponse.isSuccessful());
+ json.add("state", distributionResponse.getState().name());
+ String message = distributionResponse.getMessage();
+ if (message != null) {
+ json.add("message", message);
+ }
+
+ } catch (JsonException e) {
+ log.error("Cannot write json", e);
+ }
+ return json.build();
+ }
+
+ protected static JsonObject buildBody(String message, @Nullable
Map<String, String> kv) {
+ JsonObjectBuilder json = Json.createObjectBuilder();
+ try {
+ if (message != null) {
+ json.add("message", message);
+ }
if (kv != null && kv.size() > 0) {
for (Map.Entry<String, String> entry : kv.entrySet()) {
json.add(entry.getKey(), entry.getValue());
@@ -83,14 +106,6 @@ class ServletJsonUtils {
} catch (JsonException e) {
log.error("Cannot write json", e);
}
- response.setStatus(status);
-
- append(json.build(), response.getWriter());
- }
-
- private static void append(JsonObject json, Writer writer) throws
IOException {
- StringWriter buffer = new StringWriter();
- Json.createWriter(buffer).writeObject(json);
- writer.append(buffer.toString());
+ return json.build();
}
}
diff --git
a/src/test/java/org/apache/sling/distribution/servlet/ServletJsonUtilsTest.java
b/src/test/java/org/apache/sling/distribution/servlet/ServletJsonUtilsTest.java
new file mode 100644
index 0000000..1ccbe4e
--- /dev/null
+++
b/src/test/java/org/apache/sling/distribution/servlet/ServletJsonUtilsTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.distribution.servlet;
+
+import java.util.Collections;
+import java.util.Map;
+
+import javax.json.JsonObject;
+
+import org.apache.sling.distribution.DistributionRequestState;
+import org.apache.sling.distribution.DistributionResponse;
+import org.apache.sling.distribution.impl.SimpleDistributionResponse;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class ServletJsonUtilsTest {
+
+ @Test
+ public void testBuildBodyWithMessageAndNoProperties() throws Exception {
+ String message1 = "message #1";
+ JsonObject body = ServletJsonUtils.buildBody(message1, null);
+ assertEquals(message1, body.getString("message"));
+ }
+
+ @Test
+ public void testBuildBodyWithMessageAndProperties() throws Exception {
+ String message2 = "message #2";
+ String k1 = "keyOne", v1 = "value #1";
+ Map<String,String> props = Collections.singletonMap(k1, v1);
+ JsonObject body = ServletJsonUtils.buildBody(message2, props);
+ assertEquals(message2, body.getString("message"));
+ assertEquals(v1, body.getString(k1));
+ }
+
+ @Test
+ public void testBuildBodyWithDistributionResponseContainingMessage()
throws Exception {
+ String message1 = "message #1";
+ DistributionRequestState state = DistributionRequestState.ACCEPTED;
+ DistributionResponse response = new SimpleDistributionResponse(state,
message1);
+ JsonObject body = ServletJsonUtils.buildBody(response);
+ assertEquals(message1, body.getString("message"));
+ }
+
+ @Test
+ public void testBuildBodyWithDistributionResponseContainingNoMessage()
throws Exception {
+ DistributionRequestState state = DistributionRequestState.ACCEPTED;
+ DistributionResponse response = new SimpleDistributionResponse(state,
null);
+ JsonObject body = ServletJsonUtils.buildBody(response);
+ assertFalse(body.containsKey("message"));
+ }
+
+}
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].