Java Code Examples for org.apache.nifi.util.MockFlowFile#assertAttributeNotExists()

The following examples show how to use org.apache.nifi.util.MockFlowFile#assertAttributeNotExists() . 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 File: PutGCSObjectIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimplePut() throws Exception {
    final TestRunner runner = buildNewRunner(new PutGCSObject());
    runner.setProperty(PutGCSObject.BUCKET, BUCKET);
    runner.setProperty(PutGCSObject.KEY, KEY);

    runner.enqueue(CONTENT);

    runner.run();

    runner.assertAllFlowFilesTransferred(ListGCSBucket.REL_SUCCESS, 1);
    assertTrue(fileEquals(KEY, CONTENT));

    final MockFlowFile flowFile = runner.getFlowFilesForRelationship(ListGCSBucket.REL_SUCCESS).get(0);
    flowFile.assertAttributeNotExists(ENCRYPTION_ALGORITHM_ATTR);

    for (Map.Entry<String, String> entry : flowFile.getAttributes().entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
}
 
Example 2
Source File: TestConvertJSONToSQL.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateQuotedTableIdentifier() throws InitializationException, ProcessException, SQLException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(ConvertJSONToSQL.class);
    runner.addControllerService("dbcp", service);
    runner.enableControllerService(service);

    runner.setProperty(ConvertJSONToSQL.CONNECTION_POOL, "dbcp");
    runner.setProperty(ConvertJSONToSQL.TABLE_NAME, "PERSONS");
    runner.setProperty(ConvertJSONToSQL.STATEMENT_TYPE, "UPDATE");
    runner.setProperty(ConvertJSONToSQL.QUOTED_TABLE_IDENTIFIER, "true");
    runner.enqueue(Paths.get("src/test/resources/TestConvertJSONToSQL/person-with-null-code.json"));
    runner.run();

    runner.assertTransferCount(ConvertJSONToSQL.REL_ORIGINAL, 1);
    runner.getFlowFilesForRelationship(ConvertJSONToSQL.REL_ORIGINAL).get(0).assertAttributeEquals(FRAGMENT_COUNT.key(), "1");
    runner.assertTransferCount(ConvertJSONToSQL.REL_SQL, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertJSONToSQL.REL_SQL).get(0);
    out.assertAttributeEquals("sql.args.1.type", String.valueOf(java.sql.Types.VARCHAR));
    out.assertAttributeEquals("sql.args.1.value", "Mark");
    out.assertAttributeEquals("sql.args.2.type", String.valueOf(java.sql.Types.INTEGER));
    out.assertAttributeNotExists("sql.args.2.value");
    out.assertAttributeEquals("sql.args.3.type", String.valueOf(java.sql.Types.INTEGER));
    out.assertAttributeEquals("sql.args.3.value", "1");

    out.assertContentEquals("UPDATE \"PERSONS\" SET NAME = ?, CODE = ? WHERE ID = ?");
}
 
Example 3
Source File: TestWait.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpired() throws InitializationException, InterruptedException {
    runner.setProperty(Wait.RELEASE_SIGNAL_IDENTIFIER, "${releaseSignalAttribute}");
    runner.setProperty(Wait.EXPIRATION_DURATION, "100 ms");

    final Map<String, String> props = new HashMap<>();
    props.put("releaseSignalAttribute", "1");
    runner.enqueue(new byte[]{}, props);

    runner.run();

    runner.assertAllFlowFilesTransferred(Wait.REL_WAIT, 1);
    MockFlowFile ff = runner.getFlowFilesForRelationship(Wait.REL_WAIT).get(0);
    ff.assertAttributeExists(Wait.WAIT_START_TIMESTAMP); // timestamp must be set

    runner.clearTransferState();
    runner.enqueue(ff);

    Thread.sleep(101L);
    runner.run();

    runner.assertAllFlowFilesTransferred(Wait.REL_EXPIRED, 1);
    ff = runner.getFlowFilesForRelationship(Wait.REL_EXPIRED).get(0);
    ff.assertAttributeNotExists(Wait.WAIT_START_TIMESTAMP); // timestamp must be cleared
    runner.clearTransferState();
}
 
Example 4
Source File: TestCompareFuzzyHash.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSsdeepCompareFuzzyHashWithInvalidHashList() {
    // This is different from "BlankHashList series of tests in that the file lacks headers and as such is totally
    // invalid
    double matchingSimilarity = 80;
    runner.setProperty(CompareFuzzyHash.HASH_ALGORITHM, CompareFuzzyHash.allowableValueSSDEEP.getValue());
    runner.setProperty(CompareFuzzyHash.ATTRIBUTE_NAME, "fuzzyhash.value");
    runner.setProperty(CompareFuzzyHash.HASH_LIST_FILE, "src/test/resources/empty.list");
    runner.setProperty(CompareFuzzyHash.MATCH_THRESHOLD, String.valueOf(matchingSimilarity));

    Map<String, String> attributes = new HashMap<>();
    attributes.put("fuzzyhash.value", "6:hERjIfhRrlB63J0FDw1NBQmEH68xwMSELN:hZrlB62IwMS");

    runner.enqueue("bogus".getBytes(), attributes);
    runner.run();

    runner.assertQueueEmpty();
    runner.assertAllFlowFilesTransferred(CompareFuzzyHash.REL_NOT_FOUND, 1);

    final MockFlowFile outFile = runner.getFlowFilesForRelationship(CompareFuzzyHash.REL_NOT_FOUND).get(0);

    outFile.assertAttributeNotExists("fuzzyhash.value.0.match");
}
 
Example 5
Source File: TestParseUri.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseUriFail() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "Groovy");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/misc/parse_uri.groovy");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("input.url", "total_bogus");
    runner.enqueue("nothing".getBytes(StandardCharsets.UTF_8), attributes);
    runner.run();

    runner.assertAllFlowFilesTransferred("failure", 1);
    final List<MockFlowFile> failureFlowFiles = runner.getFlowFilesForRelationship("failure");
    MockFlowFile result = failureFlowFiles.get(0);
    result.assertAttributeNotExists("uri.scheme");
    result.assertAttributeNotExists("uri.host");
    result.assertAttributeNotExists("uri.path");
    result.assertAttributeEquals("parse_url.error", "no protocol: total_bogus");
    result.assertAttributeEquals("input.url", "total_bogus");
}
 
Example 6
Source File: TestExtractAvroMetadata.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtractionWithSHA256() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ExtractAvroMetadata());
    runner.setProperty(ExtractAvroMetadata.FINGERPRINT_ALGORITHM, ExtractAvroMetadata.SHA_256);

    final Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    final ByteArrayOutputStream out = getOutputStreamWithOneUser(schema);
    runner.enqueue(out.toByteArray());
    runner.run();

    runner.assertAllFlowFilesTransferred(ExtractAvroMetadata.REL_SUCCESS, 1);

    final MockFlowFile flowFile = runner.getFlowFilesForRelationship(ExtractAvroMetadata.REL_SUCCESS).get(0);
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_FINGERPRINT_ATTR, "683f8f51ecd208038f4f0d39820ee9dd0ef3e32a3bee9371de0a2016d501b113");
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_TYPE_ATTR, Schema.Type.RECORD.getName());
    flowFile.assertAttributeEquals(ExtractAvroMetadata.SCHEMA_NAME_ATTR, "User");
    flowFile.assertAttributeNotExists(AVRO_SCHEMA_ATTR);
}
 
Example 7
Source File: TestPutDistributedMapCache.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMaxCacheEntrySize() throws InitializationException, IOException {

    runner.setProperty(PutDistributedMapCache.CACHE_ENTRY_IDENTIFIER, "${uuid}");
    runner.setProperty(PutDistributedMapCache.CACHE_ENTRY_MAX_BYTES, "10 B");

    // max length is 10 bytes, flow file content is 20 bytes
    String flowFileContent = "contentwhichistoobig";
    runner.enqueue(flowFileContent.getBytes("UTF-8"));

    runner.run();

    // no cache key attribute
    runner.assertAllFlowFilesTransferred(PutDistributedMapCache.REL_FAILURE, 1);
    runner.assertTransferCount(PutDistributedMapCache.REL_FAILURE, 1);

    final MockFlowFile outputFlowFile = runner.getFlowFilesForRelationship(PutDistributedMapCache.REL_FAILURE).get(0);
    outputFlowFile.assertAttributeNotExists("cached");
    outputFlowFile.assertContentEquals(flowFileContent);


    runner.clearTransferState();
    runner.setProperty(PutDistributedMapCache.CACHE_ENTRY_MAX_BYTES, "1 MB");
}
 
Example 8
Source File: TestCompareFuzzyHash.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testTLSHCompareFuzzyHashWithBlankFile() {
    // This is different from "BlankHashList series of tests in that the file lacks headers and as such is totally
    // invalid
    double matchingSimilarity = 200;
    runner.setProperty(CompareFuzzyHash.HASH_ALGORITHM, CompareFuzzyHash.allowableValueTLSH.getValue());
    runner.setProperty(CompareFuzzyHash.ATTRIBUTE_NAME, "fuzzyhash.value");
    runner.setProperty(CompareFuzzyHash.HASH_LIST_FILE, "src/test/resources/empty.list");
    runner.setProperty(CompareFuzzyHash.MATCH_THRESHOLD, String.valueOf(matchingSimilarity));

    Map<String, String> attributes = new HashMap<>();
    attributes.put("fuzzyhash.value", "E2F0818B7AE7173906A72221570E30979B11C0FC47B518A1E89D257E2343CEC02381ED");

    runner.enqueue("bogus".getBytes(), attributes);
    runner.run();

    runner.assertQueueEmpty();
    runner.assertAllFlowFilesTransferred(CompareFuzzyHash.REL_NOT_FOUND, 1);

    final MockFlowFile outFile = runner.getFlowFilesForRelationship(CompareFuzzyHash.REL_NOT_FOUND).get(0);

    outFile.assertAttributeNotExists("fuzzyhash.value.0.match");
}
 
Example 9
Source File: TestAttributesToCSV.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testSchemaWithCoreAttribuesToContent() throws Exception {
    final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToCSV());
    //set the destination of the csv string to be an attribute
    testRunner.setProperty(AttributesToCSV.DESTINATION, OUTPUT_OVERWRITE_CONTENT);
    testRunner.setProperty(AttributesToCSV.INCLUDE_CORE_ATTRIBUTES, "true");
    testRunner.setProperty(AttributesToCSV.NULL_VALUE_FOR_EMPTY_STRING, "false");
    testRunner.setProperty(AttributesToCSV.INCLUDE_SCHEMA, "true");
    testRunner.setProperty(AttributesToCSV.ATTRIBUTES_REGEX, "beach-.*");

    Map<String, String> attrs = new HashMap<String, String>(){{
        put("beach-name", "Malibu Beach");
        put("beach-location", "California, US");
        put("attribute-should-be-eliminated", "This should not be in CSVData!");
    }};

    testRunner.enqueue(new byte[0], attrs);
    testRunner.run();

    testRunner.assertTransferCount(AttributesToCSV.REL_SUCCESS, 1);
    testRunner.assertTransferCount(AttributesToCSV.REL_FAILURE, 0);

    MockFlowFile flowFile = testRunner.getFlowFilesForRelationship(AttributesToCSV.REL_SUCCESS).get(0);
    flowFile.assertAttributeNotExists("CSVData");
    flowFile.assertAttributeNotExists("CSVSchema");

    final String path = flowFile.getAttribute("path");
    final String filename = flowFile.getAttribute("filename");
    final String uuid = flowFile.getAttribute("uuid");

    final byte[] contentData = testRunner.getContentAsByteArray(flowFile);

    final String contentDataString = new String(contentData, "UTF-8");
    assertEquals(contentDataString.split(newline)[0], "beach-name,beach-location,path,filename,uuid");
    assertEquals(contentDataString.split(newline)[1], "Malibu Beach,\"California, US\"," + path + "," + filename + "," + uuid);
}
 
Example 10
Source File: TestExtractText.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testFindAllPair() throws Exception {
    final TestRunner testRunner = TestRunners.newTestRunner(new ExtractText());
    testRunner.setProperty(ExtractText.ENABLE_REPEATING_CAPTURE_GROUP, "true");
    final String attributeKey = "regex.result";
    testRunner.setProperty(attributeKey, "(\\w+)=(\\d+)");
    testRunner.enqueue("a=1,b=10,c=100".getBytes("UTF-8"));
    testRunner.run();
    testRunner.assertAllFlowFilesTransferred(ExtractText.REL_MATCH, 1);
    final MockFlowFile out = testRunner.getFlowFilesForRelationship(ExtractText.REL_MATCH).get(0);
    // Ensure the zero capture group is in the resultant attributes
    out.assertAttributeExists(attributeKey + ".0");
    out.assertAttributeExists(attributeKey + ".1");
    out.assertAttributeExists(attributeKey + ".2");
    out.assertAttributeExists(attributeKey + ".3");
    out.assertAttributeExists(attributeKey + ".4");
    out.assertAttributeExists(attributeKey + ".5");
    out.assertAttributeExists(attributeKey + ".6");
    out.assertAttributeNotExists(attributeKey + ".7"); // Ensure there's no more attributes
    out.assertAttributeEquals(attributeKey, "a");
    out.assertAttributeEquals(attributeKey + ".0", "a=1");
    out.assertAttributeEquals(attributeKey + ".1", "a");
    out.assertAttributeEquals(attributeKey + ".2", "1");
    out.assertAttributeEquals(attributeKey + ".3", "b");
    out.assertAttributeEquals(attributeKey + ".4", "10");
    out.assertAttributeEquals(attributeKey + ".5", "c");
    out.assertAttributeEquals(attributeKey + ".6", "100");
}
 
Example 11
Source File: FetchGCSObjectIT.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleFetch() throws Exception {
    putTestFile(KEY, CONTENT);
    assertTrue(fileExists(KEY));

    final TestRunner runner = buildNewRunner(new FetchGCSObject());
    runner.setProperty(FetchGCSObject.BUCKET, BUCKET);

    runner.enqueue(new byte[0], ImmutableMap.of(
            "filename", KEY
    ));

    runner.run();

    runner.assertAllFlowFilesTransferred(FetchGCSObject.REL_SUCCESS, 1);
    final List<MockFlowFile> ffs = runner.getFlowFilesForRelationship(FetchGCSObject.REL_SUCCESS);
    MockFlowFile ff = ffs.get(0);

    ff.assertContentEquals(CONTENT);

    ff.assertAttributeNotExists(StorageAttributes.ENCRYPTION_ALGORITHM_ATTR);
    ff.assertAttributeNotExists(StorageAttributes.ENCRYPTION_SHA256_ATTR);

    for (final Map.Entry<String, String> entry : ff.getAttributes().entrySet()) {
        System.out.println(entry.getKey() + ":" + entry.getValue());
    }
}
 
Example 12
Source File: TestAttributeRollingWindow.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testStateFailures() throws InterruptedException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(AttributeRollingWindow.class);
    MockStateManager mockStateManager = runner.getStateManager();
    final AttributeRollingWindow processor = (AttributeRollingWindow) runner.getProcessor();
    final ProcessSessionFactory processSessionFactory = runner.getProcessSessionFactory();

    runner.setProperty(AttributeRollingWindow.VALUE_TO_TRACK, "${value}");
    runner.setProperty(AttributeRollingWindow.TIME_WINDOW, "3 sec");

    processor.onScheduled(runner.getProcessContext());

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("value", "1");

    mockStateManager.setFailOnStateGet(Scope.LOCAL, true);

    runner.enqueue(new byte[0],attributes);
    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueNotEmpty();

    mockStateManager.setFailOnStateGet(Scope.LOCAL, false);
    mockStateManager.setFailOnStateSet(Scope.LOCAL, true);

    processor.onTrigger(runner.getProcessContext(), processSessionFactory.createSession());

    runner.assertQueueEmpty();

    runner.assertAllFlowFilesTransferred(AttributeRollingWindow.REL_FAILED_SET_STATE, 1);
    MockFlowFile mockFlowFile = runner.getFlowFilesForRelationship(REL_FAILED_SET_STATE).get(0);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_VALUE_KEY);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_COUNT_KEY);
    mockFlowFile.assertAttributeNotExists(ROLLING_WINDOW_MEAN_KEY);
}
 
Example 13
Source File: TestConvertJSONToSQL.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsertWithNullValue() throws InitializationException, ProcessException, SQLException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(ConvertJSONToSQL.class);
    final File tempDir = folder.getRoot();
    final File dbDir = new File(tempDir, "db");
    final DBCPService service = new MockDBCPService(dbDir.getAbsolutePath());
    runner.addControllerService("dbcp", service);
    runner.enableControllerService(service);

    try (final Connection conn = service.getConnection()) {
        try (final Statement stmt = conn.createStatement()) {
            stmt.executeUpdate(createPersons);
        }
    }

    runner.setProperty(ConvertJSONToSQL.CONNECTION_POOL, "dbcp");
    runner.setProperty(ConvertJSONToSQL.TABLE_NAME, "PERSONS");
    runner.setProperty(ConvertJSONToSQL.STATEMENT_TYPE, "INSERT");
    runner.enqueue(Paths.get("src/test/resources/TestConvertJSONToSQL/person-with-null-code.json"));
    runner.run();

    runner.assertTransferCount(ConvertJSONToSQL.REL_ORIGINAL, 1);
    runner.getFlowFilesForRelationship(ConvertJSONToSQL.REL_ORIGINAL).get(0).assertAttributeEquals(FRAGMENT_COUNT.key(), "1");
    runner.assertTransferCount(ConvertJSONToSQL.REL_SQL, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertJSONToSQL.REL_SQL).get(0);
    out.assertAttributeEquals("sql.args.1.type", String.valueOf(java.sql.Types.INTEGER));
    out.assertAttributeEquals("sql.args.1.value", "1");
    out.assertAttributeEquals("sql.args.2.type", String.valueOf(java.sql.Types.VARCHAR));
    out.assertAttributeEquals("sql.args.2.value", "Mark");
    out.assertAttributeEquals("sql.args.3.type", String.valueOf(java.sql.Types.INTEGER));
    out.assertAttributeNotExists("sql.args.3.value");

    out.assertContentEquals("INSERT INTO PERSONS (ID, NAME, CODE) VALUES (?, ?, ?)");
}
 
Example 14
Source File: TestInvokeAWSGatewayApiCommon.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPut() throws Exception {
    addHandler(new MutativeMethodHandler(MutativeMethod.PUT));

    setupEndpointAndRegion();

    runner.setProperty(InvokeAWSGatewayApi.PROP_RESOURCE_NAME, "/post");
    runner.setProperty(InvokeAWSGatewayApi.PROP_METHOD, "PUT");

    createFlowFiles(runner);

    runner.run();
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_SUCCESS_REQ, 1);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_RESPONSE, 1);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_RETRY, 0);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_NO_RETRY, 0);
    runner.assertTransferCount(InvokeAWSGatewayApi.REL_FAILURE, 0);
    runner.assertPenalizeCount(0);

    final MockFlowFile bundle = runner.getFlowFilesForRelationship(InvokeAWSGatewayApi.REL_SUCCESS_REQ).get(0);
    bundle.assertContentEquals("Hello".getBytes("UTF-8"));
    bundle.assertAttributeEquals(InvokeAWSGatewayApi.STATUS_CODE, "200");
    bundle.assertAttributeEquals(InvokeAWSGatewayApi.STATUS_MESSAGE, "OK");
    bundle.assertAttributeEquals("Foo", "Bar");

    final MockFlowFile bundle1 = runner.getFlowFilesForRelationship(InvokeAWSGatewayApi.REL_RESPONSE).get(0);
    bundle1.assertContentEquals("".getBytes("UTF-8"));
    bundle1.assertAttributeEquals(InvokeAWSGatewayApi.STATUS_CODE, "200");
    bundle1.assertAttributeEquals(InvokeAWSGatewayApi.STATUS_MESSAGE, "OK");
    bundle1.assertAttributeEquals("Foo", "Bar");
    bundle1.assertAttributeNotExists("Content-Type");

    final String actual1 = new String(bundle1.toByteArray(), StandardCharsets.UTF_8);
    final String expected1 = "";
    Assert.assertEquals(expected1, actual1);
}
 
Example 15
Source File: TestInvokeHttpCommon.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPatch() throws Exception {
    addHandler(new MutativeMethodHandler(MutativeMethod.PATCH));

    runner.setProperty(InvokeHTTP.PROP_METHOD, "PATCH");
    runner.setProperty(InvokeHTTP.PROP_URL, url + "/patch");

    createFlowFiles(runner);

    runner.run();
    runner.assertTransferCount(InvokeHTTP.REL_SUCCESS_REQ, 1);
    runner.assertTransferCount(InvokeHTTP.REL_RESPONSE, 1);
    runner.assertTransferCount(InvokeHTTP.REL_RETRY, 0);
    runner.assertTransferCount(InvokeHTTP.REL_NO_RETRY, 0);
    runner.assertTransferCount(InvokeHTTP.REL_FAILURE, 0);
    runner.assertPenalizeCount(0);

    final MockFlowFile bundle = runner.getFlowFilesForRelationship(InvokeHTTP.REL_SUCCESS_REQ).get(0);
    bundle.assertContentEquals("Hello".getBytes("UTF-8"));
    bundle.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200");
    bundle.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK");
    bundle.assertAttributeEquals("Foo", "Bar");

    final MockFlowFile bundle1 = runner.getFlowFilesForRelationship(InvokeHTTP.REL_RESPONSE).get(0);
    bundle1.assertContentEquals("".getBytes("UTF-8"));
    bundle1.assertAttributeEquals(InvokeHTTP.STATUS_CODE, "200");
    bundle1.assertAttributeEquals(InvokeHTTP.STATUS_MESSAGE, "OK");
    bundle1.assertAttributeEquals("Foo", "Bar");
    bundle1.assertAttributeNotExists("Content-Type");

    final String actual1 = new String(bundle1.toByteArray(), StandardCharsets.UTF_8);
    final String expected1 = "";
    Assert.assertEquals(expected1, actual1);
}
 
Example 16
Source File: TestExtractText.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessor() throws Exception {

    final TestRunner testRunner = TestRunners.newTestRunner(new ExtractText());

    testRunner.setProperty("regex.result1", "(?s)(.*)");
    testRunner.setProperty("regex.result2", "(?s).*(bar1).*");
    testRunner.setProperty("regex.result3", "(?s).*?(bar\\d).*"); // reluctant gets first
    testRunner.setProperty("regex.result4", "(?s).*?(?:bar\\d).*?(bar\\d).*?(bar3).*"); // reluctant w/ repeated pattern gets second
    testRunner.setProperty("regex.result5", "(?s).*(bar\\d).*"); // greedy gets last
    testRunner.setProperty("regex.result6", "(?s)^(.*)$");
    testRunner.setProperty("regex.result7", "(?s)(XXX)");

    testRunner.enqueue(SAMPLE_STRING.getBytes("UTF-8"));
    testRunner.run();

    testRunner.assertAllFlowFilesTransferred(ExtractText.REL_MATCH, 1);
    final MockFlowFile out = testRunner.getFlowFilesForRelationship(ExtractText.REL_MATCH).get(0);
    out.assertAttributeEquals("regex.result1", SAMPLE_STRING);
    out.assertAttributeEquals("regex.result2", "bar1");
    out.assertAttributeEquals("regex.result3", "bar1");
    out.assertAttributeEquals("regex.result4", "bar2");
    out.assertAttributeEquals("regex.result4.0", SAMPLE_STRING);
    out.assertAttributeEquals("regex.result4.1", "bar2");
    out.assertAttributeEquals("regex.result4.2", "bar3");
    out.assertAttributeNotExists("regex.result4.3");
    out.assertAttributeEquals("regex.result5", "bar3");
    out.assertAttributeEquals("regex.result6", SAMPLE_STRING);
    out.assertAttributeEquals("regex.result7", null);
}
 
Example 17
Source File: TestMonitorActivity.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testFirstMessage() throws InterruptedException, IOException {
    final TestRunner runner = TestRunners.newTestRunner(new TestableProcessor(1000L));
    runner.setProperty(MonitorActivity.CONTINUALLY_SEND_MESSAGES, "false");
    runner.setProperty(MonitorActivity.THRESHOLD, "100 millis");

    runner.enqueue(new byte[0]);
    runner.run();
    runner.assertAllFlowFilesTransferred(MonitorActivity.REL_SUCCESS, 1);
    runner.clearTransferState();

    Thread.sleep(1000L);

    runNext(runner);
    runner.assertAllFlowFilesTransferred(MonitorActivity.REL_INACTIVE, 1);
    runner.clearTransferState();

    // ensure we don't keep creating the message
    for (int i = 0; i < 10; i++) {
        runNext(runner);
        runner.assertTransferCount(MonitorActivity.REL_SUCCESS, 0);
        runner.assertTransferCount(MonitorActivity.REL_INACTIVE, 0);
        runner.assertTransferCount(MonitorActivity.REL_ACTIVITY_RESTORED, 0);
        Thread.sleep(100L);
    }

    Map<String, String> attributes = new HashMap<>();
    attributes.put("key", "value");
    attributes.put("key1", "value1");

    runner.enqueue(new byte[0], attributes);
    runNext(runner);

    runner.assertTransferCount(MonitorActivity.REL_SUCCESS, 1);
    runner.assertTransferCount(MonitorActivity.REL_ACTIVITY_RESTORED, 1);

    MockFlowFile restoredFlowFile = runner.getFlowFilesForRelationship(MonitorActivity.REL_ACTIVITY_RESTORED).get(0);
    String flowFileContent = new String(restoredFlowFile.toByteArray());
    Assert.assertTrue(Pattern.matches("Activity restored at time: (.*) after being inactive for 0 minutes", flowFileContent));
    restoredFlowFile.assertAttributeNotExists("key");
    restoredFlowFile.assertAttributeNotExists("key1");

    runner.clearTransferState();
    runner.setProperty(MonitorActivity.CONTINUALLY_SEND_MESSAGES, "true");
    Thread.sleep(200L);

    for (int i = 0; i < 10; i++) {
        runNext(runner);
        Thread.sleep(200L);
    }

    runner.assertTransferCount(MonitorActivity.REL_INACTIVE, 10);
    runner.assertTransferCount(MonitorActivity.REL_ACTIVITY_RESTORED, 0);
    runner.assertTransferCount(MonitorActivity.REL_SUCCESS, 0);
    runner.clearTransferState();

    runner.enqueue(new byte[0], attributes);
    runNext(runner);

    runner.assertTransferCount(MonitorActivity.REL_INACTIVE, 0);
    runner.assertTransferCount(MonitorActivity.REL_ACTIVITY_RESTORED, 1);
    runner.assertTransferCount(MonitorActivity.REL_SUCCESS, 1);

    restoredFlowFile = runner.getFlowFilesForRelationship(MonitorActivity.REL_ACTIVITY_RESTORED).get(0);
    flowFileContent = new String(restoredFlowFile.toByteArray());
    Assert.assertTrue(Pattern.matches("Activity restored at time: (.*) after being inactive for 0 minutes", flowFileContent));
    restoredFlowFile.assertAttributeNotExists("key");
    restoredFlowFile.assertAttributeNotExists("key1");
}
 
Example 18
Source File: TestWait.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testWaitForTotalCount() throws InitializationException, IOException {
    Map<String, String> cachedAttributes = new HashMap<>();
    cachedAttributes.put("both", "notifyValue");
    cachedAttributes.put("uuid", "notifyUuid");
    cachedAttributes.put("notify.only", "notifyValue");

    // Setup existing cache entry.
    final WaitNotifyProtocol protocol = new WaitNotifyProtocol(service);
    protocol.notify("key", "counter-A", 1, cachedAttributes);

    runner.setProperty(Wait.RELEASE_SIGNAL_IDENTIFIER, "${releaseSignalAttribute}");
    runner.setProperty(Wait.TARGET_SIGNAL_COUNT, "${targetSignalCount}");

    final Map<String, String> waitAttributes = new HashMap<>();
    waitAttributes.put("releaseSignalAttribute", "key");
    waitAttributes.put("targetSignalCount", "3");
    waitAttributes.put("wait.only", "waitValue");
    waitAttributes.put("both", "waitValue");
    String flowFileContent = "content";
    runner.enqueue(flowFileContent.getBytes("UTF-8"), waitAttributes);

    /*
     * 1st iteration
     */
    runner.run();
    runner.assertAllFlowFilesTransferred(Wait.REL_WAIT, 1);
    MockFlowFile waitingFlowFile = runner.getFlowFilesForRelationship(Wait.REL_WAIT).get(0);
    waitingFlowFile.assertAttributeExists(Wait.WAIT_START_TIMESTAMP); // timestamp must be set
    String initialTimestamp = waitingFlowFile.getAttribute(Wait.WAIT_START_TIMESTAMP);

    /*
     * 2nd iteration.
     */
    runner.clearTransferState();
    runner.enqueue(waitingFlowFile);

    // Notify with other counter.
    protocol.notify("key", "counter-B", 1, cachedAttributes);

    runner.run();
    runner.assertAllFlowFilesTransferred(Wait.REL_WAIT, 1);
    // Still waiting since total count doesn't reach to 3.
    waitingFlowFile = runner.getFlowFilesForRelationship(Wait.REL_WAIT).get(0);
    waitingFlowFile.assertAttributeExists(Wait.WAIT_START_TIMESTAMP); // timestamp must be set
    waitingFlowFile.assertAttributeEquals(Wait.WAIT_START_TIMESTAMP, initialTimestamp); // timestamp must remain constant

    /*
     * 3rd iteration.
     */
    runner.clearTransferState();
    runner.enqueue(waitingFlowFile);

    runner.run();
    runner.assertAllFlowFilesTransferred(Wait.REL_WAIT, 1);
    // Still waiting since total count doesn't reach to 3.
    waitingFlowFile = runner.getFlowFilesForRelationship(Wait.REL_WAIT).get(0);
    waitingFlowFile.assertAttributeExists(Wait.WAIT_START_TIMESTAMP); // timestamp must be set
    waitingFlowFile.assertAttributeEquals(Wait.WAIT_START_TIMESTAMP, initialTimestamp); // timestamp must remain constant

    /*
     * 4th iteration.
     */
    runner.clearTransferState();
    runner.enqueue(waitingFlowFile);

    // Notify with other counter.
    protocol.notify("key", "counter-C", 1, cachedAttributes);

    runner.run();
    runner.assertAllFlowFilesTransferred(Wait.REL_SUCCESS, 1);

    final MockFlowFile outputFlowFile = runner.getFlowFilesForRelationship(Wait.REL_SUCCESS).get(0);
    outputFlowFile.assertAttributeNotExists(Wait.WAIT_START_TIMESTAMP);  // timestamp must be cleared

    // show a new attribute was copied from the cache
    assertEquals("notifyValue", outputFlowFile.getAttribute("notify.only"));
    // show that the original attributes are still there
    assertEquals("waitValue", outputFlowFile.getAttribute("wait.only"));
    // show that the original attribute is kept
    assertEquals("waitValue", outputFlowFile.getAttribute("both"));
    runner.clearTransferState();

    assertNull("The key no longer exist", protocol.getSignal("key"));
}
 
Example 19
Source File: TestExecuteSQL.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithOutputBatching() throws SQLException {
    // remove previous test database, if any
    final File dbLocation = new File(DB_LOCATION);
    dbLocation.delete();

    // load test data to database
    final Connection con = ((DBCPService) runner.getControllerService("dbcp")).getConnection();
    Statement stmt = con.createStatement();

    try {
        stmt.execute("drop table TEST_NULL_INT");
    } catch (final SQLException sqle) {
    }

    stmt.execute("create table TEST_NULL_INT (id integer not null, val1 integer, val2 integer, constraint my_pk primary key (id))");

    for (int i = 0; i < 1000; i++) {
        stmt.execute("insert into TEST_NULL_INT (id, val1, val2) VALUES (" + i + ", 1, 1)");
    }

    runner.setIncomingConnection(false);
    runner.setProperty(ExecuteSQL.MAX_ROWS_PER_FLOW_FILE, "5");
    runner.setProperty(ExecuteSQL.OUTPUT_BATCH_SIZE, "5");
    runner.setProperty(ExecuteSQL.SQL_SELECT_QUERY, "SELECT * FROM TEST_NULL_INT");
    runner.run();

    runner.assertAllFlowFilesTransferred(ExecuteSQL.REL_SUCCESS, 200);
    runner.assertAllFlowFilesContainAttribute(ExecuteSQL.REL_SUCCESS, FragmentAttributes.FRAGMENT_INDEX.key());
    runner.assertAllFlowFilesContainAttribute(ExecuteSQL.REL_SUCCESS, FragmentAttributes.FRAGMENT_ID.key());

    MockFlowFile firstFlowFile = runner.getFlowFilesForRelationship(ExecuteSQL.REL_SUCCESS).get(0);

    firstFlowFile.assertAttributeEquals(ExecuteSQL.RESULT_ROW_COUNT, "5");
    firstFlowFile.assertAttributeNotExists(FragmentAttributes.FRAGMENT_COUNT.key());
    firstFlowFile.assertAttributeEquals(FragmentAttributes.FRAGMENT_INDEX.key(), "0");
    firstFlowFile.assertAttributeEquals(ExecuteSQL.RESULTSET_INDEX, "0");

    MockFlowFile lastFlowFile = runner.getFlowFilesForRelationship(ExecuteSQL.REL_SUCCESS).get(199);

    lastFlowFile.assertAttributeEquals(ExecuteSQL.RESULT_ROW_COUNT, "5");
    lastFlowFile.assertAttributeEquals(FragmentAttributes.FRAGMENT_INDEX.key(), "199");
    lastFlowFile.assertAttributeEquals(ExecuteSQL.RESULTSET_INDEX, "0");
}
 
Example 20
Source File: TestLookupAttribute.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyValueLookupAttribute() throws InitializationException {
    final SimpleKeyValueLookupService service = new SimpleKeyValueLookupService();

    final TestRunner runner = TestRunners.newTestRunner(new LookupAttribute());
    runner.addControllerService("simple-key-value-lookup-service", service);
    runner.setProperty(service, "key1", "value1");
    runner.setProperty(service, "key2", "value2");
    runner.setProperty(service, "key3", "value3");
    runner.setProperty(service, "key4", "  ");
    runner.enableControllerService(service);
    runner.assertValid(service);
    runner.setProperty(LookupAttribute.LOOKUP_SERVICE, "simple-key-value-lookup-service");
    runner.setProperty(LookupAttribute.INCLUDE_EMPTY_VALUES, "true");
    runner.setProperty("foo", "key1");
    runner.setProperty("bar", "key2");
    runner.setProperty("baz", "${attr1}");
    runner.setProperty("qux", "key4");
    runner.setProperty("zab", "key5");
    runner.assertValid();

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("attr1", "key3");

    runner.enqueue("some content".getBytes(), attributes);
    runner.run(1, false);
    runner.assertAllFlowFilesTransferred(LookupAttribute.REL_MATCHED, 1);

    final MockFlowFile flowFile = runner.getFlowFilesForRelationship(LookupAttribute.REL_MATCHED).get(0);

    assertNotNull(flowFile);

    flowFile.assertAttributeExists("foo");
    flowFile.assertAttributeExists("bar");
    flowFile.assertAttributeExists("baz");
    flowFile.assertAttributeExists("qux");
    flowFile.assertAttributeExists("zab");
    flowFile.assertAttributeNotExists("zar");

    flowFile.assertAttributeEquals("foo", "value1");
    flowFile.assertAttributeEquals("bar", "value2");
    flowFile.assertAttributeEquals("baz", "value3");
    flowFile.assertAttributeEquals("qux", "");
    flowFile.assertAttributeEquals("zab", "null");
}