Skip to content

Commit 99dd64a

Browse files
committed
unit test graphql
1 parent bc94920 commit 99dd64a

File tree

5 files changed

+545
-21
lines changed

5 files changed

+545
-21
lines changed
Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,86 @@
11
package com.tvd12.ezyhttp.server.graphql.data;
22

3-
import lombok.Builder;
3+
import com.tvd12.ezyfox.builder.EzyBuilder;
44
import lombok.Getter;
55

6-
import java.util.List;
7-
import java.util.Map;
6+
import java.util.*;
87

98
@Getter
10-
@Builder
119
public class GraphQLError {
12-
private String message;
13-
private List<Map<String, Object>> locations;
14-
private List<String> path;
15-
private Map<String, Object> extensions;
10+
11+
private final String message;
12+
private final List<Map<String, Object>> locations;
13+
private final List<String> path;
14+
private final Map<String, Object> extensions;
15+
16+
protected GraphQLError(Builder builder) {
17+
this.message = builder.message;
18+
this.locations = builder.locations;
19+
this.path = builder.path;
20+
this.extensions = builder.extensions;
21+
}
22+
23+
public static Builder builder() {
24+
return new Builder();
25+
}
26+
27+
public static class Builder implements EzyBuilder<GraphQLError> {
28+
protected String message;
29+
protected List<Map<String, Object>> locations;
30+
protected List<String> path;
31+
protected Map<String, Object> extensions;
32+
33+
public Builder message(String message) {
34+
this.message = message;
35+
return this;
36+
}
37+
38+
public Builder location(Map<String, Object> location) {
39+
if (locations == null) {
40+
locations = new ArrayList<>();
41+
}
42+
locations.add(location);
43+
return this;
44+
}
45+
46+
public Builder locations(
47+
List<Map<String, Object>> locations
48+
) {
49+
if (this.locations == null) {
50+
this.locations = new ArrayList<>();
51+
}
52+
this.locations.addAll(locations);
53+
return this;
54+
}
55+
56+
public Builder path(String... paths) {
57+
return path(Arrays.asList(paths));
58+
}
59+
60+
public Builder path(List<String> path) {
61+
this.path = path;
62+
return this;
63+
}
64+
65+
public Builder extension(String key, Object value) {
66+
if (extensions == null) {
67+
extensions = new HashMap<>();
68+
}
69+
extensions.put(key, value);
70+
return this;
71+
}
72+
73+
public Builder extensions(Map<String, Object> extensions) {
74+
if (this.extensions == null) {
75+
this.extensions = new HashMap<>();
76+
}
77+
this.extensions.putAll(extensions);
78+
return this;
79+
}
80+
81+
@Override
82+
public GraphQLError build() {
83+
return new GraphQLError(this);
84+
}
85+
}
1686
}
Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.tvd12.ezyhttp.server.graphql.exception;
22

3+
import com.tvd12.ezyfox.builder.EzyBuilder;
34
import com.tvd12.ezyhttp.server.graphql.data.GraphQLError;
45
import lombok.Getter;
56

7+
import java.util.ArrayList;
68
import java.util.HashMap;
79
import java.util.List;
810
import java.util.Map;
@@ -13,19 +15,10 @@ public class GraphQLFetcherException extends RuntimeException {
1315
private final Map<String, Object> data;
1416
private final List<GraphQLError> errors;
1517

16-
public GraphQLFetcherException(
17-
List<GraphQLError> errors
18-
) {
19-
this(null, errors);
20-
}
21-
22-
public GraphQLFetcherException(
23-
Map<String, Object> data,
24-
List<GraphQLError> errors
25-
) {
26-
super("data: " + data + ", errors: " + errors);
27-
this.data = data;
28-
this.errors = errors;
18+
protected GraphQLFetcherException(Builder builder) {
19+
super("data: " + builder.data + ", errors: " + builder.errors);
20+
this.data = builder.data;
21+
this.errors = builder.errors;
2922
}
3023

3124
public Map<String, Object> toDataMap() {
@@ -36,4 +29,65 @@ public Map<String, Object> toDataMap() {
3629
answer.put("errors", errors);
3730
return answer;
3831
}
32+
33+
public static Builder builder() {
34+
return new Builder();
35+
}
36+
37+
public static class Builder implements EzyBuilder<GraphQLFetcherException> {
38+
protected Map<String, Object> data;
39+
protected List<GraphQLError> errors;
40+
41+
public Builder data(Map<String, Object> data) {
42+
if (this.data == null) {
43+
this.data = new HashMap<>();
44+
}
45+
this.data.putAll(data);
46+
return this;
47+
}
48+
49+
@SuppressWarnings("unchecked")
50+
public Builder errorDataFieldAndPaths(
51+
String errorDataField,
52+
String... paths
53+
) {
54+
if (data == null) {
55+
data = new HashMap<>();
56+
}
57+
Map<String, Object> lastObject = data;
58+
for (String path : paths) {
59+
Object object = lastObject.get(path);
60+
if (object instanceof Map) {
61+
lastObject = (Map<String, Object>) object;
62+
} else {
63+
Map<String, Object> map = new HashMap<>();
64+
lastObject.put(path, map);
65+
lastObject = map;
66+
}
67+
}
68+
lastObject.put(errorDataField, null);
69+
return this;
70+
}
71+
72+
public Builder error(GraphQLError error) {
73+
if (errors == null) {
74+
errors = new ArrayList<>();
75+
}
76+
errors.add(error);
77+
return this;
78+
}
79+
80+
public Builder errors(List<GraphQLError> errors) {
81+
if (this.errors == null) {
82+
this.errors = new ArrayList<>();
83+
}
84+
this.errors.addAll(errors);
85+
return this;
86+
}
87+
88+
@Override
89+
public GraphQLFetcherException build() {
90+
return new GraphQLFetcherException(this);
91+
}
92+
}
3993
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.tvd12.ezyhttp.server.graphql.test.controller;
2+
3+
import com.tvd12.ezyfox.util.EzyMapBuilder;
4+
import com.tvd12.ezyhttp.server.graphql.controller.GraphQLExceptionHandler;
5+
import com.tvd12.ezyhttp.server.graphql.data.GraphQLError;
6+
import com.tvd12.ezyhttp.server.graphql.exception.GraphQLFetcherException;
7+
import com.tvd12.ezyhttp.server.graphql.exception.GraphQLInvalidSchemeException;
8+
import com.tvd12.ezyhttp.server.graphql.exception.GraphQLObjectMapperException;
9+
import com.tvd12.test.assertion.Asserts;
10+
import org.testng.annotations.Test;
11+
12+
import java.util.ArrayList;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
public class GraphQLExceptionHandlerTest {
18+
19+
@Test
20+
public void handleGraphQLFetcherExceptionTest() {
21+
// given
22+
Map<String, Object> data = new HashMap<>();
23+
List<GraphQLError> errors = new ArrayList<>();
24+
GraphQLFetcherException exception = GraphQLFetcherException.builder()
25+
.data(data)
26+
.errors(errors)
27+
.build();
28+
29+
GraphQLExceptionHandler instance = new GraphQLExceptionHandler();
30+
31+
// when
32+
Object actual = instance.handle(exception);
33+
34+
// then
35+
Asserts.assertEquals(
36+
actual,
37+
EzyMapBuilder.mapBuilder()
38+
.put("data", data)
39+
.put("errors", errors)
40+
.toMap(),
41+
false
42+
);
43+
}
44+
45+
@Test
46+
public void handleGraphQLInvalidSchemeExceptionTest() {
47+
// given
48+
List<GraphQLError> errors = new ArrayList<>();
49+
GraphQLInvalidSchemeException exception = new GraphQLInvalidSchemeException(
50+
errors
51+
);
52+
53+
GraphQLExceptionHandler instance = new GraphQLExceptionHandler();
54+
55+
// when
56+
Object actual = instance.handle(exception);
57+
58+
// then
59+
Asserts.assertEquals(
60+
actual,
61+
errors
62+
);
63+
}
64+
65+
@Test
66+
public void handleGraphQLObjectMapperExceptionTest() {
67+
// given
68+
List<GraphQLError> errors = new ArrayList<>();
69+
GraphQLObjectMapperException exception = new GraphQLObjectMapperException(
70+
errors,
71+
new Exception("test")
72+
);
73+
74+
GraphQLExceptionHandler instance = new GraphQLExceptionHandler();
75+
76+
// when
77+
Object actual = instance.handle(exception);
78+
79+
// then
80+
Asserts.assertEquals(
81+
actual,
82+
errors
83+
);
84+
}
85+
}

0 commit comments

Comments
 (0)