Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ApiClient NPE #1129

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions api/src/main/resources/custom_templates/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import java.nio.file.Paths;
import java.lang.reflect.Type;
import java.net.URI;

import com.okta.commons.http.config.HttpClientConfiguration;
import com.okta.sdk.cache.Caches;
import com.okta.sdk.cache.Cache;
import com.okta.sdk.cache.CacheManager;

Expand Down Expand Up @@ -156,10 +156,6 @@ protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>

private Cache<String, Object> cache;

private CacheManager cacheManager;

private HttpClientConfiguration httpClientConfiguration;

private int statusCode;
private Map<String, List<String>> responseHeaders;

Expand All @@ -168,7 +164,10 @@ protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>
// Methods that can have a request body
private static List<String> bodyMethods = Arrays.asList("POST", "PUT", "DELETE", "PATCH");

public ApiClient(CloseableHttpClient httpClient, CacheManager cacheManager, HttpClientConfiguration httpClientConfiguration) {
public ApiClient(CloseableHttpClient httpClient, CacheManager cacheManager) {
if (httpClient == null) throw new IllegalArgumentException("httpClient cannot be null");
if (cacheManager == null) throw new IllegalArgumentException("cacheManager cannot be null");

objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
Expand Down Expand Up @@ -201,13 +200,11 @@ protected List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>

this.httpClient = httpClient;

this.cacheManager = cacheManager;
this.httpClientConfiguration = httpClientConfiguration;
this.cache = cacheManager.getCache("default");
}

public ApiClient() {
this(HttpClients.createDefault(), null, null);
this(HttpClients.createDefault(), Caches.newDisabledCacheManager());
}

public static DateFormat buildDefaultDateFormat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public ApiClient build() {
setProxy(httpClientBuilder, clientConfig);
}

ApiClient apiClient = new ApiClient(httpClientBuilder.build(), this.cacheManager, this.clientConfig);
ApiClient apiClient = new ApiClient(httpClientBuilder.build(), this.cacheManager);
apiClient.setBasePath(this.clientConfig.getBaseUrl());

String userAgentValue = ApplicationInfo.get().entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.okta.sdk.impl.client

import com.okta.sdk.authc.credentials.TokenClientCredentials
import com.okta.sdk.cache.Caches
import com.okta.sdk.client.AuthenticationScheme
import com.okta.sdk.client.AuthorizationMode
import com.okta.sdk.client.ClientBuilder
Expand All @@ -29,6 +30,9 @@ import com.okta.sdk.impl.oauth2.OAuth2HttpException
import com.okta.sdk.impl.oauth2.OAuth2TokenRetrieverException
import com.okta.sdk.impl.test.RestoreEnvironmentVariables
import com.okta.sdk.impl.test.RestoreSystemProperties
import com.okta.sdk.resource.client.ApiClient
import com.okta.sdk.resource.client.Configuration
import org.apache.hc.client5.http.impl.classic.HttpClients
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
import org.testng.annotations.Listeners
Expand Down Expand Up @@ -82,6 +86,21 @@ class DefaultClientBuilderTest {
assertTrue(Clients.builder() instanceof DefaultClientBuilder)
}

@Test
void testDefaultApiClient() {
Configuration.getDefaultApiClient()
}

@Test(expectedExceptions = IllegalArgumentException.class)
void testIncorrectApiClient_nullHttpClient() {
new ApiClient(null, Caches.newDisabledCacheManager())
}

@Test(expectedExceptions = IllegalArgumentException.class)
void testIncorrectApiClient_nullCacheManager() {
new ApiClient(HttpClients.createDefault(), null)
}

@Test
void testConfigureBaseProperties() {
clearOktaEnvAndSysProps()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.okta.sdk.impl.oauth2

import com.okta.commons.http.config.HttpClientConfiguration
import com.okta.sdk.cache.CacheManager
import com.okta.sdk.impl.config.ClientConfiguration
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient
Expand Down Expand Up @@ -68,7 +67,7 @@ class Oauth2ClientCredentialsTest {

@Test
void testReplaceAuthentication() {
def apiClient = new ApiClient(mock(CloseableHttpClient), mock(CacheManager), mock(HttpClientConfiguration))
def apiClient = new ApiClient(mock(CloseableHttpClient), mock(CacheManager))
def accessTokenRetrieverService = new AccessTokenRetrieverServiceImpl(mock(ClientConfiguration), apiClient)
def credentials = new OAuth2ClientCredentials(accessTokenRetrieverService)
apiClient.replaceAuthentication("oauth2", credentials)
Expand All @@ -78,7 +77,7 @@ class Oauth2ClientCredentialsTest {

@Test
void testReplaceAuthentication2WrongName() {
def apiClient = new ApiClient(mock(CloseableHttpClient), mock(CacheManager), mock(HttpClientConfiguration))
def apiClient = new ApiClient(mock(CloseableHttpClient), mock(CacheManager))
def credentials = new OAuth2ClientCredentials(mock(AccessTokenRetrieverServiceImpl))
def exception = expectThrows(RuntimeException.class) {
apiClient.replaceAuthentication("oauth", credentials)
Expand All @@ -88,7 +87,7 @@ class Oauth2ClientCredentialsTest {

@Test
void testReplaceAuthenticationWrongType() {
def apiClient = new ApiClient(mock(CloseableHttpClient), mock(CacheManager), mock(HttpClientConfiguration))
def apiClient = new ApiClient(mock(CloseableHttpClient), mock(CacheManager))
def exception = expectThrows(RuntimeException.class) {
apiClient.replaceAuthentication("oauth2", new ApiKeyAuth("", ""))
}
Expand Down