Repository: searchbox-io/Jest (java)

Issue:
Bulk does not have equals and hashCode
## Overview of the Issue
Bulk does not have equals and hashCode method, so it falls back to methods of AbstractAction which does not consider `bulkableActions`.

## Motivation for or Use Case
I cannot stub a method having `Bulk` as argument to behave differently based on payload.

## Is it a regression?
No

## Reproduce the Error
```java
import org.junit.Test;
import java.util.Collections;
import static org.junit.Assert.assertNotEquals;

@Test
public void testEqualsWithUnequalActions() {
    final String index = "twitter";
    final String type = "tweet";
    final Object payload = Collections.singletonMap("field", "value");
    final Update update = new Update.Builder(payload).build();

    final Bulk left = new Bulk.Builder()
            .addAction(update).defaultIndex(index).defaultType(type)
            .build();

    final Bulk right = new Bulk.Builder()
            .defaultIndex(index).defaultType(type)
            .build();

    assertNotEquals(left, right);
}


@Test
public void testHashCodeWithUnequalActions() {
    final String index = "twitter";
    final String type = "tweet";

    final Object leftPayload = Collections.singletonMap("field", "value");
    final Update leftAction = new Update.Builder(leftPayload).build();
    final Bulk left = new Bulk.Builder()
            .addAction(leftAction).defaultIndex(index).defaultType(type)
            .build();

    final Bulk right = new Bulk.Builder()
            .defaultIndex(index).defaultType(type)
            .build();

    assertNotEquals(left.hashCode(), right.hashCode());
}

```
## Related Issues
- Add equals method to Count, Search, and other action objects #180

## Suggest a Fix
```java
--- a/jest-common/src/main/java/io/searchbox/core/Bulk.java
+++ b/jest-common/src/main/java/io/searchbox/core/Bulk.java
@@ -155,6 +155,16 @@ public class Bulk extends AbstractAction<BulkResult> {
         return result;
     }
 
+    @Override
+    public boolean equals(Object obj) {
+        return super.equals(obj) && Objects.equals(bulkableActions, ((Bulk) obj).bulkableActions);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(super.hashCode(), this.bulkableActions);
+    }
+
     public static class Builder extends GenericResultAbstractAction.Builder<Bulk, Builder> {
         private List<BulkableAction> actions = new LinkedList<BulkableAction>();
         private String defaultIndex;

```

Pull request description:
added equals and hashCode methods to Bulk - fixes #651

Patch:
diff --git a/jest-common/src/main/java/io/searchbox/core/Bulk.java b/jest-common/src/main/java/io/searchbox/core/Bulk.java
index ee6b2498..cb28f704 100644
--- a/jest-common/src/main/java/io/searchbox/core/Bulk.java
+++ b/jest-common/src/main/java/io/searchbox/core/Bulk.java
@@ -155,6 +155,16 @@ public class Bulk extends AbstractAction<BulkResult> {
         return result;
     }
 
+    @Override
+    public boolean equals(Object obj) {
+        return super.equals(obj) && Objects.equals(bulkableActions, ((Bulk) obj).bulkableActions);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(super.hashCode(), this.bulkableActions);
+    }
+
     public static class Builder extends GenericResultAbstractAction.Builder<Bulk, Builder> {
         private List<BulkableAction> actions = new LinkedList<BulkableAction>();
         private String defaultIndex;


Test patch:
diff --git a/jest-common/src/test/java/io/searchbox/core/BulkTest.java b/jest-common/src/test/java/io/searchbox/core/BulkTest.java
index c4a8f98a..e64ce365 100644
--- a/jest-common/src/test/java/io/searchbox/core/BulkTest.java
+++ b/jest-common/src/test/java/io/searchbox/core/BulkTest.java
@@ -1,7 +1,9 @@
 package io.searchbox.core;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -109,6 +111,42 @@ public class BulkTest {
         assertEquals("twitter/tweet/_bulk", bulkWitIndexAndType.getURI(ElasticsearchVersion.UNKNOWN));
     }
 
+    @Test
+    public void testEqualsWithUnequalActions() {
+        final String index = "twitter";
+        final String type = "tweet";
+
+        final Object leftPayload = Collections.singletonMap("field", "value");
+        final Update leftAction = new Update.Builder(leftPayload).build();
+        final Bulk left = new Bulk.Builder()
+                .addAction(leftAction).defaultIndex(index).defaultType(type)
+                .build();
+
+        final Bulk right = new Bulk.Builder()
+                .defaultIndex(index).defaultType(type)
+                .build();
+
+        assertNotEquals(left, right);
+    }
+
+    @Test
+    public void testHashCodeWithUnequalActions() {
+        final String index = "twitter";
+        final String type = "tweet";
+
+        final Object leftPayload = Collections.singletonMap("field", "value");
+        final Update leftAction = new Update.Builder(leftPayload).build();
+        final Bulk left = new Bulk.Builder()
+                .addAction(leftAction).defaultIndex(index).defaultType(type)
+                .build();
+
+        final Bulk right = new Bulk.Builder()
+                .defaultIndex(index).defaultType(type)
+                .build();
+
+        assertNotEquals(left.hashCode(), right.hashCode());
+    }
+
     private void executeAsserts(Bulk bulk) {
         assertEquals("POST", bulk.getRestMethodName());
         assertEquals("/_bulk", bulk.getURI(ElasticsearchVersion.UNKNOWN));