Skip to content

Commit

Permalink
fix: Abide to the DataOfferCreationRequest.PolicyEnum rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ununhexium committed Oct 2, 2024
1 parent fa90bb1 commit 131098a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 23 deletions.
1 change: 1 addition & 0 deletions extensions/wrapper/wrapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies {
annotationProcessor(libs.lombok)
compileOnly(libs.lombok)

api(project(":extensions:policy-always-true"))
api(project(":extensions:wrapper:wrapper-api"))
api(project(":extensions:wrapper:wrapper-common-mappers"))
api(project(":utils:catalog-parser"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.sovity.edc.ext.wrapper.api.ui.pages.asset.AssetApiService;
import de.sovity.edc.ext.wrapper.api.ui.pages.contract_definitions.ContractDefinitionApiService;
import de.sovity.edc.ext.wrapper.api.ui.pages.policy.PolicyDefinitionApiService;
import de.sovity.edc.extension.policy.AlwaysTruePolicyConstants;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.eclipse.edc.spi.types.domain.asset.Asset;
Expand Down Expand Up @@ -66,22 +67,30 @@ private boolean isIdAvailable(DSLContext dsl, Table<?> table, TableField<?, Stri
}

public IdResponseDto createDataOffer(DSLContext dsl, DataOfferCreationRequest dataOfferCreationRequest) {

val commonId = dataOfferCreationRequest.getUiAssetCreateRequest().getId();

val assetIdExists = checkIfAssetIdAvailable(dsl, commonId).isAvailable();
if (!assetIdExists) {
throw new InvalidRequestException("Asset with id %s already exists".formatted(commonId));
}
return switch (dataOfferCreationRequest.getPolicy()) {
case DONT_PUBLISH -> createButDontPublish(dsl, dataOfferCreationRequest, commonId);
case PUBLISH_UNRESTRICTED -> createAndPublishUnrestricted(dsl, dataOfferCreationRequest, commonId);
case PUBLISH_RESTRICTED -> createAndPublishRestricted(dsl, dataOfferCreationRequest, commonId);
};
}

val policyIdExists = checkIfPolicyIdAvailable(dsl, commonId).isAvailable();
if (!policyIdExists) {
throw new InvalidRequestException("Policy with id %s already exists".formatted(commonId));
}
private @NotNull IdResponseDto createAndPublishUnrestricted(DSLContext dsl, DataOfferCreationRequest dataOfferCreationRequest, String commonId) {
checkAssetIdAvailable(dsl, commonId);
checkContractDefinitionIdAvailable(dsl, commonId);
val policyId = AlwaysTruePolicyConstants.POLICY_DEFINITION_ID;

val contractDefinitionIdExists = checkIfContractDefinitionIdAvailable(dsl, commonId).isAvailable();
if (!contractDefinitionIdExists) {
throw new InvalidRequestException("Contract definition with id %s already exists".formatted(commonId));
}
assetApiService.createAsset(dataOfferCreationRequest.getUiAssetCreateRequest());

return createContractDefinition(commonId, policyId, commonId);
}

private @NotNull IdResponseDto createAndPublishRestricted(DSLContext dsl, DataOfferCreationRequest dataOfferCreationRequest, String commonId) {
checkAssetIdAvailable(dsl, commonId);
checkPolicyIdAvailable(dsl, commonId);
checkContractDefinitionIdAvailable(dsl, commonId);

assetApiService.createAsset(dataOfferCreationRequest.getUiAssetCreateRequest());

Expand All @@ -90,21 +99,51 @@ public IdResponseDto createDataOffer(DSLContext dsl, DataOfferCreationRequest da
maybeNewPolicy.ifPresent(
policy -> policyDefinitionApiService.createPolicyDefinitionV2(new PolicyDefinitionCreateDto(commonId, policy)));

createContractDefinition(commonId, commonId, commonId);

return new IdResponseDto(commonId, OffsetDateTime.now());
}

private @NotNull IdResponseDto createButDontPublish(DSLContext dsl, DataOfferCreationRequest dataOfferCreationRequest, String commonId) {
checkAssetIdAvailable(dsl, commonId);
return assetApiService.createAsset(dataOfferCreationRequest.getUiAssetCreateRequest());
}

private void checkContractDefinitionIdAvailable(DSLContext dsl, String commonId) {
val contractDefinitionIdExists = checkIfContractDefinitionIdAvailable(dsl, commonId).isAvailable();
if (!contractDefinitionIdExists) {
throw new InvalidRequestException("Contract definition with id %s already exists".formatted(commonId));
}
}

private void checkPolicyIdAvailable(DSLContext dsl, String commonId) {
val policyIdExists = checkIfPolicyIdAvailable(dsl, commonId).isAvailable();
if (!policyIdExists) {
throw new InvalidRequestException("Policy with id %s already exists".formatted(commonId));
}
}

private void checkAssetIdAvailable(DSLContext dsl, String commonId) {
val assetIdExists = checkIfAssetIdAvailable(dsl, commonId).isAvailable();
if (!assetIdExists) {
throw new InvalidRequestException("Asset with id %s already exists".formatted(commonId));
}
}

private @NotNull IdResponseDto createContractDefinition(String assetId, String policyId, String contractDefinitionId) {
val cd = new ContractDefinitionRequest();
cd.setAssetSelector(List.of(UiCriterion.builder()
.operandLeft(Asset.PROPERTY_ID)
.operator(UiCriterionOperator.EQ)
.operandRight(UiCriterionLiteral.builder()
.type(UiCriterionLiteralType.VALUE)
.value(commonId)
.value(assetId)
.build())
.build()));
cd.setAccessPolicyId(commonId);
cd.setContractPolicyId(commonId);
cd.setContractDefinitionId(commonId);
cd.setAccessPolicyId(policyId);
cd.setContractPolicyId(policyId);
cd.setContractDefinitionId(contractDefinitionId);

contractDefinitionApiService.createContractDefinition(cd);

return new IdResponseDto(commonId, OffsetDateTime.now());
return contractDefinitionApiService.createContractDefinition(cd);
}
}
87 changes: 83 additions & 4 deletions tests/src/test/java/de/sovity/edc/e2e/UiApiWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,7 @@ void canCreateDataOfferWithoutAnyNewPolicy(
assertThat(getAllPoliciesExceptTheAlwaysTruePolicy(providerClient)).hasSize(0);

assertThat(providerClient.uiApi().getContractDefinitionPage().getContractDefinitions())
.extracting(ContractDefinitionEntry::getContractDefinitionId)
.first()
.isEqualTo(assetId);
.hasSize(0);
}

@Test
Expand All @@ -753,7 +751,7 @@ void canCreateDataOfferWithNewPolicy(

val dataOfferCreateRequest = new DataOfferCreationRequest(
asset,
DataOfferCreationRequest.PolicyEnum.DONT_PUBLISH,
DataOfferCreationRequest.PolicyEnum.PUBLISH_RESTRICTED,
UiPolicyExpression.builder()
.constraint(UiPolicyConstraint.builder()
.left("foo")
Expand Down Expand Up @@ -919,6 +917,87 @@ void dontCreateAnythingIfTheContractDefinitionAlreadyExists(
.isEqualTo(assetId);
}

@Test
void reuseTheAlwaysTruePolicyWhenPublishingUnrestricted(
E2eScenario scenario,
@Provider EdcClient providerClient
) {
// arrange
val assetId = "assetId";

// act
providerClient.uiApi()
.createDataOffer(DataOfferCreationRequest.builder()
.uiAssetCreateRequest(UiAssetCreateRequest.builder()
.id(assetId)
.dataSource(UiDataSource.builder()
.type(DataSourceType.ON_REQUEST)
.onRequest(UiDataSourceOnRequest.builder()
.contactEmail("foo@example.com")
.contactPreferredEmailSubject("Subject")
.build())
.build())
.build())
.policy(DataOfferCreationRequest.PolicyEnum.PUBLISH_UNRESTRICTED)
.build());

// assert
assertThat(providerClient.uiApi().getAssetPage().getAssets())
// the asset used for the placeholder contract definition
.hasSize(1)
.extracting(UiAsset::getAssetId)
.first()
.isEqualTo(assetId);

assertThat(getAllPoliciesExceptTheAlwaysTruePolicy(providerClient)).hasSize(0);

assertThat(providerClient.uiApi().getContractDefinitionPage().getContractDefinitions())
.hasSize(1)
.filteredOn(it -> it.getContractDefinitionId().equals(assetId))
.extracting(ContractDefinitionEntry::getContractDefinitionId)
.first()
// the already existing one, before the data offer creation attempt
.isEqualTo(assetId);
}

@Test
void onlyCreateTheAssetWhenDontPublish(
E2eScenario scenario,
@Provider EdcClient providerClient
) {
// arrange
val assetId = "assetId";

// act
providerClient.uiApi()
.createDataOffer(DataOfferCreationRequest.builder()
.uiAssetCreateRequest(UiAssetCreateRequest.builder()
.id(assetId)
.dataSource(UiDataSource.builder()
.type(DataSourceType.ON_REQUEST)
.onRequest(UiDataSourceOnRequest.builder()
.contactEmail("foo@example.com")
.contactPreferredEmailSubject("Subject")
.build())
.build())
.build())
.policy(DataOfferCreationRequest.PolicyEnum.DONT_PUBLISH)
.build());

// assert
assertThat(providerClient.uiApi().getAssetPage().getAssets())
// the asset used for the placeholder contract definition
.hasSize(1)
.extracting(UiAsset::getAssetId)
.first()
.isEqualTo(assetId);

assertThat(getAllPoliciesExceptTheAlwaysTruePolicy(providerClient)).hasSize(0);

assertThat(providerClient.uiApi().getContractDefinitionPage().getContractDefinitions())
.hasSize(0);
}

private static @NotNull List<PolicyDefinitionDto> getAllPoliciesExceptTheAlwaysTruePolicy(EdcClient edcClient) {
return edcClient.uiApi().getPolicyDefinitionPage().getPolicies().stream().filter(it -> !it.getPolicyDefinitionId().equals(
AlwaysTruePolicyConstants.POLICY_DEFINITION_ID)).toList();
Expand Down

0 comments on commit 131098a

Please sign in to comment.