Skip to content

Commit

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

feature: 420 rework EdcContractAgreementsResponse
  • Loading branch information
ds-mmaul authored Mar 4, 2024
2 parents cff14a1 + 33e7607 commit f262c5d
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@
********************************************************************************/
package org.eclipse.tractusx.irs.edc.client.contract.model;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;

/**
* EdcContractAgreementListWrapper used for wrapping the response of
* /management/v2/contractagreements/request where a List of {@link ContractAgreement} is returned.
*/
@ToString
@Builder
@RequiredArgsConstructor
@Getter
public class EdcContractAgreementsResponse {
private final List<ContractAgreement> contractAgreementList;

@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public record EdcContractAgreementsResponse(@JsonProperty("@id") String contractAgreementId,
@JsonProperty("edc:providerId") String providerId,
@JsonProperty("edc:consumerId") String consumerId,
@JsonProperty("edc:contractSigningDate") long contractSigningDate,
@JsonProperty("edc:assetId") String assetId,
@JsonProperty("@type") String type) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;
import org.apache.commons.lang3.ArrayUtils;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiation;
import org.eclipse.tractusx.irs.edc.client.EdcConfiguration;
import org.eclipse.tractusx.irs.edc.client.EdcConfiguration.ControlplaneConfig.EndpointConfig;
Expand Down Expand Up @@ -58,21 +58,21 @@ public EdcContractAgreementService(final EdcConfiguration config,
this.edcRestTemplate = edcRestTemplate;
}

public List<ContractAgreement> getContractAgreements(final String... contractAgreementIds)
public List<EdcContractAgreementsResponse> getContractAgreements(final String... contractAgreementIds)
throws ContractAgreementException {

final EdcContractAgreementRequest edcContractAgreementRequest = buildContractAgreementRequest(
contractAgreementIds);

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

final EdcContractAgreementsResponse contractAgreementListWrapper = edcContractAgreementListResponseEntity.getBody();
if (contractAgreementListWrapper != null) {
return contractAgreementListWrapper.getContractAgreementList();
final EdcContractAgreementsResponse[] contractAgreementListWrapper = edcContractAgreementListResponseEntity.getBody();
if (ArrayUtils.isNotEmpty(contractAgreementListWrapper)) {
return List.of(contractAgreementListWrapper);
} else {
throw new ContractAgreementException(
"Empty message body on edc response: " + edcContractAgreementListResponseEntity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/********************************************************************************
* Copyright (c) 2022,2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs.edc.client.contract.model;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

class EdcContractAgreementsResponseTest {

@Test
void shouldParseEdcContractAgreementsResponse() throws JsonProcessingException {
//GIVEN
String contractAgreementResponse = """
[
{
"@type": "edc:ContractAgreement",
"@id": "OWY1Y2U2OWUtZjI2Yy00MzQ5LTg1MTktNjY2Y2Q3MDgzNWEx:cmVnaXN0cnktYXNzZXQ=:MWYwNmMyYjktN2I2OS00YjhiLTk0NmUtM2FmNzFiYjA2NWU4",
"edc:assetId": "registry-asset",
"edc:policy": {
"@id": "eb0c8486-914a-4d36-84c0-b4971cbc52e4",
"@type": "odrl:Set",
"odrl:permission": {
"odrl:target": "registry-asset",
"odrl:action": {
"odrl:type": "USE"
},
"odrl:constraint": {
"odrl:or": {
"odrl:leftOperand": "PURPOSE",
"odrl:operator": {
"@id": "odrl:eq"
},
"odrl:rightOperand": "ID 3.0 Trace"
}
}
},
"odrl:prohibition": [],
"odrl:obligation": [],
"odrl:target": "registry-asset"
},
"edc:contractSigningDate": 1708951087,
"edc:consumerId": "BPNL00000003CML1",
"edc:providerId": "BPNL00000003CML1",
"@context": {
"dct": "https://purl.org/dc/terms/",
"tx": "https://w3id.org/tractusx/v0.0.1/ns/",
"edc": "https://w3id.org/edc/v0.0.1/ns/",
"dcat": "https://www.w3.org/ns/dcat/",
"odrl": "http://www.w3.org/ns/odrl/2/",
"dspace": "https://w3id.org/dspace/v0.8/"
}
}
]
""";

final ObjectMapper objectMapper = new ObjectMapper();

//WHEN
final EdcContractAgreementsResponse[] contractAgreements = objectMapper.readValue(contractAgreementResponse,
EdcContractAgreementsResponse[].class);
//THEN
assertThat(contractAgreements).isNotNull();
assertThat(contractAgreements[0].contractAgreementId()).isEqualTo(
"OWY1Y2U2OWUtZjI2Yy00MzQ5LTg1MTktNjY2Y2Q3MDgzNWEx:cmVnaXN0cnktYXNzZXQ=:MWYwNmMyYjktN2I2OS00YjhiLTk0NmUtM2FmNzFiYjA2NWU4");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
********************************************************************************/
package org.eclipse.tractusx.irs.edc.client.contract.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -29,9 +28,7 @@

import java.util.List;

import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiation;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.tractusx.irs.edc.client.EdcConfiguration;
import org.eclipse.tractusx.irs.edc.client.contract.model.EdcContractAgreementsResponse;
import org.eclipse.tractusx.irs.edc.client.contract.model.exception.ContractAgreementException;
Expand Down Expand Up @@ -71,40 +68,35 @@ void shouldReturnContractAgreements() throws ContractAgreementException {
//GIVEN
String[] contractAgreementIds = { "contractAgreementId" };

final ContractAgreement contractAgreement = ContractAgreement.Builder.newInstance()
.id("id")
.assetId("assetId")
.consumerId("consumerId")
.providerId("providerId")
.policy(Policy.Builder.newInstance()
.build())
.build();
final EdcContractAgreementsResponse edcContractAgreementsResponse = EdcContractAgreementsResponse.builder()
.contractAgreementList(
List.of(contractAgreement))
.build();
final EdcContractAgreementsResponse[] edcContractAgreementsResponse = new EdcContractAgreementsResponse[1];
edcContractAgreementsResponse[0] = EdcContractAgreementsResponse.builder().contractAgreementId("id")
.assetId("assetId")
.consumerId("consumerId")
.providerId("providerId")
.build();
when(restTemplate.exchange(anyString(), eq(HttpMethod.POST), any(),
eq(EdcContractAgreementsResponse.class))).thenReturn(ResponseEntity.ok(edcContractAgreementsResponse));
eq(EdcContractAgreementsResponse[].class))).thenReturn(
ResponseEntity.ok(edcContractAgreementsResponse));

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

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

@Test
void shouldThrowContractAgreementExceptionWhenResponseBodyIsEmtpy() {
//GIVEN
String[] contractAgreementIds = { "contractAgreementId" };

when(restTemplate.exchange(anyString(), any(), any(), eq(EdcContractAgreementsResponse.class))).thenReturn(
ResponseEntity.ok().build());
when(restTemplate.exchange(anyString(), any(), any(), eq(EdcContractAgreementsResponse[].class))).thenReturn(
ResponseEntity.ok(new EdcContractAgreementsResponse[0]));

//WHEN
final ContractAgreementException contractAgreementException = assertThrows(ContractAgreementException.class,
Expand All @@ -114,8 +106,8 @@ void shouldThrowContractAgreementExceptionWhenResponseBodyIsEmtpy() {
Mockito.verify(restTemplate)
.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());
any(), any(), eq(EdcContractAgreementsResponse[].class));
assertThat(contractAgreementException.getMessage()).startsWith("Empty message body on edc response:");
}

@Test
Expand All @@ -141,6 +133,6 @@ void shouldReturnContractAgreementNegotiation() {
.exchange(
eq("https://irs-consumer-controlplane.dev.demo.net/data/management/v2/contractagreements/contractAgreementId/negotiation"),
any(), any(), eq(ContractNegotiation.class));
assertNotNull(contractAgreementNegotiation);
assertThat(contractAgreementNegotiation).isNotNull();
}
}

0 comments on commit f262c5d

Please sign in to comment.