Fix tests by parsing the created JSON reply
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / JsonReturnObject.java
index 3b237da..45bbb4f 100644 (file)
@@ -6,12 +6,13 @@ package net.pterodactylus.sone.web.ajax;
 
 import java.util.Map;
 
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
 import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonUnwrapped;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.BooleanNode;
 import com.fasterxml.jackson.databind.node.IntNode;
 import com.fasterxml.jackson.databind.node.TextNode;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Maps;
 
 /**
@@ -26,7 +27,6 @@ public class JsonReturnObject {
        private final boolean success;
 
        /** The returned values. */
-       @JsonUnwrapped
        private final Map<String, JsonNode> content = Maps.newHashMap();
 
        /**
@@ -40,6 +40,43 @@ public class JsonReturnObject {
        }
 
        //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns whether the request was successful.
+        *
+        * @return {@code true} if the request was successful, {@code false} otherwise
+        */
+       @VisibleForTesting
+       public boolean isSuccess() {
+               return success;
+       }
+
+       /**
+        * Returns the value stored under the given key.
+        *
+        * @param key
+        *              The key of the value to retrieve
+        * @return The value of the key, or {@code null} if there is no value for the
+        *         given key
+        */
+       @VisibleForTesting
+       public JsonNode get(String key) {
+               return content.get(key);
+       }
+
+       /**
+        * Returns the content of this object for serialization.
+        *
+        * @return The content of this object
+        */
+       @JsonAnyGetter
+       public Map<String, JsonNode> getContent() {
+               return content;
+       }
+
+       //
        // ACTIONS
        //
 
@@ -79,7 +116,7 @@ public class JsonReturnObject {
         * @return This JSON return object
         */
        public JsonReturnObject put(String key, String value) {
-               return put(key, new TextNode(value));
+               return put(key, TextNode.valueOf(value));
        }
 
        /**
@@ -96,4 +133,18 @@ public class JsonReturnObject {
                return this;
        }
 
+       @Override
+       public int hashCode() {
+               return Boolean.valueOf(success).hashCode() ^ content.hashCode();
+       }
+
+       @Override
+       public boolean equals(Object object) {
+               if ((object == null) || (object.getClass() != getClass())) {
+                       return false;
+               }
+               JsonReturnObject other = (JsonReturnObject) object;
+               return (success == other.success) && content.equals(other.content);
+       }
+
 }