Skip to content

Commit

Permalink
Added New Criterion Object, NewLines Fix and Function Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadEGI committed Aug 1, 2023
1 parent 94395a6 commit 753c67e
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2023 sovity GmbH
*
* 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
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* sovity GmbH - initial API and implementation
*
*/

package de.sovity.edc.client;

import org.eclipse.edc.connector.spi.contractdefinition.ContractDefinitionService;
import org.eclipse.edc.junit.annotations.ApiTest;
import org.eclipse.edc.junit.extensions.EdcExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static de.sovity.edc.client.ContractDefinitionTestUtils.createContractDefinition;

import java.text.ParseException;
import java.util.Map;

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

@ApiTest
@ExtendWith(EdcExtension.class)
class ContractDefinitionPageApiServiceTest {

@BeforeEach
void setUp(EdcExtension extension) {
extension.setConfiguration(TestUtils.createConfiguration(Map.of()));
}

@Test
void startContractDefinition(ContractDefinitionService contractDefinitionService) throws ParseException {
var client = TestUtils.edcClient();

// arrange
createContractDefinition(contractDefinitionService);


// act
var result = client.uiApi().contractDefinitionPageEndpoint();

// Get the contract definition
var contractDefinitions = result.getContractDefinitions();

// assert
var contractDefinition = contractDefinitions.get(0);
assertThat(contractDefinition.getContractDefinitionId()).isEqualTo(ContractDefinitionTestUtils.CONTRACT_DEFINITION_ID);
assertThat(contractDefinition.getContractPolicyId()).isEqualTo(ContractDefinitionTestUtils.CONTRACT_POLICY_ID);
assertThat(contractDefinition.getAccessPolicyId()).isEqualTo(ContractDefinitionTestUtils.ACCESS_POLICY_ID);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2022 sovity GmbH
*
* 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
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* sovity GmbH - initial API and implementation
*
*/

package de.sovity.edc.client;

import org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition;
import org.eclipse.edc.connector.spi.contractdefinition.ContractDefinitionService;
import org.eclipse.edc.spi.asset.AssetSelectorExpression;
import org.eclipse.edc.spi.query.Criterion;
import org.jetbrains.annotations.NotNull;
import java.text.ParseException;
import java.util.Arrays;

public class ContractDefinitionTestUtils {

public static final String CONTRACT_DEFINITION_ID = "contract-definition:eb934d1f-6582-4bab-85e6-af19a76f7e2b";

public static final String CONTRACT_POLICY_ID = "contract-policy:f52a5d30-6356-4a55-a75a-3c45d7a88c3e";

public static final String ACCESS_POLICY_ID = "access-policy:eb934d1f-6582-4bab-85e6-af19a76f7e2b";

@NotNull
public static void createContractDefinition(ContractDefinitionService contractDefinitionService) throws ParseException {

// Create an AssetSelectorExpression
AssetSelectorExpression selectorExpression = AssetSelectorExpression.Builder.newInstance()
.criteria(Arrays.asList(
new Criterion("criteria1", "=", "value1"),
new Criterion("criteria2", "=", "value2")
))
.build();

var definition = ContractDefinition.Builder.newInstance()
.id(CONTRACT_DEFINITION_ID)
.contractPolicyId(CONTRACT_POLICY_ID)
.accessPolicyId(ACCESS_POLICY_ID)
.validity(10000L)
.selectorExpression(selectorExpression)
.build();

contractDefinitionService.create(definition);


}


}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void initialize(ServiceExtensionContext context) {
policyEngine,
transferProcessService,
transferProcessStore,
(org.eclipse.edc.connector.contract.spi.offer.ContractDefinitionService) contractDefinitionService
contractDefinitionService
);

wrapperExtensionContext.jaxRsResources().forEach(resource ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.edc.connector.policy.spi.store.PolicyDefinitionStore;
import org.eclipse.edc.connector.spi.asset.AssetService;
import org.eclipse.edc.connector.spi.contractagreement.ContractAgreementService;
import org.eclipse.edc.connector.spi.contractdefinition.ContractDefinitionService;
import org.eclipse.edc.connector.spi.contractnegotiation.ContractNegotiationService;
import org.eclipse.edc.connector.spi.transferprocess.TransferProcessService;
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore;
Expand Down Expand Up @@ -74,8 +75,8 @@ public static WrapperExtensionContext buildContext(
PolicyDefinitionStore policyDefinitionStore,
PolicyEngine policyEngine,
TransferProcessService transferProcessService,
TransferProcessStore transferProcessStore
) {
TransferProcessStore transferProcessStore,
ContractDefinitionService contractDefinitionService) {
// UI API
var transferProcessStateService = new TransferProcessStateService();
var contractAgreementPageCardBuilder = new ContractAgreementPageCardBuilder(
Expand All @@ -92,8 +93,7 @@ public static WrapperExtensionContext buildContext(
var transferHistoryPageApiService = new TransferHistoryPageApiService(assetService, contractAgreementService, contractNegotiationStore,
transferProcessService, transferProcessStateService);
var transferHistoryPageAssetFetcherService = new TransferHistoryPageAssetFetcherService(assetService, transferProcessService);
var transferHistoryPageApiService = new TransferHistoryPageApiService();
var contractDefinitionApiService = new ContractDefinitionApiService();
var contractDefinitionApiService = new ContractDefinitionApiService(contractDefinitionService);
var transformerRegistryUtils = new TransformerRegistryUtils(dtoTransformerRegistry);
var contractNegotiationUtils = new ContractNegotiationUtils(contractNegotiationService);
var contractAgreementUtils = new ContractAgreementUtils(contractAgreementService);
Expand All @@ -111,9 +111,10 @@ public static WrapperExtensionContext buildContext(
var uiResource = new UiResource(
contractAgreementApiService,
contractAgreementTransferApiService,
contractDefinitionApiService,
transferHistoryPageApiService,
transferHistoryPageAssetFetcherService
contractDefinitionApiService

);

// Use Case API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
import de.sovity.edc.ext.wrapper.api.ui.pages.transferhistory.TransferHistoryPageAssetFetcherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import lombok.RequiredArgsConstructor;
import org.eclipse.edc.api.model.IdResponseDto;
Expand Down Expand Up @@ -84,7 +80,7 @@ public AssetDto getTransferProcessAsset(@PathParam("transferProcessId") String t
@GET
@Path("pages/contract-definition-page")
@Produces(MediaType.APPLICATION_JSON)
public ContractDefinitionPage contractDefinitionPageEndpoint() {
public ContractDefinitionPage contractDefinitionPage() {
return new ContractDefinitionPage(contractDefinitionApiService.getContractDefinitions());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.eclipse.edc.spi.query.Criterion;
import org.eclipse.edc.api.model.CriterionDto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2022 sovity GmbH
*
* 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
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* sovity GmbH - initial API and implementation
*
*/

package de.sovity.edc.ext.wrapper.api.ui.model;


import java.util.List;

public class Criterion {

private String operandLeft;
private String operator;
private String operandRight;
private List<String> operandRightArray;
private CriterionType type;

public Criterion(String operandLeft, String operator, String operandRight, CriterionType type) {
this.operandLeft = operandLeft;
this.operator = operator;
this.operandRight = operandRight;
this.type = type;
}

public Criterion(String operandLeft, String operator, List<String> operandRightArray, CriterionType type) {
this.operandLeft = operandLeft;
this.operator = operator;
this.operandRightArray = operandRightArray;
this.type = type;
}

public String getOperandLeft() {
return operandLeft;
}

public void setOperandLeft(String operandLeft) {
this.operandLeft = operandLeft;
}

public String getOperator() {
return operator;
}

public void setOperator(String operator) {
this.operator = operator;
}

public String getOperandRight() {
return operandRight;
}

public void setOperandRight(String operandRight) {
this.operandRight = operandRight;
}

public List<String> getOperandRightArray() {
return operandRightArray;
}

public void setOperandRightArray(List<String> operandRightArray) {
this.operandRightArray = operandRightArray;
}

public CriterionType getType() {
return type;
}

public void setType(CriterionType type) {
this.type = type;
}

public enum CriterionType {
STRING, STRING_ARRAY, JSON
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,32 @@
package de.sovity.edc.ext.wrapper.api.ui.pages.contracts;

import de.sovity.edc.ext.wrapper.api.ui.model.ContractDefinitionEntry;
import de.sovity.edc.ext.wrapper.api.ui.pages.contracts.services.ContractAgreementData;
import lombok.RequiredArgsConstructor;
import org.eclipse.edc.connector.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiation;
import org.eclipse.edc.connector.contract.spi.types.offer.ContractDefinition;
import org.eclipse.edc.connector.spi.asset.AssetService;
import org.eclipse.edc.connector.spi.contractagreement.ContractAgreementService;
import org.eclipse.edc.connector.spi.contractdefinition.ContractDefinitionService;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.spi.types.domain.asset.Asset;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;


@RequiredArgsConstructor
public class ContractDefinitionApiService {


private final ContractDefinitionService contractDefinitionService;

public List<ContractDefinitionEntry> getContractDefinitions() {
// Obtain all contract definitions
var definitions = getAllContractDefinitons();
return definitions.stream()
.map(definition -> {
var entry = new ContractDefinitionEntry();
entry.setContractDefinitionId(definition.getId());
entry.setAccessPolicyId(definition.getAccessPolicyId());
entry.setContractPolicyId(definition.getContractPolicyId());
entry.setCriteria(definition.getSelectorExpression().getCriteria()); // This depends on how the criteria are stored in your system
return entry;
})
.collect(Collectors.toList());
var definitions = getAllContractDefinitions();
return definitions.stream().map(definition -> {
var entry = new ContractDefinitionEntry();
entry.setContractDefinitionId(definition.getId());
entry.setAccessPolicyId(definition.getAccessPolicyId());
entry.setContractPolicyId(definition.getContractPolicyId());
entry.setCriteria(definition.getSelectorExpression().getCriteria()); // This depends on how the criteria are stored in your system
return entry;
}).toList();
}
private List<ContractDefinition> getAllContractDefinitons() {

private List<ContractDefinition> getAllContractDefinitions() {
return contractDefinitionService.query(QuerySpec.max()).getContent().toList();
}

Expand Down

0 comments on commit 753c67e

Please sign in to comment.