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

Java tutorial

Introduction

Here is the source code for com.sysunite.weaver.nifi.CreateObjectPropertyTest.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 com.weaverplatform.sdk.Weaver;
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;

import static org.junit.Assert.assertEquals;

public class CreateObjectPropertyTest {

    private TestRunner testRunner;
    public Weaver weaver;

    @Before
    public void init() {
        testRunner = TestRunners.newTestRunner(CreateObjectProperty.class);
        weaver = new Weaver();
        weaver.connect("http://localhost:9487");
    }

    @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 CreateObjectProperty());

            // Add properites
            runner.setProperty(CreateObjectProperty.PROP_NODE, "FunctionalPhysicalObject");
            runner.setProperty(CreateObjectProperty.PROP_NODE_ATTRIBUTE, "id");

            runner.setProperty(CreateObjectProperty.PROP_CHILDNODE, "HasAsSubject");
            runner.setProperty(CreateObjectProperty.PROP_CHILDNODE_ATTRIBUTE, "noattr");
            //optional
            runner.setProperty(CreateObjectProperty.PROP_CHILDNODE_SUB, "Geometry");
            runner.setProperty(CreateObjectProperty.PROP_CHILDNODE_SUB_ATTRIBUTE, "id");

            // 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(CreateObjectProperty.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));
            //
            //
            //            //check weaver
            //            String checkValue = weaver.get("816ee370-4274-e211-a3a8-b8ac6f902f00").toString();
            //
            //            assertEquals(checkValue, resultValue);

            //System.out.println("Match: " + IOUtils.toString(runner.getContentAsByteArray(result)));
        } catch (IOException e) {
            System.out.println("FOUT!!");
            System.out.println(e.getStackTrace());
        }
    }

}