Commit d9c1ea76 authored by Fotis Soldatos's avatar Fotis Soldatos
Browse files

feat(policy): add validation for PolicyRuleBasic object fields

parent 70d1a20a
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -140,6 +140,12 @@
            <artifactId>quarkus-arc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-config-yaml</artifactId>
+13 −0
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package eu.teraflow.policy.model;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import eu.teraflow.policy.common.Util;
import java.util.List;

@@ -35,11 +38,21 @@ public class PolicyRuleBasic {
            List<PolicyRuleCondition> policyRuleConditions,
            BooleanOperator booleanOperator,
            List<PolicyRuleAction> policyRuleActions) {
        checkNotNull(policyRuleId, "Policy rule ID must not be null.");
        checkArgument(!policyRuleId.isBlank(), "Policy rule ID must not be empty.");
        this.policyRuleId = policyRuleId;
        this.policyRuleState = policyRuleState;
        checkArgument(priority >= 0, "Priority value must be greater or equal than zero.");
        this.priority = priority;
        checkNotNull(policyRuleConditions, "Policy Rule conditions cannot be null.");
        checkArgument(!policyRuleConditions.isEmpty(), "Policy Rule conditions cannot be empty.");
        this.policyRuleConditions = policyRuleConditions;
        checkArgument(
                booleanOperator != BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED,
                "Boolean operator cannot be undefined");
        this.booleanOperator = booleanOperator;
        checkNotNull(policyRuleActions, "Policy Rule actions cannot be null.");
        checkArgument(!policyRuleActions.isEmpty(), "Policy Rule actions cannot be empty.");
        this.policyRuleActions = policyRuleActions;
    }

+359 −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.model.BooleanOperator;
import eu.teraflow.policy.model.NumericalOperator;
import eu.teraflow.policy.model.PolicyRuleAction;
import eu.teraflow.policy.model.PolicyRuleActionEnum;
import eu.teraflow.policy.model.PolicyRuleBasic;
import eu.teraflow.policy.model.PolicyRuleCondition;
import eu.teraflow.policy.model.PolicyRuleState;
import eu.teraflow.policy.model.RuleState;
import eu.teraflow.policy.monitoring.model.IntegerKpiValue;
import eu.teraflow.policy.monitoring.model.KpiValue;
import io.quarkus.test.junit.QuarkusTest;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.Test;

@QuarkusTest
class PolicyRuleBasicValidationTest {

    private PolicyRuleBasic createPolicyRuleBasic(
            String policyRuleId,
            int priority,
            PolicyRuleState policyRuleState,
            BooleanOperator booleanOperator,
            List<PolicyRuleCondition> policyRuleConditions,
            List<PolicyRuleAction> policyRuleActions) {

        return new PolicyRuleBasic(
                policyRuleId,
                policyRuleState,
                priority,
                policyRuleConditions,
                booleanOperator,
                policyRuleActions);
    }

    private List<PolicyRuleCondition> createPolicyRuleConditions(
            String kpiId, NumericalOperator numericalOperator, KpiValue kpiValue) {
        final var policyRuleCondition = new PolicyRuleCondition(kpiId, numericalOperator, kpiValue);

        return List.of(policyRuleCondition);
    }

    private List<PolicyRuleAction> createPolicyRuleActions(
            PolicyRuleActionEnum policyRuleActionEnum, List<String> parameters) {
        final var policyRuleAction = new PolicyRuleAction(policyRuleActionEnum, parameters);

        return List.of(policyRuleAction);
    }

    @Test
    void shouldThrowIllegalArgumentExceptionGivenNullPolicyRuleId() {
        final var policyRuleConditions =
                createPolicyRuleConditions(
                        UUID.randomUUID().toString(),
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN,
                        new IntegerKpiValue(3));
        final var policyRuleActions =
                createPolicyRuleActions(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_ADD_SERVICE_CONSTRAINT,
                        List.of(UUID.randomUUID().toString(), UUID.randomUUID().toString()));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_EFFECTIVE);

        assertThatExceptionOfType(NullPointerException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        null,
                                        3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                                        policyRuleConditions,
                                        policyRuleActions));
    }

    @Test
    void shouldThrowIllegalArgumentExceptionGivenEmptyPolicyRuleId() {
        final var policyRuleConditions =
                createPolicyRuleConditions(
                        UUID.randomUUID().toString(),
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN,
                        new IntegerKpiValue(3));
        final var policyRuleActions =
                createPolicyRuleActions(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_ADD_SERVICE_CONFIGRULE,
                        List.of(UUID.randomUUID().toString(), UUID.randomUUID().toString()));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_ENFORCED);

        assertThatExceptionOfType(IllegalArgumentException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        "",
                                        3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                                        policyRuleConditions,
                                        policyRuleActions));
    }

    @Test
    void shouldThrowIllegalArgumentExceptionGivenWhiteSpacedPolicyRuleId() {
        final var policyRuleConditions =
                createPolicyRuleConditions(
                        UUID.randomUUID().toString(),
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_NOT_EQUAL,
                        new IntegerKpiValue(3));
        final var policyRuleActions =
                createPolicyRuleActions(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
                        List.of(UUID.randomUUID().toString(), UUID.randomUUID().toString()));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_ENFORCED);

        assertThatExceptionOfType(IllegalArgumentException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        "  ",
                                        3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                                        policyRuleConditions,
                                        policyRuleActions));
    }

    @Test
    void shouldThrowIllegalArgumentExceptionGivenNegativePriority() {
        final var policyRuleConditions =
                createPolicyRuleConditions(
                        UUID.randomUUID().toString(),
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN_EQUAL,
                        new IntegerKpiValue(3));
        final var policyRuleActions =
                createPolicyRuleActions(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_SET_DEVICE_STATUS,
                        List.of(UUID.randomUUID().toString(), UUID.randomUUID().toString()));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_INSERTED);

        final var policyRuleId = UUID.randomUUID().toString();

        assertThatExceptionOfType(IllegalArgumentException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        policyRuleId,
                                        -3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                                        policyRuleConditions,
                                        policyRuleActions));
    }

    @Test
    void shouldThrowNullPointerExceptionGivenNullPolicyRuleConditions() {
        final var policyRuleActions =
                createPolicyRuleActions(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_ADD_SERVICE_CONFIGRULE,
                        List.of(UUID.randomUUID().toString(), UUID.randomUUID().toString()));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_ENFORCED);

        final var policyRuleId = UUID.randomUUID().toString();

        assertThatExceptionOfType(NullPointerException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        policyRuleId,
                                        3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                                        null,
                                        policyRuleActions));
    }

    @Test
    void shouldThrowIllegalArgumentExceptionGivenEmptyPolicyRuleConditions() {
        final var policyRuleConditions = Collections.<PolicyRuleCondition>emptyList();
        final var policyRuleActions =
                createPolicyRuleActions(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_ADD_SERVICE_CONFIGRULE,
                        List.of(UUID.randomUUID().toString(), UUID.randomUUID().toString()));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_REMOVED);

        final var policyRuleId = UUID.randomUUID().toString();

        assertThatExceptionOfType(IllegalArgumentException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        policyRuleId,
                                        3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                                        policyRuleConditions,
                                        policyRuleActions));
    }

    @Test
    void shouldThrowIllegalArgumentExceptionGivenUndefinedBooleanOperator() {
        final var policyRuleConditions =
                createPolicyRuleConditions(
                        UUID.randomUUID().toString(),
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN,
                        new IntegerKpiValue(3));
        final var policyRuleActions =
                createPolicyRuleActions(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_ADD_SERVICE_CONFIGRULE,
                        List.of(UUID.randomUUID().toString(), UUID.randomUUID().toString()));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_VALIDATED);

        final var policyRuleId = UUID.randomUUID().toString();

        assertThatExceptionOfType(IllegalArgumentException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        policyRuleId,
                                        3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED,
                                        policyRuleConditions,
                                        policyRuleActions));
    }

    @Test
    void shouldThrowNullPointerExceptionGivenNullPolicyRuleActions() {
        final var policyRuleConditions =
                createPolicyRuleConditions(
                        UUID.randomUUID().toString(),
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN,
                        new IntegerKpiValue(3));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_PROVISIONED);

        final var policyRuleId = UUID.randomUUID().toString();

        assertThatExceptionOfType(NullPointerException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        policyRuleId,
                                        3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                                        policyRuleConditions,
                                        null));
    }

    @Test
    void shouldThrowIllegalArgumentExceptionGivenEmptyPolicyPolicyRuleActions() {
        final var policyRuleConditions =
                createPolicyRuleConditions(
                        UUID.randomUUID().toString(),
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN,
                        new IntegerKpiValue(3));
        final var policyRuleActions = Collections.<PolicyRuleAction>emptyList();

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_FAILED);

        final var policyRuleId = UUID.randomUUID().toString();

        assertThatExceptionOfType(IllegalArgumentException.class)
                .isThrownBy(
                        () ->
                                createPolicyRuleBasic(
                                        policyRuleId,
                                        3,
                                        policyRuleState,
                                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                                        policyRuleConditions,
                                        policyRuleActions));
    }

    @Test
    void shouldCreatePolicyRuleBasicObject() {
        final var expectedPolicyRuleId = "expectedPolicyRuleId";
        final var expectedPolicyRuleState = new PolicyRuleState(RuleState.POLICY_EFFECTIVE);
        final var expectedPriority = 3;

        final var firstKpiValue = new IntegerKpiValue(22);

        final var firstExpectedPolicyRuleCondition =
                new PolicyRuleCondition(
                        "firstExpectedPolicyRuleConditionVariable",
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN,
                        firstKpiValue);

        final var expectedPolicyRuleConditions = List.of(firstExpectedPolicyRuleCondition);

        final var expectedBooleanOperator = BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR;

        final var firstExpectedPolicyRuleAction =
                new PolicyRuleAction(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_SET_DEVICE_STATUS,
                        List.of("parameter1", "parameter2"));

        final var expectedPolicyRuleActions = List.of(firstExpectedPolicyRuleAction);

        final var expectedPolicyRuleBasic =
                new PolicyRuleBasic(
                        expectedPolicyRuleId,
                        expectedPolicyRuleState,
                        expectedPriority,
                        expectedPolicyRuleConditions,
                        expectedBooleanOperator,
                        expectedPolicyRuleActions);

        final var policyRuleConditions =
                createPolicyRuleConditions(
                        "firstExpectedPolicyRuleConditionVariable",
                        NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN,
                        new IntegerKpiValue(22));
        final var policyRuleActions =
                createPolicyRuleActions(
                        PolicyRuleActionEnum.POLICY_RULE_ACTION_SET_DEVICE_STATUS,
                        List.of("parameter1", "parameter2"));

        final var policyRuleState = new PolicyRuleState(RuleState.POLICY_EFFECTIVE);

        final var policyRuleBasic =
                createPolicyRuleBasic(
                        "expectedPolicyRuleId",
                        3,
                        policyRuleState,
                        BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
                        policyRuleConditions,
                        policyRuleActions);

        assertThat(policyRuleBasic).usingRecursiveComparison().isEqualTo(expectedPolicyRuleBasic);
    }
}