-
Notifications
You must be signed in to change notification settings - Fork 37
/
ServerDtoTest.java
298 lines (235 loc) · 13.1 KB
/
ServerDtoTest.java
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*******************************************************************************
* Copyright (c) 2012-2015 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.dto;
import org.eclipse.che.dto.definitions.ComplicatedDto;
import org.eclipse.che.dto.definitions.DtoWithAny;
import org.eclipse.che.dto.definitions.DtoWithDelegate;
import org.eclipse.che.dto.definitions.DtoWithFieldNames;
import org.eclipse.che.dto.definitions.SimpleDto;
import org.eclipse.che.dto.server.DtoFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Tests that the interfaces specified in org.eclipse.che.dto.definitions have
* corresponding generated server implementations.
*
* @author <a href="mailto:azatsarynnyy@codenvy.com">Artem Zatsarynnyy</a>
*/
public class ServerDtoTest {
protected final static DtoFactory dtoFactory = DtoFactory.getInstance();
@Test
public void testCreateSimpleDto() throws Exception {
final String fooString = "Something";
final int fooId = 1;
final String _default = "test_default_keyword";
SimpleDto dto = dtoFactory.createDto(SimpleDto.class).withName(fooString).withId(fooId).withDefault(_default);
// Check to make sure things are in a sane state.
checkSimpleDto(dto, fooString, fooId, _default);
}
@Test
public void testSimpleDtoSerializer() throws Exception {
final String fooString = "Something";
final int fooId = 1;
final String _default = "test_default_keyword";
SimpleDto dto = dtoFactory.createDto(SimpleDto.class).withName(fooString).withId(fooId).withDefault(_default);
final String json = dtoFactory.toJson(dto);
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
Assert.assertEquals(jsonObject.get("name").getAsString(), fooString);
Assert.assertEquals(jsonObject.get("id").getAsInt(), fooId);
Assert.assertEquals(jsonObject.get("default").getAsString(), _default);
}
@Test
public void testSimpleDtoDeserializer() throws Exception {
final String fooString = "Something";
final int fooId = 1;
final String _default = "test_default_keyword";
JsonObject json = new JsonObject();
json.add("name", new JsonPrimitive(fooString));
json.add("id", new JsonPrimitive(fooId));
json.add("default", new JsonPrimitive(_default));
SimpleDto dto = dtoFactory.createDtoFromJson(json.toString(), SimpleDto.class);
// Check to make sure things are in a sane state.
checkSimpleDto(dto, fooString, fooId, _default);
}
@Test
public void testSerializerWithFieldNames() throws Exception {
final String fooString = "Something";
final String _default = "test_default_keyword";
DtoWithFieldNames dto = dtoFactory.createDto(DtoWithFieldNames.class).withTheName(fooString).withTheDefault(_default);
final String json = dtoFactory.toJson(dto);
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
Assert.assertEquals(jsonObject.get(DtoWithFieldNames.THENAME_FIELD).getAsString(), fooString);
Assert.assertEquals(jsonObject.get(DtoWithFieldNames.THEDEFAULT_FIELD).getAsString(), _default);
}
@Test
public void testDeerializerWithFieldNames() throws Exception {
final String fooString = "Something";
final String _default = "test_default_keyword";
JsonObject json = new JsonObject();
json.add(DtoWithFieldNames.THENAME_FIELD, new JsonPrimitive(fooString));
json.add(DtoWithFieldNames.THEDEFAULT_FIELD, new JsonPrimitive(_default));
DtoWithFieldNames dto = dtoFactory.createDtoFromJson(json.toString(), DtoWithFieldNames.class);
Assert.assertEquals(dto.getTheName(), fooString);
Assert.assertEquals(dto.getTheDefault(), _default);
}
@Test
public void testSerializerWithAny() throws Exception {
DtoWithAny dto = dtoFactory.createDto(DtoWithAny.class).withStuff(createTestValueForAny());
final String json = dtoFactory.toJson(dto);
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
Assert.assertEquals(jsonObject.get("stuff"), createTestValueForAny());
}
@Test
public void testDeerializerWithAny() throws Exception {
final JsonElement stuff = createTestValueForAny();
JsonObject json = new JsonObject();
json.add("stuff", stuff);
DtoWithAny dto = dtoFactory.createDtoFromJson(json.toString(), DtoWithAny.class);
Assert.assertEquals(dto.getStuff(), createTestValueForAny());
}
/** Intentionally call several times to ensure non-reference equality */
private static JsonElement createTestValueForAny() {
return new JsonParser().parse("{a:100,b:{c:'blah',d:null}}");
}
@Test
public void testListSimpleDtoDeserializer() throws Exception {
final String fooString_1 = "Something 1";
final int fooId_1 = 1;
final String _default_1 = "test_default_keyword_1";
final String fooString_2 = "Something 2";
final int fooId_2 = 2;
final String _default_2 = "test_default_keyword_2";
JsonObject json1 = new JsonObject();
json1.add("name", new JsonPrimitive(fooString_1));
json1.add("id", new JsonPrimitive(fooId_1));
json1.add("default", new JsonPrimitive(_default_1));
JsonObject json2 = new JsonObject();
json2.add("name", new JsonPrimitive(fooString_2));
json2.add("id", new JsonPrimitive(fooId_2));
json2.add("default", new JsonPrimitive(_default_2));
JsonArray jsonArray = new JsonArray();
jsonArray.add(json1);
jsonArray.add(json2);
org.eclipse.che.dto.shared.JsonArray<SimpleDto> listDtoFromJson =
dtoFactory.createListDtoFromJson(jsonArray.toString(), SimpleDto.class);
Assert.assertEquals(listDtoFromJson.get(0).getName(), fooString_1);
Assert.assertEquals(listDtoFromJson.get(0).getId(), fooId_1);
Assert.assertEquals(listDtoFromJson.get(0).getDefault(), _default_1);
Assert.assertEquals(listDtoFromJson.get(1).getName(), fooString_2);
Assert.assertEquals(listDtoFromJson.get(1).getId(), fooId_2);
Assert.assertEquals(listDtoFromJson.get(1).getDefault(), _default_2);
}
@Test
public void testComplicatedDtoSerializer() throws Exception {
final String fooString = "Something";
final int fooId = 1;
final String _default = "test_default_keyword";
List<String> listStrings = new ArrayList<>(2);
listStrings.add("Something 1");
listStrings.add("Something 2");
ComplicatedDto.SimpleEnum simpleEnum = ComplicatedDto.SimpleEnum.ONE;
// Assume that SimpleDto works. Use it to test nested objects
SimpleDto simpleDto = dtoFactory.createDto(SimpleDto.class).withName(fooString).withId(fooId).withDefault(_default);
Map<String, SimpleDto> mapDtos = new HashMap<>(1);
mapDtos.put(fooString, simpleDto);
List<SimpleDto> listDtos = new ArrayList<>(1);
listDtos.add(simpleDto);
List<List<ComplicatedDto.SimpleEnum>> listOfListOfEnum = new ArrayList<>(1);
List<ComplicatedDto.SimpleEnum> listOfEnum = new ArrayList<>(3);
listOfEnum.add(ComplicatedDto.SimpleEnum.ONE);
listOfEnum.add(ComplicatedDto.SimpleEnum.TWO);
listOfEnum.add(ComplicatedDto.SimpleEnum.THREE);
listOfListOfEnum.add(listOfEnum);
ComplicatedDto dto = dtoFactory.createDto(ComplicatedDto.class).withStrings(listStrings).
withSimpleEnum(simpleEnum).withMap(mapDtos).withSimpleDtos(listDtos).
withArrayOfArrayOfEnum(listOfListOfEnum);
final String json = dtoFactory.toJson(dto);
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
Assert.assertTrue(jsonObject.has("strings"));
JsonArray jsonArray = jsonObject.get("strings").getAsJsonArray();
Assert.assertEquals(jsonArray.get(0).getAsString(), listStrings.get(0));
Assert.assertEquals(jsonArray.get(1).getAsString(), listStrings.get(1));
Assert.assertTrue(jsonObject.has("simpleEnum"));
Assert.assertEquals(jsonObject.get("simpleEnum").getAsString(), simpleEnum.name());
Assert.assertTrue(jsonObject.has("map"));
JsonObject jsonMap = jsonObject.get("map").getAsJsonObject();
JsonObject value = jsonMap.get(fooString).getAsJsonObject();
Assert.assertEquals(value.get("name").getAsString(), fooString);
Assert.assertEquals(value.get("id").getAsInt(), fooId);
Assert.assertEquals(value.get("default").getAsString(), _default);
Assert.assertTrue(jsonObject.has("simpleDtos"));
JsonArray simpleDtos = jsonObject.get("simpleDtos").getAsJsonArray();
JsonObject simpleDtoJsonObject = simpleDtos.get(0).getAsJsonObject();
Assert.assertEquals(simpleDtoJsonObject.get("name").getAsString(), fooString);
Assert.assertEquals(simpleDtoJsonObject.get("id").getAsInt(), fooId);
Assert.assertEquals(simpleDtoJsonObject.get("default").getAsString(), _default);
Assert.assertTrue(jsonObject.has("arrayOfArrayOfEnum"));
JsonArray arrayOfArrayOfEnum = jsonObject.get("arrayOfArrayOfEnum").getAsJsonArray().get(0).getAsJsonArray();
Assert.assertEquals(arrayOfArrayOfEnum.get(0).getAsString(), ComplicatedDto.SimpleEnum.ONE.name());
Assert.assertEquals(arrayOfArrayOfEnum.get(1).getAsString(), ComplicatedDto.SimpleEnum.TWO.name());
Assert.assertEquals(arrayOfArrayOfEnum.get(2).getAsString(), ComplicatedDto.SimpleEnum.THREE.name());
}
@Test
public void testComplicatedDtoDeserializer() throws Exception {
final String fooString = "Something";
final int fooId = 1;
final String _default = "test_default_keyword";
JsonArray jsonArray = new JsonArray();
jsonArray.add(new JsonPrimitive(fooString));
JsonObject simpleDtoJsonObject = new JsonObject();
simpleDtoJsonObject.add("name", new JsonPrimitive(fooString));
simpleDtoJsonObject.add("id", new JsonPrimitive(fooId));
simpleDtoJsonObject.add("default", new JsonPrimitive(_default));
JsonObject jsonMap = new JsonObject();
jsonMap.add(fooString, simpleDtoJsonObject);
JsonArray simpleDtosArray = new JsonArray();
simpleDtosArray.add(simpleDtoJsonObject);
JsonArray arrayOfEnum = new JsonArray();
arrayOfEnum.add(new JsonPrimitive(ComplicatedDto.SimpleEnum.ONE.name()));
arrayOfEnum.add(new JsonPrimitive(ComplicatedDto.SimpleEnum.TWO.name()));
arrayOfEnum.add(new JsonPrimitive(ComplicatedDto.SimpleEnum.THREE.name()));
JsonArray arrayOfArrayEnum = new JsonArray();
arrayOfArrayEnum.add(arrayOfEnum);
JsonObject complicatedDtoJsonObject = new JsonObject();
complicatedDtoJsonObject.add("strings", jsonArray);
complicatedDtoJsonObject.add("simpleEnum", new JsonPrimitive(ComplicatedDto.SimpleEnum.ONE.name()));
complicatedDtoJsonObject.add("map", jsonMap);
complicatedDtoJsonObject.add("simpleDtos", simpleDtosArray);
complicatedDtoJsonObject.add("arrayOfArrayOfEnum", arrayOfArrayEnum);
ComplicatedDto complicatedDto =
dtoFactory.createDtoFromJson(complicatedDtoJsonObject.toString(), ComplicatedDto.class);
Assert.assertEquals(complicatedDto.getStrings().get(0), fooString);
Assert.assertEquals(complicatedDto.getSimpleEnum(), ComplicatedDto.SimpleEnum.ONE);
checkSimpleDto(complicatedDto.getMap().get(fooString), fooString, fooId, _default);
checkSimpleDto(complicatedDto.getSimpleDtos().get(0), fooString, fooId, _default);
Assert.assertEquals(complicatedDto.getArrayOfArrayOfEnum().get(0).get(0), ComplicatedDto.SimpleEnum.ONE);
Assert.assertEquals(complicatedDto.getArrayOfArrayOfEnum().get(0).get(1), ComplicatedDto.SimpleEnum.TWO);
Assert.assertEquals(complicatedDto.getArrayOfArrayOfEnum().get(0).get(2), ComplicatedDto.SimpleEnum.THREE);
}
private void checkSimpleDto(SimpleDto dto, String expectedName, int expectedId, String expectedDefault) {
Assert.assertEquals(dto.getName(), expectedName);
Assert.assertEquals(dto.getId(), expectedId);
Assert.assertEquals(dto.getDefault(), expectedDefault);
}
@Test
public void testDelegate() {
Assert.assertEquals(DtoFactory.getInstance().createDto(DtoWithDelegate.class).withName("TEST").nameWithPrefix("### "), "### TEST");
}
}