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

chore/increase-code-coverage #378

Merged
merged 4 commits into from
Jul 8, 2023
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@
********************************************************************************/
package org.eclipse.tractusx.traceability.infrastructure.edc.blackbox;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.catalog.spi.Catalog;
import org.eclipse.edc.catalog.spi.DataService;
import org.eclipse.edc.catalog.spi.Dataset;
import org.eclipse.edc.catalog.spi.Distribution;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.tractusx.traceability.infrastructure.edc.blackbox.catalog.CatalogItem;
import org.eclipse.tractusx.traceability.infrastructure.edc.blackbox.jsontransformer.EdcTransformer;
import org.eclipse.tractusx.traceability.infrastructure.edc.blackbox.negotiation.NegotiationResponse;
import org.eclipse.tractusx.traceability.infrastructure.edc.blackbox.negotiation.Response;
import org.eclipse.tractusx.traceability.infrastructure.edc.blackbox.transferprocess.TransferProcessRequest;
import org.eclipse.tractusx.traceability.infrastructure.edc.properties.EdcProperties;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -39,9 +45,10 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class EdcServiceTest {
Expand Down Expand Up @@ -91,4 +98,105 @@ void testGetCatalog() throws IOException {

}

@Test
void testCatalogThrowBadRequestExceptionWhenDatasetIsEmpty() throws IOException {
//GIVEN
String consumerEdcDataManagementUrl = "https://example.com/consumer-edc";
String providerConnectorControlPlaneIDSUrl = "https://example.com/provider-connector";
Map<String, String> header = Collections.singletonMap("Authorization", "Bearer token");

Catalog emptyCatalog = Catalog.Builder.newInstance().datasets(Collections.<Dataset>emptyList()).build();
when(httpCallService.getCatalogForNotification(consumerEdcDataManagementUrl, providerConnectorControlPlaneIDSUrl, header))
.thenReturn(emptyCatalog);

//WHEN
BadRequestException badRequestException = assertThrows(
BadRequestException.class,
() -> edcService.getCatalog(consumerEdcDataManagementUrl, providerConnectorControlPlaneIDSUrl, header)
);
//THEN
verify(httpCallService).getCatalogForNotification(any(), any(), any());
assertEquals("Provider has no contract offers for us. Catalog is empty.", badRequestException.getMessage());
}

@Test
void testInitializeContractNegotiation() throws IOException, InterruptedException {
//GIVEN
String providerConnectorUrl = "https://example.com/provider-connector";
String consumerEdcUrl = "https://example.com/consumer-edc";
Map<String, String> header = Collections.singletonMap("Authorization", "Bearer token");

Policy policy = Policy.Builder.newInstance().target("policyTarget").build();
CatalogItem catalogItem = CatalogItem.builder().offerId("offerId").policy(policy).build();

JsonObject mockedJsonObject = Json.createObjectBuilder().add("", "").build();

when(edcTransformer.transformNegotiationRequestToJson(any())).thenReturn(mockedJsonObject);

Response response = Response.builder().responseId("negotiationId").build();
when(httpCallService.sendRequest(any(), eq(Response.class))).thenReturn(response);

NegotiationResponse inProgressNegotiationResponse = NegotiationResponse.builder().contractAgreementId("ContractAgreementID").state("IN_PROGRESS").build();
NegotiationResponse finalizedNegotiationResponse = NegotiationResponse.builder().contractAgreementId("ContractAgreementID").state("FINALIZED").build();
when(httpCallService.sendNegotiationRequest(any()))
.thenReturn(inProgressNegotiationResponse)
.thenReturn(finalizedNegotiationResponse);

//WHEN
String initializeContractNegotiation = edcService.initializeContractNegotiation(providerConnectorUrl, catalogItem, consumerEdcUrl, header);

//THEN
assertEquals("ContractAgreementID", initializeContractNegotiation);
verify(edcTransformer).transformNegotiationRequestToJson(any());
verify(httpCallService).sendRequest(any(), any());
verify(httpCallService, times(2)).sendNegotiationRequest(any());
}

@Test
void testInitializeContractNegotiationThrowExecutionException() throws IOException, InterruptedException {
//GIVEN
String providerConnectorUrl = "https://example.com/provider-connector";
String consumerEdcUrl = "https://example.com/consumer-edc";
Map<String, String> header = Collections.singletonMap("Authorization", "Bearer token");

Policy policy = Policy.Builder.newInstance().target("policyTarget").build();
CatalogItem catalogItem = CatalogItem.builder().offerId("offerId").policy(policy).build();

JsonObject mockedJsonObject = Json.createObjectBuilder().add("", "").build();

when(edcTransformer.transformNegotiationRequestToJson(any())).thenReturn(mockedJsonObject);

Response response = Response.builder().responseId("negotiationId").build();
when(httpCallService.sendRequest(any(), eq(Response.class))).thenReturn(response);

when(httpCallService.sendNegotiationRequest(any())).thenThrow(new IOException());

//WHEN
RuntimeException runtimeException = assertThrows(
RuntimeException.class,
() -> edcService.initializeContractNegotiation(providerConnectorUrl, catalogItem, consumerEdcUrl, header));

//THEN
assertNotNull(runtimeException);
}

@Test
void testInitiateHttpProxyTransferProcess() throws IOException {
//GIVEN
String providerConnectorControlPlaneIDSUrl = "https://example.com/provider-connector";
String consumerEdcDataManagementUrl = "https://example.com/consumer-edc";
Map<String, String> header = Collections.singletonMap("Authorization", "Bearer token");
TransferProcessRequest transferProcessRequest = TransferProcessRequest.builder().build();

JsonObject mockedJsonObject = Json.createObjectBuilder().add("", "").build();
when(edcTransformer.transformTransferProcessRequestToJson(any())).thenReturn(mockedJsonObject);
// when(httpCallService.sendRequest(any(), eq(Response.class)));//do nothing
ds-lcapellino marked this conversation as resolved.
Show resolved Hide resolved

//WHEN
edcService.initiateHttpProxyTransferProcess(consumerEdcDataManagementUrl, providerConnectorControlPlaneIDSUrl
, transferProcessRequest, header);
//THEN
verify(edcTransformer).transformTransferProcessRequestToJson(any());
verify(httpCallService).sendRequest(any(), eq(Response.class));
}
}