|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.camel.component.drill; |
| 18 | + |
| 19 | +import java.sql.Connection; |
| 20 | +import java.sql.ResultSet; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.sql.Statement; |
| 23 | +import java.util.Collections; |
| 24 | + |
| 25 | +import org.apache.camel.Exchange; |
| 26 | +import org.apache.camel.Message; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import static org.mockito.ArgumentMatchers.any; |
| 31 | +import static org.mockito.Mockito.*; |
| 32 | + |
| 33 | +class DrillProducerTest { |
| 34 | + |
| 35 | + private DrillEndpoint endpoint; |
| 36 | + private Connection connection; |
| 37 | + private Statement statement; |
| 38 | + private ResultSet resultSet; |
| 39 | + private Exchange exchange; |
| 40 | + private Message message; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + endpoint = mock(DrillEndpoint.class); |
| 45 | + when(endpoint.getCamelContext()).thenReturn(null); |
| 46 | + when(endpoint.getEndpointUri()).thenReturn("drill://localhost"); |
| 47 | + |
| 48 | + connection = mock(Connection.class); |
| 49 | + statement = mock(Statement.class); |
| 50 | + resultSet = mock(ResultSet.class); |
| 51 | + exchange = mock(Exchange.class); |
| 52 | + message = mock(Message.class); |
| 53 | + |
| 54 | + when(exchange.getIn()).thenReturn(message); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testProcessClosesStatementAndResultSet() throws Exception { |
| 59 | + String query = "SELECT * FROM test"; |
| 60 | + when(message.getHeader(DrillConstants.DRILL_QUERY, String.class)).thenReturn(query); |
| 61 | + when(connection.createStatement()).thenReturn(statement); |
| 62 | + when(statement.executeQuery(query)).thenReturn(resultSet); |
| 63 | + when(endpoint.queryForList(any(ResultSet.class))).thenReturn(Collections.emptyList()); |
| 64 | + |
| 65 | + DrillProducer producer = new DrillProducer(endpoint); |
| 66 | + setConnection(producer, connection); |
| 67 | + |
| 68 | + producer.process(exchange); |
| 69 | + |
| 70 | + verify(resultSet).close(); |
| 71 | + verify(statement).close(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testProcessClosesResourcesOnQueryFailure() throws Exception { |
| 76 | + String query = "SELECT * FROM test"; |
| 77 | + when(message.getHeader(DrillConstants.DRILL_QUERY, String.class)).thenReturn(query); |
| 78 | + when(connection.createStatement()).thenReturn(statement); |
| 79 | + when(statement.executeQuery(query)).thenThrow(new SQLException("query failed")); |
| 80 | + |
| 81 | + DrillProducer producer = new DrillProducer(endpoint); |
| 82 | + setConnection(producer, connection); |
| 83 | + |
| 84 | + try { |
| 85 | + producer.process(exchange); |
| 86 | + } catch (SQLException e) { |
| 87 | + // expected |
| 88 | + } |
| 89 | + |
| 90 | + verify(statement).close(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void testDoStopClosesConnection() throws Exception { |
| 95 | + DrillProducer producer = new DrillProducer(endpoint); |
| 96 | + setConnection(producer, connection); |
| 97 | + |
| 98 | + producer.doStop(); |
| 99 | + |
| 100 | + verify(connection).close(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testDoStopHandlesCloseException() throws Exception { |
| 105 | + doThrow(new SQLException("close failed")).when(connection).close(); |
| 106 | + |
| 107 | + DrillProducer producer = new DrillProducer(endpoint); |
| 108 | + setConnection(producer, connection); |
| 109 | + |
| 110 | + // should not throw |
| 111 | + producer.doStop(); |
| 112 | + |
| 113 | + verify(connection).close(); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void testDoStopHandlesNullConnection() throws Exception { |
| 118 | + DrillProducer producer = new DrillProducer(endpoint); |
| 119 | + setConnection(producer, null); |
| 120 | + |
| 121 | + // should not throw |
| 122 | + producer.doStop(); |
| 123 | + } |
| 124 | + |
| 125 | + private void setConnection(DrillProducer producer, Connection conn) throws Exception { |
| 126 | + java.lang.reflect.Field field = DrillProducer.class.getDeclaredField("connection"); |
| 127 | + field.setAccessible(true); |
| 128 | + field.set(producer, conn); |
| 129 | + } |
| 130 | +} |
0 commit comments