Skip to content

Commit

Permalink
feat(charts):[TRI-1213] Adjust test structure to IRS Code standards
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-jhartmann committed Apr 17, 2023
1 parent af31bf5 commit ed7fcdf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@
class EdcDiscoveryClientImplTest {

private final RestTemplate restTemplate = mock(RestTemplate.class);
private final EdcDiscoveryClient bpdmClient = new EdcDiscoveryClientImpl(restTemplate,
private final EdcDiscoveryClient edcDiscoveryClient = new EdcDiscoveryClientImpl(restTemplate,
"url/api/administration/connectors/discovery");

@Test
void shouldCallExternalServiceOnceAndGetEdcAddressResponse() {
// Arrange
final String bpn = "BPNS000000000DDD";
final EdcAddressResponse[] mockResponse = new EdcAddressResponse[] { EdcAddressResponse.builder()
.bpn(bpn)
Expand All @@ -53,8 +54,10 @@ void shouldCallExternalServiceOnceAndGetEdcAddressResponse() {
};
doReturn(mockResponse).when(restTemplate).postForObject(any(), any(), eq(EdcAddressResponse[].class));

final List<EdcAddressResponse> edcAddressResponse = List.of(bpdmClient.getEdcBaseUrl(bpn));
// Act
final List<EdcAddressResponse> edcAddressResponse = List.of(edcDiscoveryClient.getEdcBaseUrl(bpn));

// Assert
assertThat(edcAddressResponse).isNotNull().hasSize(1);
assertThat(edcAddressResponse.get(0).getBpn()).isEqualTo(bpn);
assertThat(edcAddressResponse.get(0).getConnectorEndpoint()).isNotEmpty();
Expand All @@ -63,6 +66,7 @@ void shouldCallExternalServiceOnceAndGetEdcAddressResponse() {

@Test
void shouldCallExternalServiceWithTwoEndpointsOnceAndGetEdcAddressResponse() {
// Arrange
final String bpn = "BPNS000000000DDD";
final EdcAddressResponse[] mockResponse = new EdcAddressResponse[] { EdcAddressResponse.builder()
.bpn(bpn)
Expand All @@ -72,8 +76,10 @@ void shouldCallExternalServiceWithTwoEndpointsOnceAndGetEdcAddressResponse() {
};
doReturn(mockResponse).when(restTemplate).postForObject(any(), any(), eq(EdcAddressResponse[].class));

final List<EdcAddressResponse> edcAddressResponse = List.of(bpdmClient.getEdcBaseUrl(bpn));
// Act
final List<EdcAddressResponse> edcAddressResponse = List.of(edcDiscoveryClient.getEdcBaseUrl(bpn));

// Assert
assertThat(edcAddressResponse).isNotNull().hasSize(1);
assertThat(edcAddressResponse.get(0).getBpn()).isEqualTo(bpn);
assertThat(edcAddressResponse.get(0).getConnectorEndpoint()).hasSize(2);
Expand All @@ -84,12 +90,15 @@ void shouldCallExternalServiceWithTwoEndpointsOnceAndGetEdcAddressResponse() {

@Test
void shouldReturnNullWhenRestTemplateReturnsNull() {
// Arrange
final String bpn = "BPNS000000000DDD";
doReturn(null).when(restTemplate).postForObject(any(), any(), eq(EdcAddressResponse[].class));

final EdcAddressResponse[] edcBaseUrl = bpdmClient.getEdcBaseUrl(bpn);
assertThat(edcBaseUrl).isNull();
// Act
final EdcAddressResponse[] edcBaseUrl = edcDiscoveryClient.getEdcBaseUrl(bpn);

// Assert
assertThat(edcBaseUrl).isNull();
verify(this.restTemplate, times(1)).postForObject(any(), any(), eq(EdcAddressResponse[].class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,44 +41,56 @@ class EdcDiscoveryFacadeTest {

@Test
void shouldReturnEdcBaseUrl() {
// Arrange
final String bpn = "BPNS000000000DDD";
final String url = "http://edc-url";
when(edcDiscoveryMockConfig.getMockEdcAddress()).thenReturn(Map.of(bpn, List.of(url)));

// Act
final Optional<String> edcBaseUrl = edcDiscoveryFacade.getEdcBaseUrl(bpn);

// Assert
assertThat(edcBaseUrl).isNotEmpty().contains(url);
}

@Test
void shouldReturnEdcBaseUrls() {
// Arrange
final String bpn = "BPNS000000000DDD";
final String url1 = "http://edc-url1";
final String url2 = "http://edc-url2";
when(edcDiscoveryMockConfig.getMockEdcAddress()).thenReturn(Map.of(bpn, List.of(url1, url2)));

// Act
final Optional<String> edcBaseUrl = edcDiscoveryFacade.getEdcBaseUrl(bpn);

// Assert
assertThat(edcBaseUrl).isNotEmpty().contains(url1);
}

@Test
void shouldReturnResponseWithEmptyConnectorEndpointList() {
// Arrange
when(edcDiscoveryMockConfig.getMockEdcAddress()).thenReturn(Map.of());

// Act
final Optional<String> edcBaseUrl = edcDiscoveryFacade.getEdcBaseUrl("not_existing");

// Assert
assertThat(edcBaseUrl).isEmpty();
}

@Test
void shouldReturnEmptyOptionalWhenClientReturnsNull() {
// Arrange
final EdcDiscoveryClientLocalStub clientMock = mock(EdcDiscoveryClientLocalStub.class);
final EdcDiscoveryFacade edcDiscoveryFacade = new EdcDiscoveryFacade(clientMock);
when(clientMock.getEdcBaseUrl(anyString())).thenReturn(null);

// Act
final Optional<String> edcBaseUrl = edcDiscoveryFacade.getEdcBaseUrl("not_existing");

// Assert
assertThat(edcBaseUrl).isEmpty();
}

Expand Down

0 comments on commit ed7fcdf

Please sign in to comment.