JsonMerger { public static JsonNode merge(JsonNode source, JsonNode replacement) throws IOException { return new MergeStrategy().execute(source, replacement); }  static JsonNode merge(JsonNode source, JsonNode replacement); static JsonNode merge(JsonNode source, JsonNode replacement, MergeStrategy mergeStrategy); static MergeStrategy mergeStrategy();  }
@Test public void mergeDefinitions_PreservesFieldsNotDefinedInReplacement() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"foo\": \"bar\", \"fields\": \"[{\\\"name\\\":\\\"sourceField\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"sourceFieldFormatMap\\\"}}\"}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[]\", \"fieldFormatMap\": \"{}\"}"); JsonNode resultDefinitions = JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy); assertThat(resultDefinitions.get("foo").textValue()).isEqualTo("bar"); assertThat(mapper.readTree(resultDefinitions.get("fields").textValue())) .isEqualTo(mapper.readTree(sourceDefinitions.get("fields").textValue())); assertThat(mapper.readTree(resultDefinitions.get("fieldFormatMap").textValue())) .isEqualTo(mapper.readTree(sourceDefinitions.get("fieldFormatMap").textValue())); }
@Test public void mergeDefinitions_AddsFieldsDefinedInReplacement() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"sourceField\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"sourceFieldFormatMap\\\"}}\"}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"stagemonitorField\\\"}]\", \"fieldFormatMap\": \"{\\\"stagemonitor\\\": {\\\"name\\\": \\\"stagemonitorFieldFormatMap\\\"}}\"}"); JsonNode resultDefinitions = JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy); JsonNode resultFields = mapper.readTree(resultDefinitions.get("fields").textValue()); assertThat(resultFields).containsExactlyInAnyOrder( mapper.createObjectNode().put("name", "sourceField"), mapper.createObjectNode().put("name", "stagemonitorField")); JsonNode resultFieldFormat = mapper.readTree(resultDefinitions.get("fieldFormatMap").textValue()); assertThat(resultFieldFormat).hasSize(2); assertThat(resultFieldFormat.get("foo")).isEqualTo(mapper.createObjectNode().put("name", "sourceFieldFormatMap")); assertThat(resultFieldFormat.get("stagemonitor")).isEqualTo(mapper.createObjectNode().put("name", "stagemonitorFieldFormatMap")); }
@Test public void mergeDefinitions_UpdatesFieldsDefinedInReplacementAndSource() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"fooField\\\", \\\"value\\\": \\\"old\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooOld\\\"}}\"}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"fooField\\\", \\\"value\\\": \\\"new\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooNew\\\"}}\"}"); JsonNode resultDefinitions = JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy); JsonNode resultFields = mapper.readTree(resultDefinitions.get("fields").textValue()); assertThat(resultFields).containsExactlyInAnyOrder( mapper.createObjectNode().put("name", "fooField").put("value", "new")); JsonNode resultFieldFormat = mapper.readTree(resultDefinitions.get("fieldFormatMap").textValue()); assertThat(resultFieldFormat).hasSize(1); assertThat(resultFieldFormat.get("foo")).isEqualTo(mapper.createObjectNode().put("name", "fooNew")); }
@Test public void mergeDefinitions_ThrowsIllegalArgumentExceptionOnInvalidFieldsDefinition() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"fields\": false, \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooOld\\\"}}\"}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"fooField\\\", \\\"value\\\": \\\"new\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooNew\\\"}}\"}"); assertThatIllegalArgumentException().isThrownBy(() -> JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy)) .withMessage("error before decoding fields. Expected textual or null field, but encountered BOOLEAN"); }
@Test public void mergeDefinitions_ThrowsIllegalArgumentExceptionOnInvalidFieldsType() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"fields\": \"{}\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooOld\\\"}}\"}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"fooField\\\", \\\"value\\\": \\\"new\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooNew\\\"}}\"}"); assertThatIllegalArgumentException().isThrownBy(() -> JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy)) .withMessage("error after decodingfields. Expected decoded type to be ARRAY, but encountered OBJECT"); }
@Test public void mergeDefinitions_ThrowsIllegalArgumentExceptionOnInvalidFieldsJson() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"fields\": \"invalid\", \"fieldFormatMap\": \"{}\"}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"fooField\\\", \\\"value\\\": \\\"new\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooNew\\\"}}\"}"); assertThatExceptionOfType(JsonParseException.class).isThrownBy(() -> JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy)) .withMessageContaining("Unrecognized token 'invalid': was expecting ('true', 'false' or 'null')"); }
@Test public void mergeDefinitions_ThrowsIllegalArgumentExceptionOnInvalidFieldFormatMapDefinition() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"fields\": \"[]\", \"fieldFormatMap\": false}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"fooField\\\", \\\"value\\\": \\\"new\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooNew\\\"}}\"}"); assertThatIllegalArgumentException().isThrownBy(() -> JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy)) .withMessage("error before decoding fieldFormatMap. Expected textual or null field, but encountered BOOLEAN"); }
@Test public void mergeDefinitions_ThrowsIllegalArgumentExceptionOnInvalidFieldFormatMapType() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"fields\": \"[]\", \"fieldFormatMap\": \"[]\"}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"fooField\\\", \\\"value\\\": \\\"new\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooNew\\\"}}\"}"); assertThatIllegalArgumentException().isThrownBy(() -> JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy)) .withMessage("error after decodingfieldFormatMap. Expected decoded type to be OBJECT, but encountered ARRAY"); }
@Test public void mergeDefinitions_ThrowsIllegalArgumentExceptionOnInvalidFieldFormatJson() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{\"fields\": \"[]\", \"fieldFormatMap\": \"invalid\"}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"fooField\\\", \\\"value\\\": \\\"new\\\"}]\", \"fieldFormatMap\": \"{\\\"foo\\\": {\\\"name\\\": \\\"fooNew\\\"}}\"}"); assertThatExceptionOfType(JsonParseException.class).isThrownBy(() -> JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy)) .withMessageContaining("Unrecognized token 'invalid': was expecting ('true', 'false' or 'null')"); }
@Test public void mergeDefinitions_AddsFieldsDefinedInReplacement_IfNoDefinitionIsPresent() throws Exception { JsonNode sourceDefinitions = mapper.readTree("{}"); JsonNode stagemonitorDefinitions = mapper.readTree("{\"fields\": \"[{\\\"name\\\": \\\"stagemonitorField\\\"}]\", \"fieldFormatMap\": \"{\\\"stagemonitor\\\": {\\\"name\\\": \\\"stagemonitorFieldFormatMap\\\"}}\"}"); JsonNode resultDefinitions = JsonMerger.merge(sourceDefinitions, stagemonitorDefinitions, mergeStrategy); JsonNode resultFields = mapper.readTree(resultDefinitions.get("fields").textValue()); assertThat(resultFields).containsExactlyInAnyOrder( mapper.createObjectNode().put("name", "stagemonitorField")); JsonNode resultFieldFormat = mapper.readTree(resultDefinitions.get("fieldFormatMap").textValue()); assertThat(resultFieldFormat).hasSize(1); assertThat(resultFieldFormat.get("stagemonitor")).isEqualTo(mapper.createObjectNode().put("name", "stagemonitorFieldFormatMap")); }