Skip to content

Commit

Permalink
chore: simplify test
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtreier committed Aug 18, 2023
1 parent 7b65c4a commit d207380
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,49 @@
package de.sovity.edc.client;


import com.fasterxml.jackson.databind.ObjectMapper;
import de.sovity.edc.client.gen.model.PolicyDefinitionCreateRequest;
import de.sovity.edc.client.gen.model.PolicyDefinitionDto;
import de.sovity.edc.client.gen.model.UiPolicyConstraint;
import de.sovity.edc.client.gen.model.UiPolicyCreateRequest;
import de.sovity.edc.client.gen.model.UiPolicyLiteral;
import de.sovity.edc.ext.wrapper.api.common.mappers.OperatorMapper;
import de.sovity.edc.ext.wrapper.api.common.mappers.PolicyMapper;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.AtomicConstraintMapper;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.ConstraintExtractor;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.LiteralMapper;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.PolicyValidator;
import de.sovity.edc.ext.wrapper.api.common.model.OperatorDto;
import de.sovity.edc.ext.wrapper.api.common.model.UiPolicyLiteralType;
import org.eclipse.edc.connector.policy.spi.PolicyDefinition;
import lombok.SneakyThrows;
import org.eclipse.edc.connector.spi.policydefinition.PolicyDefinitionService;
import org.eclipse.edc.junit.annotations.ApiTest;
import org.eclipse.edc.junit.extensions.EdcExtension;
import org.eclipse.edc.spi.entity.Entity;
import org.eclipse.edc.spi.query.QuerySpec;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.List;

import static de.sovity.edc.client.PolicyTestUtils.buildPolicyConstraints;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

@ApiTest
@ExtendWith(EdcExtension.class)
class PolicyDefinitionApiServiceTest {
private static final String DATA_SINK = "http://my-data-sink/api/stuff";
private static final String COUNTER_PARTY_ADDRESS = "http://some-other-connector/api/v1/ids/data";
ObjectMapper jsonLdObjectMapper;
ConstraintExtractor constraintExtractor;
AtomicConstraintMapper atomicConstraintMapper;
PolicyValidator policyValidator;
LiteralMapper literalMapper;
OperatorMapper operatorMapper;
PolicyMapper policyMapper;
EdcClient client;

UiPolicyConstraint constraint = UiPolicyConstraint.builder()
.left("a")
.operator(UiPolicyConstraint.OperatorEnum.EQ)
.right(UiPolicyLiteral.builder()
.type(UiPolicyLiteral.TypeEnum.STRING)
.value("b")
.build())
.build();

@BeforeEach
void setUp(EdcExtension extension) {
TestUtils.setupExtension(extension);
jsonLdObjectMapper = new ObjectMapper();
policyValidator = new PolicyValidator();
literalMapper = new LiteralMapper(jsonLdObjectMapper);
operatorMapper = new OperatorMapper();
atomicConstraintMapper = new AtomicConstraintMapper(literalMapper, operatorMapper);
constraintExtractor = new ConstraintExtractor(policyValidator, atomicConstraintMapper);
policyMapper = new PolicyMapper(jsonLdObjectMapper, constraintExtractor, atomicConstraintMapper);
client = TestUtils.edcClient();
}

@Test
void policyDefinitionPage(PolicyDefinitionService policyDefinitionService) {
void test_create_list() {
// arrange
var client = TestUtils.edcClient();
var constraint = de.sovity.edc.client.gen.model.UiPolicyConstraint.builder()
.left("left-1")
.operator(UiPolicyConstraint.OperatorEnum.EQ)
.right(UiPolicyLiteral.builder()
.type(UiPolicyLiteral.TypeEnum.STRING)
.value("right-1")
.build())
.build();

createPolicyDefinition(policyDefinitionService, List.of(constraint));
createPolicyDefinition("my-policy-def-1");

// act
var response = client.uiApi().policyPage();
Expand All @@ -90,144 +66,58 @@ void policyDefinitionPage(PolicyDefinitionService policyDefinitionService) {
var policyDefinitions = response.getPolicies();
assertThat(policyDefinitions).hasSize(1);
var policyDefinition = policyDefinitions.get(0);
assertThat(policyDefinition.getPolicyDefinitionId()).isEqualTo("my-policy-def-1");
assertThat(policyDefinition.getUiPolicyDto().getConstraints()).hasSize(1);

var constraintEntry = policyDefinition.getUiPolicyDto().getConstraints().get(0);
assertThat(constraintEntry.getLeft()).isEqualTo("left-1");
assertThat(constraintEntry.getOperator()).isEqualTo(UiPolicyConstraint.OperatorEnum.EQ);
assertThat(constraintEntry.getRight().getType()).isEqualTo(UiPolicyLiteral.TypeEnum.STRING);
assertThat(constraintEntry).usingRecursiveComparison().isEqualTo(constraint);
}

@Test
void PolicyDefinitionPageSorting(PolicyDefinitionService policyDefinitionService) {
void test_sorting(PolicyDefinitionService policyDefinitionService) {
// arrange
var client = TestUtils.edcClient();
var constraint1 = de.sovity.edc.client.gen.model.UiPolicyConstraint.builder()
.left("left-1")
.operator(UiPolicyConstraint.OperatorEnum.EQ)
.right(UiPolicyLiteral.builder()
.type(UiPolicyLiteral.TypeEnum.STRING)
.value("right-1")
.build())
.build();
var constraint2 = de.sovity.edc.client.gen.model.UiPolicyConstraint.builder()
.left("left-2")
.operator(UiPolicyConstraint.OperatorEnum.EQ)
.right(UiPolicyLiteral.builder()
.type(UiPolicyLiteral.TypeEnum.STRING)
.value("right-2")
.build())
.build();
var constraint3 = de.sovity.edc.client.gen.model.UiPolicyConstraint.builder()
.left("left-3")
.operator(UiPolicyConstraint.OperatorEnum.EQ)
.right(UiPolicyLiteral.builder()
.type(UiPolicyLiteral.TypeEnum.STRING)
.value("right-3")
.build())
.build();

createPolicyDefinition(policyDefinitionService, List.of(constraint1), 1628956802000L);
createPolicyDefinition(policyDefinitionService, List.of(constraint3), 1628956800000L);
createPolicyDefinition(policyDefinitionService, List.of(constraint2), 1628956801000L);
createPolicyDefinition(policyDefinitionService, "my-policy-def-2", 1628956802000L);
createPolicyDefinition(policyDefinitionService, "my-policy-def-0", 1628956800000L);
createPolicyDefinition(policyDefinitionService, "my-policy-def-1", 1628956801000L);

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

// assert
assertThat(result.getPolicies())
.extracting(policyDefinition -> policyDefinition.getUiPolicyDto().getConstraints().get(0).getLeft())
.containsExactly("left-1", "left-2", "left-3");
.extracting(PolicyDefinitionDto::getPolicyDefinitionId)
.containsExactly("my-policy-def-2", "my-policy-def-1", "my-policy-def-0");
}

@Test
void testPolicyDefinitionCreation(PolicyDefinitionService policyDefinitionService) {
/// arrange
var client = TestUtils.edcClient();
var constraint = de.sovity.edc.client.gen.model.UiPolicyConstraint.builder()
.left("left-1")
.operator(UiPolicyConstraint.OperatorEnum.EQ)
.right(UiPolicyLiteral.builder()
.type(UiPolicyLiteral.TypeEnum.STRING)
.value("right-1")
.build())
.build();

var policyRequest = UiPolicyCreateRequest.builder()
.constraints(List.of(constraint))
.build();
var policyDefinition = PolicyDefinitionCreateRequest.builder()
.uiPolicyDto(policyRequest)
.build();

// act
var response = client.uiApi().createPolicyDefinition(policyDefinition);

// assert
assertThat(response).isNotNull();
var policyDefinitions = policyDefinitionService.query(QuerySpec.max()).getContent().toList();
assertThat(policyDefinitions).hasSize(1);
var policyDefinitionEntry = policyDefinitions.get(0);
var policyDto = policyMapper.buildPolicyDto(policyDefinitionEntry.getPolicy());
assertThat(policyDto.getConstraints()).hasSize(1);

var constraintEntry = policyDto.getConstraints().get(0);
assertThat(constraintEntry.getLeft()).isEqualTo("left-1");
assertThat(constraintEntry.getOperator()).isEqualTo(OperatorDto.EQ);
assertThat(constraintEntry.getRight().getType()).isEqualTo(UiPolicyLiteralType.STRING);
}

@Test
void testDeletePolicyDefinition(PolicyDefinitionService policyDefinitionService) {
void test_delete(PolicyDefinitionService policyDefinitionService) {
// arrange
var client = TestUtils.edcClient();
var constraint = de.sovity.edc.client.gen.model.UiPolicyConstraint.builder()
.left("left-1")
.operator(UiPolicyConstraint.OperatorEnum.EQ)
.right(UiPolicyLiteral.builder()
.type(UiPolicyLiteral.TypeEnum.STRING)
.value("right-1")
.build())
.build();
createPolicyDefinition(policyDefinitionService, List.of(constraint));
assertThat(policyDefinitionService.query(QuerySpec.max()).getContent().toList()).hasSize(1);
var policyDefinition = policyDefinitionService.query(QuerySpec.max()).getContent().toList().get(0);

createPolicyDefinition("my-policy-def-1");
assertThat(policyDefinitionService.query(QuerySpec.max()).getContent().toList()).extracting(Entity::getId).containsExactly("my-policy-def-1");

// act
var response = client.uiApi().deletePolicyDefinition(policyDefinition.getId());
var response = client.uiApi().deletePolicyDefinition("my-policy-def-1");

// assert
assertThat(response.getId()).isEqualTo(policyDefinition.getId());
assertThat(response.getId()).isEqualTo("my-policy-def-1");
assertThat(policyDefinitionService.query(QuerySpec.max()).getContent()).isEmpty();
}

private void createPolicyDefinition(PolicyDefinitionService policyDefinitionService,
List<UiPolicyConstraint> constraints,
long createdAt) {
var generatedUiPolicyDto = UiPolicyCreateRequest.builder()
.constraints(constraints)
.build();
var uiPolicyDto = de.sovity.edc.ext.wrapper.api.common.model.UiPolicyCreateRequest.builder()
.constraints(buildPolicyConstraints(generatedUiPolicyDto.getConstraints()))
.build();
var policyDefinition = PolicyDefinition.Builder.newInstance()
.policy(policyMapper.buildPolicy(uiPolicyDto))
.createdAt(createdAt)
.build();
policyDefinitionService.create(policyDefinition);
private void createPolicyDefinition(String policyDefinitionId) {
var policy = new UiPolicyCreateRequest(List.of(constraint));
var policyDefinition = new PolicyDefinitionCreateRequest(policyDefinitionId, policy);
client.uiApi().createPolicyDefinition(policyDefinition);
}

private void createPolicyDefinition(PolicyDefinitionService policyDefinitionService, List<UiPolicyConstraint> constraints) {
var generatedUiPolicyDto = UiPolicyCreateRequest.builder()
.constraints(constraints)
.build();
var uiPolicyDto = de.sovity.edc.ext.wrapper.api.common.model.UiPolicyCreateRequest.builder()
.constraints(buildPolicyConstraints(generatedUiPolicyDto.getConstraints()))
.build();
var policyDefinition = PolicyDefinition.Builder.newInstance()
.policy(policyMapper.buildPolicy(uiPolicyDto))
.build();
policyDefinitionService.create(policyDefinition);
@SneakyThrows
private void createPolicyDefinition(PolicyDefinitionService policyDefinitionService, String policyDefinitionId, long createdAt) {
createPolicyDefinition(policyDefinitionId);
var policyDefinition = policyDefinitionService.findById(policyDefinitionId);

// Forcefully overwrite createdAt
var createdAtField = Entity.class.getDeclaredField("createdAt");
createdAtField.setAccessible(true);
createdAtField.set(policyDefinition, createdAt);
policyDefinitionService.update(policyDefinition);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
@RequiredArgsConstructor
@Schema(description = "Data for creating a Policy Definition")
public class PolicyDefinitionCreateRequest {
@Schema(description = "ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String policyDefinitionId;

@Schema(description = "UIPolicy Create Dto", requiredMode = Schema.RequiredMode.REQUIRED)
private UiPolicyCreateRequest uiPolicyDto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
@RequiredArgsConstructor
@Schema(description = "Policy Definition as required for the Policy Definition Page")
public class PolicyDefinitionDto {
@Schema(description = "ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String policyDefinitionId;

@Schema(description = "UIPolicy Dto", requiredMode = Schema.RequiredMode.REQUIRED)
private UiPolicyDto uiPolicyDto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ public List<PolicyDefinitionDto> getPolicies() {
var policyDefinitions = getAllPolicies();
return policyDefinitions.stream()
.sorted(Comparator.comparing(PolicyDefinition::getCreatedAt).reversed())
.map(policyDefinition -> {
var entry = buildPolicyDefinitionDto(policyDefinition);
return entry;
}).toList();
.map(this::buildPolicyDefinitionDto)
.toList();
}

@NotNull
Expand All @@ -59,14 +57,21 @@ public IdResponseDto deletePolicy(String policyDefinitionId) {
}

private List<PolicyDefinition> getAllPolicies() {
var policyDefinitions = policyDefinitionService.query(QuerySpec.max()).getContent().toList();
return policyDefinitions;
return policyDefinitionService.query(QuerySpec.max()).getContent().toList();
}
public PolicyDefinitionDto buildPolicyDefinitionDto(PolicyDefinition policyDefinition) {
return PolicyDefinitionDto.builder().uiPolicyDto(policyMapper.buildPolicyDto(policyDefinition.getPolicy())).build();
var policy = policyMapper.buildPolicyDto(policyDefinition.getPolicy());
return PolicyDefinitionDto.builder()
.policyDefinitionId(policyDefinition.getId())
.uiPolicyDto(policy)
.build();
}

public PolicyDefinition buildPolicyDefinition(PolicyDefinitionCreateRequest policyDefinitionDto) {
return PolicyDefinition.Builder.newInstance().policy(policyMapper.buildPolicy(policyDefinitionDto.getUiPolicyDto())).build();
var policy = policyMapper.buildPolicy(policyDefinitionDto.getUiPolicyDto());
return PolicyDefinition.Builder.newInstance()
.id(policyDefinitionDto.getPolicyDefinitionId())
.policy(policy)
.build();
}
}

0 comments on commit d207380

Please sign in to comment.