-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd7ebcd
commit d525e3a
Showing
9 changed files
with
407 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/it/gov/pagopa/payhub/auth/connector/BaseApiHolderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package it.gov.pagopa.payhub.auth.connector; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.function.Function; | ||
import java.util.stream.IntStream; | ||
|
||
public abstract class BaseApiHolderTest { | ||
|
||
@Mock | ||
protected RestTemplate restTemplateMock; | ||
|
||
protected <T> void assertAuthenticationShouldBeSetInThreadSafeMode(Function<String, T> apiInvoke, Class<T> apiReturnedType, Runnable apiUnloader) throws InterruptedException { | ||
// Configuring useCases in a single thread | ||
List<Pair<String, T>> useCases = IntStream.rangeClosed(0, 100) | ||
.mapToObj(i -> { | ||
try { | ||
String accessToken = "accessToken" + i; | ||
T expectedResult = apiReturnedType.getConstructor().newInstance(); | ||
|
||
Mockito.doReturn(ResponseEntity.ok(expectedResult)) | ||
.when(restTemplateMock) | ||
.exchange( | ||
Mockito.argThat(req -> | ||
req.getHeaders().getOrDefault(HttpHeaders.AUTHORIZATION, Collections.emptyList()).getFirst() | ||
.equals("Bearer " + accessToken)), | ||
Mockito.eq(apiReturnedType)); | ||
return Pair.of(accessToken, expectedResult); | ||
} catch (Exception e) { | ||
throw new IllegalStateException(e); | ||
} | ||
}) | ||
.toList(); | ||
|
||
try (ExecutorService executorService = Executors.newFixedThreadPool(10)) { | ||
executorService.invokeAll(useCases.stream() | ||
.map(p -> (Callable<?>) () -> { | ||
// Given | ||
String accessToken = p.getKey(); | ||
T expectedResult = p.getValue(); | ||
|
||
// When | ||
T result = apiInvoke.apply(accessToken); | ||
|
||
// Then | ||
Assertions.assertSame(expectedResult, result); | ||
return true; | ||
}) | ||
.toList()); | ||
} | ||
|
||
apiUnloader.run(); | ||
|
||
Mockito.verify(restTemplateMock, Mockito.times(useCases.size())) | ||
.exchange(Mockito.any(), Mockito.<ParameterizedTypeReference<?>>any()); | ||
} | ||
|
||
} |
102 changes: 102 additions & 0 deletions
102
src/test/java/it/gov/pagopa/payhub/auth/connector/client/OrganizationSearchClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package it.gov.pagopa.payhub.auth.connector.client; | ||
|
||
import it.gov.pagopa.payhub.auth.connector.client.OrganizationSearchClient; | ||
import it.gov.pagopa.payhub.auth.connector.config.OrganizationApisHolder; | ||
import it.gov.pagopa.pu.p4pa_organization.controller.generated.OrganizationSearchControllerApi; | ||
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Organization; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class OrganizationSearchClientTest { | ||
@Mock | ||
private OrganizationApisHolder organizationApisHolder; | ||
@Mock | ||
private OrganizationSearchControllerApi organizationSearchControllerApiMock; | ||
|
||
private OrganizationSearchClient organizationSearchClient; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
organizationSearchClient = new OrganizationSearchClient(organizationApisHolder); | ||
} | ||
|
||
@AfterEach | ||
void verifyNoMoreInteractions() { | ||
Mockito.verifyNoMoreInteractions( | ||
organizationApisHolder | ||
); | ||
} | ||
|
||
@Test | ||
void whenGetOrganizationByIpaCodeThenInvokeWithAccessToken() { | ||
String orgIpaCode = "ORGIPACODE"; | ||
String accessToken = "ACCESSTOKEN"; | ||
Organization expectedResult = new Organization(); | ||
|
||
Mockito.when(organizationApisHolder.getOrganizationSearchControllerApi(accessToken)) | ||
.thenReturn(organizationSearchControllerApiMock); | ||
Mockito.when(organizationSearchControllerApiMock.crudOrganizationsFindByIpaCode(orgIpaCode)) | ||
.thenReturn(expectedResult); | ||
|
||
Organization result = organizationSearchClient.getOrganizationByIpaCode(orgIpaCode, accessToken); | ||
|
||
Assertions.assertSame(expectedResult, result); | ||
} | ||
|
||
@Test | ||
void givenNoExistentIpaCodeWhenGetOrganizationByIpaCodeThenNull() { | ||
String orgIpaCode = "ORGIPACODE"; | ||
String accessToken = "ACCESSTOKEN"; | ||
|
||
Mockito.when(organizationApisHolder.getOrganizationSearchControllerApi(accessToken)) | ||
.thenReturn(organizationSearchControllerApiMock); | ||
Mockito.when(organizationSearchControllerApiMock.crudOrganizationsFindByIpaCode(orgIpaCode)) | ||
.thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND)); | ||
|
||
Organization result = organizationSearchClient.getOrganizationByIpaCode(orgIpaCode, accessToken); | ||
|
||
Assertions.assertNull(result); | ||
} | ||
|
||
@Test | ||
void givenGenericHttpExceptionWhenGetOrganizationByIpaCodeThenThrowIt() { | ||
String orgIpaCode = "ORGIPACODE"; | ||
String accessToken = "ACCESSTOKEN"; | ||
HttpClientErrorException expectedException = new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR); | ||
|
||
Mockito.when(organizationApisHolder.getOrganizationSearchControllerApi(accessToken)) | ||
.thenReturn(organizationSearchControllerApiMock); | ||
Mockito.when(organizationSearchControllerApiMock.crudOrganizationsFindByIpaCode(orgIpaCode)) | ||
.thenThrow(expectedException); | ||
|
||
HttpClientErrorException result = Assertions.assertThrows(expectedException.getClass(), () -> organizationSearchClient.getOrganizationByIpaCode(orgIpaCode, accessToken)); | ||
|
||
Assertions.assertSame(expectedException, result); | ||
} | ||
|
||
@Test | ||
void givenGenericExceptionWhenGetOrganizationByIpaCodeThenThrowIt() { | ||
String orgIpaCode = "ORGIPACODE"; | ||
String accessToken = "ACCESSTOKEN"; | ||
RuntimeException expectedException = new RuntimeException(); | ||
|
||
Mockito.when(organizationApisHolder.getOrganizationSearchControllerApi(accessToken)) | ||
.thenReturn(organizationSearchControllerApiMock); | ||
Mockito.when(organizationSearchControllerApiMock.crudOrganizationsFindByIpaCode(orgIpaCode)) | ||
.thenThrow(expectedException); | ||
|
||
RuntimeException result = Assertions.assertThrows(expectedException.getClass(), () -> organizationSearchClient.getOrganizationByIpaCode(orgIpaCode, accessToken)); | ||
|
||
Assertions.assertSame(expectedException, result); | ||
} | ||
|
||
} |
Oops, something went wrong.