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

feature: 420 add headers to EdcContractAgreementService #455

Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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);
}
}
Loading