Skip to content

Commit 46c0323

Browse files
authored
Adding hashcode and equals for defer & stream payloads (graphql-java#3769)
* Add defer payload equals method * Add hashcode method to defer payload * Add stream payload hashcode and equals * Tidy up
1 parent c6b3b90 commit 46c0323

File tree

5 files changed

+149
-5
lines changed

5 files changed

+149
-5
lines changed

src/main/java/graphql/incremental/DeferPayload.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.LinkedHashMap;
99
import java.util.List;
1010
import java.util.Map;
11+
import java.util.Objects;
1112

1213
/**
1314
* Represents a defer payload
@@ -41,6 +42,20 @@ public Map<String, Object> toSpecification() {
4142
return map;
4243
}
4344

45+
@Override
46+
public int hashCode() {
47+
return Objects.hash(super.hashCode(), data);
48+
}
49+
50+
@Override
51+
public boolean equals(Object obj) {
52+
if (this == obj) return true;
53+
if (obj == null || getClass() != obj.getClass()) return false;
54+
if (!super.equals(obj)) return false;
55+
DeferPayload that = (DeferPayload) obj;
56+
return Objects.equals(data, that.data);
57+
}
58+
4459
/**
4560
* @return a {@link DeferPayload.Builder} that can be used to create an instance of {@link DeferPayload}
4661
*/

src/main/java/graphql/incremental/IncrementalPayload.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.LinkedHashMap;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.Objects;
1213

1314
import static java.util.stream.Collectors.toList;
1415

@@ -82,6 +83,19 @@ protected Object errorsToSpec(List<GraphQLError> errors) {
8283
return errors.stream().map(GraphQLError::toSpecification).collect(toList());
8384
}
8485

86+
public int hashCode() {
87+
return Objects.hash(path, label, errors, extensions);
88+
}
89+
90+
public boolean equals(Object obj) {
91+
if (this == obj) return true;
92+
if (obj == null || getClass() != obj.getClass()) return false;
93+
IncrementalPayload that = (IncrementalPayload) obj;
94+
return Objects.equals(path, that.path) &&
95+
Objects.equals(label, that.label) &&
96+
Objects.equals(errors, that.errors) &&
97+
Objects.equals(extensions, that.extensions);
98+
}
8599

86100
protected static abstract class Builder<T extends Builder<T>> {
87101
protected List<Object> path;

src/main/java/graphql/incremental/StreamPayload.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.LinkedHashMap;
88
import java.util.List;
99
import java.util.Map;
10+
import java.util.Objects;
1011

1112
/**
1213
* Represents a stream payload
@@ -40,6 +41,20 @@ public Map<String, Object> toSpecification() {
4041
return map;
4142
}
4243

44+
@Override
45+
public int hashCode() {
46+
return Objects.hash(super.hashCode(), items);
47+
}
48+
49+
@Override
50+
public boolean equals(Object obj) {
51+
if (this == obj) return true;
52+
if (obj == null || getClass() != obj.getClass()) return false;
53+
if (!super.equals(obj)) return false;
54+
StreamPayload that = (StreamPayload) obj;
55+
return Objects.equals(items, that.items);
56+
}
57+
4358
/**
4459
* @return a {@link Builder} that can be used to create an instance of {@link StreamPayload}
4560
*/

src/test/groovy/graphql/incremental/DeferPayloadTest.groovy

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import graphql.GraphqlErrorBuilder
44
import graphql.execution.ResultPath
55
import spock.lang.Specification
66

7+
import static graphql.GraphQLError.newError
8+
79
class DeferPayloadTest extends Specification {
810
def "null data is included"() {
911
def payload = DeferPayload.newDeferredItem()
@@ -96,4 +98,48 @@ class DeferPayloadTest extends Specification {
9698
path : ["test", "echo"],
9799
]
98100
}
101+
102+
def "equals and hashcode methods work correctly"() {
103+
when:
104+
def payload = DeferPayload.newDeferredItem()
105+
.data("data1")
106+
.path(["path1"])
107+
.label("label1")
108+
.errors([newError().message("message1").build()])
109+
.extensions([key: "value1"])
110+
.build()
111+
112+
def equivalentPayload = DeferPayload.newDeferredItem()
113+
.data("data1")
114+
.path(["path1"])
115+
.label("label1")
116+
.errors([newError().message("message1").build()])
117+
.extensions([key: "value1"])
118+
.build()
119+
120+
def totallyDifferentPayload = DeferPayload.newDeferredItem()
121+
.data("data2")
122+
.path(["path2"])
123+
.label("label2")
124+
.errors([newError().message("message2").build()])
125+
.extensions([key: "value2"])
126+
.build()
127+
128+
def slightlyDifferentPayload = DeferPayload.newDeferredItem()
129+
.data("data1")
130+
.path(["path1"])
131+
.label("label1")
132+
.errors([newError().message("message1").build()])
133+
.extensions([key: "value2"])
134+
.build()
135+
136+
then:
137+
payload == equivalentPayload
138+
payload != totallyDifferentPayload
139+
payload != slightlyDifferentPayload
140+
141+
payload.hashCode() == equivalentPayload.hashCode()
142+
payload.hashCode() != totallyDifferentPayload.hashCode()
143+
payload.hashCode() != slightlyDifferentPayload.hashCode()
144+
}
99145
}

src/test/groovy/graphql/incremental/StreamPayloadTest.groovy

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package graphql.incremental
22

3-
import graphql.GraphqlErrorBuilder
43
import graphql.execution.ResultPath
54
import spock.lang.Specification
65

6+
import static graphql.GraphqlErrorBuilder.newError
7+
78
class StreamPayloadTest extends Specification {
89
def "null data is included"() {
910
def payload = StreamPayload.newStreamedItem()
@@ -25,11 +26,11 @@ class StreamPayloadTest extends Specification {
2526
.items(["twow is that a bee"])
2627
.path(["hello"])
2728
.errors([])
28-
.addError(GraphqlErrorBuilder.newError()
29+
.addError(newError()
2930
.message("wow")
3031
.build())
3132
.addErrors([
32-
GraphqlErrorBuilder.newError()
33+
newError()
3334
.message("yep")
3435
.build(),
3536
])
@@ -65,12 +66,12 @@ class StreamPayloadTest extends Specification {
6566
def payload = StreamPayload.newStreamedItem()
6667
.items(["twow is that a bee"])
6768
.path(ResultPath.fromList(["test", "echo"]))
68-
.addError(GraphqlErrorBuilder.newError()
69+
.addError(newError()
6970
.message("wow")
7071
.build())
7172
.addErrors([])
7273
.errors([
73-
GraphqlErrorBuilder.newError()
74+
newError()
7475
.message("yep")
7576
.build(),
7677
])
@@ -96,4 +97,57 @@ class StreamPayloadTest extends Specification {
9697
path : ["test", "echo"],
9798
]
9899
}
100+
101+
def "test equals and hashCode methods work"() {
102+
given:
103+
def items1 = ["test1"]
104+
def items2 = ["test2", "test3"]
105+
def path1 = ["test", "echo"]
106+
def path2 = ["test", "echo", "foo"]
107+
def errors1 = [newError().message("error1").build()]
108+
def errors2 = [newError().message("error2").build()]
109+
def extensions1 = [echo: "1"]
110+
def extensions2 = [echo: "2"]
111+
112+
def payload = new StreamPayload.Builder()
113+
.items(items1)
114+
.path(path1)
115+
.label("label1")
116+
.errors(errors1)
117+
.extensions(extensions1)
118+
.build()
119+
120+
def equivalentPayload = new StreamPayload.Builder()
121+
.items(items1)
122+
.path(path1)
123+
.label("label1")
124+
.errors(errors1)
125+
.extensions(extensions1)
126+
.build()
127+
128+
def totallyDifferentPayload = new StreamPayload.Builder()
129+
.items(items2)
130+
.path(path2)
131+
.label("label2")
132+
.errors(errors2)
133+
.extensions(extensions2)
134+
.build()
135+
136+
def slightlyDifferentPayload = new StreamPayload.Builder()
137+
.items(items2)
138+
.path(path2)
139+
.label("label1")
140+
.errors(errors1)
141+
.extensions(extensions2)
142+
.build()
143+
144+
expect:
145+
payload == equivalentPayload
146+
payload != totallyDifferentPayload
147+
payload != slightlyDifferentPayload
148+
149+
payload.hashCode() == equivalentPayload.hashCode()
150+
payload.hashCode() != totallyDifferentPayload.hashCode()
151+
payload.hashCode() != slightlyDifferentPayload.hashCode()
152+
}
99153
}

0 commit comments

Comments
 (0)