|
18 | 18 | */ |
19 | 19 | package org.apache.pulsar.client.impl.auth; |
20 | 20 |
|
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 23 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 24 | +import static org.mockito.Mockito.mock; |
| 25 | +import static org.mockito.Mockito.when; |
21 | 26 | import static org.testng.Assert.assertEquals; |
22 | 27 | import static org.testng.Assert.assertFalse; |
| 28 | +import static org.testng.Assert.assertNull; |
23 | 29 | import static org.testng.Assert.assertTrue; |
24 | 30 | import static org.testng.Assert.fail; |
| 31 | +import org.mockito.MockedConstruction; |
| 32 | +import org.mockito.Mockito; |
25 | 33 | import org.testng.annotations.Test; |
26 | 34 | import org.apache.pulsar.common.util.ObjectMapperFactory; |
27 | 35 | import static org.apache.pulsar.common.util.Codec.encode; |
@@ -287,4 +295,53 @@ public void testRoleHeaderSetting() throws Exception { |
287 | 295 | assertEquals(auth2.getAuthData().getHttpHeaders().iterator().next().getKey(), "Test-Role-Header"); |
288 | 296 | auth2.close(); |
289 | 297 | } |
| 298 | + |
| 299 | + @Test |
| 300 | + public void testZtsProxyUrlSetting() throws Exception { |
| 301 | + final String ztsProxyUrl = "https://example.com:4443/"; |
| 302 | + final String paramsStr = new String(Files.readAllBytes(Paths.get("./src/test/resources/authParams.json"))); |
| 303 | + final ObjectMapper jsonMapper = ObjectMapperFactory.create(); |
| 304 | + final Map<String, String> authParamsMap = jsonMapper.readValue(paramsStr, new TypeReference<HashMap<String, String>>() { }); |
| 305 | + |
| 306 | + try (MockedConstruction<ZTSClient> mockedZTSClient = Mockito.mockConstruction(ZTSClient.class, (mock, context) -> { |
| 307 | + final String actualZtsProxyUrl = (String) context.arguments().get(1); |
| 308 | + assertNull(actualZtsProxyUrl); |
| 309 | + |
| 310 | + when(mock.getRoleToken(any(), any(), anyInt(), anyInt(), anyBoolean())).thenReturn(mock(RoleToken.class)); |
| 311 | + })) { |
| 312 | + authParamsMap.remove("ztsProxyUrl"); |
| 313 | + final AuthenticationAthenz auth1 = new AuthenticationAthenz(); |
| 314 | + auth1.configure(jsonMapper.writeValueAsString(authParamsMap)); |
| 315 | + auth1.getAuthData(); |
| 316 | + |
| 317 | + assertEquals(mockedZTSClient.constructed().size(), 1); |
| 318 | + |
| 319 | + auth1.close(); |
| 320 | + |
| 321 | + authParamsMap.put("ztsProxyUrl", ""); |
| 322 | + final AuthenticationAthenz auth2 = new AuthenticationAthenz(); |
| 323 | + auth2.configure(jsonMapper.writeValueAsString(authParamsMap)); |
| 324 | + auth2.getAuthData(); |
| 325 | + |
| 326 | + assertEquals(mockedZTSClient.constructed().size(), 2); |
| 327 | + |
| 328 | + auth2.close(); |
| 329 | + } |
| 330 | + |
| 331 | + try (MockedConstruction<ZTSClient> mockedZTSClient = Mockito.mockConstruction(ZTSClient.class, (mock, context) -> { |
| 332 | + final String actualZtsProxyUrl = (String) context.arguments().get(1); |
| 333 | + assertEquals(actualZtsProxyUrl, ztsProxyUrl); |
| 334 | + |
| 335 | + when(mock.getRoleToken(any(), any(), anyInt(), anyInt(), anyBoolean())).thenReturn(mock(RoleToken.class)); |
| 336 | + })) { |
| 337 | + authParamsMap.put("ztsProxyUrl", ztsProxyUrl); |
| 338 | + final AuthenticationAthenz auth3 = new AuthenticationAthenz(); |
| 339 | + auth3.configure(jsonMapper.writeValueAsString(authParamsMap)); |
| 340 | + auth3.getAuthData(); |
| 341 | + |
| 342 | + assertEquals(mockedZTSClient.constructed().size(), 1); |
| 343 | + |
| 344 | + auth3.close(); |
| 345 | + } |
| 346 | + } |
290 | 347 | } |
0 commit comments