From e17e1c142ee5e9497f930a5557dfe3a6ae448afb Mon Sep 17 00:00:00 2001 From: ds-lcapellino Date: Thu, 29 Feb 2024 14:03:06 +0100 Subject: [PATCH 1/2] feature: 420 add headers to EdcContractAgreementService --- .../service/EdcContractAgreementService.java | 26 +++++++++++++++---- .../EdcContractAgreementServiceTest.java | 16 +++++++----- 2 files changed, 30 insertions(+), 12 deletions(-) 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..d71f62dea2 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 @@ -43,6 +43,7 @@ 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; @@ -81,7 +82,7 @@ void shouldReturnContractAgreements() throws ContractAgreementException { .contractAgreementList( List.of(contractAgreement)) .build(); - when(restTemplate.postForEntity(anyString(), any(), eq(EdcContractAgreementsResponse.class))).thenReturn( + when(restTemplate.exchange(anyString(),eq(HttpMethod.POST), any(), eq(EdcContractAgreementsResponse.class))).thenReturn( ResponseEntity.ok(edcContractAgreementsResponse)); //WHEN @@ -90,7 +91,7 @@ void shouldReturnContractAgreements() throws ContractAgreementException { //THEN Mockito.verify(restTemplate) - .postForEntity(eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"), any(), + .exchange(eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"), any(), any(), eq(EdcContractAgreementsResponse.class)); assertNotNull(contractAgreements); } @@ -100,7 +101,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,7 +110,7 @@ void shouldThrowContractAgreementExceptionWhenResponseBodyIsEmtpy() { //THEN Mockito.verify(restTemplate) - .postForEntity(eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"), any(), + .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 +126,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 +135,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 From a1cca695a044fc4d2c11037ec92e7ef20f8248fc Mon Sep 17 00:00:00 2001 From: ds-lcapellino Date: Thu, 29 Feb 2024 14:05:04 +0100 Subject: [PATCH 2/2] feature: 420 add headers to EdcContractAgreementService --- .../EdcContractAgreementServiceTest.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) 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 d71f62dea2..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,7 +38,6 @@ 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; @@ -60,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); } @@ -80,10 +81,10 @@ void shouldReturnContractAgreements() throws ContractAgreementException { .build(); final EdcContractAgreementsResponse edcContractAgreementsResponse = EdcContractAgreementsResponse.builder() .contractAgreementList( - List.of(contractAgreement)) + List.of(contractAgreement)) .build(); - when(restTemplate.exchange(anyString(),eq(HttpMethod.POST), 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( @@ -91,8 +92,9 @@ void shouldReturnContractAgreements() throws ContractAgreementException { //THEN Mockito.verify(restTemplate) - .exchange(eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"), any(), 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); } @@ -101,7 +103,7 @@ void shouldThrowContractAgreementExceptionWhenResponseBodyIsEmtpy() { //GIVEN String[] contractAgreementIds = { "contractAgreementId" }; - when(restTemplate.exchange(anyString(),any(), any(), eq(EdcContractAgreementsResponse.class))).thenReturn( + when(restTemplate.exchange(anyString(), any(), any(), eq(EdcContractAgreementsResponse.class))).thenReturn( ResponseEntity.ok().build()); //WHEN @@ -110,8 +112,9 @@ void shouldThrowContractAgreementExceptionWhenResponseBodyIsEmtpy() { //THEN Mockito.verify(restTemplate) - .exchange(eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/request"),any(), 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()); } @@ -126,7 +129,7 @@ void shouldReturnContractAgreementNegotiation() { .counterPartyAddress("") .protocol("") .build(); - when(restTemplate.exchange(anyString(),any(),any(), eq(ContractNegotiation.class))).thenReturn( + when(restTemplate.exchange(anyString(), any(), any(), eq(ContractNegotiation.class))).thenReturn( ResponseEntity.ok(contractAgreementNegotiationMock)); //WHEN @@ -135,9 +138,9 @@ void shouldReturnContractAgreementNegotiation() { //THEN Mockito.verify(restTemplate) - .exchange(eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/contractAgreementId/negotiation"), - any(),any(), - eq(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