forked from slackapi/java-slack-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONUtilityTest.java
More file actions
231 lines (185 loc) · 9.57 KB
/
JSONUtilityTest.java
File metadata and controls
231 lines (185 loc) · 9.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package test_locally.util;
import com.google.gson.*;
import com.slack.api.model.annotation.FieldPredicate;
import com.slack.api.model.block.ContextBlockElement;
import com.slack.api.model.block.DividerBlock;
import com.slack.api.model.block.LayoutBlock;
import com.slack.api.model.block.composition.MarkdownTextObject;
import com.slack.api.model.block.composition.PlainTextObject;
import com.slack.api.model.block.composition.TextObject;
import com.slack.api.model.block.element.BlockElement;
import com.slack.api.model.block.element.ImageElement;
import com.slack.api.model.block.element.OverflowMenuElement;
import com.slack.api.model.event.FunctionExecutedEvent;
import com.slack.api.model.annotation.Required;
import com.slack.api.util.json.*;
import lombok.Builder;
import lombok.Data;
import org.junit.Test;
import org.junit.runners.model.TestClass;
import test_locally.unit.GsonFactory;
import java.lang.reflect.Type;
import java.util.Arrays;
import static com.slack.api.model.block.composition.BlockCompositions.markdownText;
import static com.slack.api.model.block.composition.BlockCompositions.plainText;
import static com.slack.api.model.block.element.BlockElements.image;
import static com.slack.api.model.block.element.BlockElements.overflowMenu;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
public class JSONUtilityTest {
class GsonContextImpl implements JsonSerializationContext, JsonDeserializationContext {
@Override
public JsonElement serialize(Object src) {
return gson.toJsonTree(src);
}
@Override
public JsonElement serialize(Object src, Type typeOfSrc) {
return gson.toJsonTree(src, typeOfSrc);
}
@SuppressWarnings("unchecked")
@Override
public <R> R deserialize(JsonElement json, Type typeOfT) throws JsonParseException {
return (R) gson.fromJson(json, typeOfT);
}
}
final Gson gson = GsonFactory.createSnakeCase();
final GsonContextImpl context = new GsonContextImpl();
@Test
public void testGsonBlockElementFactory() {
GsonBlockElementFactory f = new GsonBlockElementFactory();
OverflowMenuElement overflowMenu = overflowMenu(r -> r.actionId("action"));
JsonElement json = f.serialize(overflowMenu, OverflowMenuElement.class, context);
assertNotNull(json);
BlockElement elem = f.deserialize(json, OverflowMenuElement.class, context);
assertNotNull(elem);
}
@Test
public void testGsonLayoutBlockFactory() {
GsonLayoutBlockFactory f = new GsonLayoutBlockFactory();
DividerBlock block = DividerBlock.builder().build();
JsonElement json = f.serialize(block, DividerBlock.class, context);
assertNotNull(json);
LayoutBlock b = f.deserialize(json, DividerBlock.class, context);
assertNotNull(b);
}
@Test
public void testGsonContextBlockElementFactory_image() {
GsonContextBlockElementFactory f = new GsonContextBlockElementFactory();
ImageElement image = image(r -> r.imageUrl("https://www.example.com/image.png"));
JsonElement json = f.serialize(image, ImageElement.class, context);
assertNotNull(json);
ContextBlockElement i = f.deserialize(json, ImageElement.class, context);
assertNotNull(i);
}
@Test
public void testGsonContextBlockElementFactory_markdownText() {
GsonContextBlockElementFactory f = new GsonContextBlockElementFactory();
MarkdownTextObject elm = markdownText(r -> r);
JsonElement json = f.serialize(elm, MarkdownTextObject.class, context);
assertNotNull(json);
ContextBlockElement i = f.deserialize(json, MarkdownTextObject.class, context);
assertNotNull(i);
}
@Test
public void testGsonContextBlockElementFactory_plainText() {
GsonContextBlockElementFactory f = new GsonContextBlockElementFactory();
PlainTextObject elm = plainText(r -> r);
JsonElement json = f.serialize(elm, PlainTextObject.class, context);
assertNotNull(json);
ContextBlockElement i = f.deserialize(json, PlainTextObject.class, context);
assertNotNull(i);
}
@Test
public void testGsonTextObjectFactory_markdownText() {
GsonTextObjectFactory f = new GsonTextObjectFactory();
MarkdownTextObject elm = markdownText(r -> r);
JsonElement json = f.serialize(elm, MarkdownTextObject.class, context);
assertNotNull(json);
TextObject i = f.deserialize(json, MarkdownTextObject.class, context);
assertNotNull(i);
}
@Test
public void testGsonTextObjectFactory_plainText() {
GsonTextObjectFactory f = new GsonTextObjectFactory();
PlainTextObject elm = plainText(r -> r);
JsonElement json = f.serialize(elm, PlainTextObject.class, context);
assertNotNull(json);
TextObject i = f.deserialize(json, PlainTextObject.class, context);
assertNotNull(i);
}
@Test
public void testGsonFunctionExecutedEventInputValueFactory() {
GsonFunctionExecutedEventInputValueFactory f = new GsonFunctionExecutedEventInputValueFactory();
FunctionExecutedEvent.InputValue value = new FunctionExecutedEvent.InputValue();
value.setStringValue("U111");
JsonElement json = f.serialize(value, FunctionExecutedEvent.InputValue.class, context);
assertNotNull(json);
FunctionExecutedEvent.InputValue parsed = f.deserialize(json, FunctionExecutedEvent.InputValue.class, context);
assertThat(parsed.asString(), is("U111"));
assertThat(parsed.asStringArray(), is(Arrays.asList("U111")));
value.setStringValue(null);
value.setStringValues(Arrays.asList("C111", "C222"));
json = f.serialize(value, FunctionExecutedEvent.InputValue.class, context);
assertNotNull(json);
parsed = f.deserialize(json, FunctionExecutedEvent.InputValue.class, context);
assertThat(parsed.asStringArray(), is(Arrays.asList("C111", "C222")));
}
@Test
public void testRequiredAdapterFactory_basicCase() {
Gson gson = GsonFactory.getBuilder(true, true)
.registerTypeAdapterFactory(new RequiredAdapterFactory()).create();
// Serialization
TestClassWithRequiredBasic instance = TestClassWithRequiredBasic.builder().build();
assertThrows(JsonParseException.class, () -> gson.toJson(instance));
// Deserialization
String json = "{\"name\": \"Hello\"}";
assertThrows(JsonParseException.class, () -> gson.fromJson(json, TestClassWithRequiredBasic.class));
}
@Test
public void testRequiredAdapterFactory_advancedCase() {
Gson gson = GsonFactory.getBuilder(true, true).registerTypeAdapterFactory(new RequiredAdapterFactory()).create();
// Serialization
JsonParseException e = assertThrows(JsonParseException.class, () -> gson.toJson(TestClassWithRequiredAdvanced.builder().build()));
assertThat(e.getMessage(), equalToIgnoringCase("Required field 'id' failed validation in TestClassWithRequiredAdvanced using predicate IntegerGreaterThanZero"));
e = assertThrows(JsonParseException.class, () -> gson.toJson(TestClassWithRequiredAdvanced.builder().id(1).build()));
assertThat(e.getMessage(), equalToIgnoringCase("Required field 'name' failed validation in TestClassWithRequiredAdvanced using predicate NonEmptyString"));
e = assertThrows(JsonParseException.class, () -> gson.toJson(TestClassWithRequiredAdvanced.builder().id(1).name("").build()));
assertThat(e.getMessage(), equalToIgnoringCase("Required field 'name' failed validation in TestClassWithRequiredAdvanced using predicate NonEmptyString"));
// Deserialization
e = assertThrows(JsonParseException.class, () -> gson.fromJson("{\"id\": 0}", TestClassWithRequiredAdvanced.class));
assertThat(e.getMessage(), equalToIgnoringCase("Required field 'id' failed validation in TestClassWithRequiredAdvanced using predicate IntegerGreaterThanZero"));
e = assertThrows(JsonParseException.class, () -> gson.fromJson("{\"id\": 1}", TestClassWithRequiredAdvanced.class));
assertThat(e.getMessage(), equalToIgnoringCase("Required field 'name' failed validation in TestClassWithRequiredAdvanced using predicate NonEmptyString"));
e = assertThrows(JsonParseException.class, () -> gson.fromJson("{\"id\": 1, \"name\": ''}", TestClassWithRequiredAdvanced.class));
assertThat(e.getMessage(), equalToIgnoringCase("Required field 'name' failed validation in TestClassWithRequiredAdvanced using predicate NonEmptyString"));
}
@Data
@Builder
private static class TestClassWithRequiredBasic {
@Required private Integer id;
private String name;
}
@Data
@Builder
private static class TestClassWithRequiredAdvanced {
@Required(validator = IntegerGreaterThanZero.class)
private int id;
@Required(validator = NonEmptyString.class)
private String name;
public static class IntegerGreaterThanZero implements FieldPredicate {
@Override
public boolean test(Object obj) {
return obj instanceof Integer && (int)obj > 0;
}
}
public static class NonEmptyString implements FieldPredicate {
@Override
public boolean test(Object obj) {
return obj instanceof String && !((String) obj).isEmpty();
}
}
}
}