Skip to content

Commit

Permalink
chore: improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtreier committed Sep 19, 2023
1 parent a72cd04 commit f2f8a65
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import de.sovity.edc.client.gen.model.UiAsset;
import de.sovity.edc.client.gen.model.UiAssetCreateRequest;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyMapperUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.FailedMappingException;
import de.sovity.edc.utils.jsonld.vocab.Prop;
import lombok.SneakyThrows;
Expand All @@ -41,12 +41,12 @@
public class AssetApiServiceTest {

public static final String DATA_SINK = "http://my-data-sink/api/stuff";
EdcPropertyMapperUtils edcPropertyUtils;
EdcPropertyUtils edcPropertyUtils;

@BeforeEach
void setUp(EdcExtension extension) {
TestUtils.setupExtension(extension);
edcPropertyUtils = new EdcPropertyMapperUtils();
edcPropertyUtils = new EdcPropertyUtils();
}

@Test
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Map;

@RequiredArgsConstructor
public class EdcPropertyMapperUtils {
public class EdcPropertyUtils {

/**
* Converts a {@code Map<String, Object>} to {@code Map<String, String>}.
Expand All @@ -34,6 +34,10 @@ public class EdcPropertyMapperUtils {
public Map<String, String> truncateToMapOfString(Map<String, Object> map) {
Map<String, String> result = new HashMap<>();

if (map == null) {
return result;
}

for (Map.Entry<String, Object> entry : map.entrySet()) {
Object value = entry.getValue();

Expand All @@ -57,6 +61,9 @@ public Map<String, String> truncateToMapOfString(Map<String, Object> map) {

@SuppressWarnings({"unchecked", "rawtypes", "java:S1905"})
public Map<String, Object> toMapOfObject(Map<String, String> map) {
if (map == null) {
return Map.of();
}
return new HashMap<>((Map<String, Object>) (Map) map);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

@RequiredArgsConstructor
public class UiAssetMapper {
private final EdcPropertyMapperUtils edcPropertyMapperUtils;
private final EdcPropertyUtils edcPropertyUtils;
private final JsonLd jsonLd;

public UiAsset buildUiAsset(JsonObject assetJsonLd) {
Expand Down Expand Up @@ -122,7 +122,7 @@ private String trueIfTrue(Map<String, String> dataAddressProperties, String key)
}

private JsonObjectBuilder getDataAddress(UiAssetCreateRequest uiAssetCreateRequest) {
var props = edcPropertyMapperUtils.toMapOfObject(uiAssetCreateRequest.getDataAddressProperties());
var props = edcPropertyUtils.toMapOfObject(uiAssetCreateRequest.getDataAddressProperties());
return Json.createObjectBuilder()
.add(Prop.TYPE, Prop.Edc.TYPE_DATA_ADDRESS)
.add(Prop.Edc.PROPERTIES, Json.createObjectBuilder(props));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.sovity.edc.ext.wrapper.api.common.mappers;

import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyMapperUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.UiAssetMapper;
import de.sovity.edc.utils.JsonUtils;
import de.sovity.edc.utils.jsonld.vocab.Prop;
Expand All @@ -27,7 +27,7 @@ class AssetMapperTest {
void setup() {
var jsonLd = new TitaniumJsonLd(mock(Monitor.class));
var typeTransformerRegistry = mock(TypeTransformerRegistry.class);
var uiAssetBuilder = new UiAssetMapper(new EdcPropertyMapperUtils(), jsonLd);
var uiAssetBuilder = new UiAssetMapper(new EdcPropertyUtils(), jsonLd);
assetMapper = new AssetMapper(typeTransformerRegistry, uiAssetBuilder, jsonLd);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

package de.sovity.edc.ext.wrapper.api.common.mappers.utils;

import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyMapperUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -23,12 +22,12 @@

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

class EdcPropertyMapperUtilsTest {
EdcPropertyMapperUtils edcPropertyUtils;
class EdcPropertyUtilsTest {
EdcPropertyUtils edcPropertyUtils;

@BeforeEach
void setup() {
edcPropertyUtils = new EdcPropertyMapperUtils();
edcPropertyUtils = new EdcPropertyUtils();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
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.EdcPropertyMapperUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyUtils;
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.mappers.utils.UiAssetMapper;
Expand Down Expand Up @@ -119,8 +119,8 @@ public static WrapperExtensionContext buildContext(
constraintExtractor,
atomicConstraintMapper,
typeTransformerRegistry);
var edcPropertyMapperUtils = new EdcPropertyMapperUtils();
var assetBuilder = new UiAssetMapper(edcPropertyMapperUtils, jsonLd);
var edcPropertyUtils = new EdcPropertyUtils();
var assetBuilder = new UiAssetMapper(edcPropertyUtils, jsonLd);
var assetMapper = new AssetMapper(typeTransformerRegistry, assetBuilder, jsonLd);
var transferProcessStateService = new TransferProcessStateService();
var contractAgreementPageCardBuilder = new ContractAgreementPageCardBuilder(
Expand Down Expand Up @@ -159,7 +159,7 @@ public static WrapperExtensionContext buildContext(
objectMapper,
contractAgreementUtils,
contractNegotiationUtils,
edcPropertyMapperUtils,
edcPropertyUtils,
serviceExtensionContext.getConnectorId()
);
var contractAgreementTransferApiService = new ContractAgreementTransferApiService(
Expand Down Expand Up @@ -203,7 +203,7 @@ public static WrapperExtensionContext buildContext(
policyDefinitionStore,
contractDefinitionStore,
policyMappingService,
edcPropertyMapperUtils);
edcPropertyUtils);
var useCaseResource = new UseCaseResource(
kpiApiService,
supportedPolicyApiService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package de.sovity.edc.ext.wrapper.api.ui.pages.contracts.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyMapperUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyUtils;
import de.sovity.edc.ext.wrapper.api.ui.model.ContractAgreementTransferRequest;
import de.sovity.edc.ext.wrapper.api.ui.model.ContractAgreementTransferRequestParams;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,7 +24,6 @@
import org.eclipse.edc.protocol.dsp.spi.types.HttpMessageProtocol;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

Expand All @@ -34,7 +33,7 @@ public class TransferRequestBuilder {
private final ObjectMapper objectMapper;
private final ContractAgreementUtils contractAgreementUtils;
private final ContractNegotiationUtils contractNegotiationUtils;
private final EdcPropertyMapperUtils edcPropertyUtils;
private final EdcPropertyUtils edcPropertyUtils;
private final String connectorId;

public TransferRequest buildTransferRequest(
Expand Down Expand Up @@ -63,7 +62,7 @@ private TransferRequest buildTransferRequest(
.contractId(contractId)
.assetId(agreement.getAssetId())
.dataDestination(address)
.privateProperties(edcPropertyUtils.toMapOfObject(params.getTransferProcessProperties() == null ? Map.of() : params.getTransferProcessProperties()))
.privateProperties(edcPropertyUtils.toMapOfObject(params.getTransferProcessProperties()))
.callbackAddresses(List.of())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.sovity.edc.ext.wrapper.api.usecase.services;

import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyMapperUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyUtils;
import de.sovity.edc.ext.wrapper.api.usecase.model.AssetEntryDto;
import de.sovity.edc.ext.wrapper.api.usecase.model.ContractDefinitionRequestDto;
import de.sovity.edc.ext.wrapper.api.usecase.model.CreateOfferingDto;
Expand Down Expand Up @@ -31,7 +31,7 @@ public class OfferingService {
private final PolicyDefinitionStore policyDefinitionStore;
private final ContractDefinitionStore contractDefinitionStore;
private final PolicyMappingService policyMappingService;
private final EdcPropertyMapperUtils edcPropertyUtils;
private final EdcPropertyUtils edcPropertyUtils;

/**
* Creates the asset, policy and contract definition in the connector. First, transforms the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.sovity.edc.ext.wrapper.api.usecase.services;

import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyMapperUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyUtils;
import de.sovity.edc.ext.wrapper.api.common.model.PermissionDto;
import de.sovity.edc.ext.wrapper.api.common.model.PolicyDto;
import de.sovity.edc.ext.wrapper.api.usecase.model.AssetEntryDto;
Expand Down Expand Up @@ -60,7 +60,7 @@ void setUp() {
policyDefinitionStore,
contractDefinitionStore,
policyMappingService,
new EdcPropertyMapperUtils());
new EdcPropertyUtils());

this.assetEntryDto = assetDto();
this.asset = asset();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.utils;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

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

class MapUtilsTest {

@Test
void mapValues() {
assertThat(MapUtils.mapValues(Map.of(1, "a"), String::toUpperCase)).isEqualTo(Map.of(1, "A"));
}

@Test
void associateBy() {
assertThat(MapUtils.associateBy(List.of("a"), String::toUpperCase)).isEqualTo(Map.of("A", "a"));
}

@Test
void reverse() {
assertThat(MapUtils.reverse(Map.of("a", 1))).isEqualTo(Map.of(1, "a"));
}
}
Loading

0 comments on commit f2f8a65

Please sign in to comment.