use of com.google.gson.JsonParser in project che by eclipse.
the class ServerDtoTest method testSerializerWithAny.
@Test
public void testSerializerWithAny() throws Exception {
final List<Object> objects = createListTestValueForAny();
DtoWithAny dto = dtoFactory.createDto(DtoWithAny.class).withStuff(createTestValueForAny()).withObjects(objects);
final String json = dtoFactory.toJson(dto);
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
Assert.assertEquals(jsonObject.get("stuff"), createTestValueForAny());
Assert.assertTrue(jsonObject.has("objects"));
JsonArray jsonArray = jsonObject.get("objects").getAsJsonArray();
assertEquals(jsonArray.size(), objects.size());
for (int i = 0; i < jsonArray.size(); i++) {
assertEquals(jsonArray.get(i), objects.get(i));
}
}
use of com.google.gson.JsonParser in project che by eclipse.
the class ServerDtoTest method testComplicatedDtoSerializer.
@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();
assertEquals(jsonArray.get(0).getAsString(), listStrings.get(0));
assertEquals(jsonArray.get(1).getAsString(), listStrings.get(1));
Assert.assertTrue(jsonObject.has("simpleEnum"));
assertEquals(jsonObject.get("simpleEnum").getAsString(), simpleEnum.name());
Assert.assertTrue(jsonObject.has("map"));
JsonObject jsonMap = jsonObject.get("map").getAsJsonObject();
JsonObject value = jsonMap.get(fooString).getAsJsonObject();
assertEquals(value.get("name").getAsString(), fooString);
assertEquals(value.get("id").getAsInt(), fooId);
assertEquals(value.get("default").getAsString(), _default);
Assert.assertTrue(jsonObject.has("simpleDtos"));
JsonArray simpleDtos = jsonObject.get("simpleDtos").getAsJsonArray();
JsonObject simpleDtoJsonObject = simpleDtos.get(0).getAsJsonObject();
assertEquals(simpleDtoJsonObject.get("name").getAsString(), fooString);
assertEquals(simpleDtoJsonObject.get("id").getAsInt(), fooId);
assertEquals(simpleDtoJsonObject.get("default").getAsString(), _default);
Assert.assertTrue(jsonObject.has("arrayOfArrayOfEnum"));
JsonArray arrayOfArrayOfEnum = jsonObject.get("arrayOfArrayOfEnum").getAsJsonArray().get(0).getAsJsonArray();
assertEquals(arrayOfArrayOfEnum.get(0).getAsString(), ComplicatedDto.SimpleEnum.ONE.name());
assertEquals(arrayOfArrayOfEnum.get(1).getAsString(), ComplicatedDto.SimpleEnum.TWO.name());
assertEquals(arrayOfArrayOfEnum.get(2).getAsString(), ComplicatedDto.SimpleEnum.THREE.name());
}
use of com.google.gson.JsonParser in project che by eclipse.
the class ServerDtoTest method testSerializerWithFieldNames.
@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();
assertEquals(jsonObject.get(DtoWithFieldNames.THENAME_FIELD).getAsString(), fooString);
assertEquals(jsonObject.get(DtoWithFieldNames.THEDEFAULT_FIELD).getAsString(), _default);
}
use of com.google.gson.JsonParser in project che by eclipse.
the class ServerDtoTest method testCloneWithNullAny.
@Test
public void testCloneWithNullAny() throws Exception {
DtoWithAny dto1 = dtoFactory.createDto(DtoWithAny.class);
DtoWithAny dto2 = dtoFactory.clone(dto1);
Assert.assertEquals(dto1, dto2);
JsonElement json = new JsonParser().parse(dtoFactory.toJson(dto1));
JsonObject expJson = new JsonObject();
expJson.addProperty("id", 0);
expJson.add("objects", new JsonArray());
assertEquals(expJson, json);
}
use of com.google.gson.JsonParser in project che by eclipse.
the class NodeJsBreakpointsParser method parse.
@Override
public Breakpoints parse(NodeJsOutput nodeJsOutput) throws NodeJsDebuggerParseException {
final List<Breakpoint> breakpoints = new ArrayList<>();
JsonObject json = new JsonParser().parse(nodeJsOutput.getOutput()).getAsJsonObject();
if (json.has("breakpoints")) {
Iterator<JsonElement> iter = json.getAsJsonArray("breakpoints").iterator();
while (iter.hasNext()) {
JsonObject item = iter.next().getAsJsonObject();
try {
final String condition = item.has("condition") && !item.get("condition").isJsonNull() ? item.get("condition").getAsString() : null;
final boolean isEnabled = item.has("active") && !item.get("active").isJsonNull() && item.get("active").getAsBoolean();
final int lineNumber = item.get("line").getAsInt();
final String target;
String targetType = item.get("type").getAsString();
switch(targetType) {
case "scriptId":
target = String.valueOf(item.get("script_id").getAsInt());
break;
case "scriptRegExp":
target = item.get("script_regexp").getAsString();
break;
default:
throw new IllegalArgumentException("Unsupported 'type' value: " + targetType);
}
Location location = new LocationImpl(targetType + ":" + target, lineNumber + 1);
Breakpoint breakpoint = new BreakpointImpl(location, isEnabled, condition);
breakpoints.add(breakpoint);
} catch (Exception e) {
LOG.error("Failed to parse breakpoint: " + item.toString(), e);
}
}
}
return new Breakpoints(breakpoints);
}
Aggregations