Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/2735#discussion_r191038355
  
    --- Diff: 
nifi-nar-bundles/nifi-standard-services/nifi-dbcp-service-bundle/nifi-dbcp-service/src/test/java/org/apache/nifi/dbcp/TestDBCPConnectionPoolLookup.java
 ---
    @@ -0,0 +1,182 @@
    +/*
    + * 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 org.apache.nifi.dbcp;
    +
    +import org.apache.nifi.controller.AbstractControllerService;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.reporting.InitializationException;
    +import org.apache.nifi.util.TestRunner;
    +import org.apache.nifi.util.TestRunners;
    +import org.junit.Before;
    +import org.junit.Test;
    +
    +import java.sql.Connection;
    +import java.util.HashMap;
    +import java.util.Map;
    +
    +import static org.junit.Assert.assertEquals;
    +import static org.junit.Assert.assertNotNull;
    +import static org.junit.Assert.assertTrue;
    +import static org.mockito.Mockito.mock;
    +import static org.mockito.Mockito.when;
    +
    +public class TestDBCPConnectionPoolLookup {
    +
    +    private MockConnection connectionA;
    +    private MockConnection connectionB;
    +
    +    private MockDBCPService dbcpServiceA;
    +    private MockDBCPService dbcpServiceB;
    +
    +    private DBCPService dbcpLookupService;
    +    private TestRunner runner;
    +
    +    @Before
    +    public void setup() throws InitializationException {
    +        connectionA = mock(MockConnection.class);
    +        when(connectionA.getName()).thenReturn("A");
    +
    +        connectionB = mock(MockConnection.class);
    +        when(connectionB.getName()).thenReturn("B");
    +
    +        dbcpServiceA = new MockDBCPService(connectionA);
    +        dbcpServiceB = new MockDBCPService(connectionB);
    +
    +        dbcpLookupService = new DBCPConnectionPoolLookup();
    +
    +        runner = TestRunners.newTestRunner(TestProcessor.class);
    +
    +        final String dbcpServiceAIdentifier = "dbcp-a";
    +        runner.addControllerService(dbcpServiceAIdentifier, dbcpServiceA);
    +
    +        final String dbcpServiceBIdentifier = "dbcp-b";
    +        runner.addControllerService(dbcpServiceBIdentifier, dbcpServiceB);
    +
    +        runner.addControllerService("dbcp-lookup", dbcpLookupService);
    +        runner.setProperty(dbcpLookupService, "a", dbcpServiceAIdentifier);
    +        runner.setProperty(dbcpLookupService, "b", dbcpServiceBIdentifier);
    +
    +        runner.enableControllerService(dbcpServiceA);
    +        runner.enableControllerService(dbcpServiceB);
    +        runner.enableControllerService(dbcpLookupService);
    +
    +    }
    +
    +    @Test
    +    public void testLookupServiceA() {
    +        final Map<String,String> attributes = new HashMap<>();
    +        attributes.put(DBCPConnectionPoolLookup.DATABASE_NAME_ATTRIBUTE, 
"a");
    +
    +        final Connection connection = 
dbcpLookupService.getConnection(attributes);
    +        assertNotNull(connection);
    +        assertTrue(connection instanceof MockConnection);
    +
    +        final MockConnection mockConnection = (MockConnection)connection;
    +        assertEquals(connectionA.getName(), mockConnection.getName());
    +    }
    +
    +    @Test
    +    public void testLookupServiceB() {
    --- End diff --
    
    Our other "unit" tests use Derby, what about setting a service A to point 
to something that wouldn't connect, and B to a Derby, then make a valid query? 
It's more of an integration test but in this area it's not without precedent :)


---

Reply via email to