Search in sources :

Example 21 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class TestPutCouchbaseKey method testDocIdExp.

@Test
public void testDocIdExp() throws Exception {
    String docIdExp = "${'someProperty'}";
    String somePropertyValue = "doc-p";
    String inFileData = "{\"key\":\"value\"}";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);
    Bucket bucket = mock(Bucket.class);
    when(bucket.upsert(any(RawJsonDocument.class), eq(PersistTo.NONE), eq(ReplicateTo.NONE))).thenReturn(RawJsonDocument.create(somePropertyValue, inFileData));
    setupMockBucket(bucket);
    testRunner.setProperty(DOC_ID, docIdExp);
    Map<String, String> properties = new HashMap<>();
    properties.put("someProperty", somePropertyValue);
    testRunner.enqueue(inFileDataBytes, properties);
    testRunner.run();
    ArgumentCaptor<RawJsonDocument> capture = ArgumentCaptor.forClass(RawJsonDocument.class);
    verify(bucket, times(1)).upsert(capture.capture(), eq(PersistTo.NONE), eq(ReplicateTo.NONE));
    assertEquals(somePropertyValue, capture.getValue().id());
    assertEquals(inFileData, capture.getValue().content());
    testRunner.assertTransferCount(REL_SUCCESS, 1);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile outFile = testRunner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    outFile.assertContentEquals(inFileData);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Bucket(com.couchbase.client.java.Bucket) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) RawJsonDocument(com.couchbase.client.java.document.RawJsonDocument) Test(org.junit.Test)

Example 22 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class TestPutCouchbaseKey method testDurabilityConstraint.

@Test
public void testDurabilityConstraint() throws Exception {
    String docId = "doc-a";
    String inFileData = "{\"key\":\"value\"}";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);
    Bucket bucket = mock(Bucket.class);
    when(bucket.upsert(any(RawJsonDocument.class), eq(PersistTo.MASTER), eq(ReplicateTo.ONE))).thenReturn(RawJsonDocument.create(docId, inFileData));
    setupMockBucket(bucket);
    testRunner.enqueue(inFileDataBytes);
    testRunner.setProperty(DOC_ID, docId);
    testRunner.setProperty(PutCouchbaseKey.PERSIST_TO, PersistTo.MASTER.toString());
    testRunner.setProperty(PutCouchbaseKey.REPLICATE_TO, ReplicateTo.ONE.toString());
    testRunner.run();
    verify(bucket, times(1)).upsert(any(RawJsonDocument.class), eq(PersistTo.MASTER), eq(ReplicateTo.ONE));
    testRunner.assertAllFlowFilesTransferred(REL_SUCCESS);
    testRunner.assertTransferCount(REL_SUCCESS, 1);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile outFile = testRunner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    outFile.assertContentEquals(inFileData);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Bucket(com.couchbase.client.java.Bucket) Matchers.anyString(org.mockito.Matchers.anyString) RawJsonDocument(com.couchbase.client.java.document.RawJsonDocument) Test(org.junit.Test)

Example 23 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class TestCompareFuzzyHash method testMissingAttribute.

@Test
public void testMissingAttribute() {
    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));
    runner.setProperty(CompareFuzzyHash.MATCHING_MODE, CompareFuzzyHash.multiMatch.getValue());
    runner.enqueue("bogus".getBytes());
    runner.run();
    runner.assertQueueEmpty();
    runner.assertAllFlowFilesTransferred(CompareFuzzyHash.REL_FAILURE, 1);
    final MockFlowFile outFile = runner.getFlowFilesForRelationship(CompareFuzzyHash.REL_FAILURE).get(0);
    outFile.assertAttributeNotExists("fuzzyhash.value.0.match");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Test(org.junit.Test)

Example 24 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class TestCompareFuzzyHash method testAttributeIsEmptyString.

@Test
public void testAttributeIsEmptyString() {
    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));
    runner.setProperty(CompareFuzzyHash.MATCHING_MODE, CompareFuzzyHash.multiMatch.getValue());
    Map<String, String> attributes = new HashMap<>();
    attributes.put("fuzzyhash.value", "");
    runner.enqueue("bogus".getBytes(), attributes);
    runner.run();
    runner.assertQueueEmpty();
    runner.assertAllFlowFilesTransferred(CompareFuzzyHash.REL_FAILURE, 1);
    final MockFlowFile outFile = runner.getFlowFilesForRelationship(CompareFuzzyHash.REL_FAILURE).get(0);
    outFile.assertAttributeNotExists("fuzzyhash.value.0.match");
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 25 with MockFlowFile

use of org.apache.nifi.util.MockFlowFile in project nifi by apache.

the class TestCompareFuzzyHash method testTLSHCompareFuzzyHashMultipleMatches.

@Test
public void testTLSHCompareFuzzyHashMultipleMatches() {
    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/tlsh.list");
    runner.setProperty(CompareFuzzyHash.MATCH_THRESHOLD, String.valueOf(matchingSimilarity));
    runner.setProperty(CompareFuzzyHash.MATCHING_MODE, CompareFuzzyHash.multiMatch.getValue());
    Map<String, String> attributes = new HashMap<>();
    attributes.put("fuzzyhash.value", tlshInput);
    runner.enqueue("bogus".getBytes(), attributes);
    runner.run();
    runner.assertQueueEmpty();
    runner.assertAllFlowFilesTransferred(CompareFuzzyHash.REL_FOUND, 1);
    final MockFlowFile outFile = runner.getFlowFilesForRelationship(CompareFuzzyHash.REL_FOUND).get(0);
    outFile.assertAttributeEquals("fuzzyhash.value.0.match", "nifi-nar-bundles/nifi-lumberjack-bundle/nifi-lumberjack-processors/pom.xml");
    double similarity = Double.valueOf(outFile.getAttribute("fuzzyhash.value.0.similarity"));
    Assert.assertTrue(similarity <= matchingSimilarity);
    outFile.assertAttributeEquals("fuzzyhash.value.1.match", "nifi-nar-bundles/nifi-beats-bundle/nifi-beats-processors/pom.xml");
    similarity = Double.valueOf(outFile.getAttribute("fuzzyhash.value.1.similarity"));
    Assert.assertTrue(similarity <= matchingSimilarity);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

MockFlowFile (org.apache.nifi.util.MockFlowFile)1228 Test (org.junit.Test)1198 TestRunner (org.apache.nifi.util.TestRunner)725 HashMap (java.util.HashMap)279 File (java.io.File)155 Matchers.anyString (org.mockito.Matchers.anyString)59 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)48 FlowFile (org.apache.nifi.flowfile.FlowFile)42 ByteArrayInputStream (java.io.ByteArrayInputStream)41 ProcessContext (org.apache.nifi.processor.ProcessContext)37 InputStream (java.io.InputStream)34 Connection (java.sql.Connection)31 GenericRecord (org.apache.avro.generic.GenericRecord)31 Statement (java.sql.Statement)30 Schema (org.apache.avro.Schema)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)27 SQLException (java.sql.SQLException)25 DBCPService (org.apache.nifi.dbcp.DBCPService)25 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)24