Commit 9c15bfe4 authored by Fotis Soldatos's avatar Fotis Soldatos
Browse files

refactor(policy): implement builder pattern for EndPoint

parent 8a159159
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import context.ContextOuterClass.ConfigRule_Custom;
import context.ContextOuterClass.ContextId;
import context.ContextOuterClass.DeviceId;
import context.ContextOuterClass.DeviceOperationalStatusEnum;
import context.ContextOuterClass.Location.LocationCase;
import context.ContextOuterClass.Uuid;
import eu.teraflow.policy.acl.AclAction;
import eu.teraflow.policy.acl.AclEntry;
@@ -2109,12 +2110,14 @@ public class Serializer {
        final var serializedEndPointId = serialize(endPointId);
        final var serializedKpiSampleTypes =
                kpiSampleTypes.stream().map(this::serialize).collect(Collectors.toList());
        if (endPointLocation != null) {
            final var serializedEndPointLocation = serialize(endPointLocation);
            builder.setEndpointLocation(serializedEndPointLocation);
        }

        builder.setEndpointId(serializedEndPointId);
        builder.setEndpointType(endPointType);
        builder.addAllKpiSampleTypes(serializedKpiSampleTypes);
        builder.setEndpointLocation(serializedEndPointLocation);

        return builder.build();
    }
@@ -2128,9 +2131,15 @@ public class Serializer {
        final var endPointId = deserialize(serializedEndPointId);
        final var kpiSampleTypes =
                serializedKpiSampleTypes.stream().map(this::deserialize).collect(Collectors.toList());

        if (serializedEndPointLocation.getLocationCase() != LocationCase.LOCATION_NOT_SET) {
            final var endPointLocation = deserialize(serializedEndPointLocation);
            return new EndPoint.EndPointBuilder(endPointId, endPointType, kpiSampleTypes)
                    .location(endPointLocation)
                    .build();
        }

        return new EndPoint(endPointId, endPointType, kpiSampleTypes, endPointLocation);
        return new EndPoint.EndPointBuilder(endPointId, endPointType, kpiSampleTypes).build();
    }

    public ContextOuterClass.Device serialize(Device device) {
+48 −9
Original line number Diff line number Diff line
@@ -26,15 +26,11 @@ public class EndPoint {
    private final List<KpiSampleType> kpiSampleTypes;
    private final Location endPointLocation;

    public EndPoint(
            EndPointId endPointId,
            String endPointType,
            List<KpiSampleType> kpiSampleTypes,
            Location endPointLocation) {
        this.endPointId = endPointId;
        this.endPointType = endPointType;
        this.kpiSampleTypes = kpiSampleTypes;
        this.endPointLocation = endPointLocation;
    EndPoint(EndPointBuilder builder) {
        this.endPointId = builder.endPointId;
        this.endPointType = builder.endPointType;
        this.kpiSampleTypes = builder.kpiSampleTypes;
        this.endPointLocation = builder.endPointLocation;
    }

    public EndPointId getEndPointId() {
@@ -63,4 +59,47 @@ public class EndPoint {
                Util.toString(kpiSampleTypes),
                endPointLocation);
    }

    public static class EndPointBuilder {
        private final EndPointId endPointId;
        private final String endPointType;
        private final List<KpiSampleType> kpiSampleTypes;
        private Location endPointLocation;

        public EndPointBuilder(
                EndPointId endPointId, String endPointType, List<KpiSampleType> kpiSampleTypes) {
            this.endPointId = endPointId;
            this.endPointType = endPointType;
            this.kpiSampleTypes = kpiSampleTypes;
        }

        public EndPointBuilder location(Location endPointLocation) {
            this.endPointLocation = endPointLocation;
            return this;
        }

        public EndPoint build() {
            EndPoint endPoint = new EndPoint(this);
            validateEndPointObject(endPoint);
            return endPoint;
        }

        private void validateEndPointObject(EndPoint endPoint) {
            final var validatedEndPointId = endPoint.getEndPointId();
            final var validatedEndPointType = endPoint.getEndPointType();
            final var validatedKpiSampleTypes = endPoint.getKpiSampleTypes();

            if (validatedEndPointId == null) {
                throw new IllegalStateException("EndPoint ID cannot be null");
            }

            if (validatedEndPointType == null) {
                throw new IllegalStateException("EndPoint type cannot be null");
            }

            if (validatedKpiSampleTypes == null) {
                throw new IllegalStateException("Kpi sample types cannot be null");
            }
        }
    }
}
+124 −0
Original line number Diff line number Diff line
/*
* Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.teraflow.policy;

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

import eu.teraflow.policy.context.model.EndPoint;
import eu.teraflow.policy.context.model.EndPointId;
import eu.teraflow.policy.context.model.Location;
import eu.teraflow.policy.context.model.LocationTypeRegion;
import eu.teraflow.policy.context.model.TopologyId;
import eu.teraflow.policy.kpi_sample_types.model.KpiSampleType;
import io.quarkus.test.junit.QuarkusTest;
import java.util.List;
import org.junit.jupiter.api.Test;

@QuarkusTest
class EndPointCreationTest {

    @Test
    void shouldCreateEndPointObjectGivenAllAvailableFields() {
        final var expectedTopologyId = new TopologyId("contextId", "id");
        final var expectedDeviceId = "expectedDeviceId";
        final var expectedId = "expectedId";

        final var expectedEndPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId);
        final var expectedEndPointType = "expectedEndPointType";
        final var expectedKpiSampleTypes =
                List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED);

        final var expectedLocationType = new LocationTypeRegion("ATH");
        final var expectedEndPointLocation = new Location(expectedLocationType);

        final var expectedEndPoint =
                new EndPoint.EndPointBuilder(
                                expectedEndPointId, expectedEndPointType, expectedKpiSampleTypes)
                        .location(expectedEndPointLocation)
                        .build();

        assertThat(expectedEndPoint.getEndPointId()).isEqualTo(expectedEndPointId);
        assertThat(expectedEndPoint.getEndPointType()).isEqualTo(expectedEndPointType);
        assertThat(expectedEndPoint.getKpiSampleTypes()).isEqualTo(expectedKpiSampleTypes);
        assertThat(expectedEndPoint.getEndPointLocation()).isEqualTo(expectedEndPointLocation);
    }

    @Test
    void shouldCreateEndPointObjectGivenAllFieldsExceptFromLocation() {
        final var expectedTopologyId = new TopologyId("contextId", "id");
        final var expectedDeviceId = "expectedDeviceId";
        final var expectedId = "expectedId";

        final var expectedEndPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId);
        final var expectedEndPointType = "expectedEndPointType";
        final var expectedKpiSampleTypes =
                List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED);

        final var expectedEndPoint =
                new EndPoint.EndPointBuilder(
                                expectedEndPointId, expectedEndPointType, expectedKpiSampleTypes)
                        .build();

        assertThat(expectedEndPoint.getEndPointId()).isEqualTo(expectedEndPointId);
        assertThat(expectedEndPoint.getEndPointType()).isEqualTo(expectedEndPointType);
        assertThat(expectedEndPoint.getKpiSampleTypes()).isEqualTo(expectedKpiSampleTypes);
    }

    @Test
    void shouldThrowIllegalStateExceptionDuringCreationOfEndPointObjectUponMissingEndPointId() {
        final var expectedEndPointType = "expectedEndPointType";
        final var expectedKpiSampleTypes =
                List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED);

        final var endPoint =
                new EndPoint.EndPointBuilder(null, expectedEndPointType, expectedKpiSampleTypes);

        assertThatExceptionOfType(IllegalStateException.class).isThrownBy(endPoint::build);
    }

    @Test
    void shouldThrowIllegalStateExceptionDuringCreationOfEndPointObjectUponMissingEndPointType() {
        final var expectedTopologyId = new TopologyId("contextId", "id");
        final var expectedDeviceId = "expectedDeviceId";
        final var expectedId = "expectedId";

        final var expectedEndPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId);
        final var expectedKpiSampleTypes =
                List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED);

        final var endPoint =
                new EndPoint.EndPointBuilder(expectedEndPointId, null, expectedKpiSampleTypes);

        assertThatExceptionOfType(IllegalStateException.class).isThrownBy(endPoint::build);
    }

    @Test
    void shouldThrowIllegalStateExceptionDuringCreationOfEndPointObjectUponMissingKpiSampleTypes() {
        final var expectedTopologyId = new TopologyId("contextId", "id");
        final var expectedDeviceId = "expectedDeviceId";
        final var expectedId = "expectedId";

        final var expectedEndPointId = new EndPointId(expectedTopologyId, expectedDeviceId, expectedId);
        final var expectedEndPointType = "expectedEndPointType";

        final var endPoint =
                new EndPoint.EndPointBuilder(expectedEndPointId, expectedEndPointType, null);

        assertThatExceptionOfType(IllegalStateException.class).isThrownBy(endPoint::build);
    }
}
+22 −9
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ import eu.teraflow.policy.context.model.DeviceConfig;
import eu.teraflow.policy.context.model.DeviceDriverEnum;
import eu.teraflow.policy.context.model.DeviceOperationalStatus;
import eu.teraflow.policy.context.model.Empty;
import eu.teraflow.policy.context.model.EndPoint;
import eu.teraflow.policy.context.model.EndPoint.EndPointBuilder;
import eu.teraflow.policy.context.model.EndPointId;
import eu.teraflow.policy.context.model.Event;
import eu.teraflow.policy.context.model.EventTypeEnum;
@@ -3484,8 +3484,8 @@ class SerializerTest {

        final var locationTypeRegion = new LocationTypeRegion("ATH");
        final var location = new Location(locationTypeRegion);

        final var endPoint = new EndPoint(endPointId, endPointType, kpiSampleTypes, location);
        final var endPoint =
                new EndPointBuilder(endPointId, endPointType, kpiSampleTypes).location(location).build();

        final var serializedEndPointId = serializer.serialize(endPointId);
        final var serializedKpiSampleTypes =
@@ -3521,8 +3521,9 @@ class SerializerTest {
        final var expectedLocation = new Location(expectedLocationTypeRegion);

        final var expectedEndPoint =
                new EndPoint(
                        expectedEndPointId, expectedEndPointType, expectedKpiSampleTypes, expectedLocation);
                new EndPointBuilder(expectedEndPointId, expectedEndPointType, expectedKpiSampleTypes)
                        .location(expectedLocation)
                        .build();

        final var serializedEndPointId = serializer.serialize(expectedEndPointId);
        final var serializedKpiSampleTypes =
@@ -3575,7 +3576,10 @@ class SerializerTest {
                List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED);
        final var locationTypeRegionA = new LocationTypeRegion("ATH");
        final var locationA = new Location(locationTypeRegionA);
        final var endPointA = new EndPoint(endPointIdA, endPointTypeA, kpiSampleTypesA, locationA);
        final var endPointA =
                new EndPointBuilder(endPointIdA, endPointTypeA, kpiSampleTypesA)
                        .location(locationA)
                        .build();

        final var expectedTopologyIdB = new TopologyId("contextIdB", "idB");
        final var expectedDeviceIdB = "expectedDeviceIdB";
@@ -3587,7 +3591,10 @@ class SerializerTest {
                List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED);
        final var locationTypeRegionB = new LocationTypeRegion("ATH");
        final var locationB = new Location(locationTypeRegionB);
        final var endPointB = new EndPoint(endPointIdB, endPointTypeB, kpiSampleTypesB, locationB);
        final var endPointB =
                new EndPointBuilder(endPointIdB, endPointTypeB, kpiSampleTypesB)
                        .location(locationB)
                        .build();

        final var endPoints = List.of(endPointA, endPointB);

@@ -3654,7 +3661,10 @@ class SerializerTest {
                List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED);
        final var locationTypeRegionA = new LocationTypeRegion("ATH");
        final var locationA = new Location(locationTypeRegionA);
        final var endPointA = new EndPoint(endPointIdA, endPointTypeA, kpiSampleTypesA, locationA);
        final var endPointA =
                new EndPointBuilder(endPointIdA, endPointTypeA, kpiSampleTypesA)
                        .location(locationA)
                        .build();

        final var expectedTopologyIdB = new TopologyId("contextIdB", "idB");
        final var expectedDeviceIdB = "expectedDeviceIdB";
@@ -3666,7 +3676,10 @@ class SerializerTest {
                List.of(KpiSampleType.BYTES_RECEIVED, KpiSampleType.BYTES_TRANSMITTED);
        final var locationTypeRegionB = new LocationTypeRegion("ATH");
        final var locationB = new Location(locationTypeRegionB);
        final var endPointB = new EndPoint(endPointIdB, endPointTypeB, kpiSampleTypesB, locationB);
        final var endPointB =
                new EndPointBuilder(endPointIdB, endPointTypeB, kpiSampleTypesB)
                        .location(locationB)
                        .build();

        final var endPoints = List.of(endPointA, endPointB);

+0 −4
Original line number Diff line number Diff line
@@ -31,10 +31,6 @@ metadata:
    app.quarkus.io/commit-id: a25756b932a3146d95a993cf529e26e48654455e
    app.quarkus.io/build-timestamp: 2022-08-01 - 12:51:14 +0000
  labels:
    app: policyservice
    app.kubernetes.io/name: policyservice
  name: policyservice
spec:
  replicas: 1
  selector:
    matchLabels: