Skip to content

Commit

Permalink
Merge pull request #455 from catenax-ng/feature/420-add-contractagree…
Browse files Browse the repository at this point in the history
…ments

feature: 420 add headers to EdcContractAgreementService
  • Loading branch information
ds-jhartmann authored Feb 29, 2024
2 parents 22c71bc + a1cca69 commit 278ea45
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractAgreementsResponse;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Expand All @@ -57,9 +61,9 @@ public List<ContractAgreement> getContractAgreements(final String... contractAgr

final EndpointConfig endpoint = config.getControlplane().getEndpoint();
final String contractAgreements = endpoint.getContractAgreements();
final ResponseEntity<EdcContractAgreementsResponse> edcContractAgreementListResponseEntity = edcRestTemplate.postForEntity(
endpoint.getData() + contractAgreements + EDC_REQUEST_SUFFIX, querySpec,
EdcContractAgreementsResponse.class);
final ResponseEntity<EdcContractAgreementsResponse> edcContractAgreementListResponseEntity = edcRestTemplate.exchange(
endpoint.getData() + contractAgreements + EDC_REQUEST_SUFFIX, HttpMethod.POST,
new HttpEntity<>(querySpec, headers()), EdcContractAgreementsResponse.class);

final EdcContractAgreementsResponse contractAgreementListWrapper = edcContractAgreementListResponseEntity.getBody();
if (contractAgreementListWrapper != null) {
Expand All @@ -74,8 +78,9 @@ public List<ContractAgreement> getContractAgreements(final String... contractAgr
public ContractNegotiation getContractAgreementNegotiation(final String contractAgreementId) {
final EndpointConfig endpoint = config.getControlplane().getEndpoint();
final String contractAgreements = endpoint.getContractAgreements();
final ResponseEntity<ContractNegotiation> contractNegotiationResponseEntity = edcRestTemplate.getForEntity(
endpoint.getData() + contractAgreements + "/" + contractAgreementId + "/negotiation", ContractNegotiation.class);
final ResponseEntity<ContractNegotiation> contractNegotiationResponseEntity = edcRestTemplate.exchange(
endpoint.getData() + contractAgreements + "/" + contractAgreementId + "/negotiation", HttpMethod.GET,
new HttpEntity<>(headers()), ContractNegotiation.class);
return contractNegotiationResponseEntity.getBody();
}

Expand All @@ -91,4 +96,15 @@ private QuerySpec buildQuerySpec(final String... contractAgreementIds) {
return QuerySpec.Builder.newInstance().filter(criterionList).build();
}

private HttpHeaders headers() {
final HttpHeaders headers = new HttpHeaders();
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
final String apiKeyHeader = config.getControlplane().getApiKey().getHeader();
if (apiKeyHeader != null) {
headers.add(apiKeyHeader, config.getControlplane().getApiKey().getSecret());
}
return headers;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

Expand All @@ -59,7 +59,9 @@ class EdcContractAgreementServiceTest {
@BeforeEach
void setUp() {
edcConfiguration.getControlplane().setEndpoint(new EdcConfiguration.ControlplaneConfig.EndpointConfig());
edcConfiguration.getControlplane().getEndpoint().setData("https://irs-consumer-controlplane.dev.demo.net/data/management");
edcConfiguration.getControlplane()
.getEndpoint()
.setData("https://irs-consumer-controlplane.dev.demo.net/data/management");
edcConfiguration.getControlplane().getEndpoint().setContractAgreements("/v2/contractagreements");
this.edcContractAgreementService = new EdcContractAgreementService(edcConfiguration, restTemplate);
}
Expand All @@ -79,19 +81,20 @@ void shouldReturnContractAgreements() throws ContractAgreementException {
.build();
final EdcContractAgreementsResponse edcContractAgreementsResponse = EdcContractAgreementsResponse.builder()
.contractAgreementList(
List.of(contractAgreement))
List.of(contractAgreement))
.build();
when(restTemplate.postForEntity(anyString(), any(), eq(EdcContractAgreementsResponse.class))).thenReturn(
ResponseEntity.ok(edcContractAgreementsResponse));
when(restTemplate.exchange(anyString(), eq(HttpMethod.POST), any(),
eq(EdcContractAgreementsResponse.class))).thenReturn(ResponseEntity.ok(edcContractAgreementsResponse));

//WHEN
final List<ContractAgreement> contractAgreements = edcContractAgreementService.getContractAgreements(
contractAgreementIds);

//THEN
Mockito.verify(restTemplate)
.postForEntity(eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"), any(),
eq(EdcContractAgreementsResponse.class));
.exchange(
eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"),
any(), any(), eq(EdcContractAgreementsResponse.class));
assertNotNull(contractAgreements);
}

Expand All @@ -100,7 +103,7 @@ void shouldThrowContractAgreementExceptionWhenResponseBodyIsEmtpy() {
//GIVEN
String[] contractAgreementIds = { "contractAgreementId" };

when(restTemplate.postForEntity(anyString(), any(), eq(EdcContractAgreementsResponse.class))).thenReturn(
when(restTemplate.exchange(anyString(), any(), any(), eq(EdcContractAgreementsResponse.class))).thenReturn(
ResponseEntity.ok().build());

//WHEN
Expand All @@ -109,8 +112,9 @@ void shouldThrowContractAgreementExceptionWhenResponseBodyIsEmtpy() {

//THEN
Mockito.verify(restTemplate)
.postForEntity(eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"), any(),
eq(EdcContractAgreementsResponse.class));
.exchange(
eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"),
any(), any(), eq(EdcContractAgreementsResponse.class));
assertEquals("Empty message body on edc response: <200 OK OK,[]>", contractAgreementException.getMessage());
}

Expand All @@ -125,7 +129,7 @@ void shouldReturnContractAgreementNegotiation() {
.counterPartyAddress("")
.protocol("")
.build();
when(restTemplate.getForEntity(anyString(), eq(ContractNegotiation.class))).thenReturn(
when(restTemplate.exchange(anyString(), any(), any(), eq(ContractNegotiation.class))).thenReturn(
ResponseEntity.ok(contractAgreementNegotiationMock));

//WHEN
Expand All @@ -134,8 +138,9 @@ void shouldReturnContractAgreementNegotiation() {

//THEN
Mockito.verify(restTemplate)
.getForEntity("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/contractAgreementId/negotiation",
ContractNegotiation.class);
.exchange(
eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/contractAgreementId/negotiation"),
any(), any(), eq(ContractNegotiation.class));
assertNotNull(contractAgreementNegotiation);
}
}

0 comments on commit 278ea45

Please sign in to comment.