Java Code Examples for org.apache.nifi.util.TestRunner#setProperty()

The following examples show how to use org.apache.nifi.util.TestRunner#setProperty() . 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: TestPutHBaseJSON.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleJsonDocsRouteToFailure() throws IOException, InitializationException {
    final TestRunner runner = getTestRunner(DEFAULT_TABLE_NAME, DEFAULT_COLUMN_FAMILY, "1");
    final MockHBaseClientService hBaseClient = getHBaseClientService(runner);
    runner.setProperty(PutHBaseJSON.ROW_ID, DEFAULT_ROW);

    final String content1 = "{ \"field1\" : \"value1\", \"field2\" : \"value2\" }";
    final String content2 = "{ \"field3\" : \"value3\", \"field4\" : \"value4\" }";
    final String content = "[ " + content1 + " , " + content2 + " ]";

    runner.enqueue(content.getBytes(StandardCharsets.UTF_8));
    runner.run();
    runner.assertAllFlowFilesTransferred(PutHBaseCell.REL_FAILURE, 1);

    final MockFlowFile outFile = runner.getFlowFilesForRelationship(PutHBaseCell.REL_FAILURE).get(0);
    outFile.assertContentEquals(content);

    // should be no provenance events
    assertEquals(0, runner.getProvenanceEvents().size());

    // no puts should have made it to the client
    assertEquals(0, hBaseClient.getFlowFilePuts().size());
}
 
Example 2
Source File: TestState.java    From nifi-scripting-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Demonstrates reading and writing processor state values
 * @throws Exception
 */
@Test
public void testStateJavascript() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/state/state.js");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    StateManager stateManager = runner.getStateManager();
    stateManager.clear(Scope.CLUSTER);
    Map<String, String> initialStateValues = new HashMap<>();
    initialStateValues.put("some-state", "foo");
    stateManager.setState(initialStateValues, Scope.CLUSTER);

    runner.enqueue("sample text".getBytes(StandardCharsets.UTF_8));
    runner.run();

    runner.assertAllFlowFilesTransferred("success", 1);
    StateMap resultStateValues = stateManager.getState(Scope.CLUSTER);
    Assert.assertEquals("foobar", resultStateValues.get("some-state"));
}
 
Example 3
Source File: TestRouteOnAttribute.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleEquals() {
    final TestRunner runner = TestRunners.newTestRunner(new RouteOnAttribute());
    runner.setProperty("RouteA", "${a:equals('b')}");

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("a", "b");
    runner.enqueue(new byte[0], attributes);

    runner.run();

    runner.assertAllFlowFilesTransferred(new Relationship.Builder().name("RouteA").build(), 1);
    final List<MockFlowFile> flowFiles = runner.getFlowFilesForRelationship("RouteA");
    flowFiles.get(0).assertAttributeEquals("a", "b");
    flowFiles.get(0).assertAttributeEquals(RouteOnAttribute.ROUTE_ATTRIBUTE_KEY, "RouteA");
}
 
Example 4
Source File: TestConvertJSONToSQL.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testInsertBoolToInteger() 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, "INSERT");
    runner.enqueue(Paths.get("src/test/resources/TestConvertJSONToSQL/person-with-bool.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", "Bool");
    out.assertAttributeEquals("sql.args.3.type", String.valueOf(java.sql.Types.INTEGER));
    out.assertAttributeEquals("sql.args.3.value", "1");

    out.assertContentEquals("INSERT INTO PERSONS (ID, NAME, CODE) VALUES (?, ?, ?)");
}
 
Example 5
Source File: PutHDFSTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutFileWithGSSException() throws IOException {
    FileSystem noCredentialsFileSystem = new MockFileSystem() {
        @Override
        public FileStatus getFileStatus(Path path) throws IOException {
            throw new IOException("ioe", new SaslException("sasle", new GSSException(13)));
        }
    };
    TestRunner runner = TestRunners.newTestRunner(new TestablePutHDFS(kerberosProperties, noCredentialsFileSystem));
    runner.setProperty(PutHDFS.DIRECTORY, "target/test-classes");
    runner.setProperty(PutHDFS.CONFLICT_RESOLUTION, "replace");

    try (FileInputStream fis = new FileInputStream("src/test/resources/testdata/randombytes-1")) {
        Map<String, String> attributes = new HashMap<>();
        attributes.put(CoreAttributes.FILENAME.key(), "randombytes-1");
        runner.enqueue(fis, attributes);
        runner.run();
    }

    // assert no flowfiles transferred to outgoing relationships
    runner.assertTransferCount(PutHDFS.REL_SUCCESS, 0);
    runner.assertTransferCount(PutHDFS.REL_FAILURE, 0);
    // assert the input flowfile was penalized
    List<MockFlowFile> penalizedFlowFiles = runner.getPenalizedFlowFiles();
    assertEquals(1, penalizedFlowFiles.size());
    assertEquals("randombytes-1", penalizedFlowFiles.iterator().next().getAttribute(CoreAttributes.FILENAME.key()));
    // assert the processor's queue is not empty
    assertFalse(runner.isQueueEmpty());
    assertEquals(1, runner.getQueueSize().getObjectCount());
    // assert the input file is back on the queue
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    FlowFile queuedFlowFile = session.get();
    assertNotNull(queuedFlowFile);
    assertEquals("randombytes-1", queuedFlowFile.getAttribute(CoreAttributes.FILENAME.key()));
    session.rollback();
}
 
Example 6
Source File: TestCSVToAvroProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicConversion() throws IOException {
    TestRunner runner = TestRunners.newTestRunner(ConvertCSVToAvro.class);
    runner.assertNotValid();
    runner.setProperty(ConvertCSVToAvro.SCHEMA, SCHEMA.toString());
    runner.assertValid();

    runner.enqueue(streamFor(CSV_CONTENT));
    runner.run();

    long converted = runner.getCounterValue("Converted records");
    long errors = runner.getCounterValue("Conversion errors");
    Assert.assertEquals("Should convert 2 rows", 2, converted);
    Assert.assertEquals("Should reject 1 row", 1, errors);

    runner.assertTransferCount("success", 1);
    runner.assertTransferCount("failure", 0);
    runner.assertTransferCount("incompatible", 1);

    MockFlowFile incompatible = runner.getFlowFilesForRelationship("incompatible").get(0);
    String failureContent = new String(runner.getContentAsByteArray(incompatible),
            StandardCharsets.UTF_8);
    Assert.assertEquals("Should reject an invalid string and double",
            CSV_CONTENT, failureContent);
    Assert.assertEquals("Should accumulate error messages",
            FAILURE_SUMMARY, incompatible.getAttribute("errors"));
}
 
Example 7
Source File: TestContent.java    From nifi-scripting-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Demonstrates splitting an array in a single incoming FlowFile into multiple output FlowFiles
 * @throws Exception
 */
@Test
public void testSplitJavascript() throws Exception {
    final TestRunner runner = TestRunners.newTestRunner(new ExecuteScript());
    runner.setValidateExpressionUsage(false);
    runner.setProperty(SCRIPT_ENGINE, "ECMAScript");
    runner.setProperty(ScriptingComponentUtils.SCRIPT_FILE, "src/test/resources/executescript/content/split.js");
    runner.setProperty(ScriptingComponentUtils.MODULES, "src/test/resources/executescript");
    runner.assertValid();

    String inputContent = "[";
    inputContent += "{ \"color\": \"blue\" },";
    inputContent += "{ \"color\": \"green\" },";
    inputContent += "{ \"color\": \"red\" }";
    inputContent += "]";
    runner.enqueue(inputContent.getBytes(StandardCharsets.UTF_8));
    runner.run();

    MockComponentLog log = runner.getLogger();
    List<LogMessage> infoMessages = log.getInfoMessages();

    runner.assertAllFlowFilesTransferred("success", 3);
    final List<MockFlowFile> successFlowFiles = runner.getFlowFilesForRelationship("success");
    MockFlowFile blueFlowFile = successFlowFiles.get(0);
    blueFlowFile.assertAttributeEquals("color", "blue");
    MockFlowFile greenFlowFile = successFlowFiles.get(1);
    greenFlowFile.assertAttributeEquals("color", "green");
    MockFlowFile redFlowFile = successFlowFiles.get(2);
    redFlowFile.assertAttributeEquals("color", "red");
}
 
Example 8
Source File: TestValidateXml.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalid() throws IOException, SAXException {
    final TestRunner runner = TestRunners.newTestRunner(new ValidateXml());
    runner.setProperty(ValidateXml.SCHEMA_FILE, "src/test/resources/TestXml/XmlBundle.xsd");

    runner.enqueue("<this>is an invalid</xml>");
    runner.run();

    runner.assertAllFlowFilesTransferred(ValidateXml.REL_INVALID, 1);
    runner.assertAllFlowFilesContainAttribute(ValidateXml.REL_INVALID, ValidateXml.ERROR_ATTRIBUTE_KEY);
}
 
Example 9
Source File: PutGCSObjectIT.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutWithAcl() throws Exception {
    final TestRunner runner = buildNewRunner(new PutGCSObject());
    runner.setProperty(PutGCSObject.BUCKET, BUCKET);
    runner.setProperty(PutGCSObject.KEY, KEY);
    runner.setProperty(PutGCSObject.ACL, PutGCSObject.ACL_BUCKET_OWNER_READ);

    runner.enqueue(CONTENT);

    runner.run();

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

    final Blob blob = storage.get(BlobId.of(BUCKET, KEY));

    boolean userIsOwner = false;
    boolean projectOwnerIsReader = false;
    for (Acl acl : blob.listAcls()) {
        if (acl.getEntity().getType() == Acl.Entity.Type.USER
                && acl.getRole() == Acl.Role.OWNER) {
            userIsOwner = true;
        }

        if (acl.getEntity().getType() == Acl.Entity.Type.PROJECT
                && acl.getRole() == Acl.Role.READER) {
            projectOwnerIsReader = true;
        }
    }

    assertTrue(userIsOwner);
    assertTrue(projectOwnerIsReader);
}
 
Example 10
Source File: TestEvaluateJsonPath.java    From nifi with 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);
}
 
Example 11
Source File: TestAttributesToJSON.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testAttribute_singleNonExistingUserDefinedAttribute() throws Exception {
    final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToJSON());
    testRunner.setProperty(AttributesToJSON.ATTRIBUTES_LIST, "NonExistingAttribute");
    testRunner.setProperty(AttributesToJSON.DESTINATION, AttributesToJSON.DESTINATION_ATTRIBUTE);

    ProcessSession session = testRunner.getProcessSessionFactory().createSession();
    FlowFile ff = session.create();
    ff = session.putAttribute(ff, TEST_ATTRIBUTE_KEY, TEST_ATTRIBUTE_VALUE);

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

    testRunner.getFlowFilesForRelationship(AttributesToJSON.REL_SUCCESS).get(0)
            .assertAttributeExists(AttributesToJSON.JSON_ATTRIBUTE_NAME);
    testRunner.assertTransferCount(AttributesToJSON.REL_SUCCESS, 1);
    testRunner.assertTransferCount(AttributesToJSON.REL_FAILURE, 0);

    String json = testRunner.getFlowFilesForRelationship(AttributesToJSON.REL_SUCCESS)
            .get(0).getAttribute(AttributesToJSON.JSON_ATTRIBUTE_NAME);

    ObjectMapper mapper = new ObjectMapper();
    Map<String, String> val = mapper.readValue(json, HashMap.class);

    //If a Attribute is requested but does not exist then it is placed in the JSON with an empty string
    assertTrue(val.get("NonExistingAttribute").equals(""));
    assertTrue(val.size() == 1);
}
 
Example 12
Source File: TestParseSyslog5424.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidMessage() {
    final TestRunner runner = TestRunners.newTestRunner(new ParseSyslog5424());
    runner.setProperty(ParseSyslog5424.NIL_POLICY,NilPolicy.DASH.name());
    runner.enqueue(SYSLOG_LINE_ALL.getBytes());
    runner.run();
    runner.assertAllFlowFilesTransferred(ParseSyslog5424.REL_SUCCESS,1);
}
 
Example 13
Source File: TestConvertAvroToJSON.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleSchemalessAvroMessage_wrapSingleMessage() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new ConvertAvroToJSON());
    runner.setProperty(ConvertAvroToJSON.CONTAINER_OPTIONS, ConvertAvroToJSON.CONTAINER_ARRAY);
    runner.setProperty(ConvertAvroToJSON.WRAP_SINGLE_RECORD, Boolean.toString(true));
    Schema schema = new Schema.Parser().parse(new File("src/test/resources/user.avsc"));
    String stringSchema = schema.toString();
    runner.setProperty(ConvertAvroToJSON.SCHEMA, stringSchema);

    final GenericRecord user1 = new GenericData.Record(schema);
    user1.put("name", "Alyssa");
    user1.put("favorite_number", 256);

    final ByteArrayOutputStream out1 = new ByteArrayOutputStream();
    final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out1, null);
    final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    datumWriter.write(user1, encoder);

    encoder.flush();
    out1.flush();
    byte[] test = out1.toByteArray();
    runner.enqueue(test);

    runner.run();

    runner.assertAllFlowFilesTransferred(ConvertAvroToJSON.REL_SUCCESS, 1);
    final MockFlowFile out = runner.getFlowFilesForRelationship(ConvertAvroToJSON.REL_SUCCESS).get(0);
    out.assertContentEquals("[{\"name\": \"Alyssa\", \"favorite_number\": 256, \"favorite_color\": null}]");
}
 
Example 14
Source File: TestHBase_2_ClientMapCacheService.java    From nifi with Apache License 2.0 5 votes vote down vote up
private AtomicDistributedMapCacheClient<byte[]> configureHBaseCacheService(final TestRunner runner, final HBaseClientService service) throws InitializationException {
    final HBase_2_ClientMapCacheService cacheService = new HBase_2_ClientMapCacheService();
    runner.addControllerService("hbaseCache", cacheService);
    runner.setProperty(cacheService, HBase_2_ClientMapCacheService.HBASE_CLIENT_SERVICE, "hbaseClient");
    runner.setProperty(cacheService, HBase_2_ClientMapCacheService.HBASE_CACHE_TABLE_NAME, tableName);
    runner.setProperty(cacheService, HBase_2_ClientMapCacheService.HBASE_COLUMN_FAMILY, columnFamily);
    runner.setProperty(cacheService, HBase_2_ClientMapCacheService.HBASE_COLUMN_QUALIFIER, columnQualifier);
    runner.enableControllerService(cacheService);
    runner.setProperty(TestProcessor.HBASE_CACHE_SERVICE,"hbaseCache");
    return cacheService;
}
 
Example 15
Source File: TestSplitText.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleHeaderIndicators() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new SplitText());
    runner.setProperty(SplitText.HEADER_LINE_COUNT, "1");
    runner.setProperty(SplitText.HEADER_MARKER, "Head");
    runner.setProperty(SplitText.LINE_SPLIT_COUNT, "5");
    runner.setProperty(SplitText.REMOVE_TRAILING_NEWLINES, "false");

    runner.enqueue(file);
    runner.run();

    runner.assertTransferCount(SplitText.REL_FAILURE, 0);
    runner.assertTransferCount(SplitText.REL_ORIGINAL, 1);
    runner.getFlowFilesForRelationship(SplitText.REL_ORIGINAL).get(0).assertAttributeEquals("fragment.count", "3");
    runner.assertTransferCount(SplitText.REL_SPLITS, 3);

    final List<MockFlowFile> splits = runner.getFlowFilesForRelationship(SplitText.REL_SPLITS);
    splits.get(0).assertAttributeEquals(SplitText.SPLIT_LINE_COUNT, "5");
    splits.get(0).assertAttributeEquals(SplitText.FRAGMENT_SIZE, "62");
    splits.get(1).assertAttributeEquals(SplitText.SPLIT_LINE_COUNT, "5");
    splits.get(1).assertAttributeEquals(SplitText.FRAGMENT_SIZE, "55");
    splits.get(2).assertAttributeEquals(SplitText.SPLIT_LINE_COUNT, "1");
    splits.get(2).assertAttributeEquals(SplitText.FRAGMENT_SIZE, "23");
    final String fragmentUUID = splits.get(0).getAttribute(SplitText.FRAGMENT_ID);
    for (int i = 0; i < splits.size(); i++) {
        final MockFlowFile split = splits.get(i);
        split.assertAttributeEquals(SplitText.FRAGMENT_INDEX, String.valueOf(i + 1));
        split.assertAttributeEquals(SplitText.FRAGMENT_ID, fragmentUUID);
        split.assertAttributeEquals(SplitText.FRAGMENT_COUNT, String.valueOf(splits.size()));
        split.assertAttributeEquals(SplitText.SEGMENT_ORIGINAL_FILENAME, file.getFileName().toString());
    }
}
 
Example 16
Source File: TestSplitText.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testZeroByteInput() throws IOException {
    final TestRunner runner = TestRunners.newTestRunner(new SplitText());
    runner.setProperty(SplitText.HEADER_LINE_COUNT, "1");
    runner.setProperty(SplitText.LINE_SPLIT_COUNT, "1");

    runner.enqueue("".getBytes());
    runner.run();
    runner.assertTransferCount(SplitText.REL_SPLITS, 0);
    runner.assertTransferCount(SplitText.REL_ORIGINAL, 1);
    runner.assertTransferCount(SplitText.REL_FAILURE, 0);
}
 
Example 17
Source File: TestServerAndClient.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testPersistentMapServerAndClientWithLFUEvictions() throws InitializationException, IOException {
    /**
     * This bypasses the test for build environments in OS X running Java 1.8 due to a JVM bug
     * See:  https://issues.apache.org/jira/browse/NIFI-437
     */
    Assume.assumeFalse("test is skipped due to build environment being OS X with JDK 1.8. See https://issues.apache.org/jira/browse/NIFI-437",
        SystemUtils.IS_OS_MAC && SystemUtils.IS_JAVA_1_8);

    LOGGER.info("Testing " + Thread.currentThread().getStackTrace()[1].getMethodName());
    // Create server
    final File dataFile = new File("target/cache-data");
    deleteRecursively(dataFile);

    // Create server
    final TestRunner runner = TestRunners.newTestRunner(Mockito.mock(Processor.class));
    final DistributedMapCacheServer server = new MapServer();
    runner.addControllerService("server", server);
    runner.setProperty(server, DistributedMapCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.setProperty(server, DistributedMapCacheServer.MAX_CACHE_ENTRIES, "3");
    runner.setProperty(server, DistributedMapCacheServer.EVICTION_POLICY, DistributedMapCacheServer.EVICTION_STRATEGY_LFU);
    runner.enableControllerService(server);

    DistributedMapCacheClientService client = createMapClient(server.getPort());
    final Serializer<String> serializer = new StringSerializer();
    final boolean added = client.putIfAbsent("test", "1", serializer, serializer);
    waitABit();
    final boolean added2 = client.putIfAbsent("test2", "2", serializer, serializer);
    waitABit();
    final boolean added3 = client.putIfAbsent("test3", "3", serializer, serializer);
    waitABit();
    assertTrue(added);
    assertTrue(added2);
    assertTrue(added3);

    final boolean contains = client.containsKey("test", serializer);
    final boolean contains2 = client.containsKey("test2", serializer);
    assertTrue(contains);
    assertTrue(contains2);

    final boolean addedAgain = client.putIfAbsent("test", "1", serializer, serializer);
    assertFalse(addedAgain);

    final boolean added4 = client.putIfAbsent("test4", "4", serializer, serializer);
    assertTrue(added4);

    // ensure that added3 was evicted because it was used least frequently
    assertFalse(client.containsKey("test3", serializer));

    server.shutdownServer();

    final DistributedMapCacheServer newServer = new MapServer();
    runner.addControllerService("server2", newServer);
    runner.setProperty(newServer, DistributedMapCacheServer.PERSISTENCE_PATH, dataFile.getAbsolutePath());
    runner.enableControllerService(newServer);
    client.close();
    client = createMapClient(newServer.getPort());

    assertTrue(client.containsKey("test", serializer));
    assertTrue(client.containsKey("test2", serializer));
    assertFalse(client.containsKey("test3", serializer));
    assertTrue(client.containsKey("test4", serializer));

    newServer.shutdownServer();
    client.close();
}
 
Example 18
Source File: TestUpdateAttribute.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleRuleHitsWithNoFlowFilePolicySpecified() throws Exception {
    final Criteria criteria = getCriteria();
    addRule(criteria, "rule 1", Arrays.asList(
            // conditions
            "${attribute.1:equals('value.1')}"), getMap(
                    // actions
                    "attribute.2", "value.2"));
    addRule(criteria, "rule 2", Arrays.asList(
            // conditions
            "${attribute.1:equals('value.1')}"), getMap(
                    // actions
                    "attribute.3", "value.3"));

    final TestRunner runner = TestRunners.newTestRunner(new UpdateAttribute());
    runner.setAnnotationData(serialize(criteria));
    runner.setProperty("attribute.2", "default.value.2");

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("attribute.1", "value.1");
    runner.enqueue(TEST_CONTENT.getBytes(StandardCharsets.UTF_8), attributes);

    runner.run();

    runner.assertAllFlowFilesTransferred(UpdateAttribute.REL_SUCCESS, 2);
    final List<MockFlowFile> result = runner.getFlowFilesForRelationship(UpdateAttribute.REL_SUCCESS);

    final MockFlowFile flowfile1 = result.get(0);
    final MockFlowFile flowfile2 = result.get(1);

    // ensure the attributes are as expected
    if ("rule 1".equals(flowfile1.getAttribute(runner.getProcessor().getClass().getSimpleName() + ".matchedRule"))) {
        flowfile1.assertAttributeEquals("attribute.2", "value.2");
        flowfile2.assertAttributeEquals("attribute.3", "value.3");
        flowfile2.assertAttributeEquals("attribute.2", "default.value.2");
    } else {
        flowfile2.assertAttributeEquals("attribute.2", "value.2");
        flowfile1.assertAttributeEquals("attribute.3", "value.3");
        flowfile1.assertAttributeEquals("attribute.2", "default.value.2");
    }

    // ensure the content was copied as well
    flowfile1.assertContentEquals(TEST_CONTENT.getBytes(StandardCharsets.UTF_8));
    flowfile2.assertContentEquals(TEST_CONTENT.getBytes(StandardCharsets.UTF_8));
}
 
Example 19
Source File: TestAttributesToCSV.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testAttrListWithCommasInNameFromExpCoreNullOffToAttribute() throws IOException {
    final TestRunner testRunner = TestRunners.newTestRunner(new AttributesToCSV());
    testRunner.setProperty(AttributesToCSV.DESTINATION, OUTPUT_NEW_ATTRIBUTE);
    testRunner.setProperty(AttributesToCSV.INCLUDE_CORE_ATTRIBUTES, "true");
    testRunner.setProperty(AttributesToCSV.ATTRIBUTES_LIST, "${myAttribs}");
    testRunner.setProperty(AttributesToCSV.NULL_VALUE_FOR_EMPTY_STRING, "false");


    Map<String, String> attrsCommaInName = new HashMap<String, String>(){{
        put("beach,name", "Malibu Beach");
        put("beach,location", "California, US");
        put("beach,endorsement", "This is our family's favorite beach. We highly recommend it. \n\nThanks, Jim");
        put("attribute-should-be-eliminated", "This should not be in CSVData!");
        put("myAttribs", "\"beach,name\",\"beach,location\",\"beach,endorsement\"");
    }};

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

    List<MockFlowFile> flowFilesForRelationship = testRunner.getFlowFilesForRelationship(AttributesToCSV.REL_SUCCESS);

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

    //Test flow file 0 with ATTRIBUTE_LIST populated from expression language
    MockFlowFile flowFile = flowFilesForRelationship.get(0);

    assertNull(flowFile.getAttribute(CoreAttributes.MIME_TYPE.key()));

    String attributeData = flowFile.getAttribute(OUTPUT_ATTRIBUTE_NAME);

    Set<String> CSVDataValues = new HashSet<>(getStrings(attributeData));

    assertEquals(6, CSVDataValues.size());

    assertTrue(CSVDataValues.contains("Malibu Beach"));
    assertTrue(CSVDataValues.contains("\"California, US\""));
    assertTrue(CSVDataValues.contains("\"This is our family's favorite beach. We highly recommend it. \n\nThanks, Jim\""));

    assertTrue(CSVDataValues.contains(flowFile.getAttribute("filename")));
    assertTrue(CSVDataValues.contains(flowFile.getAttribute("path")));
    assertTrue(CSVDataValues.contains(flowFile.getAttribute("uuid")));

    //Test flow file 1 with ATTRIBUTE_LIST populated from expression language containing commas (output should be he same)
    flowFile = flowFilesForRelationship.get(0);

    assertNull(flowFile.getAttribute(CoreAttributes.MIME_TYPE.key()));

    attributeData = flowFile.getAttribute(OUTPUT_ATTRIBUTE_NAME);

    CSVDataValues = new HashSet<>(getStrings(attributeData));

    assertEquals(6, CSVDataValues.size());

    assertTrue(CSVDataValues.contains("Malibu Beach"));
    assertTrue(CSVDataValues.contains("\"California, US\""));
    assertTrue(CSVDataValues.contains("\"This is our family's favorite beach. We highly recommend it. \n\nThanks, Jim\""));

    assertTrue(CSVDataValues.contains(flowFile.getAttribute("filename")));
    assertTrue(CSVDataValues.contains(flowFile.getAttribute("path")));
    assertTrue(CSVDataValues.contains(flowFile.getAttribute("uuid")));

}
 
Example 20
Source File: ListGCSBucketTest.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected void addRequiredPropertiesToRunner(TestRunner runner) {
    runner.setProperty(ListGCSBucket.BUCKET, BUCKET);
}