@Override protected boolean matchesSafely(JsonObject actual) { if (actual == null) return false; for (Map.Entry<String, JsonElement> field : this.expected.entrySet()) { if (!actual.has(field.getKey())) return false; if (field.getValue().isJsonObject()) { JsonObjectMatcher inner = new JsonObjectMatcher(field.getValue().getAsJsonObject()); JsonObject otherObj = actual.get(field.getKey()).getAsJsonObject(); if (!inner.matchesSafely(otherObj)) return false; } else if (field.getValue().isJsonArray()) { JsonArray thisArray = field.getValue().getAsJsonArray(); JsonArray otherArray = actual.get(field.getKey()).getAsJsonArray(); if (otherArray == null) return false; if (thisArray.size() != otherArray.size()) return false; for (int i = 0; i < thisArray.size(); i++) { JsonElement thisItem = thisArray.get(i); if (!equalToAnyOther(thisItem, otherArray)) return false; } } else if (!field.getValue().equals(actual.get(field.getKey()))) return false; } return true; }
private boolean equalToAnyOther(JsonElement thisItem, JsonArray otherArray) { for (int i = 0; i < otherArray.size(); i++) { JsonElement otherItem = otherArray.get(i); if (thisItem.isJsonObject()) { if (!otherItem.isJsonObject()) continue; if (new JsonObjectMatcher(thisItem.getAsJsonObject()).matchesSafely(otherItem.getAsJsonObject())) return true; } else if (thisItem.equals(otherItem)) return true; } return false; }
private void testTransform(String caseFile, String expectedFile) throws IOException { JsonObject expectedObject = (JsonObject) readJsonObject("parts/materials/" + expectedFile + ".json"); JsonObject jsonObject = parser.transform(readYamlObject("parts/materials/" + caseFile + ".yaml")); assertThat(jsonObject, is(new JsonObjectMatcher(expectedObject))); }
private void testTransform(String caseFile, String expectedFile) throws IOException { JsonObject expectedObject = (JsonObject) readJsonObject("parts/stages/" + expectedFile + ".json"); JsonObject jsonObject = parser.transform(readYamlObject("parts/stages/" + caseFile + ".yaml")); assertThat(jsonObject, is(new JsonObjectMatcher(expectedObject))); }
@Test public void shouldRespondSuccessToParseDirectoryRequestWhenSimpleCaseFile() throws UnhandledRequestTypeException, IOException { GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("simple")); assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE)); JsonObject responseJsonObject = getJsonObjectFromResponse(response); assertNoError(responseJsonObject); JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray(); assertThat(pipelines.size(), is(1)); JsonObject expected = (JsonObject) readJsonObject("examples.out/simple.gocd.json"); assertThat(responseJsonObject, is(new JsonObjectMatcher(expected))); }
private void testTransform(String caseFile, String expectedFile) throws IOException { JsonObject expectedObject = (JsonObject) readJsonObject("parts/" + expectedFile + ".json"); JsonObject jsonObject = parser.transform(readYamlObject("parts/" + caseFile + ".yaml")); assertThat(jsonObject, is(new JsonObjectMatcher(expectedObject))); }
private JsonObject testTransform(String caseFile, String expectedFile) throws IOException { JsonObject expectedObject = (JsonObject) readJsonObject("parts/tasks/" + expectedFile + ".json"); JsonObject jsonObject = parser.transform(readYamlObject("parts/tasks/" + caseFile + ".yaml")); assertThat(jsonObject, is(new JsonObjectMatcher(expectedObject))); return jsonObject; }
private JsonObject testTransform(String caseFile, String expectedFile) throws IOException { JsonObject expectedObject = (JsonObject) readJsonObject("parts/jobs/" + expectedFile + ".json"); JsonObject jsonObject = parser.transform(readYamlObject("parts/jobs/" + caseFile + ".yaml")); assertThat(jsonObject, is(new JsonObjectMatcher(expectedObject))); return jsonObject; }
private JsonObject testTransform(String caseFile, String expectedFile) throws IOException { JsonObject expectedObject = (JsonObject) readJsonObject("parts/environments/" + expectedFile + ".json"); JsonObject jsonObject = parser.transform(readYamlObject("parts/environments/" + caseFile + ".yaml")); assertThat(jsonObject, is(new JsonObjectMatcher(expectedObject))); return jsonObject; } }
@Test public void shouldRespondSuccessToParseDirectoryRequestWhenAliasesCaseFile() throws UnhandledRequestTypeException, IOException { GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("aliases")); assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE)); JsonObject responseJsonObject = getJsonObjectFromResponse(response); assertNoError(responseJsonObject); JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray(); assertThat(pipelines.size(), is(1)); JsonObject expected = (JsonObject) readJsonObject("examples.out/aliases.gocd.json"); assertThat(responseJsonObject, is(new JsonObjectMatcher(expected))); }
@Test public void shouldRespondSuccessToParseDirectoryRequestWhenRichCaseFile() throws UnhandledRequestTypeException, IOException { GoPluginApiResponse response = parseAndGetResponseForDir(setupCase("rich")); assertThat(response.responseCode(), is(SUCCESS_RESPONSE_CODE)); JsonObject responseJsonObject = getJsonObjectFromResponse(response); assertNoError(responseJsonObject); JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray(); assertThat(pipelines.size(), is(1)); JsonObject expected = (JsonObject) readJsonObject("examples.out/rich.gocd.json"); assertThat(responseJsonObject, is(new JsonObjectMatcher(expected))); }
@Test public void respondsToParseContentRequest() throws Exception { final Gson gson = new Gson(); DefaultGoPluginApiRequest request = new DefaultGoPluginApiRequest("configrepo", "2.0", ConfigRepoMessages.REQ_PARSE_CONTENT); StringWriter w = new StringWriter(); IOUtils.copy(getResourceAsStream("examples/simple.gocd.yaml"), w); request.setRequestBody(gson.toJson( Collections.singletonMap("contents", Collections.singletonMap("simple.gocd.yaml", w.toString()) ) )); GoPluginApiResponse response = plugin.handle(request); assertEquals(SUCCESS_RESPONSE_CODE, response.responseCode()); JsonObject responseJsonObject = getJsonObjectFromResponse(response); assertNoError(responseJsonObject); JsonArray pipelines = responseJsonObject.get("pipelines").getAsJsonArray(); assertThat(pipelines.size(), is(1)); JsonObject expected = (JsonObject) readJsonObject("examples.out/simple.gocd.json"); assertThat(responseJsonObject, is(new JsonObjectMatcher(expected))); }