killbill-aplcache

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/TagDefinitionJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/TagDefinitionJson.java
index 01c1dc8..908f6a0 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/TagDefinitionJson.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/TagDefinitionJson.java
@@ -15,6 +15,8 @@
  */
 package com.ning.billing.jaxrs.json;
 
+import javax.annotation.Nullable;
+
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
 
@@ -23,13 +25,12 @@ public class TagDefinitionJson {
     private final String description;
 
     public TagDefinitionJson() {
-        this.name = null;
-        this.description = null;
+        this(null, null);
     }
 
     @JsonCreator
-    public TagDefinitionJson(@JsonProperty("name") final String name,
-                             @JsonProperty("description") final String description) {
+    public TagDefinitionJson(@JsonProperty("name") @Nullable final String name,
+                             @JsonProperty("description") @Nullable final String description) {
         super();
         this.name = name;
         this.description = description;
@@ -44,41 +45,30 @@ public class TagDefinitionJson {
     }
 
     @Override
-    public int hashCode() {
-        final int prime = 31;
-        int result = 1;
-        result = prime * result
-                + ((description == null) ? 0 : description.hashCode());
-        result = prime * result + ((name == null) ? 0 : name.hashCode());
-        return result;
-    }
-
-    @Override
-    public boolean equals(final Object obj) {
-        if (this == obj) {
+    public boolean equals(final Object o) {
+        if (this == o) {
             return true;
         }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
+        if (o == null || getClass() != o.getClass()) {
             return false;
         }
-        final TagDefinitionJson other = (TagDefinitionJson) obj;
-        if (description == null) {
-            if (other.description != null) {
-                return false;
-            }
-        } else if (!description.equals(other.description)) {
+
+        final TagDefinitionJson that = (TagDefinitionJson) o;
+
+        if (description != null ? !description.equals(that.description) : that.description != null) {
             return false;
         }
-        if (name == null) {
-            if (other.name != null) {
-                return false;
-            }
-        } else if (!name.equals(other.name)) {
+        if (name != null ? !name.equals(that.name) : that.name != null) {
             return false;
         }
+
         return true;
     }
+
+    @Override
+    public int hashCode() {
+        int result = name != null ? name.hashCode() : 0;
+        result = 31 * result + (description != null ? description.hashCode() : 0);
+        return result;
+    }
 }
diff --git a/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestTagDefinitionJson.java b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestTagDefinitionJson.java
new file mode 100644
index 0000000..16a2cb6
--- /dev/null
+++ b/jaxrs/src/test/java/com/ning/billing/jaxrs/json/TestTagDefinitionJson.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2010-2012 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.jaxrs.json;
+
+import java.util.UUID;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+public class TestTagDefinitionJson {
+    private static final ObjectMapper mapper = new ObjectMapper();
+
+    @Test(groups = "fast")
+    public void testJson() throws Exception {
+        final String name = UUID.randomUUID().toString();
+        final String description = UUID.randomUUID().toString();
+        final TagDefinitionJson tagDefinitionJson = new TagDefinitionJson(name, description);
+        Assert.assertEquals(tagDefinitionJson.getName(), name);
+        Assert.assertEquals(tagDefinitionJson.getDescription(), description);
+
+        final String asJson = mapper.writeValueAsString(tagDefinitionJson);
+        Assert.assertEquals(asJson, "{\"name\":\"" + tagDefinitionJson.getName() + "\"," +
+                "\"description\":\"" + tagDefinitionJson.getDescription() + "\"}");
+
+        final TagDefinitionJson fromJson = mapper.readValue(asJson, TagDefinitionJson.class);
+        Assert.assertEquals(fromJson, tagDefinitionJson);
+    }
+}