|
28 | 28 | import java.io.IOException;
|
29 | 29 | import java.io.InputStreamReader;
|
30 | 30 | import java.net.Socket;
|
31 |
| -import java.security.NoSuchAlgorithmException; |
32 |
| -import java.security.spec.InvalidKeySpecException; |
33 | 31 | import java.util.concurrent.ConcurrentHashMap;
|
34 | 32 | import java.util.concurrent.Executors;
|
35 |
| -import org.junit.After; |
36 |
| -import org.junit.Before; |
| 33 | +import org.junit.AfterClass; |
| 34 | +import org.junit.BeforeClass; |
37 | 35 | import org.junit.Test;
|
38 | 36 |
|
39 | 37 | public class ConnectorTest {
|
40 | 38 |
|
41 | 39 | private static final String INSTANCE_NAME =
|
42 | 40 | "projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>";
|
43 |
| - private static final String PRIVATE_IP = "127.0.0.2"; |
44 |
| - private static final String DNS_NAME = "localhost"; |
45 | 41 | private static final String SERVER_MESSAGE = "HELLO";
|
46 | 42 | private static final String ERROR_MESSAGE_NOT_FOUND = "Resource 'instance' was not found";
|
47 |
| - private static final String ERROR_MESSAGE_PERMISSION_DENIED = |
48 |
| - "Location not found or access is unauthorized."; |
49 |
| - private static final String ERROR_MESSAGE_INTERNAL = "Internal Error"; |
50 | 43 | private static final String USER_AGENT = "unit tests";
|
51 | 44 |
|
52 |
| - ListeningScheduledExecutorService defaultExecutor; |
| 45 | + static ListeningScheduledExecutorService defaultExecutor; |
| 46 | + private static FakeSslServer sslServer; |
53 | 47 |
|
54 |
| - @Before |
55 |
| - public void setUp() throws Exception { |
| 48 | + @BeforeClass |
| 49 | + public static void beforeClass() throws Exception { |
56 | 50 | defaultExecutor = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(8));
|
| 51 | + sslServer = new FakeSslServer(SERVER_MESSAGE); |
| 52 | + sslServer.start("127.0.0.1"); |
57 | 53 | }
|
58 | 54 |
|
59 |
| - @After |
60 |
| - public void tearDown() throws Exception { |
| 55 | + @AfterClass |
| 56 | + public static void afterClass() { |
61 | 57 | defaultExecutor.shutdownNow();
|
| 58 | + sslServer.stop(); |
62 | 59 | }
|
63 | 60 |
|
64 | 61 | @Test
|
65 |
| - public void create_successfulPrivateConnection() |
66 |
| - throws IOException, InterruptedException, NoSuchAlgorithmException, InvalidKeySpecException { |
67 |
| - FakeSslServer sslServer = new FakeSslServer(SERVER_MESSAGE); |
68 |
| - sslServer.start(PRIVATE_IP); |
69 |
| - |
| 62 | + public void create_successfulPrivateConnection() throws IOException { |
| 63 | + MockAlloyDBAdminGrpc mock = new MockAlloyDBAdminGrpc("127.0.0.1", IpType.PRIVATE); |
70 | 64 | ConnectionConfig config =
|
71 | 65 | new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(INSTANCE_NAME)).build();
|
72 |
| - |
73 |
| - MockAlloyDBAdminGrpc mock = new MockAlloyDBAdminGrpc(PRIVATE_IP, IpType.PRIVATE); |
74 | 66 | Connector connector = newConnector(config.getConnectorConfig(), mock);
|
75 |
| - Socket socket = connector.connect(config); |
76 |
| - |
77 |
| - assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE); |
78 |
| - sslServer.stop(); |
79 |
| - } |
80 |
| - |
81 |
| - @Test |
82 |
| - public void create_successfulPscConnection() |
83 |
| - throws IOException, InterruptedException, NoSuchAlgorithmException, InvalidKeySpecException { |
84 |
| - FakeSslServer sslServer = new FakeSslServer(SERVER_MESSAGE); |
85 |
| - sslServer.start(DNS_NAME); |
86 |
| - |
87 |
| - ConnectionConfig config = |
88 |
| - new ConnectionConfig.Builder() |
89 |
| - .withInstanceName(InstanceName.parse(INSTANCE_NAME)) |
90 |
| - .withIpType(IpType.PSC) |
91 |
| - .build(); |
92 | 67 |
|
93 |
| - MockAlloyDBAdminGrpc mock = new MockAlloyDBAdminGrpc(DNS_NAME, IpType.PSC); |
94 |
| - Connector connector = newConnector(config.getConnectorConfig(), mock); |
95 | 68 | Socket socket = connector.connect(config);
|
96 | 69 |
|
97 | 70 | assertThat(readLine(socket)).isEqualTo(SERVER_MESSAGE);
|
98 |
| - sslServer.stop(); |
99 | 71 | }
|
100 | 72 |
|
101 | 73 | @Test
|
102 |
| - public void create_throwsTerminalException_notFound() |
103 |
| - throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { |
| 74 | + public void create_throwsTerminalException() { |
104 | 75 | MockAlloyDBAdminGrpc mock =
|
105 | 76 | new MockAlloyDBAdminGrpc(Code.NOT_FOUND.getNumber(), ERROR_MESSAGE_NOT_FOUND);
|
106 |
| - |
107 | 77 | ConnectionConfig config =
|
108 | 78 | new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(INSTANCE_NAME)).build();
|
109 | 79 | Connector connector = newConnector(config.getConnectorConfig(), mock);
|
110 | 80 |
|
111 | 81 | TerminalException ex = assertThrows(TerminalException.class, () -> connector.connect(config));
|
112 |
| - assertThat(ex).hasMessageThat().contains(ERROR_MESSAGE_NOT_FOUND); |
113 |
| - } |
114 | 82 |
|
115 |
| - @Test |
116 |
| - public void create_throwsTerminalException_notAuthorized() |
117 |
| - throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { |
118 |
| - MockAlloyDBAdminGrpc mock = |
119 |
| - new MockAlloyDBAdminGrpc( |
120 |
| - Code.PERMISSION_DENIED.getNumber(), ERROR_MESSAGE_PERMISSION_DENIED); |
121 |
| - |
122 |
| - ConnectionConfig config = |
123 |
| - new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(INSTANCE_NAME)).build(); |
124 |
| - Connector connector = newConnector(config.getConnectorConfig(), mock); |
125 |
| - |
126 |
| - TerminalException ex = assertThrows(TerminalException.class, () -> connector.connect(config)); |
127 |
| - assertThat(ex).hasMessageThat().contains(ERROR_MESSAGE_PERMISSION_DENIED); |
128 |
| - } |
129 |
| - |
130 |
| - @Test |
131 |
| - public void create_throwsNonTerminalException_internalError() |
132 |
| - throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { |
133 |
| - MockAlloyDBAdminGrpc mock = |
134 |
| - new MockAlloyDBAdminGrpc(Code.INTERNAL.getNumber(), ERROR_MESSAGE_INTERNAL); |
135 |
| - |
136 |
| - ConnectionConfig config = |
137 |
| - new ConnectionConfig.Builder().withInstanceName(InstanceName.parse(INSTANCE_NAME)).build(); |
138 |
| - Connector connector = newConnector(config.getConnectorConfig(), mock); |
139 |
| - |
140 |
| - RuntimeException ex = assertThrows(RuntimeException.class, () -> connector.connect(config)); |
141 |
| - assertThat(ex).hasMessageThat().contains(ERROR_MESSAGE_INTERNAL); |
| 83 | + assertThat(ex).hasMessageThat().contains(ERROR_MESSAGE_NOT_FOUND); |
142 | 84 | }
|
143 | 85 |
|
144 |
| - private Connector newConnector(ConnectorConfig config, MockAlloyDBAdminGrpc mock) |
145 |
| - throws NoSuchAlgorithmException, InvalidKeySpecException { |
| 86 | + private Connector newConnector(ConnectorConfig config, MockAlloyDBAdminGrpc mock) { |
146 | 87 | CredentialFactoryProvider stubCredentialFactoryProvider =
|
147 | 88 | new CredentialFactoryProvider(new StubCredentialFactory());
|
148 | 89 | CredentialFactory instanceCredentialFactory =
|
|
0 commit comments