Loading src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleCondition.java +31 −0 Original line number Diff line number Diff line Loading @@ -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.monitoring.model.KpiValue; public class PolicyRuleCondition { Loading @@ -26,8 +29,18 @@ public class PolicyRuleCondition { public PolicyRuleCondition( String kpiId, NumericalOperator numericalOperator, KpiValue<?> kpiValue) { checkNotNull(kpiId, "Kpi ID must not be null."); checkArgument(!kpiId.isBlank(), "Kpi ID must not be empty."); this.kpiId = kpiId; checkArgument( numericalOperator != NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_UNDEFINED, "Numerical operator cannot be undefined"); this.numericalOperator = numericalOperator; checkNotNull(kpiValue, "Kpi value must not be null."); checkArgument( isKpiValueValid(kpiValue), "Kpi value must be: String, Float, Boolean or Integer but it was [%s].", kpiValue.getValue().getClass().getName()); this.kpiValue = kpiValue; } Loading @@ -43,6 +56,24 @@ public class PolicyRuleCondition { return kpiValue; } private boolean isKpiValueValid(KpiValue<?> kpiValue) { final var kpiValueType = kpiValue.getValue(); if (kpiValueType instanceof String) { return true; } if (kpiValueType instanceof Boolean) { return true; } if (kpiValueType instanceof Integer) { return true; } return kpiValueType instanceof Float; } @Override public String toString() { return String.format( Loading src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleDevice.java +6 −0 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -25,7 +28,10 @@ public class PolicyRuleDevice { private final List<String> deviceIds; public PolicyRuleDevice(PolicyRuleBasic policyRuleBasic, List<String> deviceIds) { checkNotNull(policyRuleBasic, "PolicyRuleBasic must not be null."); this.policyRuleBasic = policyRuleBasic; checkNotNull(deviceIds, "Device Ids must not be null."); checkArgument(!deviceIds.isEmpty(), "Device Ids must not be empty."); this.deviceIds = deviceIds; } Loading src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleService.java +5 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package eu.teraflow.policy.model; import static com.google.common.base.Preconditions.checkNotNull; import eu.teraflow.policy.common.Util; import eu.teraflow.policy.context.model.ServiceId; import java.util.List; Loading @@ -28,8 +30,11 @@ public class PolicyRuleService { public PolicyRuleService( PolicyRuleBasic policyRuleBasic, ServiceId serviceId, List<String> deviceIds) { checkNotNull(policyRuleBasic, "PolicyRuleBasic must not be null."); this.policyRuleBasic = policyRuleBasic; checkNotNull(serviceId, "Service Id must not be null."); this.serviceId = serviceId; checkNotNull(deviceIds, "Device Ids must not be null."); this.deviceIds = deviceIds; } Loading src/policy/src/test/java/eu/teraflow/policy/PolicyRuleBasicValidationTest.java +2 −2 Original line number Diff line number Diff line Loading @@ -56,7 +56,7 @@ class PolicyRuleBasicValidationTest { } private List<PolicyRuleCondition> createPolicyRuleConditions( String kpiId, NumericalOperator numericalOperator, KpiValue kpiValue) { String kpiId, NumericalOperator numericalOperator, KpiValue<?> kpiValue) { final var policyRuleCondition = new PolicyRuleCondition(kpiId, numericalOperator, kpiValue); return List.of(policyRuleCondition); Loading @@ -70,7 +70,7 @@ class PolicyRuleBasicValidationTest { } @Test void shouldThrowIllegalArgumentExceptionGivenNullPolicyRuleId() { void shouldThrowNullPointerExceptionGivenNullPolicyRuleId() { final var policyRuleConditions = createPolicyRuleConditions( UUID.randomUUID().toString(), Loading src/policy/src/test/java/eu/teraflow/policy/PolicyRuleConditionValidationTest.java 0 → 100644 +139 −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.NumericalOperator; import eu.teraflow.policy.model.PolicyRuleCondition; import eu.teraflow.policy.monitoring.model.BooleanKpiValue; import eu.teraflow.policy.monitoring.model.FloatKpiValue; import eu.teraflow.policy.monitoring.model.IntegerKpiValue; import eu.teraflow.policy.monitoring.model.KpiValue; import eu.teraflow.policy.monitoring.model.StringKpiValue; import io.quarkus.test.junit.QuarkusTest; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mockito; @QuarkusTest class PolicyRuleConditionValidationTest { private PolicyRuleCondition createPolicyRuleCondition( String kpiId, NumericalOperator numericalOperator, KpiValue<?> kpiValue) { return new PolicyRuleCondition(kpiId, numericalOperator, kpiValue); } private static Stream<Arguments> provideKpiValues() { return Stream.of( Arguments.of(new StringKpiValue("stringKpiValue")), Arguments.of(new BooleanKpiValue(true)), Arguments.of(new IntegerKpiValue(44)), Arguments.of(new FloatKpiValue(12.3f))); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldThrowNullPointerExceptionGivenNullKpiId(KpiValue<?> kpiValue) { assertThatExceptionOfType(NullPointerException.class) .isThrownBy( () -> createPolicyRuleCondition( null, NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue)); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldThrowIllegalArgumentExceptionGivenEmptyKpiId(KpiValue<?> kpiValue) { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy( () -> createPolicyRuleCondition( "", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_NOT_EQUAL, kpiValue)); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldThrowIllegalArgumentExceptionGivenWhiteSpacedKpiId(KpiValue<?> kpiValue) { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy( () -> createPolicyRuleCondition( " ", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_NOT_EQUAL, kpiValue)); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldThrowIllegalArgumentExceptionGivenUndefinedNumericalOperator(KpiValue<?> kpiValue) { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy( () -> createPolicyRuleCondition( "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_UNDEFINED, kpiValue)); } @Test void shouldThrowNullPointerExceptionGivenNullKpiValue() { assertThatExceptionOfType(NullPointerException.class) .isThrownBy( () -> createPolicyRuleCondition( "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, null)); } @Test void shouldThrowIllegalArgumentExceptionIfIsKpiValueIsOfInvalidType() { final var kpiValue = Mockito.mock(KpiValue.class); Mockito.when(kpiValue.getValue()).thenReturn(1_2L); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy( () -> createPolicyRuleCondition( "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue)); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldCreatePolicyRuleConditionObject(KpiValue<?> kpiValue) { final var expectedKpiId = "expectedKpiId"; final var expectedNumericalOperator = NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN; final var expectedPolicyRuleCondition = new PolicyRuleCondition(expectedKpiId, expectedNumericalOperator, kpiValue); final var policyRuleCondition = createPolicyRuleCondition( "expectedKpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN, kpiValue); assertThat(policyRuleCondition) .usingRecursiveComparison() .isEqualTo(expectedPolicyRuleCondition); } } Loading
src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleCondition.java +31 −0 Original line number Diff line number Diff line Loading @@ -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.monitoring.model.KpiValue; public class PolicyRuleCondition { Loading @@ -26,8 +29,18 @@ public class PolicyRuleCondition { public PolicyRuleCondition( String kpiId, NumericalOperator numericalOperator, KpiValue<?> kpiValue) { checkNotNull(kpiId, "Kpi ID must not be null."); checkArgument(!kpiId.isBlank(), "Kpi ID must not be empty."); this.kpiId = kpiId; checkArgument( numericalOperator != NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_UNDEFINED, "Numerical operator cannot be undefined"); this.numericalOperator = numericalOperator; checkNotNull(kpiValue, "Kpi value must not be null."); checkArgument( isKpiValueValid(kpiValue), "Kpi value must be: String, Float, Boolean or Integer but it was [%s].", kpiValue.getValue().getClass().getName()); this.kpiValue = kpiValue; } Loading @@ -43,6 +56,24 @@ public class PolicyRuleCondition { return kpiValue; } private boolean isKpiValueValid(KpiValue<?> kpiValue) { final var kpiValueType = kpiValue.getValue(); if (kpiValueType instanceof String) { return true; } if (kpiValueType instanceof Boolean) { return true; } if (kpiValueType instanceof Integer) { return true; } return kpiValueType instanceof Float; } @Override public String toString() { return String.format( Loading
src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleDevice.java +6 −0 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -25,7 +28,10 @@ public class PolicyRuleDevice { private final List<String> deviceIds; public PolicyRuleDevice(PolicyRuleBasic policyRuleBasic, List<String> deviceIds) { checkNotNull(policyRuleBasic, "PolicyRuleBasic must not be null."); this.policyRuleBasic = policyRuleBasic; checkNotNull(deviceIds, "Device Ids must not be null."); checkArgument(!deviceIds.isEmpty(), "Device Ids must not be empty."); this.deviceIds = deviceIds; } Loading
src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleService.java +5 −0 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package eu.teraflow.policy.model; import static com.google.common.base.Preconditions.checkNotNull; import eu.teraflow.policy.common.Util; import eu.teraflow.policy.context.model.ServiceId; import java.util.List; Loading @@ -28,8 +30,11 @@ public class PolicyRuleService { public PolicyRuleService( PolicyRuleBasic policyRuleBasic, ServiceId serviceId, List<String> deviceIds) { checkNotNull(policyRuleBasic, "PolicyRuleBasic must not be null."); this.policyRuleBasic = policyRuleBasic; checkNotNull(serviceId, "Service Id must not be null."); this.serviceId = serviceId; checkNotNull(deviceIds, "Device Ids must not be null."); this.deviceIds = deviceIds; } Loading
src/policy/src/test/java/eu/teraflow/policy/PolicyRuleBasicValidationTest.java +2 −2 Original line number Diff line number Diff line Loading @@ -56,7 +56,7 @@ class PolicyRuleBasicValidationTest { } private List<PolicyRuleCondition> createPolicyRuleConditions( String kpiId, NumericalOperator numericalOperator, KpiValue kpiValue) { String kpiId, NumericalOperator numericalOperator, KpiValue<?> kpiValue) { final var policyRuleCondition = new PolicyRuleCondition(kpiId, numericalOperator, kpiValue); return List.of(policyRuleCondition); Loading @@ -70,7 +70,7 @@ class PolicyRuleBasicValidationTest { } @Test void shouldThrowIllegalArgumentExceptionGivenNullPolicyRuleId() { void shouldThrowNullPointerExceptionGivenNullPolicyRuleId() { final var policyRuleConditions = createPolicyRuleConditions( UUID.randomUUID().toString(), Loading
src/policy/src/test/java/eu/teraflow/policy/PolicyRuleConditionValidationTest.java 0 → 100644 +139 −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.NumericalOperator; import eu.teraflow.policy.model.PolicyRuleCondition; import eu.teraflow.policy.monitoring.model.BooleanKpiValue; import eu.teraflow.policy.monitoring.model.FloatKpiValue; import eu.teraflow.policy.monitoring.model.IntegerKpiValue; import eu.teraflow.policy.monitoring.model.KpiValue; import eu.teraflow.policy.monitoring.model.StringKpiValue; import io.quarkus.test.junit.QuarkusTest; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mockito; @QuarkusTest class PolicyRuleConditionValidationTest { private PolicyRuleCondition createPolicyRuleCondition( String kpiId, NumericalOperator numericalOperator, KpiValue<?> kpiValue) { return new PolicyRuleCondition(kpiId, numericalOperator, kpiValue); } private static Stream<Arguments> provideKpiValues() { return Stream.of( Arguments.of(new StringKpiValue("stringKpiValue")), Arguments.of(new BooleanKpiValue(true)), Arguments.of(new IntegerKpiValue(44)), Arguments.of(new FloatKpiValue(12.3f))); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldThrowNullPointerExceptionGivenNullKpiId(KpiValue<?> kpiValue) { assertThatExceptionOfType(NullPointerException.class) .isThrownBy( () -> createPolicyRuleCondition( null, NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue)); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldThrowIllegalArgumentExceptionGivenEmptyKpiId(KpiValue<?> kpiValue) { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy( () -> createPolicyRuleCondition( "", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_NOT_EQUAL, kpiValue)); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldThrowIllegalArgumentExceptionGivenWhiteSpacedKpiId(KpiValue<?> kpiValue) { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy( () -> createPolicyRuleCondition( " ", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_NOT_EQUAL, kpiValue)); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldThrowIllegalArgumentExceptionGivenUndefinedNumericalOperator(KpiValue<?> kpiValue) { assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy( () -> createPolicyRuleCondition( "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_UNDEFINED, kpiValue)); } @Test void shouldThrowNullPointerExceptionGivenNullKpiValue() { assertThatExceptionOfType(NullPointerException.class) .isThrownBy( () -> createPolicyRuleCondition( "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, null)); } @Test void shouldThrowIllegalArgumentExceptionIfIsKpiValueIsOfInvalidType() { final var kpiValue = Mockito.mock(KpiValue.class); Mockito.when(kpiValue.getValue()).thenReturn(1_2L); assertThatExceptionOfType(IllegalArgumentException.class) .isThrownBy( () -> createPolicyRuleCondition( "kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue)); } @ParameterizedTest @MethodSource("provideKpiValues") void shouldCreatePolicyRuleConditionObject(KpiValue<?> kpiValue) { final var expectedKpiId = "expectedKpiId"; final var expectedNumericalOperator = NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN; final var expectedPolicyRuleCondition = new PolicyRuleCondition(expectedKpiId, expectedNumericalOperator, kpiValue); final var policyRuleCondition = createPolicyRuleCondition( "expectedKpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN, kpiValue); assertThat(policyRuleCondition) .usingRecursiveComparison() .isEqualTo(expectedPolicyRuleCondition); } }