Skip to content

Commit

Permalink
Fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
enricovianello committed Sep 19, 2024
1 parent 69ec46f commit adccdf7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

import org.italiangrid.storm.webdav.config.OAuthProperties;
import org.slf4j.Logger;
Expand Down Expand Up @@ -99,6 +100,9 @@ public Map<String, Object> loadConfigurationForIssuer(String issuer) {
throw new OidcConfigurationResolutionError(
format("Received status code: %s", response.getStatusCodeValue()));
}
if (Objects.isNull(response.getBody())) {
throw new OidcConfigurationResolutionError("Received null body");
}
metadataChecks(issuer, response.getBody());
return response.getBody();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,27 @@ public Object getNativeCache() {
@Override
@Nullable
protected Object lookup(Object key) {
return value;
return value;
}

@Override
public void put(Object key, Object value) {
// Nothing to do
}

@Override
public void evict(Object key) {
// Nothing to do
}

@Override
public void clear() {
// Nothing to do
}

@SuppressWarnings("unchecked")
@Override
public <T> T get(Object key, Callable<T> valueLoader) {
return (T) fromStoreValue(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import eu.emi.security.authn.x509.X509CertChainValidatorExt;

@ExtendWith(MockitoExtension.class)
public class TLSConnectorBuilderTest {
class TLSConnectorBuilderTest {

@Test
void tlsConnectorBuilderErrorTests() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@

import com.google.common.collect.Maps;
import com.nimbusds.jose.KeySourceException;
import com.nimbusds.jose.RemoteKeySourceException;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.KeyType;

@ExtendWith(MockitoExtension.class)
class OidcConfigurationFetcherTest {

static final String ISSUER = "https://iam-dev.cloud.cnaf.infn.it/";
final static String JWK_URI = ISSUER + "jwk";
static final String JWK_URI = ISSUER + "jwk";

static final String ANOTHER_ISSUER = "https://iam.cloud.infn.it/";
static final String ANOTHER_JWK_URI = ANOTHER_ISSUER + "jwk";
Expand Down Expand Up @@ -122,6 +123,18 @@ private OidcConfigurationFetcher getFetcher(ResponseEntity<Map<String, Object>>

lenient().when(restTemplate.exchange(any(), eq(typeReference))).thenReturn(wellKnownResponse);
lenient().when(restTemplate.exchange(any(), eq(String.class))).thenReturn(jwkResponse);
return getFetcher(restTemplate);
}

private OidcConfigurationFetcher getFetcherWithException(ResponseEntity<Map<String, Object>> wellKnownResponse) {

lenient().when(restTemplate.exchange(any(), eq(typeReference))).thenReturn(wellKnownResponse);
lenient().when(restTemplate.exchange(any(), eq(String.class))).thenThrow(new RuntimeException("ERROR"));
return getFetcher(restTemplate);
}

private OidcConfigurationFetcher getFetcher(RestTemplate restTemplate) {

lenient().when(restBuilder.build()).thenReturn(restTemplate);
lenient().when(restBuilder.setConnectTimeout(any())).thenReturn(restBuilder);
lenient().when(restBuilder.setReadTimeout(any())).thenReturn(restBuilder);
Expand Down Expand Up @@ -174,15 +187,21 @@ private OidcConfigurationFetcher getFetcherWithErrorOnFetch() throws RestClientE
return getFetcher(mockedResponseMapEntity, null);
}

private OidcConfigurationFetcher getFetcherWithErrorOnGetJwk()
throws RestClientException, IOException {
private OidcConfigurationFetcher getFetcherWithErrorOnGetJwk() throws RestClientException {

ResponseEntity<Map<String, Object>> mockedResponseMapEntity =
getWellKnownResponse(OK, getMapWithIssuerAndJwkUri(ISSUER, JWK_URI));
ResponseEntity<String> mockedResponseStringEntity = getJWKURIResponse(NOT_FOUND, null);
return getFetcher(mockedResponseMapEntity, mockedResponseStringEntity);
}

private OidcConfigurationFetcher getFetcherWithRuntimeExceptionOnGetJwk() throws RestClientException {

ResponseEntity<Map<String, Object>> mockedResponseMapEntity =
getWellKnownResponse(OK, getMapWithIssuerAndJwkUri(ISSUER, JWK_URI));
return getFetcherWithException(mockedResponseMapEntity);
}

@BeforeEach
public void setDebugLevel() {
System.setProperty("logging.level.org.italiangrid.storm", "DEBUG");
Expand All @@ -199,7 +218,7 @@ void fetchWellKnownEndpointWithSuccessTests() throws RestClientException, IOExce
}

@Test
void fetchWellKnownEndpointWithErrorTests() throws RestClientException, IOException {
void fetchWellKnownEndpointWithErrorTests() throws RestClientException {

OidcConfigurationFetcher fetcher = getFetcherWithErrorOnFetch();
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
Expand Down Expand Up @@ -271,4 +290,18 @@ void fetchJWKEndpointWithErrorTests() throws RestClientException, IOException {

assertEquals(expectedMessage, actualMessage);
}

@Test
void fetchJWKEndpointWithRuntimeException() throws RestClientException, IOException {

OidcConfigurationFetcher fetcher = getFetcherWithRuntimeExceptionOnGetJwk();
final URI jwkUri = URI.create(JWK_URI);
RemoteKeySourceException exception = assertThrows(RemoteKeySourceException.class, () -> {
fetcher.loadJWKSourceForURL(jwkUri);
});
String expectedMessage = "Unable to get JWK from 'https://iam-dev.cloud.cnaf.infn.it/jwk'";
String actualMessage = exception.getMessage();

assertEquals(expectedMessage, actualMessage);
}
}

0 comments on commit adccdf7

Please sign in to comment.