Search in sources :

Example 6 with JsonParser

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

the class WorkspaceConfigJsonAdapterTest method testWorkspaceConfigAdaptationBasedOnDockerfileLocationSource.

@Test
public void testWorkspaceConfigAdaptationBasedOnDockerfileLocationSource() throws Exception {
    final String content = loadContent("ws_conf_machine_source_dockerfile_location.json");
    final JsonObject wsConfig = new JsonParser().parse(content).getAsJsonObject();
    configAdapter.adaptModifying(wsConfig);
    // The type of environments must be changed from array to map
    assertTrue(wsConfig.has("environments"), "contains environments object");
    assertTrue(wsConfig.get("environments").isJsonObject(), "environments is json object");
    // Environment must be moved out of the environment object
    final JsonObject environmentsObj = wsConfig.get("environments").getAsJsonObject();
    assertTrue(environmentsObj.has("dev-env"), "'dev-env' is present in environments list");
    assertTrue(environmentsObj.get("dev-env").isJsonObject(), "'dev-env' is json object");
    final JsonObject environmentObj = environmentsObj.get("dev-env").getAsJsonObject();
    // 'machineConfigs' -> 'machines'
    assertTrue(environmentObj.has("machines"), "'machines' are present in environment object");
    assertTrue(environmentObj.get("machines").isJsonObject(), "'machines' is json object");
    final JsonObject machinesObj = environmentObj.get("machines").getAsJsonObject();
    assertEquals(machinesObj.entrySet().size(), 1, "machines size");
    // check 'dev' machine
    assertTrue(machinesObj.has("dev"), "'machines' contains machine with name 'dev-machine'");
    assertTrue(machinesObj.get("dev").isJsonObject(), "dev machine is json object");
    final JsonObject devMachineObj = machinesObj.get("dev").getAsJsonObject();
    assertTrue(devMachineObj.has("servers"), "dev machine contains servers field");
    assertTrue(devMachineObj.get("servers").isJsonObject(), "dev machine servers is json object");
    final JsonObject devMachineServersObj = devMachineObj.get("servers").getAsJsonObject();
    assertTrue(devMachineServersObj.has("ref"), "contains servers with reference 'ref'");
    assertTrue(devMachineServersObj.get("ref").isJsonObject(), "server is json object");
    final JsonObject devMachineServerObj = devMachineServersObj.get("ref").getAsJsonObject();
    assertEquals(devMachineServerObj.get("port").getAsString(), "9090/udp");
    assertEquals(devMachineServerObj.get("protocol").getAsString(), "protocol");
    assertTrue(devMachineObj.has("agents"), "dev machine has agents");
    assertTrue(devMachineObj.has("attributes"), "dev machine has attributes");
    assertTrue(devMachineObj.get("attributes").isJsonObject(), "dev machine attributes is json object");
    final JsonObject attributes = devMachineObj.getAsJsonObject("attributes");
    assertTrue(attributes.has("memoryLimitBytes"), "has memory limit");
    assertEquals(attributes.get("memoryLimitBytes").getAsString(), "2147483648");
    // check environment recipe
    assertTrue(environmentObj.has("recipe"), "environment contains recipe");
    assertTrue(environmentObj.get("recipe").isJsonObject(), "environment recipe is json object");
    final JsonObject recipeObj = environmentObj.get("recipe").getAsJsonObject();
    assertEquals(recipeObj.get("type").getAsString(), "dockerfile");
    assertEquals(recipeObj.get("contentType").getAsString(), "text/x-dockerfile");
    assertEquals(recipeObj.get("location").getAsString(), "https://somewhere/Dockerfile");
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 7 with JsonParser

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

the class WorkspaceConfigJsonAdapterTest method testNotValidWorkspaceConfigAdaptations.

@Test(expectedExceptions = IllegalArgumentException.class, dataProvider = "invalidConfigs")
public void testNotValidWorkspaceConfigAdaptations(String filename) throws Exception {
    final String content = loadContent(INVALID_CONFIGS_DIR_NAME + File.separatorChar + filename);
    new WorkspaceConfigJsonAdapter().adaptModifying(new JsonParser().parse(content).getAsJsonObject());
}
Also used : JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 8 with JsonParser

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

the class WorkspaceConfigJsonAdapterTest method testAdaptionOfWorkspaceConfigWithSourceBasedOnDockerImage.

@Test(dependsOnMethods = "testWorkspaceConfigAdaptationBasedOnDockerfileLocationSource")
public void testAdaptionOfWorkspaceConfigWithSourceBasedOnDockerImage() throws Exception {
    final String content = loadContent("ws_conf_machine_source_dockerimage.json");
    final JsonObject wsConfig = new JsonParser().parse(content).getAsJsonObject();
    configAdapter.adaptModifying(wsConfig);
    // check environment recipe
    final JsonObject recipeObj = wsConfig.getAsJsonObject("environments").getAsJsonObject("dev-env").getAsJsonObject("recipe");
    assertEquals(recipeObj.get("type").getAsString(), "dockerimage");
    assertEquals(recipeObj.get("location").getAsString(), "codenvy/ubuntu_jdk8");
}
Also used : JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 9 with JsonParser

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

the class StackMessageBodyAdapter method adapt.

@Override
protected String adapt(String body) {
    if (!isOldFormat(body)) {
        return body;
    }
    final JsonElement stackEl = new JsonParser().parse(body);
    if (!stackEl.isJsonObject()) {
        return body;
    }
    stackJsonAdapter.adaptModifying(stackEl.getAsJsonObject());
    return stackEl.toString();
}
Also used : JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Example 10 with JsonParser

use of com.google.gson.JsonParser in project sonarqube by SonarSource.

the class ScannerWsClient method tryParseAsJsonError.

public static String tryParseAsJsonError(String responseContent) {
    try {
        JsonParser parser = new JsonParser();
        JsonObject obj = parser.parse(responseContent).getAsJsonObject();
        JsonArray errors = obj.getAsJsonArray("errors");
        List<String> errorMessages = new ArrayList<>();
        for (JsonElement e : errors) {
            errorMessages.add(e.getAsJsonObject().get("msg").getAsString());
        }
        return Joiner.on(", ").join(errorMessages);
    } catch (Exception e) {
        return responseContent;
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) HttpException(org.sonarqube.ws.client.HttpException) MessageException(org.sonar.api.utils.MessageException) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonParser (com.google.gson.JsonParser)323 JsonObject (com.google.gson.JsonObject)263 JsonElement (com.google.gson.JsonElement)88 JsonArray (com.google.gson.JsonArray)49 IOException (java.io.IOException)39 Gson (com.google.gson.Gson)31 InputStreamReader (java.io.InputStreamReader)24 HashMap (java.util.HashMap)20 Map (java.util.Map)18 ArrayList (java.util.ArrayList)16 JsonReader (com.google.gson.stream.JsonReader)11 Test (org.junit.Test)11 AssetManager (android.content.res.AssetManager)10 InputStream (java.io.InputStream)10 Type (java.lang.reflect.Type)10 URL (java.net.URL)9 Test (org.testng.annotations.Test)9 UserType (com.glitchcog.fontificator.bot.UserType)8 EmojiType (com.glitchcog.fontificator.emoji.EmojiType)8 JsonParseException (com.google.gson.JsonParseException)8