|
7 | 7 | package org.opensearch.jdbc;
|
8 | 8 |
|
9 | 9 | import org.opensearch.jdbc.config.AuthConnectionProperty;
|
| 10 | +import org.opensearch.jdbc.config.ConnectionConfig; |
10 | 11 | import org.opensearch.jdbc.config.ConnectionPropertyException;
|
11 | 12 | import org.opensearch.jdbc.config.PasswordConnectionProperty;
|
12 | 13 | import org.opensearch.jdbc.config.RegionConnectionProperty;
|
13 | 14 | import org.opensearch.jdbc.config.RequestCompressionConnectionProperty;
|
14 | 15 | import org.opensearch.jdbc.config.UserConnectionProperty;
|
| 16 | +import org.opensearch.jdbc.logging.NoOpLogger; |
| 17 | +import org.opensearch.jdbc.protocol.Protocol; |
| 18 | +import org.opensearch.jdbc.protocol.ProtocolFactory; |
| 19 | +import org.opensearch.jdbc.protocol.exceptions.ResponseException; |
| 20 | +import org.opensearch.jdbc.protocol.http.HttpException; |
15 | 21 | import org.opensearch.jdbc.protocol.http.JsonHttpProtocol;
|
16 | 22 | import org.opensearch.jdbc.test.PerTestWireMockServerExtension;
|
17 | 23 | import org.opensearch.jdbc.test.WireMockServerHelpers;
|
|
25 | 31 | import org.junit.jupiter.api.extension.ExtendWith;
|
26 | 32 | import org.junit.jupiter.params.ParameterizedTest;
|
27 | 33 | import org.junit.jupiter.params.provider.ValueSource;
|
| 34 | +import org.opensearch.jdbc.transport.Transport; |
| 35 | +import org.opensearch.jdbc.transport.TransportFactory; |
28 | 36 |
|
29 | 37 | import java.io.IOException;
|
30 | 38 | import java.sql.Connection;
|
|
34 | 42 |
|
35 | 43 | import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
36 | 44 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
| 45 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
37 | 46 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
| 47 | +import static org.mockito.ArgumentMatchers.any; |
| 48 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 49 | +import static org.mockito.Mockito.mock; |
| 50 | +import static org.mockito.Mockito.when; |
38 | 51 |
|
39 | 52 | @ExtendWith(PerTestWireMockServerExtension.class)
|
40 | 53 | class ConnectionTests implements WireMockServerHelpers {
|
@@ -117,6 +130,28 @@ void testConnectDefaultAuthWithUsername(final WireMockServer mockServer) throws
|
117 | 130 | con.close();
|
118 | 131 | }
|
119 | 132 |
|
| 133 | + @Test |
| 134 | + void testConnectInvalidUsernameOrPassword(final WireMockServer mockServer) throws ResponseException, IOException { |
| 135 | + TransportFactory mockTransportFactory = mock(TransportFactory.class); |
| 136 | + when(mockTransportFactory.getTransport(any(), any(), any())) |
| 137 | + .thenReturn(mock(Transport.class)); |
| 138 | + ProtocolFactory mockProtocolFactory = mock(ProtocolFactory.class); |
| 139 | + Protocol mockProtocol = mock(Protocol.class); |
| 140 | + |
| 141 | + when(mockProtocolFactory.getProtocol(any(ConnectionConfig.class), any(Transport.class))) |
| 142 | + .thenReturn(mockProtocol); |
| 143 | + when(mockProtocol.connect(anyInt())).thenThrow(new HttpException(401, "Unauthorized")); |
| 144 | + |
| 145 | + SQLException sqlException = Assertions.assertThrows(SQLException.class, |
| 146 | + () -> new ConnectionImpl(mock(ConnectionConfig.class), |
| 147 | + mockTransportFactory, mockProtocolFactory, NoOpLogger.INSTANCE)); |
| 148 | + |
| 149 | + // 28000 is the SQLSTATE for invalid authorization specification |
| 150 | + // https://docs.oracle.com/cd/E15817_01/appdev.111/b31228/appd.htm |
| 151 | + assertEquals(sqlException.getSQLState(), "28000"); |
| 152 | + assertEquals(sqlException.getMessage(), "Connection error Unauthorized"); |
| 153 | + } |
| 154 | + |
120 | 155 | @Test
|
121 | 156 | void testConnectWithRequestCompression(final WireMockServer mockServer) throws SQLException {
|
122 | 157 | // Respond only if request mentions it accepts gzip
|
|
0 commit comments