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");
}
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());
}
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");
}
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();
}
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;
}
}
Aggregations