Search in sources :

Example 31 with JsonArray

use of com.google.gson.JsonArray 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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) ComplicatedDto(org.eclipse.che.dto.definitions.ComplicatedDto) JsonArray(com.google.gson.JsonArray) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) SimpleDto(org.eclipse.che.dto.definitions.SimpleDto) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 32 with JsonArray

use of com.google.gson.JsonArray 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);
}
Also used : JsonArray(com.google.gson.JsonArray) DtoWithAny(org.eclipse.che.dto.definitions.DtoWithAny) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 33 with JsonArray

use of com.google.gson.JsonArray in project che by eclipse.

the class ServerDtoTest method testListSimpleDtoDeserializer.

@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);
    assertEquals(listDtoFromJson.get(0).getName(), fooString_1);
    assertEquals(listDtoFromJson.get(0).getId(), fooId_1);
    assertEquals(listDtoFromJson.get(0).getDefault(), _default_1);
    assertEquals(listDtoFromJson.get(1).getName(), fooString_2);
    assertEquals(listDtoFromJson.get(1).getId(), fooId_2);
    assertEquals(listDtoFromJson.get(1).getDefault(), _default_2);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) SimpleDto(org.eclipse.che.dto.definitions.SimpleDto) Test(org.testng.annotations.Test)

Example 34 with JsonArray

use of com.google.gson.JsonArray in project zeppelin by apache.

the class InterpreterRestApiTest method testCreatedInterpreterDependencies.

@Test
public void testCreatedInterpreterDependencies() throws IOException {
    // when: Create 2 interpreter settings `md1` and `md2` which have different dep.
    String md1Name = "md1";
    String md2Name = "md2";
    String md1Dep = "org.apache.drill.exec:drill-jdbc:jar:1.7.0";
    String md2Dep = "org.apache.drill.exec:drill-jdbc:jar:1.6.0";
    String reqBody1 = "{\"name\":\"" + md1Name + "\",\"group\":\"md\",\"properties\":{\"propname\":\"propvalue\"}," + "\"interpreterGroup\":[{\"class\":\"org.apache.zeppelin.markdown.Markdown\",\"name\":\"md\"}]," + "\"dependencies\":[ {\n" + "      \"groupArtifactVersion\": \"" + md1Dep + "\",\n" + "      \"exclusions\":[]\n" + "    }]," + "\"option\": { \"remote\": true, \"session\": false }}";
    PostMethod post = httpPost("/interpreter/setting", reqBody1);
    assertThat("test create method:", post, isAllowed());
    post.releaseConnection();
    String reqBody2 = "{\"name\":\"" + md2Name + "\",\"group\":\"md\",\"properties\":{\"propname\":\"propvalue\"}," + "\"interpreterGroup\":[{\"class\":\"org.apache.zeppelin.markdown.Markdown\",\"name\":\"md\"}]," + "\"dependencies\":[ {\n" + "      \"groupArtifactVersion\": \"" + md2Dep + "\",\n" + "      \"exclusions\":[]\n" + "    }]," + "\"option\": { \"remote\": true, \"session\": false }}";
    post = httpPost("/interpreter/setting", reqBody2);
    assertThat("test create method:", post, isAllowed());
    post.releaseConnection();
    // 1. Call settings API
    GetMethod get = httpGet("/interpreter/setting");
    String rawResponse = get.getResponseBodyAsString();
    get.releaseConnection();
    // 2. Parsing to List<InterpreterSettings>
    JsonObject responseJson = gson.fromJson(rawResponse, JsonElement.class).getAsJsonObject();
    JsonArray bodyArr = responseJson.getAsJsonArray("body");
    List<InterpreterSetting> settings = new Gson().fromJson(bodyArr, new TypeToken<ArrayList<InterpreterSetting>>() {
    }.getType());
    // 3. Filter interpreters out we have just created
    InterpreterSetting md1 = null;
    InterpreterSetting md2 = null;
    for (InterpreterSetting setting : settings) {
        if (md1Name.equals(setting.getName())) {
            md1 = setting;
        } else if (md2Name.equals(setting.getName())) {
            md2 = setting;
        }
    }
    // then: should get created interpreters which have different dependencies
    // 4. Validate each md interpreter has its own dependencies
    assertEquals(1, md1.getDependencies().size());
    assertEquals(1, md2.getDependencies().size());
    assertEquals(md1Dep, md1.getDependencies().get(0).getGroupArtifactVersion());
    assertEquals(md2Dep, md2.getDependencies().get(0).getGroupArtifactVersion());
}
Also used : JsonArray(com.google.gson.JsonArray) PostMethod(org.apache.commons.httpclient.methods.PostMethod) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 35 with JsonArray

use of com.google.gson.JsonArray in project weixin-java-tools by chanjarster.

the class WxCpServiceImpl method getCallbackIp.

@Override
public String[] getCallbackIp() throws WxErrorException {
    String url = "https://qyapi.weixin.qq.com/cgi-bin/getcallbackip";
    String responseContent = get(url, null);
    JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
    JsonArray jsonArray = tmpJsonElement.getAsJsonObject().get("ip_list").getAsJsonArray();
    String[] ips = new String[jsonArray.size()];
    for (int i = 0; i < jsonArray.size(); i++) {
        ips[i] = jsonArray.get(i).getAsString();
    }
    return ips;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) StringReader(java.io.StringReader) JsonReader(com.google.gson.stream.JsonReader)

Aggregations

JsonArray (com.google.gson.JsonArray)424 JsonObject (com.google.gson.JsonObject)290 JsonElement (com.google.gson.JsonElement)167 JsonPrimitive (com.google.gson.JsonPrimitive)118 Test (org.testng.annotations.Test)59 JsonParser (com.google.gson.JsonParser)57 ArrayList (java.util.ArrayList)50 HashMap (java.util.HashMap)34 Map (java.util.Map)34 Gson (com.google.gson.Gson)22 IOException (java.io.IOException)17 Test (org.junit.Test)16 Type (java.lang.reflect.Type)12 List (java.util.List)11 Matchers.anyString (org.mockito.Matchers.anyString)10 TextView (android.widget.TextView)9 Point (android.graphics.Point)8 JsonParseException (com.google.gson.JsonParseException)7 GsonUtilities.jboolean (com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean)7 KcaUtils.getStringFromException (com.antest1.kcanotify.KcaUtils.getStringFromException)6