|
6 | 6 | package com.opengamma.sdk.common; |
7 | 7 |
|
8 | 8 | import static com.opengamma.sdk.common.ServiceInvoker.SERVICE_URL; |
9 | | -import static org.testng.Assert.assertEquals; |
10 | | -import static org.testng.Assert.assertFalse; |
11 | | -import static org.testng.Assert.assertThrows; |
12 | | -import static org.testng.Assert.assertTrue; |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
13 | 11 |
|
14 | | -import org.testng.annotations.Test; |
| 12 | +import org.junit.jupiter.api.Test; |
15 | 13 |
|
16 | 14 | import com.opengamma.sdk.common.auth.AuthClient; |
17 | 15 | import com.opengamma.sdk.common.auth.AuthenticationException; |
|
23 | 21 | /** |
24 | 22 | * Test. |
25 | 23 | */ |
26 | | -@Test |
27 | 24 | public class BasicTest { |
28 | 25 |
|
29 | 26 | private static final Credentials CREDENTIALS = Credentials.ofApiKey("user", "pw"); |
30 | 27 | private static final Credentials BAD_CREDENTIALS = Credentials.ofApiKey("bad", "pw"); |
31 | 28 |
|
| 29 | + @Test |
32 | 30 | public void testBasics() { |
33 | | - assertThrows(NullPointerException.class, () -> ServiceInvoker.of(null)); |
| 31 | + assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> ServiceInvoker.of(null)); |
34 | 32 | } |
35 | 33 |
|
| 34 | + @Test |
36 | 35 | public void testAuthGood() { |
37 | 36 | AuthClient mockAuth = new TestingAuthClient(); |
38 | 37 | @SuppressWarnings("resource") |
39 | 38 | ServiceInvoker invoker = ServiceInvoker.builder(CREDENTIALS) |
40 | 39 | .authClientFactory(inv -> mockAuth) |
41 | 40 | .build(); |
42 | | - assertEquals(invoker.getServiceUrl(), SERVICE_URL); |
43 | | - assertEquals(invoker.getHttpClient().interceptors().size(), 4); |
44 | | - assertTrue(invoker.getHttpClient().followRedirects()); |
45 | | - assertFalse(invoker.getExecutor().isShutdown()); |
| 41 | + assertThat(invoker.getServiceUrl()).isEqualTo(SERVICE_URL); |
| 42 | + assertThat(invoker.getHttpClient().interceptors()).hasSize(4); |
| 43 | + assertThat(invoker.getHttpClient().followRedirects()).isTrue(); |
| 44 | + assertThat(invoker.getExecutor().isShutdown()).isFalse(); |
46 | 45 | invoker.close(); |
47 | | - assertTrue(invoker.getExecutor().isShutdown()); |
| 46 | + assertThat(invoker.getExecutor().isShutdown()).isTrue(); |
48 | 47 | } |
49 | 48 |
|
| 49 | + @Test |
50 | 50 | public void testAuthGoodHttpFactory() { |
51 | 51 | AuthClient mockAuth = new TestingAuthClient(); |
52 | 52 | try (ServiceInvoker invoker = ServiceInvoker.builder(CREDENTIALS) |
53 | 53 | .httpClientFactory( |
54 | 54 | builder -> builder.addInterceptor(chain -> chain.proceed(chain.request())).followRedirects(false).build()) |
55 | 55 | .authClientFactory(inv -> mockAuth) |
56 | 56 | .build()) { |
57 | | - assertEquals(invoker.getServiceUrl(), SERVICE_URL); |
58 | | - assertEquals(invoker.getHttpClient().interceptors().size(), 5); // logging, user-agent & auth& retry plus one from test |
59 | | - assertFalse(invoker.getHttpClient().followRedirects()); |
| 57 | + assertThat(invoker.getServiceUrl()).isEqualTo(SERVICE_URL); |
| 58 | + assertThat(invoker.getHttpClient().interceptors()).hasSize(5); // logging, user-agent & auth& retry plus one from test |
| 59 | + assertThat(invoker.getHttpClient().followRedirects()).isFalse(); |
60 | 60 | } |
61 | 61 | } |
62 | 62 |
|
| 63 | + @Test |
63 | 64 | public void testAuthGoodHttpClient() { |
64 | 65 | AuthClient mockAuth = new TestingAuthClient(); |
65 | 66 | try (ServiceInvoker invoker = ServiceInvoker.builder(CREDENTIALS) |
66 | 67 | .httpClient(new OkHttpClient()) |
67 | 68 | .authClientFactory(inv -> mockAuth) |
68 | 69 | .build()) { |
69 | | - assertEquals(invoker.getServiceUrl(), SERVICE_URL); |
70 | | - assertEquals(invoker.getHttpClient().interceptors().size(), 3); // user-agent & auth & retry |
| 70 | + assertThat(invoker.getServiceUrl()).isEqualTo(SERVICE_URL); |
| 71 | + assertThat(invoker.getHttpClient().interceptors()).hasSize(3); // user-agent & auth & retry |
71 | 72 | } |
72 | 73 | } |
73 | 74 |
|
| 75 | + @Test |
74 | 76 | public void testAuthBad() { |
75 | 77 | AuthClient mockAuth = new TestingAuthClient(); |
76 | 78 | try (ServiceInvoker serviceInvoker = ServiceInvoker.builder(BAD_CREDENTIALS).authClientFactory(inv -> mockAuth).build()) { |
77 | 79 | Request testRequest = new Request.Builder() |
78 | 80 | .url(serviceInvoker.getServiceUrl().resolve("/test")) |
79 | 81 | .get() |
80 | 82 | .build(); |
81 | | - assertThrows(AuthenticationException.class, () -> serviceInvoker.getHttpClient().newCall(testRequest).execute()); |
| 83 | + assertThatExceptionOfType(AuthenticationException.class).isThrownBy(() -> serviceInvoker.getHttpClient().newCall(testRequest).execute()); |
82 | 84 | } |
83 | 85 | } |
84 | 86 |
|
|
0 commit comments