Search in sources :

Example 11 with JsonRecordSetWriter

use of org.apache.nifi.json.JsonRecordSetWriter in project nifi by apache.

the class TestUpdateRecord method testUpdateInNullArray.

@Test
public void testUpdateInNullArray() throws InitializationException, IOException {
    final JsonTreeReader jsonReader = new JsonTreeReader();
    runner.addControllerService("reader", jsonReader);
    runner.setValidateExpressionUsage(false);
    final String inputSchemaText = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/schema/person-with-address.avsc")));
    final String outputSchemaText = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/schema/person-with-address.avsc")));
    runner.setProperty(jsonReader, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
    runner.setProperty(jsonReader, SchemaAccessUtils.SCHEMA_TEXT, inputSchemaText);
    runner.enableControllerService(jsonReader);
    final JsonRecordSetWriter jsonWriter = new JsonRecordSetWriter();
    runner.addControllerService("writer", jsonWriter);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_TEXT, outputSchemaText);
    runner.setProperty(jsonWriter, "Pretty Print JSON", "true");
    runner.setProperty(jsonWriter, "Schema Write Strategy", "full-schema-attribute");
    runner.enableControllerService(jsonWriter);
    runner.enqueue(Paths.get("src/test/resources/TestUpdateRecord/input/person-with-null-array.json"));
    runner.setProperty("/address[*]/city", "newCity");
    runner.setProperty(UpdateRecord.REPLACEMENT_VALUE_STRATEGY, UpdateRecord.LITERAL_VALUES);
    runner.run();
    runner.assertAllFlowFilesTransferred(UpdateRecord.REL_SUCCESS, 1);
    final String expectedOutput = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/output/person-with-null-array.json")));
    runner.getFlowFilesForRelationship(UpdateRecord.REL_SUCCESS).get(0).assertContentEquals(expectedOutput);
}
Also used : JsonTreeReader(org.apache.nifi.json.JsonTreeReader) JsonRecordSetWriter(org.apache.nifi.json.JsonRecordSetWriter) Test(org.junit.Test)

Example 12 with JsonRecordSetWriter

use of org.apache.nifi.json.JsonRecordSetWriter in project nifi by apache.

the class TestUpdateRecord method testSetRootPathRelativeWithMultipleValues.

@Test
public void testSetRootPathRelativeWithMultipleValues() throws InitializationException, IOException {
    final JsonTreeReader jsonReader = new JsonTreeReader();
    runner.addControllerService("reader", jsonReader);
    final String inputSchemaText = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/schema/person-with-name-record.avsc")));
    final String outputSchemaText = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/schema/name-fields-only.avsc")));
    runner.setProperty(jsonReader, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
    runner.setProperty(jsonReader, SchemaAccessUtils.SCHEMA_TEXT, inputSchemaText);
    runner.enableControllerService(jsonReader);
    final JsonRecordSetWriter jsonWriter = new JsonRecordSetWriter();
    runner.addControllerService("writer", jsonWriter);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_TEXT, outputSchemaText);
    runner.setProperty(jsonWriter, "Pretty Print JSON", "true");
    runner.setProperty(jsonWriter, "Schema Write Strategy", "full-schema-attribute");
    runner.enableControllerService(jsonWriter);
    runner.enqueue(Paths.get("src/test/resources/TestUpdateRecord/input/person.json"));
    runner.setProperty("/name/..", "/name/*");
    runner.setProperty(UpdateRecord.REPLACEMENT_VALUE_STRATEGY, UpdateRecord.RECORD_PATH_VALUES);
    runner.run();
    runner.assertAllFlowFilesTransferred(UpdateRecord.REL_SUCCESS, 1);
    final String expectedOutput = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/output/name-fields-only.json")));
    runner.getFlowFilesForRelationship(UpdateRecord.REL_SUCCESS).get(0).assertContentEquals(expectedOutput);
}
Also used : JsonTreeReader(org.apache.nifi.json.JsonTreeReader) JsonRecordSetWriter(org.apache.nifi.json.JsonRecordSetWriter) Test(org.junit.Test)

Example 13 with JsonRecordSetWriter

use of org.apache.nifi.json.JsonRecordSetWriter in project nifi by apache.

the class TestUpdateRecord method testFieldValuesInEL.

@Test
public void testFieldValuesInEL() throws InitializationException, IOException {
    final JsonTreeReader jsonReader = new JsonTreeReader();
    runner.addControllerService("reader", jsonReader);
    final String inputSchemaText = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/schema/person-with-name-record.avsc")));
    final String outputSchemaText = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/schema/person-with-name-record.avsc")));
    runner.setProperty(jsonReader, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
    runner.setProperty(jsonReader, SchemaAccessUtils.SCHEMA_TEXT, inputSchemaText);
    runner.enableControllerService(jsonReader);
    final JsonRecordSetWriter jsonWriter = new JsonRecordSetWriter();
    runner.addControllerService("writer", jsonWriter);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_TEXT, outputSchemaText);
    runner.setProperty(jsonWriter, "Pretty Print JSON", "true");
    runner.setProperty(jsonWriter, "Schema Write Strategy", "full-schema-attribute");
    runner.enableControllerService(jsonWriter);
    runner.enqueue(Paths.get("src/test/resources/TestUpdateRecord/input/person.json"));
    runner.setProperty("/name/last", "${field.value:toUpper()}");
    runner.setProperty(UpdateRecord.REPLACEMENT_VALUE_STRATEGY, UpdateRecord.LITERAL_VALUES);
    runner.run();
    runner.assertAllFlowFilesTransferred(UpdateRecord.REL_SUCCESS, 1);
    final String expectedOutput = new String(Files.readAllBytes(Paths.get("src/test/resources/TestUpdateRecord/output/person-with-capital-lastname.json")));
    runner.getFlowFilesForRelationship(UpdateRecord.REL_SUCCESS).get(0).assertContentEquals(expectedOutput);
}
Also used : JsonTreeReader(org.apache.nifi.json.JsonTreeReader) JsonRecordSetWriter(org.apache.nifi.json.JsonRecordSetWriter) Test(org.junit.Test)

Aggregations

JsonRecordSetWriter (org.apache.nifi.json.JsonRecordSetWriter)13 Test (org.junit.Test)13 JsonTreeReader (org.apache.nifi.json.JsonTreeReader)12 JsonReader (com.google.gson.stream.JsonReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 TestRunner (org.apache.nifi.util.TestRunner)1