### Question:
RenameFieldTransformator extends TransformatorProcess { public void apply(String idProcess, ParameterTransformation parameterTransformation, ObjectNode jsonValue) { if (has(parameterTransformation.getComposeField().getKey(), jsonValue)) { JsonNode valueField = at(parameterTransformation.getComposeField().getKey(), jsonValue); put(jsonValue, parameterTransformation.getComposeField().getValue(), valueField.deepCopy()); remove(jsonValue, parameterTransformation.getComposeField().getKey()); } } RenameFieldTransformator(); void apply(String idProcess, ParameterTransformation parameterTransformation, ObjectNode jsonValue);  }### Answer:
@Test public void should_Process_Ok() throws Exception { RenameFieldTransformator renameFieldValidator = new RenameFieldTransformator(); RawDataGen rd = RawDataGen.builder().messageSend("gni").project("projectvalue").type("type").build(); ObjectMapper obj = new ObjectMapper(); String value = obj.writeValueAsString(rd); ObjectNode jsonValue = JSONUtils.getInstance().parseObj(value); renameFieldValidator.apply(null, ParameterTransformation.builder() .composeField(ProcessKeyValue.builder() .key("project") .value("@project") .build() ).build(), jsonValue); assertThat(jsonValue.path("@project").asText()).isEqualTo("projectvalue"); }
@Test public void should_Process_Nested_Ok() throws Exception { RenameFieldTransformator renameFieldValidator = new RenameFieldTransformator(); RawDataGen rd = RawDataGen.builder().messageSend("gni").project("projectvalue").type("type").build(); ObjectMapper obj = new ObjectMapper(); String value = obj.writeValueAsString(rd); ObjectNode jsonValue = JSONUtils.getInstance().parseObj(value); renameFieldValidator.apply(null, ParameterTransformation.builder() .composeField(ProcessKeyValue.builder() .key("project") .value("my.project.name") .build() ).build(), jsonValue); assertThat(JSONUtils.getInstance().at(jsonValue, "my.project.name").asText()).isEqualTo("projectvalue"); }
@Test public void should_Process_Nested_SameObject_Ok() throws Exception { RenameFieldTransformator renameFieldValidator = new RenameFieldTransformator(); String value = "{\"something\":\"test\",\"comment\": {\"value\":\"value1\"}}"; ObjectNode jsonValue = JSONUtils.getInstance().parseObj(value); renameFieldValidator.apply(null, ParameterTransformation.builder() .composeField(ProcessKeyValue.builder() .key("comment.value") .value("comment") .build() ).build(), jsonValue); assertThat(JSONUtils.getInstance().at(jsonValue, "comment").asText()).isEqualTo("value1"); }
@Test public void should_Process_Ko() throws Exception { RenameFieldTransformator renameFieldValidator = new RenameFieldTransformator(); RawDataGen rd = RawDataGen.builder().messageSend("gni").project("project").type("type").build(); ObjectMapper obj = new ObjectMapper(); String value = obj.writeValueAsString(rd); ObjectNode jsonValue = JSONUtils.getInstance().parseObj(value); renameFieldValidator.apply(null, ParameterTransformation.builder() .composeField(ProcessKeyValue.builder() .key("project2") .value("@project") .build() ).build(), jsonValue); assertThat(jsonValue.get("@project")).isNull(); }
@Test public void should_Process_RenameEmpty() throws Exception { RenameFieldTransformator renameFieldValidator = new RenameFieldTransformator(); RawDataGen rd = RawDataGen.builder().messageSend("gni").project("").type("type").build(); ObjectMapper obj = new ObjectMapper(); String value = obj.writeValueAsString(rd); ObjectNode jsonValue = JSONUtils.getInstance().parseObj(value); renameFieldValidator.apply(null, ParameterTransformation.builder() .composeField(ProcessKeyValue.builder() .key("project") .value("@project") .build() ).build(), jsonValue); assertThat(jsonValue.get("@project").asText()).isEqualTo(""); }