com.sysunite.weaver.nifi.FilterXMLNodesTest.java Source code

Java tutorial

Introduction

Here is the source code for com.sysunite.weaver.nifi.FilterXMLNodesTest.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sysunite.weaver.nifi;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class FilterXMLNodesTest {

    private TestRunner testRunner;

    @Before
    public void init() {
        testRunner = TestRunners.newTestRunner(FilterXMLNodes.class);
    }

    @Test
    public void testProcessor() {

    }

    @Test
    public void testOnTrigger() {

        try {
            String file = "slagboom.xml";

            byte[] contents = FileUtils
                    .readFileToByteArray(new File(getClass().getClassLoader().getResource(file).getFile()));

            InputStream in = new ByteArrayInputStream(contents);

            InputStream cont = new ByteArrayInputStream(IOUtils.toByteArray(in));

            // Generate a test runner to mock a processor in a flow
            TestRunner runner = TestRunners.newTestRunner(new FilterXMLNodes());

            // Add properites
            runner.setProperty(FilterXMLNodes.PROP_NODE, "FunctionalPhysicalObject");
            runner.setProperty(FilterXMLNodes.PROP_NODE_ATTRIBUTE, "name");
            runner.setProperty(FilterXMLNodes.PROP_PREGMATCH, "(.*)AB(.*)CT(.*)");

            // Add the content to the runner
            runner.enqueue(cont);

            // Run the enqueued content, it also takes an int = number of contents queued
            runner.run();

            // All results were processed with out failure
            //runner.assertQueueEmpty();

            // If you need to read or do aditional tests on results you can access the content
            List<MockFlowFile> results = runner.getFlowFilesForRelationship(FilterXMLNodes.MY_RELATIONSHIP);
            //assertTrue("1 match", results.size() == 1);

            System.out.println("aantal gevonden: " + results.size());

            //MockFlowFile result = results.get(0);
            //String resultValue = new String(runner.getContentAsByteArray(result));
            //System.out.println("Match: " + IOUtils.toString(runner.getContentAsByteArray(result)));
        } catch (IOException e) {
            System.out.println("FOUT!!");
            System.out.println(e.getStackTrace());
        }
    }

}