Java Code Examples for org.apache.nifi.processor.io.OutputStreamCallback

The following examples show how to use org.apache.nifi.processor.io.OutputStreamCallback. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: localization_nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FlowFile append(final FlowFile flowFile, final OutputStreamCallback callback) {
    validateState(flowFile);
    if (callback == null || flowFile == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }
    final MockFlowFile mock = (MockFlowFile) flowFile;

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        baos.write(mock.getData());
        callback.process(baos);
    } catch (final IOException e) {
        throw new ProcessException(e.toString(), e);
    }

    final MockFlowFile newFlowFile = new MockFlowFile(mock.getId(), flowFile);
    currentVersions.put(newFlowFile.getId(), newFlowFile);

    newFlowFile.setData(baos.toByteArray());
    return newFlowFile;
}
 
Example 2
Source Project: localization_nifi   Source File: SpringContextProcessor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 *
 */
private void receiveFromSpring(ProcessSession processSession) {
    final SpringResponse<?> msgFromSpring = this.exchanger.receive(this.receiveTimeout);
    if (msgFromSpring != null) {
        FlowFile flowFileToProcess = processSession.create();
        flowFileToProcess = processSession.write(flowFileToProcess, new OutputStreamCallback() {
            @Override
            public void process(final OutputStream out) throws IOException {
                Object payload = msgFromSpring.getPayload();
                byte[] payloadBytes = payload instanceof String ? ((String) payload).getBytes() : (byte[]) payload;
                out.write(payloadBytes);
            }
        });
        flowFileToProcess = processSession.putAllAttributes(flowFileToProcess,
                this.extractFlowFileAttributesFromMessageHeaders(msgFromSpring.getHeaders()));
        processSession.transfer(flowFileToProcess, REL_SUCCESS);
        processSession.getProvenanceReporter().receive(flowFileToProcess, this.applicationContextConfigFileName);
    }
}
 
Example 3
Source Project: localization_nifi   Source File: RouteText.java    License: Apache License 2.0 6 votes vote down vote up
private void appendLine(final ProcessSession session, final Map<Relationship, Map<Group, FlowFile>> flowFileMap, final Relationship relationship,
    final FlowFile original, final String line, final Charset charset, final Group group) {

    Map<Group, FlowFile> groupToFlowFileMap = flowFileMap.get(relationship);
    if (groupToFlowFileMap == null) {
        groupToFlowFileMap = new HashMap<>();
        flowFileMap.put(relationship, groupToFlowFileMap);
    }

    FlowFile flowFile = groupToFlowFileMap.get(group);
    if (flowFile == null) {
        flowFile = session.create(original);
    }

    flowFile = session.append(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream out) throws IOException {
            out.write(line.getBytes(charset));
        }
    });

    groupToFlowFileMap.put(group, flowFile);
}
 
Example 4
Source Project: nifi   Source File: TestStandardProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAppendDoesNotDecrementContentClaimIfNotNeeded() {
    FlowFile flowFile = session.create();

    session.append(flowFile, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
            out.write("hello".getBytes());
        }
    });

    final Set<ContentClaim> existingClaims = contentRepo.getExistingClaims();
    assertEquals(1, existingClaims.size());
    final ContentClaim claim = existingClaims.iterator().next();

    final int countAfterAppend = contentRepo.getClaimantCount(claim);
    assertEquals(1, countAfterAppend);
}
 
Example 5
Source Project: nifi   Source File: MalformedChunkHandlerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testHandle() {
    String name = "name";
    byte[] badChunk = {8};
    FlowFile original = mock(FlowFile.class);
    FlowFile updated1 = mock(FlowFile.class);
    FlowFile updated2 = mock(FlowFile.class);
    FlowFile updated3 = mock(FlowFile.class);
    FlowFile updated4 = mock(FlowFile.class);
    ProcessSession session = mock(ProcessSession.class);

    when(session.create(original)).thenReturn(updated1);
    when(session.putAttribute(updated1, CoreAttributes.FILENAME.key(), name)).thenReturn(updated2);
    when(session.putAttribute(updated2, CoreAttributes.MIME_TYPE.key(), MediaType.APPLICATION_BINARY.toString())).thenReturn(updated3);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    when(session.write(eq(updated3), any(OutputStreamCallback.class))).thenAnswer(invocation -> {
        ((OutputStreamCallback) invocation.getArguments()[1]).process(out);
        return updated4;
    });

    malformedChunkHandler.handle(original, session, name, badChunk);

    verify(session).transfer(updated4, badChunkRelationship);
    assertArrayEquals(badChunk, out.toByteArray());
}
 
Example 6
Source Project: localization_nifi   Source File: SplitText.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Will concatenate the contents of the provided array of {@link FlowFile}s
 * into a single {@link FlowFile}. While this operation is as general as it
 * is described in the previous sentence, in the context of this processor
 * there can only be two {@link FlowFile}s with the first {@link FlowFile}
 * representing the header content of the split and the second
 * {@link FlowFile} represents the split itself.
 */
private FlowFile concatenateContents(FlowFile sourceFlowFile, ProcessSession session, FlowFile... flowFiles) {
    FlowFile mergedFlowFile = session.create(sourceFlowFile);
    for (FlowFile flowFile : flowFiles) {
        mergedFlowFile = session.append(mergedFlowFile, new OutputStreamCallback() {
            @Override
            public void process(OutputStream out) throws IOException {
                try (InputStream is = session.read(flowFile)) {
                    IOUtils.copy(is, out);
                }
            }
        });
    }
    session.remove(flowFiles[1]); // in current usage we always have 2 files
    return mergedFlowFile;
}
 
Example 7
Source Project: localization_nifi   Source File: TestSplitJson.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSplit_pathToNullValue() throws Exception {
    final TestRunner testRunner = TestRunners.newTestRunner(new SplitJson());
    testRunner.setProperty(SplitJson.ARRAY_JSON_PATH_EXPRESSION, "$.nullField");

    ProcessSession session = testRunner.getProcessSessionFactory().createSession();
    FlowFile ff = session.create();

    ff = session.write(ff, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
            try (OutputStream outputStream = new BufferedOutputStream(out)) {
                outputStream.write("{\"stringField\": \"String Value\", \"nullField\": null}".getBytes(StandardCharsets.UTF_8));
            }
        }
    });

    testRunner.enqueue(ff);
    testRunner.run();

    testRunner.assertTransferCount(SplitJson.REL_FAILURE, 1);
}
 
Example 8
Source Project: localization_nifi   Source File: AbstractFlumeProcessor.java    License: Apache License 2.0 6 votes vote down vote up
protected static void transferEvent(final Event event, ProcessSession session,
    Relationship relationship) {
    FlowFile flowFile = session.create();
    flowFile = session.putAllAttributes(flowFile, event.getHeaders());

    flowFile = session.write(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream out) throws IOException {
            out.write(event.getBody());
        }
    });

    session.getProvenanceReporter()
        .create(flowFile);
    session.transfer(flowFile, relationship);
}
 
Example 9
Source Project: localization_nifi   Source File: GetTCP.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void handle(InetSocketAddress sourceAddress, byte[] message, boolean partialMessage) {
    ProcessSession session = this.sessionFactory.createSession();
    FlowFile flowFile = session.create();
    flowFile = session.write(flowFile, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
            out.write(message);
        }
    });
    flowFile = session.putAttribute(flowFile, SOURCE_ENDPOINT_ATTRIBUTE, sourceAddress.toString());
    if (!GetTCP.this.dynamicAttributes.isEmpty()) {
        flowFile = session.putAllAttributes(flowFile, GetTCP.this.dynamicAttributes);
    }
    if (partialMessage) {
        session.transfer(flowFile, REL_PARTIAL);
    } else {
        session.transfer(flowFile, REL_SUCCESS);
    }
    session.commit();
}
 
Example 10
Source Project: nifi   Source File: TestStandardProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReadFromInputStream() throws IOException {
    FlowFile flowFile = session.create();
    flowFile = session.write(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream out) throws IOException {
            out.write("hello, world".getBytes());
        }
    });

    try (InputStream in = session.read(flowFile)) {
        final byte[] buffer = new byte[12];
        StreamUtils.fillBuffer(in, buffer);
        assertEquals("hello, world", new String(buffer));
    }

    session.remove(flowFile);
    session.commit();
}
 
Example 11
@Test
public void testAppendAfterSessionClosesStream() throws IOException {
    final ContentClaim claim = contentRepo.create(false);
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder()
            .contentClaim(claim)
            .addAttribute("uuid", "12345678-1234-1234-1234-123456789012")
            .entryDate(System.currentTimeMillis())
            .build();
    flowFileQueue.put(flowFileRecord);
    FlowFile flowFile = session.get();
    assertNotNull(flowFile);
    final AtomicReference<OutputStream> outputStreamHolder = new AtomicReference<>(null);
    flowFile = session.append(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream outputStream) throws IOException {
            outputStreamHolder.set(outputStream);
        }
    });
    assertDisabled(outputStreamHolder.get());
}
 
Example 12
@Test
public void testWriteAfterSessionClosesStream() throws IOException {
    final ContentClaim claim = contentRepo.create(false);
    final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder()
            .contentClaim(claim)
            .addAttribute("uuid", "12345678-1234-1234-1234-123456789012")
            .entryDate(System.currentTimeMillis())
            .build();
    flowFileQueue.put(flowFileRecord);
    FlowFile flowFile = session.get();
    assertNotNull(flowFile);
    final AtomicReference<OutputStream> outputStreamHolder = new AtomicReference<>(null);
    flowFile = session.write(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream out) throws IOException {
            outputStreamHolder.set(out);
        }
    });
    assertDisabled(outputStreamHolder.get());
}
 
Example 13
@Test
public void testAppendDoesNotDecrementContentClaimIfNotNeeded() {
    FlowFile flowFile = session.create();

    session.append(flowFile, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
            out.write("hello".getBytes());
        }
    });

    final Set<ContentClaim> existingClaims = contentRepo.getExistingClaims();
    assertEquals(1, existingClaims.size());
    final ContentClaim claim = existingClaims.iterator().next();

    final int countAfterAppend = contentRepo.getClaimantCount(claim);
    assertEquals(1, countAfterAppend);
}
 
Example 14
@Test
public void testContentModifiedNotEmittedForCreate() throws IOException {
    FlowFile newFlowFile = session.create();
    newFlowFile = session.write(newFlowFile, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
        }
    });
    session.transfer(newFlowFile, new Relationship.Builder().name("A").build());
    session.commit();

    final List<ProvenanceEventRecord> events = provenanceRepo.getEvents(0L, 10000);
    assertFalse(events.isEmpty());
    assertEquals(1, events.size());

    final ProvenanceEventRecord event = events.get(0);
    assertEquals(ProvenanceEventType.CREATE, event.getEventType());
}
 
Example 15
@Test
public void testContentModifiedEmittedAndNotAttributesModified() throws IOException {
    final FlowFileRecord flowFile = new StandardFlowFileRecord.Builder()
            .id(1L)
            .addAttribute("uuid", "000000000000-0000-0000-0000-00000000")
            .build();
    this.flowFileQueue.put(flowFile);

    FlowFile existingFlowFile = session.get();
    existingFlowFile = session.write(existingFlowFile, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
        }
    });
    existingFlowFile = session.putAttribute(existingFlowFile, "attr", "a");
    session.transfer(existingFlowFile, new Relationship.Builder().name("A").build());
    session.commit();

    final List<ProvenanceEventRecord> events = provenanceRepo.getEvents(0L, 10000);
    assertFalse(events.isEmpty());
    assertEquals(1, events.size());

    final ProvenanceEventRecord event = events.get(0);
    assertEquals(ProvenanceEventType.CONTENT_MODIFIED, event.getEventType());
}
 
Example 16
@Test
public void testReadFromInputStream() throws IOException {
    FlowFile flowFile = session.create();
    flowFile = session.write(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream out) throws IOException {
            out.write("hello, world".getBytes());
        }
    });

    try (InputStream in = session.read(flowFile)) {
        final byte[] buffer = new byte[12];
        StreamUtils.fillBuffer(in, buffer);
        assertEquals("hello, world", new String(buffer));
    }

    session.remove(flowFile);
    session.commit();
}
 
Example 17
Source Project: nifi   Source File: AbstractFlumeProcessor.java    License: Apache License 2.0 6 votes vote down vote up
protected static void transferEvent(final Event event, ProcessSession session,
    Relationship relationship) {
    FlowFile flowFile = session.create();
    flowFile = session.putAllAttributes(flowFile, event.getHeaders());

    flowFile = session.write(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream out) throws IOException {
            out.write(event.getBody());
        }
    });

    session.getProvenanceReporter()
        .create(flowFile);
    session.transfer(flowFile, relationship);
}
 
Example 18
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile toRemove = session.get();
    if (toRemove != null) {
        LOG.warn("Removing flow file");
        session.remove(toRemove);
    }

    FlowFile flowFile = session.create();
    final Random random = new Random();
    final byte[] data = new byte[4096];
    random.nextBytes(data);

    flowFile = session.write(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream out) throws IOException {
            out.write(data);
        }
    });

    LOG.info("{} transferring {} to success", new Object[]{this, flowFile});
    session.transfer(flowFile, REL_SUCCESS);
    session.commit();
}
 
Example 19
@Test
public void testHandle() {
    String name = "name";
    byte[] badChunk = {8};
    FlowFile original = mock(FlowFile.class);
    FlowFile updated1 = mock(FlowFile.class);
    FlowFile updated2 = mock(FlowFile.class);
    FlowFile updated3 = mock(FlowFile.class);
    FlowFile updated4 = mock(FlowFile.class);
    ProcessSession session = mock(ProcessSession.class);

    when(session.create(original)).thenReturn(updated1);
    when(session.putAttribute(updated1, CoreAttributes.FILENAME.key(), name)).thenReturn(updated2);
    when(session.putAttribute(updated2, CoreAttributes.MIME_TYPE.key(), MediaType.APPLICATION_BINARY.toString())).thenReturn(updated3);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    when(session.write(eq(updated3), any(OutputStreamCallback.class))).thenAnswer(invocation -> {
        ((OutputStreamCallback) invocation.getArguments()[1]).process(out);
        return updated4;
    });

    malformedChunkHandler.handle(original, session, name, badChunk);

    verify(session).transfer(updated4, badChunkRelationship);
    assertArrayEquals(badChunk, out.toByteArray());
}
 
Example 20
Source Project: nifi   Source File: MockProcessSession.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FlowFile append(FlowFile flowFile, final OutputStreamCallback callback) {
    if (callback == null || flowFile == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    final MockFlowFile mock = validateState(flowFile);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        baos.write(mock.getData());
        callback.process(baos);
    } catch (final IOException e) {
        throw new ProcessException(e.toString(), e);
    }

    final MockFlowFile newFlowFile = new MockFlowFile(mock.getId(), flowFile);
    currentVersions.put(newFlowFile.getId(), newFlowFile);

    newFlowFile.setData(baos.toByteArray());
    return newFlowFile;
}
 
Example 21
Source Project: nifi   Source File: RouteText.java    License: Apache License 2.0 6 votes vote down vote up
private void appendLine(final ProcessSession session, final Map<Relationship, Map<Group, FlowFile>> flowFileMap, final Relationship relationship,
    final FlowFile original, final String line, final Charset charset, final Group group) {

    final Map<Group, FlowFile> groupToFlowFileMap = flowFileMap.computeIfAbsent(relationship, k -> new HashMap<>());

    FlowFile flowFile = groupToFlowFileMap.get(group);
    if (flowFile == null) {
        flowFile = session.create(original);
    }

    flowFile = session.append(flowFile, new OutputStreamCallback() {
        @Override
        public void process(final OutputStream out) throws IOException {
            out.write(line.getBytes(charset));
        }
    });

    groupToFlowFileMap.put(group, flowFile);
}
 
Example 22
Source Project: nifi   Source File: SplitText.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Will concatenate the contents of the provided array of {@link FlowFile}s
 * into a single {@link FlowFile}. While this operation is as general as it
 * is described in the previous sentence, in the context of this processor
 * there can only be two {@link FlowFile}s with the first {@link FlowFile}
 * representing the header content of the split and the second
 * {@link FlowFile} represents the split itself.
 */
private FlowFile concatenateContents(FlowFile sourceFlowFile, ProcessSession session, FlowFile... flowFiles) {
    FlowFile mergedFlowFile = session.create(sourceFlowFile);
    for (FlowFile flowFile : flowFiles) {
        mergedFlowFile = session.append(mergedFlowFile, new OutputStreamCallback() {
            @Override
            public void process(OutputStream out) throws IOException {
                try (InputStream is = session.read(flowFile)) {
                    IOUtils.copy(is, out);
                }
            }
        });
    }
    session.remove(flowFiles[1]); // in current usage we always have 2 files
    return mergedFlowFile;
}
 
Example 23
Source Project: nifi   Source File: GroovySessionFile.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Instantly writes into flow file contents the char sequence (string).
 *
 * @param charset charset to use for writer
 * @param c       content
 * @return reference to self
 */
public GroovySessionFile write(String charset, CharSequence c) {
    this.write(new OutputStreamCallback() {
        public void process(OutputStream out) throws IOException {
            Writer w = new OutputStreamWriter(out, charset);
            w.append(c);
            w.flush();
            w.close();
        }
    });
    return this;
}
 
Example 24
Source Project: localization_nifi   Source File: MockProcessSession.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public MockFlowFile write(final FlowFile flowFile, final OutputStreamCallback callback) {
    validateState(flowFile);
    if (callback == null || flowFile == null) {
        throw new IllegalArgumentException("argument cannot be null");
    }
    if (!(flowFile instanceof MockFlowFile)) {
        throw new IllegalArgumentException("Cannot export a flow file that I did not create");
    }
    final MockFlowFile mock = (MockFlowFile) flowFile;

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    recursionSet.add(flowFile);
    try {
        callback.process(baos);
    } catch (final IOException e) {
        throw new ProcessException(e.toString(), e);
    } finally {
        recursionSet.remove(flowFile);
    }

    final MockFlowFile newFlowFile = new MockFlowFile(mock.getId(), flowFile);
    currentVersions.put(newFlowFile.getId(), newFlowFile);

    newFlowFile.setData(baos.toByteArray());
    return newFlowFile;
}
 
Example 25
Source Project: nifi   Source File: GroovySessionFile.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Write flow file contents through writer with defined charset.
 *
 * @param charset charset to use for writer
 * @param c       content defined as writable
 * @return reference to self
 */
public GroovySessionFile write(String charset, Writable c) {
    this.write(new OutputStreamCallback() {
        public void process(OutputStream out) throws IOException {
            Writer w = new OutputStreamWriter(out, charset);
            c.writeTo(w);
            w.flush();
            w.close();
        }
    });
    return this;
}
 
Example 26
Source Project: localization_nifi   Source File: GenerateFlowFile.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    final byte[] data;
    if (context.getProperty(UNIQUE_FLOWFILES).asBoolean()) {
        data = generateData(context);
    } else if(context.getProperty(CUSTOM_TEXT).isSet()) {
        data = context.getProperty(CUSTOM_TEXT).evaluateAttributeExpressions().getValue().getBytes();
    } else {
        data = this.data.get();
    }

    Map<PropertyDescriptor, String> processorProperties = context.getProperties();
    Map<String, String> generatedAttributes = new HashMap<String, String>();
    for (final Map.Entry<PropertyDescriptor, String> entry : processorProperties.entrySet()) {
        PropertyDescriptor property = entry.getKey();
        if (property.isDynamic() && property.isExpressionLanguageSupported()) {
            String dynamicValue = context.getProperty(property).evaluateAttributeExpressions().getValue();
            generatedAttributes.put(property.getName(), dynamicValue);
        }
    }

    for (int i = 0; i < context.getProperty(BATCH_SIZE).asInteger(); i++) {
        FlowFile flowFile = session.create();
        if (data.length > 0) {
            flowFile = session.write(flowFile, new OutputStreamCallback() {
                @Override
                public void process(final OutputStream out) throws IOException {
                    out.write(data);
                }
            });
        }
        flowFile = session.putAllAttributes(flowFile, generatedAttributes);

        session.getProvenanceReporter().create(flowFile);
        session.transfer(flowFile, SUCCESS);
    }
}
 
Example 27
Source Project: localization_nifi   Source File: TestSplitJson.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSplit_pathToArrayWithNulls_emptyStringRepresentation() throws Exception {
    final TestRunner testRunner = TestRunners.newTestRunner(new SplitJson());
    testRunner.setProperty(SplitJson.ARRAY_JSON_PATH_EXPRESSION, "$.arrayOfNulls");

    ProcessSession session = testRunner.getProcessSessionFactory().createSession();
    FlowFile ff = session.create();

    ff = session.write(ff, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
            try (OutputStream outputStream = new BufferedOutputStream(out)) {
                outputStream.write("{\"stringField\": \"String Value\", \"arrayOfNulls\": [null, null, null]}".getBytes(StandardCharsets.UTF_8));
            }
        }
    });

    testRunner.enqueue(ff);
    testRunner.run();

    /* assert that three files were transferred to split and each is empty */
    int expectedFiles = 3;
    testRunner.assertTransferCount(SplitJson.REL_SPLIT, expectedFiles);
    for (int i = 0; i < expectedFiles; i++) {
        testRunner.getFlowFilesForRelationship(SplitJson.REL_SPLIT).get(i).assertContentEquals("");
    }
}
 
Example 28
Source Project: localization_nifi   Source File: TestSplitJson.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSplit_pathToArrayWithNulls_nullStringRepresentation() throws Exception {
    final TestRunner testRunner = TestRunners.newTestRunner(new SplitJson());
    testRunner.setProperty(SplitJson.ARRAY_JSON_PATH_EXPRESSION, "$.arrayOfNulls");
    testRunner.setProperty(SplitJson.NULL_VALUE_DEFAULT_REPRESENTATION,
            AbstractJsonPathProcessor.NULL_STRING_OPTION);

    ProcessSession session = testRunner.getProcessSessionFactory().createSession();
    FlowFile ff = session.create();

    ff = session.write(ff, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
            try (OutputStream outputStream = new BufferedOutputStream(out)) {
                outputStream.write("{\"stringField\": \"String Value\", \"arrayOfNulls\": [null, null, null]}".getBytes(StandardCharsets.UTF_8));
            }
        }
    });

    testRunner.enqueue(ff);
    testRunner.run();

    /* assert that three files were transferred to split and each has the word null in it */
    int expectedFiles = 3;
    testRunner.assertTransferCount(SplitJson.REL_SPLIT, expectedFiles);
    for (int i = 0; i < expectedFiles; i++) {
        testRunner.getFlowFilesForRelationship(SplitJson.REL_SPLIT).get(i).assertContentEquals("null");
    }
}
 
Example 29
Source Project: localization_nifi   Source File: TestEvaluateJsonPath.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testNullInput() throws Exception {
    final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateJsonPath());
    testRunner.setProperty(EvaluateJsonPath.RETURN_TYPE, EvaluateJsonPath.RETURN_TYPE_JSON);
    testRunner.setProperty(EvaluateJsonPath.DESTINATION, EvaluateJsonPath.DESTINATION_ATTRIBUTE);
    testRunner.setProperty("stringField", "$.stringField");
    testRunner.setProperty("missingField", "$.missingField");
    testRunner.setProperty("nullField", "$.nullField");

    ProcessSession session = testRunner.getProcessSessionFactory().createSession();
    FlowFile ff = session.create();

    ff = session.write(ff, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
            try (OutputStream outputStream = new BufferedOutputStream(out)) {
                outputStream.write("{\"stringField\": \"String Value\", \"nullField\": null}".getBytes(StandardCharsets.UTF_8));
            }
        }
    });

    testRunner.enqueue(ff);
    testRunner.run();

    testRunner.assertTransferCount(EvaluateJsonPath.REL_MATCH, 1);

    FlowFile output = testRunner.getFlowFilesForRelationship(EvaluateJsonPath.REL_MATCH).get(0);

    String validFieldValue = output.getAttribute("stringField");
    assertEquals("String Value", validFieldValue);

    String missingValue = output.getAttribute("missingField");
    assertEquals("Missing Value", "", missingValue);

    String nullValue = output.getAttribute("nullField");
    assertEquals("Null Value", "", nullValue);
}
 
Example 30
Source Project: localization_nifi   Source File: TestEvaluateJsonPath.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testNullInput_nullStringRepresentation() throws Exception {
    final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateJsonPath());
    testRunner.setProperty(EvaluateJsonPath.RETURN_TYPE, EvaluateJsonPath.RETURN_TYPE_JSON);
    testRunner.setProperty(EvaluateJsonPath.DESTINATION, EvaluateJsonPath.DESTINATION_ATTRIBUTE);
    testRunner.setProperty(EvaluateJsonPath.NULL_VALUE_DEFAULT_REPRESENTATION, AbstractJsonPathProcessor.NULL_STRING_OPTION);
    testRunner.setProperty("stringField", "$.stringField");
    testRunner.setProperty("missingField", "$.missingField");
    testRunner.setProperty("nullField", "$.nullField");

    ProcessSession session = testRunner.getProcessSessionFactory().createSession();
    FlowFile ff = session.create();

    ff = session.write(ff, new OutputStreamCallback() {
        @Override
        public void process(OutputStream out) throws IOException {
            try (OutputStream outputStream = new BufferedOutputStream(out)) {
                outputStream.write("{\"stringField\": \"String Value\", \"nullField\": null}".getBytes(StandardCharsets.UTF_8));
            }
        }
    });

    testRunner.enqueue(ff);
    testRunner.run();

    testRunner.assertTransferCount(EvaluateJsonPath.REL_MATCH, 1);

    FlowFile output = testRunner.getFlowFilesForRelationship(EvaluateJsonPath.REL_MATCH).get(0);

    String validFieldValue = output.getAttribute("stringField");
    assertEquals("String Value", validFieldValue);

    String missingValue = output.getAttribute("missingField");
    assertEquals("Missing Value", "", missingValue);

    String nullValue = output.getAttribute("nullField");
    assertEquals("Null Value", "null", nullValue);
}