Search in sources :

Example 1 with Query

use of com.influxdb.client.domain.Query in project nifi-influxdb-bundle by influxdata.

the class AbstractTestGetInfluxDatabase_2 method before.

@Before
public void before() throws IOException, GeneralSecurityException, InitializationException {
    InfluxDBClient mockInfluxDBClient = Mockito.mock(InfluxDBClient.class);
    mockQueryApi = Mockito.mock(QueryApi.class);
    Mockito.doAnswer(invocation -> mockQueryApi).when(mockInfluxDBClient).getQueryApi();
    Mockito.doAnswer(invocation -> {
        if (queryOnErrorValue != null) {
            // noinspection unchecked
            Consumer<Exception> onError = invocation.getArgument(3, Consumer.class);
            onError.accept(queryOnErrorValue);
        }
        queryOnResponseRecords.forEach(record -> {
            // noinspection unchecked
            BiConsumer<Cancellable, String> onRecord = invocation.getArgument(2, BiConsumer.class);
            onRecord.accept(Mockito.mock(Cancellable.class), record);
        });
        boolean wasException = queryOnErrorValue != null;
        try {
            return queryAnswer.answer(invocation);
        } catch (Exception e) {
            wasException = true;
            throw e;
        } finally {
            if (!wasException) {
                Runnable onComplete = invocation.getArgument(4, Runnable.class);
                onComplete.run();
            }
        }
    }).when(mockQueryApi).queryRaw(Mockito.any(Query.class), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any());
    processor = Mockito.spy(new GetInfluxDatabase_2());
    runner = TestRunners.newTestRunner(processor);
    runner.setProperty(GetInfluxDatabase_2.ORG, "my-org");
    runner.setProperty(GetInfluxDatabase_2.QUERY, "from(bucket:\"my-bucket\") |> range(start: 0) |> last()");
    runner.setProperty(GetInfluxDatabase_2.INFLUX_DB_SERVICE, "influxdb-service");
    InfluxDatabaseService_2 influxDatabaseService = Mockito.spy(new StandardInfluxDatabaseService_2());
    Mockito.doAnswer(invocation -> mockInfluxDBClient).when(influxDatabaseService).create();
    runner.addControllerService("influxdb-service", influxDatabaseService);
    runner.setProperty(influxDatabaseService, INFLUX_DB_ACCESS_TOKEN, "my-token");
    runner.enableControllerService(influxDatabaseService);
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    logger = initContext.getLogger();
    processor.initialize(initContext);
    processor.onScheduled(runner.getProcessContext());
    processor.initWriterFactory(runner.getProcessContext());
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) QueryApi(com.influxdb.client.QueryApi) Query(com.influxdb.client.domain.Query) InfluxDatabaseService_2(org.influxdata.nifi.services.InfluxDatabaseService_2) StandardInfluxDatabaseService_2(org.influxdata.nifi.services.StandardInfluxDatabaseService_2) Cancellable(com.influxdb.Cancellable) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) StandardInfluxDatabaseService_2(org.influxdata.nifi.services.StandardInfluxDatabaseService_2) InitializationException(org.apache.nifi.reporting.InitializationException) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) MockProcessContext(org.apache.nifi.util.MockProcessContext) Before(org.junit.Before)

Example 2 with Query

use of com.influxdb.client.domain.Query in project nifi-influxdb-bundle by influxdata.

the class TestGetInfluxDatabase_2 method queryValueWithCustomDialect.

@Test
public void queryValueWithCustomDialect() {
    runner.setProperty(GetInfluxDatabase_2.DIALECT_HEADER, "true");
    runner.setProperty(GetInfluxDatabase_2.DIALECT_DELIMITER, "|");
    runner.setProperty(GetInfluxDatabase_2.DIALECT_COMMENT_PREFIX, "§");
    runner.setProperty(GetInfluxDatabase_2.DIALECT_ANNOTATIONS, "datatype, group");
    runner.setProperty(GetInfluxDatabase_2.DIALECT_DATE_TIME_FORMAT, "RFC3339Nano");
    runner.enqueue("");
    runner.run();
    Query query = new Query().query("from(bucket:\"my-bucket\") |> range(start: 0) |> last()").dialect(new Dialect().header(true).delimiter("|").commentPrefix("§").addAnnotationsItem(Dialect.AnnotationsEnum.DATATYPE).addAnnotationsItem(Dialect.AnnotationsEnum.GROUP).dateTimeFormat(Dialect.DateTimeFormatEnum.RFC3339NANO));
    Mockito.verify(mockQueryApi, Mockito.only()).queryRaw(Mockito.eq(query), Mockito.eq("my-org"), Mockito.any(), Mockito.any(), Mockito.any());
}
Also used : Query(com.influxdb.client.domain.Query) Dialect(com.influxdb.client.domain.Dialect) Test(org.junit.Test)

Example 3 with Query

use of com.influxdb.client.domain.Query in project nifi-influxdb-bundle by influxdata.

the class AbstractGetInfluxDatabase_2 method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    boolean createdFlowFile = false;
    FlowFile flowFile = session.get();
    // If there aren't incoming connections.
    if (flowFile == null) {
        flowFile = session.create();
        createdFlowFile = true;
    }
    String org = context.getProperty(ORG).evaluateAttributeExpressions(flowFile).getValue();
    String flux = context.getProperty(QUERY).evaluateAttributeExpressions(flowFile).getValue();
    Dialect dialect = prepareDialect(context);
    // 
    // Records per Flowfile
    // 
    long recordsPerFlowFile = -1;
    if (context.getProperty(RECORDS_PER_FLOWFILE).isSet()) {
        recordsPerFlowFile = context.getProperty(RECORDS_PER_FLOWFILE).evaluateAttributeExpressions().asLong();
    }
    Query query = new Query().query(flux).dialect(dialect);
    try {
        QueryProcessor processor = new QueryProcessor(org, query, recordsPerFlowFile, flowFile, createdFlowFile, context, session);
        // CVS or Record based response?
        if (getSupportedPropertyDescriptors().contains(WRITER_FACTORY)) {
            processor.doQuery();
        } else {
            processor.doQueryRaw();
        }
    } catch (Exception e) {
        catchException(query, e, Collections.singletonList(flowFile), context, session);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Query(com.influxdb.client.domain.Query) Dialect(com.influxdb.client.domain.Dialect) InfluxException(com.influxdb.exceptions.InfluxException) ProcessException(org.apache.nifi.processor.exception.ProcessException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 4 with Query

use of com.influxdb.client.domain.Query in project nifi-influxdb-bundle by influxdata.

the class AbstractTestGetInfluxDatabaseRecord_2 method before.

@Before
public void before() throws IOException, GeneralSecurityException, InitializationException {
    InfluxDBClient mockInfluxDBClient = Mockito.mock(InfluxDBClient.class);
    QueryApi mockQueryApi = Mockito.mock(QueryApi.class);
    Mockito.doAnswer(invocation -> mockQueryApi).when(mockInfluxDBClient).getQueryApi();
    Mockito.doAnswer(invocation -> {
        if (queryOnErrorValue != null) {
            // noinspection unchecked
            Consumer<Exception> onError = invocation.getArgument(3, Consumer.class);
            onError.accept(queryOnErrorValue);
        }
        queryOnResponseRecords.forEach(record -> {
            // noinspection unchecked
            BiConsumer<Cancellable, FluxRecord> onRecord = invocation.getArgument(2, BiConsumer.class);
            onRecord.accept(Mockito.mock(Cancellable.class), record);
        });
        boolean wasException = queryOnErrorValue != null;
        try {
            return queryAnswer.answer(invocation);
        } catch (Exception e) {
            wasException = true;
            throw e;
        } finally {
            if (!wasException) {
                Runnable onComplete = invocation.getArgument(4, Runnable.class);
                onComplete.run();
            }
        }
    }).when(mockQueryApi).query(Mockito.any(Query.class), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any(Runnable.class));
    processor = Mockito.spy(new GetInfluxDatabaseRecord_2());
    runner = TestRunners.newTestRunner(processor);
    runner.setProperty(GetInfluxDatabaseRecord_2.ORG, "my-org");
    runner.setProperty(GetInfluxDatabaseRecord_2.QUERY, "from(bucket:\"my-bucket\") |> range(start: 0) |> last()");
    runner.setProperty(GetInfluxDatabaseRecord_2.INFLUX_DB_SERVICE, "influxdb-service");
    runner.setProperty(GetInfluxDatabaseRecord_2.WRITER_FACTORY, "writer");
    InfluxDatabaseService_2 influxDatabaseService = Mockito.spy(new StandardInfluxDatabaseService_2());
    Mockito.doAnswer(invocation -> mockInfluxDBClient).when(influxDatabaseService).create();
    runner.addControllerService("influxdb-service", influxDatabaseService);
    runner.setProperty(influxDatabaseService, INFLUX_DB_ACCESS_TOKEN, "my-token");
    runner.enableControllerService(influxDatabaseService);
    writer = new ArrayListRecordWriter(null) {

        @Override
        public RecordSchema getSchema(final Map<String, String> variables, final RecordSchema readSchema) {
            return readSchema;
        }
    };
    runner.addControllerService("writer", writer);
    runner.enableControllerService(writer);
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    logger = initContext.getLogger();
    processor.initialize(initContext);
    processor.onScheduled(runner.getProcessContext());
    processor.initWriterFactory(runner.getProcessContext());
}
Also used : ArrayListRecordWriter(org.apache.nifi.serialization.record.ArrayListRecordWriter) InfluxDBClient(com.influxdb.client.InfluxDBClient) QueryApi(com.influxdb.client.QueryApi) Query(com.influxdb.client.domain.Query) InfluxDatabaseService_2(org.influxdata.nifi.services.InfluxDatabaseService_2) StandardInfluxDatabaseService_2(org.influxdata.nifi.services.StandardInfluxDatabaseService_2) Cancellable(com.influxdb.Cancellable) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) StandardInfluxDatabaseService_2(org.influxdata.nifi.services.StandardInfluxDatabaseService_2) InitializationException(org.apache.nifi.reporting.InitializationException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) FluxRecord(com.influxdb.query.FluxRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) MockProcessContext(org.apache.nifi.util.MockProcessContext) Before(org.junit.Before)

Example 5 with Query

use of com.influxdb.client.domain.Query in project nifi-influxdb-bundle by influxdata.

the class TestGetInfluxDatabase_2 method queryValue.

@Test
public void queryValue() {
    runner.enqueue("");
    runner.run();
    Query query = new Query().query("from(bucket:\"my-bucket\") |> range(start: 0) |> last()").dialect(new Dialect().header(false).delimiter(",").commentPrefix("#").dateTimeFormat(Dialect.DateTimeFormatEnum.RFC3339));
    Mockito.verify(mockQueryApi, Mockito.only()).queryRaw(Mockito.eq(query), Mockito.eq("my-org"), Mockito.any(), Mockito.any(), Mockito.any());
}
Also used : Query(com.influxdb.client.domain.Query) Dialect(com.influxdb.client.domain.Dialect) Test(org.junit.Test)

Aggregations

Query (com.influxdb.client.domain.Query)5 Dialect (com.influxdb.client.domain.Dialect)3 IOException (java.io.IOException)3 Cancellable (com.influxdb.Cancellable)2 InfluxDBClient (com.influxdb.client.InfluxDBClient)2 QueryApi (com.influxdb.client.QueryApi)2 GeneralSecurityException (java.security.GeneralSecurityException)2 InitializationException (org.apache.nifi.reporting.InitializationException)2 MockProcessContext (org.apache.nifi.util.MockProcessContext)2 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)2 InfluxDatabaseService_2 (org.influxdata.nifi.services.InfluxDatabaseService_2)2 StandardInfluxDatabaseService_2 (org.influxdata.nifi.services.StandardInfluxDatabaseService_2)2 Before (org.junit.Before)2 Test (org.junit.Test)2 InfluxException (com.influxdb.exceptions.InfluxException)1 FluxRecord (com.influxdb.query.FluxRecord)1 SocketTimeoutException (java.net.SocketTimeoutException)1 FlowFile (org.apache.nifi.flowfile.FlowFile)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 ArrayListRecordWriter (org.apache.nifi.serialization.record.ArrayListRecordWriter)1