diff --git a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementService.java b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementService.java index a04fbdf1a5..57f7210bce 100644 --- a/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementService.java +++ b/irs-edc-client/src/main/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementService.java @@ -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; @@ -57,9 +61,9 @@ public List getContractAgreements(final String... contractAgr final EndpointConfig endpoint = config.getControlplane().getEndpoint(); final String contractAgreements = endpoint.getContractAgreements(); - final ResponseEntity edcContractAgreementListResponseEntity = edcRestTemplate.postForEntity( - endpoint.getData() + contractAgreements + EDC_REQUEST_SUFFIX, querySpec, - EdcContractAgreementsResponse.class); + final ResponseEntity 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) { @@ -74,8 +78,9 @@ public List getContractAgreements(final String... contractAgr public ContractNegotiation getContractAgreementNegotiation(final String contractAgreementId) { final EndpointConfig endpoint = config.getControlplane().getEndpoint(); final String contractAgreements = endpoint.getContractAgreements(); - final ResponseEntity contractNegotiationResponseEntity = edcRestTemplate.getForEntity( - endpoint.getData() + contractAgreements + "/" + contractAgreementId + "/negotiation", ContractNegotiation.class); + final ResponseEntity contractNegotiationResponseEntity = edcRestTemplate.exchange( + endpoint.getData() + contractAgreements + "/" + contractAgreementId + "/negotiation", HttpMethod.GET, + new HttpEntity<>(headers()), ContractNegotiation.class); return contractNegotiationResponseEntity.getBody(); } @@ -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; + } + } diff --git a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementServiceTest.java b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementServiceTest.java index ab0eea1fd0..1445f0b31e 100644 --- a/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementServiceTest.java +++ b/irs-edc-client/src/test/java/org/eclipse/tractusx/irs/edc/client/contract/service/EdcContractAgreementServiceTest.java @@ -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; @@ -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); } @@ -79,10 +81,10 @@ 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 contractAgreements = edcContractAgreementService.getContractAgreements( @@ -90,8 +92,9 @@ void shouldReturnContractAgreements() throws ContractAgreementException { //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); } @@ -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 @@ -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()); } @@ -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 @@ -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); } } \ No newline at end of file