Skip to content

Commit

Permalink
Core review
Browse files Browse the repository at this point in the history
  • Loading branch information
ununhexium committed Oct 2, 2024
1 parent 97b32cc commit 17d2cff
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 17 deletions.
6 changes: 3 additions & 3 deletions docs/api/sovity-edc-api-wrapper.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,11 @@ components:
DataSourceType:
type: string
description: Supported Data Source Types by UiDataSource
default: CUSTOM
enum:
- HTTP_DATA
- ON_REQUEST
- CUSTOM
default: CUSTOM
SecretValue:
type: object
properties:
Expand Down Expand Up @@ -835,14 +835,14 @@ components:
UiDataSourceHttpDataMethod:
type: string
description: Supported HTTP Methods by UiDataSource
default: GET
enum:
- GET
- POST
- PUT
- PATCH
- DELETE
- OPTIONS
default: GET
UiDataSourceOnRequest:
required:
- contactEmail
Expand Down Expand Up @@ -948,7 +948,7 @@ components:
$ref: '#/components/schemas/UiAssetCreateRequest'
policy:
type: string
description: Which policy to apply to this asset.
description: Which policy to apply to this asset creation.
enum:
- DONT_PUBLISH
- PUBLISH_UNRESTRICTED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
import org.eclipse.edc.protocol.dsp.api.configuration.DspApiConfiguration;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.CoreConstants;
Expand Down Expand Up @@ -74,6 +75,8 @@ public class WrapperExtension implements ServiceExtension {
@Inject
private PolicyEngine policyEngine;
@Inject
private RuleBindingRegistry ruleBindingRegistry;
@Inject
private TransferProcessService transferProcessService;
@Inject
private TransferProcessStore transferProcessStore;
Expand Down Expand Up @@ -118,6 +121,7 @@ public void initialize(ServiceExtensionContext context) {
policyDefinitionService,
policyDefinitionStore,
policyEngine,
ruleBindingRegistry,
transferProcessService,
transferProcessStore,
typeTransformerRegistry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
import de.sovity.edc.ext.wrapper.controller.PlaceholderEndpointController;
import de.sovity.edc.extension.contacttermination.ContractAgreementTerminationService;
import de.sovity.edc.extension.db.directaccess.DslContextFactory;
import de.sovity.edc.extension.policy.services.AlwaysTruePolicyDefinitionService;
import de.sovity.edc.extension.policy.services.AlwaysTruePolicyService;
import de.sovity.edc.utils.catalog.DspCatalogService;
import de.sovity.edc.utils.catalog.mapper.DspDataOfferBuilder;
import de.sovity.edc.utils.config.ConfigProps;
Expand All @@ -97,6 +99,7 @@
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.policy.engine.spi.PolicyEngine;
import org.eclipse.edc.policy.engine.spi.RuleBindingRegistry;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.spi.asset.AssetIndex;
import org.eclipse.edc.spi.monitor.Monitor;
Expand Down Expand Up @@ -136,6 +139,7 @@ public static WrapperExtensionContext buildContext(
PolicyDefinitionService policyDefinitionService,
PolicyDefinitionStore policyDefinitionStore,
PolicyEngine policyEngine,
RuleBindingRegistry ruleBindingRegistry,
TransferProcessService transferProcessService,
TransferProcessStore transferProcessStore,
TypeTransformerRegistry typeTransformerRegistry
Expand Down Expand Up @@ -251,10 +255,14 @@ public static WrapperExtensionContext buildContext(
miwConfigBuilder,
selfDescriptionService
);
var alwaysTruePolicyService = new AlwaysTruePolicyDefinitionService(
policyDefinitionService
);
var dataOfferPageApiService = new DataOfferPageApiService(
assetApiService,
contractDefinitionApiService,
policyDefinitionApiService
policyDefinitionApiService,
alwaysTruePolicyService
);
var uiResource = new UiResourceImpl(
contractAgreementApiService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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 de.sovity.edc.extension.policy.services.AlwaysTruePolicyDefinitionService;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.eclipse.edc.spi.types.domain.asset.Asset;
Expand All @@ -33,6 +34,7 @@ public class DataOfferPageApiService {
private final AssetApiService assetApiService;
private final ContractDefinitionApiService contractDefinitionApiService;
private final PolicyDefinitionApiService policyDefinitionApiService;
private final AlwaysTruePolicyDefinitionService alwaysTruePolicyDefinitionService;

@NotNull
public IdAvailabilityResponse checkIfPolicyIdAvailable(DSLContext dsl, String id) {
Expand Down Expand Up @@ -77,20 +79,33 @@ public IdResponseDto createDataOffer(DSLContext dsl, DataOfferCreationRequest da
};
}

private @NotNull IdResponseDto createAndPublishUnrestricted(DSLContext dsl, DataOfferCreationRequest dataOfferCreationRequest, String commonId) {
private @NotNull IdResponseDto createAndPublishUnrestricted(
DSLContext dsl,
DataOfferCreationRequest dataOfferCreationRequest,
String commonId
) {
val assetId = commonId;
val contractDefinitionId = commonId;
val policyId = AlwaysTruePolicyConstants.POLICY_DEFINITION_ID;

checkAssetIdAvailable(dsl, assetId);
checkContractDefinitionIdAvailable(dsl, contractDefinitionId);

if (!alwaysTruePolicyDefinitionService.exists()) {
// the default always-true policy has been deleted, recreate it.
alwaysTruePolicyDefinitionService.create();
}

assetApiService.createAsset(dataOfferCreationRequest.getUiAssetCreateRequest());

return createContractDefinition(assetId, policyId, contractDefinitionId);
}

private @NotNull IdResponseDto createAndPublishRestricted(DSLContext dsl, DataOfferCreationRequest dataOfferCreationRequest, String commonId) {
private @NotNull IdResponseDto createAndPublishRestricted(
DSLContext dsl,
DataOfferCreationRequest dataOfferCreationRequest,
String commonId
) {
val assetId = commonId;
val policyId = commonId;
val contractDefinitionId = commonId;
Expand All @@ -101,17 +116,20 @@ public IdResponseDto createDataOffer(DSLContext dsl, DataOfferCreationRequest da

assetApiService.createAsset(dataOfferCreationRequest.getUiAssetCreateRequest());

val maybeNewPolicy = Optional.ofNullable(dataOfferCreationRequest.getUiPolicyExpression());

maybeNewPolicy.ifPresent(
policy -> policyDefinitionApiService.createPolicyDefinitionV2(new PolicyDefinitionCreateDto(policyId, policy)));
val policyExpression = Optional.ofNullable(dataOfferCreationRequest.getUiPolicyExpression())
.orElseThrow(() -> new InvalidRequestException("Missing policy expression"));
policyDefinitionApiService.createPolicyDefinitionV2(new PolicyDefinitionCreateDto(policyId, policyExpression));

createContractDefinition(assetId, policyId, contractDefinitionId);

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

private @NotNull IdResponseDto createButDontPublish(DSLContext dsl, DataOfferCreationRequest dataOfferCreationRequest, String commonId) {
private @NotNull IdResponseDto createButDontPublish(
DSLContext dsl,
DataOfferCreationRequest dataOfferCreationRequest,
String commonId
) {
checkAssetIdAvailable(dsl, commonId);
return assetApiService.createAsset(dataOfferCreationRequest.getUiAssetCreateRequest());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class AlwaysTrueMigrationTest {
private static final E2eTestExtension E2E_TEST_EXTENSION = new E2eTestExtension(
withModule(":launchers:connectors:sovity-dev").toBuilder()
.consumerConfigCustomizer(config -> config.setProperty(
ConfigProps.EDC_FLYWAY_ADDITIONAL_MIGRATION_LOCATIONS, "classpath:db/additional-test-data/always-true-policy-legacy"
ConfigProps.EDC_FLYWAY_ADDITIONAL_MIGRATION_LOCATIONS, "classpath:db/additional-test-data/always-true-policy-migrated"
))
.providerConfigCustomizer(config -> config.setProperty(
ConfigProps.EDC_FLYWAY_ADDITIONAL_MIGRATION_LOCATIONS, "classpath:db/additional-test-data/always-true-policy-migrated"
ConfigProps.EDC_FLYWAY_ADDITIONAL_MIGRATION_LOCATIONS, "classpath:db/additional-test-data/always-true-policy-legacy"
))
.build()
);
Expand Down
59 changes: 55 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 @@ -583,8 +583,6 @@ void checkIdAvailability(E2eScenario scenario, @Provider EdcClient providerClien
scenario.createPolicy(policyId, OffsetDateTime.MIN, OffsetDateTime.MAX);
var contractDefinitionId = scenario.createContractDefinition(policyId, assetId);

val asset = providerClient.uiApi().getAssetPage();

// act
val negAssetResponse = providerClient.uiApi().isAssetIdAvailable(assetId);
val negPolicyResponse = providerClient.uiApi().isPolicyIdAvailable(policyId);
Expand Down Expand Up @@ -921,7 +919,6 @@ void dontCreateAnythingIfTheContractDefinitionAlreadyExists(

@Test
void reuseTheAlwaysTruePolicyWhenPublishingUnrestricted(
E2eScenario scenario,
@Provider EdcClient providerClient
) {
// arrange
Expand Down Expand Up @@ -964,7 +961,6 @@ void reuseTheAlwaysTruePolicyWhenPublishingUnrestricted(

@Test
void onlyCreateTheAssetWhenDontPublish(
E2eScenario scenario,
@Provider EdcClient providerClient
) {
// arrange
Expand Down Expand Up @@ -1000,6 +996,61 @@ void onlyCreateTheAssetWhenDontPublish(
.hasSize(0);
}

@Test
void recreateTheAlwaysTruePolicyIfDeleted(
@Provider EdcClient providerClient
) {
// arrange
val assetId = "assetId";
providerClient.uiApi().deletePolicyDefinition(AlwaysTruePolicyConstants.POLICY_DEFINITION_ID);

List<PolicyDefinitionDto> withoutDefaultAlwaysTrue =
providerClient.uiApi()
.getPolicyDefinitionPage()
.getPolicies()
.stream()
.filter(it -> !it.getPolicyDefinitionId().equals(AlwaysTruePolicyConstants.POLICY_DEFINITION_ID))
.toList();

assertThat(withoutDefaultAlwaysTrue).hasSize(0);

// 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);

List<PolicyDefinitionDto> policies =
providerClient.uiApi()
.getPolicyDefinitionPage()
.getPolicies()
.stream()
.toList();

assertThat(policies).hasSize(1);

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

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 17d2cff

Please sign in to comment.