-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapi_test.mustache
More file actions
59 lines (49 loc) · 2.03 KB
/
api_test.mustache
File metadata and controls
59 lines (49 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{{! This template had to be customized because of our changes to the DefaultApi and ApiClient classes }}
{{! Original template: https://github.com/OpenAPITools/openapi-generator/blob/v7.15.0/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/api_test.mustache }}
{{>licenseInfo}}
package {{invokerPackage}};
import cloud.stackit.sdk.core.KeyFlowAuthenticator;
import cloud.stackit.sdk.core.auth.SetupAuth;
import cloud.stackit.sdk.core.config.CoreConfiguration;
import cloud.stackit.sdk.core.utils.TestUtils;
import java.io.IOException;
import okhttp3.Authenticator;
import okhttp3.OkHttpClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DefaultApiTest {
@Test
public void TestCustomHttpClient() throws IOException {
// before
CoreConfiguration conf =
new CoreConfiguration().serviceAccountKey(TestUtils.MOCK_SERVICE_ACCOUNT_KEY);
// when
OkHttpClient httpClient = new OkHttpClient();
ApiClient apiClient = new ApiClient(httpClient, conf);
// then
Assertions.assertEquals(httpClient, apiClient.getHttpClient());
// make sure the http client object is exactly the same object
Assertions.assertSame(httpClient, apiClient.getHttpClient());
}
@Test
public void TestNoCustomHttpClient() throws IOException {
// before
CoreConfiguration conf =
new CoreConfiguration().serviceAccountKey(TestUtils.MOCK_SERVICE_ACCOUNT_KEY);
// when
ApiClient apiClient = new ApiClient(conf);
// then
/*
* verify a fresh OkHttpClient got created which will have the auth header set
* by the {@link cloud.stackit.sdk.core.KeyFlowAuthenticator}
*/
OkHttpClient httpClient = new OkHttpClient();
Authenticator authenticator =
new KeyFlowAuthenticator(httpClient, conf, SetupAuth.setupKeyFlow(conf));
httpClient = httpClient.newBuilder().authenticator(authenticator).build();
Assertions.assertNotNull(apiClient.getHttpClient());
Assertions.assertEquals(
httpClient.authenticator().getClass(),
apiClient.getHttpClient().authenticator().getClass());
}
}