Skip to content

Commit 257f309

Browse files
committed
Polishing contribution
Closes gh-486
1 parent 65facf4 commit 257f309

File tree

1 file changed

+34
-52
lines changed

1 file changed

+34
-52
lines changed

spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,23 @@
1616

1717
package org.springframework.graphql.data;
1818

19-
import java.util.ArrayList;
2019
import java.util.Collections;
2120
import java.util.HashMap;
22-
import java.util.HashSet;
2321
import java.util.List;
2422
import java.util.Map;
2523
import java.util.Objects;
2624
import java.util.Set;
2725
import java.util.stream.Collectors;
2826
import java.util.stream.IntStream;
29-
import java.util.stream.Stream;
3027

3128
import com.fasterxml.jackson.core.JsonProcessingException;
3229
import com.fasterxml.jackson.databind.ObjectMapper;
3330
import graphql.schema.DataFetchingEnvironment;
3431
import graphql.schema.DataFetchingEnvironmentImpl;
35-
import org.assertj.core.api.CollectionAssert;
3632
import org.junit.jupiter.api.Test;
3733

3834
import org.springframework.core.ResolvableType;
3935
import org.springframework.graphql.Book;
40-
import org.springframework.graphql.data.GraphQlArgumentBinder;
41-
import org.springframework.util.ReflectionUtils;
42-
import org.springframework.util.StringUtils;
4336
import org.springframework.validation.BindException;
4437
import org.springframework.validation.FieldError;
4538

@@ -61,7 +54,7 @@ class GraphQlArgumentBinderTests {
6154

6255

6356
@Test
64-
void defaultConstructor() throws Exception {
57+
void dataBinding() throws Exception {
6558

6659
Object result = this.binder.bind(
6760
environment("{\"key\":{\"name\":\"test\"}}"), "key",
@@ -72,7 +65,7 @@ void defaultConstructor() throws Exception {
7265
}
7366

7467
@Test
75-
void defaultConstructorWithNestedBeanProperty() throws Exception {
68+
void dataBindingWithNestedBeanProperty() throws Exception {
7669

7770
Object result = this.binder.bind(
7871
environment(
@@ -93,7 +86,7 @@ void defaultConstructorWithNestedBeanProperty() throws Exception {
9386
}
9487

9588
@Test
96-
void defaultConstructorWithNestedBeanListProperty() throws Exception {
89+
void dataBindingWithNestedBeanListProperty() throws Exception {
9790

9891
Object result = this.binder.bind(
9992
environment("{\"key\":{\"items\":[{\"name\":\"first\"},{\"name\":\"second\"}]}}"), "key",
@@ -105,7 +98,7 @@ void defaultConstructorWithNestedBeanListProperty() throws Exception {
10598
}
10699

107100
@Test // gh-301
108-
void defaultConstructorWithNestedBeanListEmpty() throws Exception {
101+
void dataBindingWithNestedBeanListEmpty() throws Exception {
109102

110103
Object result = this.binder.bind(
111104
environment("{\"key\":{\"items\": []}}"), "key",
@@ -116,7 +109,7 @@ void defaultConstructorWithNestedBeanListEmpty() throws Exception {
116109
}
117110

118111
@Test // gh-280
119-
void defaultConstructorBindingError() {
112+
void dataBindingBindingError() {
120113

121114
assertThatThrownBy(
122115
() -> this.binder.bind(
@@ -131,6 +124,35 @@ void defaultConstructorBindingError() {
131124
});
132125
}
133126

127+
@Test
128+
@SuppressWarnings("unchecked")
129+
void dataBindingToList() throws Exception {
130+
131+
Object result = this.binder.bind(
132+
environment("{\"key\": [\"1\", \"2\", \"3\"]}"), "key",
133+
ResolvableType.forClassWithGenerics(List.class, String.class));
134+
135+
assertThat(result).isNotNull().isInstanceOf(List.class);
136+
assertThat((List<String>) result).containsExactly("1", "2", "3");
137+
138+
// gh-486: List with null element
139+
result = this.binder.bind(
140+
environment("{\"key\": [\"1\", null, \"3\"]}"), "key",
141+
ResolvableType.forClassWithGenerics(List.class, String.class));
142+
143+
assertThat(result).isNotNull().isInstanceOf(List.class);
144+
assertThat((List<String>) result).containsExactly("1", null, "3");
145+
146+
// Empty list
147+
148+
result = this.binder.bind(
149+
environment("{\"key\": []}"), "key",
150+
ResolvableType.forClassWithGenerics(List.class, String.class));
151+
152+
assertThat(result).isNotNull().isInstanceOf(List.class);
153+
assertThat((List<String>) result).isEmpty();
154+
}
155+
134156
@Test
135157
void primaryConstructor() throws Exception {
136158

@@ -262,7 +284,6 @@ void coercionWithSingletonList() throws Exception {
262284
}
263285

264286
@Test // gh-392
265-
@SuppressWarnings("unchecked")
266287
void shouldHaveHigherDefaultAutoGrowLimit() throws Exception {
267288
String items = IntStream.range(0, 260).mapToObj(value -> "{\"name\":\"test\"}").collect(Collectors.joining(","));
268289
Object result = this.binder.bind(
@@ -282,45 +303,6 @@ void shouldUseTargetCollectionType() throws Exception {
282303
assertThat(((ItemSetHolder) result).getItems()).hasSize(5);
283304
}
284305

285-
@Test
286-
@SuppressWarnings("unchecked")
287-
void list() throws Exception {
288-
Object result = this.binder.bind(
289-
environment("{\"key\": [\"1\", \"2\", \"3\"]}"),
290-
"key",
291-
ResolvableType.forClassWithGenerics(List.class, String.class)
292-
);
293-
294-
assertThat(result).isNotNull().isInstanceOf(List.class);
295-
new CollectionAssert<>((List<String>) result).containsExactly("1", "2", "3");
296-
}
297-
298-
@Test
299-
@SuppressWarnings("unchecked")
300-
void listWithNullItem() throws Exception {
301-
Object result = this.binder.bind(
302-
environment("{\"key\": [\"1\", null, \"3\"]}"),
303-
"key",
304-
ResolvableType.forClassWithGenerics(List.class, String.class)
305-
);
306-
307-
assertThat(result).isNotNull().isInstanceOf(List.class);
308-
new CollectionAssert<>((List<String>) result).containsExactly("1", null, "3");
309-
}
310-
311-
@Test
312-
@SuppressWarnings("unchecked")
313-
void emptyList() throws Exception {
314-
Object result = this.binder.bind(
315-
environment("{\"key\": []}"),
316-
"key",
317-
ResolvableType.forClassWithGenerics(List.class, String.class)
318-
);
319-
320-
assertThat(result).isNotNull().isInstanceOf(List.class);
321-
new CollectionAssert<>((List<String>) result).isEmpty();
322-
}
323-
324306
@SuppressWarnings("unchecked")
325307
private DataFetchingEnvironment environment(String jsonPayload) throws JsonProcessingException {
326308
Map<String, Object> arguments = this.mapper.readValue(jsonPayload, Map.class);

0 commit comments

Comments
 (0)