Skip to content
Snippets Groups Projects
Commit 2b473b13 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Merge branch 'feat/111-ubi-unit-tests-for-policy-component' into 'develop'

Resolve "(UBI) Unit Tests for Policy component"

Closes #111

See merge request !201
parents c4288010 f4e3f080
No related branches found
No related tags found
2 merge requests!235Release TeraFlowSDN 3.0,!201Resolve "(UBI) Unit Tests for Policy component"
Showing
with 1076 additions and 50 deletions
......@@ -16,6 +16,8 @@
package org.etsi.tfs.policy;
import static org.etsi.tfs.policy.common.ApplicationProperties.*;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.groups.UniJoin;
......@@ -64,8 +66,7 @@ import org.jboss.logging.Logger;
public class PolicyServiceImpl implements PolicyService {
private static final Logger LOGGER = Logger.getLogger(PolicyServiceImpl.class);
private static final String INVALID_MESSAGE = "%s is invalid.";
private static final String VALID_MESSAGE = "%s is valid.";
private static final int POLICY_EVALUATION_TIMEOUT = 5;
private static final int ACCEPTABLE_NUMBER_OF_ALARMS = 3;
private static final int MONITORING_WINDOW_IN_SECONDS = 5;
......@@ -74,39 +75,6 @@ public class PolicyServiceImpl implements PolicyService {
// Temporary solution for not calling the same rpc more than it's needed
private static int noAlarms = 0;
private static final PolicyRuleState INSERTED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_INSERTED, "Successfully entered to INSERTED state");
private static final PolicyRuleState VALIDATED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_VALIDATED, "Successfully transitioned to VALIDATED state");
private static final PolicyRuleState PROVISIONED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_PROVISIONED,
"Successfully transitioned from VALIDATED to PROVISIONED state");
private static final PolicyRuleState ACTIVE_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_ACTIVE,
"Successfully transitioned from PROVISIONED to ACTIVE state");
private static final PolicyRuleState ENFORCED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_ENFORCED,
"Successfully transitioned from ACTIVE to ENFORCED state");
private static final PolicyRuleState INEFFECTIVE_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_INEFFECTIVE,
"Transitioned from ENFORCED to INEFFECTIVE state");
private static final PolicyRuleState EFFECTIVE_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_EFFECTIVE,
"Successfully transitioned from ENFORCED to EFFECTIVE state");
private static final PolicyRuleState UPDATED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_UPDATED, "Successfully entered to UPDATED state");
private static final PolicyRuleState REMOVED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_REMOVED, "Successfully entered to REMOVED state");
private final ContextService contextService;
private final MonitoringService monitoringService;
private final ServiceService serviceService;
......@@ -457,7 +425,9 @@ public class PolicyServiceImpl implements PolicyService {
policyRuleBasic.getPolicyRuleId()));
contextService.removePolicyRule(policyId).subscribe().with(x -> {});
subscriptionList.get(policyId).cancel();
// TODO: When the Map doesn't contains the policyId we should throw an exception?
if (subscriptionList.contains(policyId)) subscriptionList.get(policyId).cancel();
return policyRuleBasic.getPolicyRuleState();
});
......
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy.common;
import org.etsi.tfs.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.model.PolicyRuleStateEnum;
public class ApplicationProperties {
public static final String INVALID_MESSAGE = "%s is invalid.";
public static final String VALID_MESSAGE = "%s is valid.";
public static final PolicyRuleState INSERTED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_INSERTED, "Successfully entered to INSERTED state");
public static final PolicyRuleState VALIDATED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_VALIDATED, "Successfully transitioned to VALIDATED state");
public static final PolicyRuleState PROVISIONED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_PROVISIONED,
"Successfully transitioned from VALIDATED to PROVISIONED state");
public static final PolicyRuleState ACTIVE_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_ACTIVE,
"Successfully transitioned from PROVISIONED to ACTIVE state");
public static final PolicyRuleState ENFORCED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_ENFORCED,
"Successfully transitioned from ACTIVE to ENFORCED state");
public static final PolicyRuleState INEFFECTIVE_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_INEFFECTIVE,
"Transitioned from ENFORCED to INEFFECTIVE state");
public static final PolicyRuleState EFFECTIVE_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_EFFECTIVE,
"Successfully transitioned from ENFORCED to EFFECTIVE state");
public static final PolicyRuleState UPDATED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_UPDATED, "Successfully entered to UPDATED state");
public static final PolicyRuleState REMOVED_POLICYRULE_STATE =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_REMOVED, "Successfully entered to REMOVED state");
}
......@@ -64,7 +64,7 @@ public class PolicyRuleBasic {
this.booleanOperator = BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED;
this.policyRuleActions = new ArrayList<PolicyRuleAction>();
this.isValid = false;
this.exceptionMessage = e.toString();
this.exceptionMessage = e.getMessage();
}
}
......
......@@ -40,7 +40,7 @@ public class PolicyRuleDevice {
this.policyRuleBasic = policyRuleBasic;
this.deviceIds = new ArrayList<String>();
this.isValid = false;
this.exceptionMessage = e.toString();
this.exceptionMessage = e.getMessage();
}
}
......
......@@ -50,7 +50,7 @@ public class PolicyRuleService {
this.serviceId = new ServiceId("", "");
this.deviceIds = new ArrayList<String>();
this.isValid = false;
this.exceptionMessage = e.toString();
this.exceptionMessage = e.getMessage();
}
}
......
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.etsi.tfs.policy.common.ApplicationProperties.INVALID_MESSAGE;
import static org.etsi.tfs.policy.common.ApplicationProperties.VALIDATED_POLICYRULE_STATE;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.inject.Inject;
import org.etsi.tfs.policy.context.ContextService;
import org.etsi.tfs.policy.model.BooleanOperator;
import org.etsi.tfs.policy.model.NumericalOperator;
import org.etsi.tfs.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.model.PolicyRuleDevice;
import org.etsi.tfs.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
class PolicyAddDeviceTest {
@Inject PolicyServiceImpl policyService;
@InjectMock PolicyRuleConditionValidator policyRuleConditionValidator;
@InjectMock ContextService contextService;
static PolicyRuleBasic policyRuleBasic;
static PolicyRuleDevice policyRuleDevice;
@BeforeAll
static void init() {
String policyId = "policyRuleId";
KpiValue kpiValue = new IntegerKpiValue(100);
PolicyRuleCondition policyRuleCondition =
new PolicyRuleCondition(
"kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue);
PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value");
PolicyRuleAction policyRuleAction =
new PolicyRuleAction(
PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
Arrays.asList(policyRuleActionConfig));
policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
Arrays.asList(policyRuleCondition),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
Arrays.asList(policyRuleAction));
List<String> deviceIds = Arrays.asList("device1", "device2");
policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, deviceIds);
}
@Test
void deviceListMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Device Ids must not be empty.");
policyService
.addPolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void isPolicyRuleBasicValid() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleBasic policyRuleBasic =
new PolicyRuleBasic(
"policyId",
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
0,
new ArrayList<>(),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
new ArrayList<>());
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED, "Policy Rule conditions cannot be empty.");
policyService
.addPolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void isPolicyRuleIdValid() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED,
String.format(
INVALID_MESSAGE, policyRuleDevice.getPolicyRuleBasic().getPolicyRuleId()));
Mockito.when(policyRuleConditionValidator.isUpdatedPolicyRuleIdValid(Mockito.anyString()))
.thenReturn(Uni.createFrom().item(Boolean.FALSE));
policyService
.addPolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void successPolicyDevice() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_VALIDATED,
VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage());
Mockito.when(policyRuleConditionValidator.isDeviceIdValid(Mockito.anyString()))
.thenReturn(Uni.createFrom().item(Boolean.TRUE));
policyService
.addPolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.etsi.tfs.policy.common.ApplicationProperties.*;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.inject.Inject;
import org.etsi.tfs.policy.context.ContextService;
import org.etsi.tfs.policy.context.model.Service;
import org.etsi.tfs.policy.context.model.ServiceId;
import org.etsi.tfs.policy.context.model.ServiceTypeEnum;
import org.etsi.tfs.policy.model.BooleanOperator;
import org.etsi.tfs.policy.model.NumericalOperator;
import org.etsi.tfs.policy.model.PolicyRule;
import org.etsi.tfs.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.model.PolicyRuleService;
import org.etsi.tfs.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.monitoring.MonitoringService;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
public class PolicyAddServiceTest {
@Inject PolicyServiceImpl policyService;
@InjectMock PolicyRuleConditionValidator policyRuleConditionValidator;
@InjectMock ContextService contextService;
@InjectMock MonitoringService monitoringService;
static PolicyRuleBasic policyRuleBasic;
static PolicyRuleService policyRuleService;
@BeforeAll
static void init() {
String policyId = "policyRuleId";
KpiValue kpiValue = new IntegerKpiValue(100);
PolicyRuleCondition policyRuleCondition =
new PolicyRuleCondition(
"kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue);
PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value");
PolicyRuleAction policyRuleAction =
new PolicyRuleAction(
PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
Arrays.asList(policyRuleActionConfig));
policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
Arrays.asList(policyRuleCondition),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
Arrays.asList(policyRuleAction));
ServiceId serviceId = new ServiceId("contextId", "serviceId");
Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0);
List<String> deviceIds = Arrays.asList("device1", "device2");
policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
}
@Test
void contextOrServiceIdMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("", "");
List<String> deviceIds = Arrays.asList("device1", "device2");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED, "Context Id of Service Id must not be empty.");
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void serviceIdMustNotBeEmpty() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("sdf", "");
List<String> deviceIds = Arrays.asList("device1", "device2");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Service Id must not be empty.");
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(item -> message.complete(item));
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void policyRuleIdMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
String policyId = "";
PolicyRuleBasic policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
new ArrayList<>(),
null,
new ArrayList<>());
ServiceId serviceId = new ServiceId("contextId", "serviceId");
Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0);
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Policy rule ID must not be empty.");
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void checkMessageIfServiceIsNotValid()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("contextId", "serviceId");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, serviceId + " is invalid.");
Mockito.when(
policyRuleConditionValidator.isServiceIdValid(
Mockito.any(ServiceId.class), Mockito.anyList()))
.thenReturn(Uni.createFrom().item(Boolean.FALSE));
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void policyServiceSuccess()
throws ExecutionException, InterruptedException, TimeoutException, IOException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_VALIDATED,
VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage());
Mockito.when(
policyRuleConditionValidator.isServiceIdValid(
Mockito.any(ServiceId.class), Mockito.anyList()))
.thenReturn(Uni.createFrom().item(Boolean.TRUE));
Mockito.when(contextService.setPolicyRule(Mockito.any(PolicyRule.class)))
.thenReturn(Uni.createFrom().item("policyRuleId"));
policyService
.addPolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.etsi.tfs.policy.common.ApplicationProperties.REMOVED_POLICYRULE_STATE;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.inject.Inject;
import org.etsi.tfs.policy.context.ContextService;
import org.etsi.tfs.policy.context.model.Service;
import org.etsi.tfs.policy.context.model.ServiceId;
import org.etsi.tfs.policy.context.model.ServiceTypeEnum;
import org.etsi.tfs.policy.model.BooleanOperator;
import org.etsi.tfs.policy.model.NumericalOperator;
import org.etsi.tfs.policy.model.PolicyRule;
import org.etsi.tfs.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.model.PolicyRuleService;
import org.etsi.tfs.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.model.PolicyRuleType;
import org.etsi.tfs.policy.model.PolicyRuleTypeService;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
class PolicyDeleteServiceTest {
@Inject PolicyServiceImpl policyService;
@InjectMock ContextService contextService;
static PolicyRuleBasic policyRuleBasic;
static PolicyRuleService policyRuleService;
@BeforeAll
static void init() {
String policyId = "policyRuleId";
KpiValue kpiValue = new IntegerKpiValue(100);
PolicyRuleCondition policyRuleCondition =
new PolicyRuleCondition(
"kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue);
PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value");
PolicyRuleAction policyRuleAction =
new PolicyRuleAction(
PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
Arrays.asList(policyRuleActionConfig));
policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
Arrays.asList(policyRuleCondition),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
Arrays.asList(policyRuleAction));
ServiceId serviceId = new ServiceId("contextId", "serviceId");
Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0);
List<String> deviceIds = Arrays.asList("device1", "device2");
policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
}
@Test
void contextOrServiceIdMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
String policyRuleId = "";
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_REMOVED,
REMOVED_POLICYRULE_STATE.getPolicyRuleStateMessage());
PolicyRuleType policyRuleType = new PolicyRuleTypeService(policyRuleService);
PolicyRule policyRule = new PolicyRule(policyRuleType);
Mockito.when(contextService.getPolicyRule(Mockito.anyString()))
.thenReturn(Uni.createFrom().item(policyRule));
policyService
.deletePolicy(policyRuleId)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
}
......@@ -46,14 +46,14 @@ import policy.PolicyCondition.PolicyRuleCondition;
import policy.PolicyService;
@QuarkusTest
class PolicyServiceTest {
private static final Logger LOGGER = Logger.getLogger(PolicyServiceTest.class);
class PolicyGrpcServiceTest {
private static final Logger LOGGER = Logger.getLogger(PolicyGrpcServiceTest.class);
@GrpcClient PolicyService client;
private final Serializer serializer;
@Inject
PolicyServiceTest(Serializer serializer) {
PolicyGrpcServiceTest(Serializer serializer) {
this.serializer = serializer;
}
......
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.etsi.tfs.policy.common.ApplicationProperties.INVALID_MESSAGE;
import static org.etsi.tfs.policy.common.ApplicationProperties.VALIDATED_POLICYRULE_STATE;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.inject.Inject;
import org.etsi.tfs.policy.model.BooleanOperator;
import org.etsi.tfs.policy.model.NumericalOperator;
import org.etsi.tfs.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.model.PolicyRuleDevice;
import org.etsi.tfs.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
class PolicyUpdateDeviceTest {
@Inject PolicyServiceImpl policyService;
@InjectMock PolicyRuleConditionValidator policyRuleConditionValidator;
static PolicyRuleBasic policyRuleBasic;
static PolicyRuleDevice policyRuleDevice;
@BeforeAll
static void init() {
String policyId = "policyRuleId";
KpiValue kpiValue = new IntegerKpiValue(100);
PolicyRuleCondition policyRuleCondition =
new PolicyRuleCondition(
"kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue);
PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value");
PolicyRuleAction policyRuleAction =
new PolicyRuleAction(
PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
Arrays.asList(policyRuleActionConfig));
policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
Arrays.asList(policyRuleCondition),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
Arrays.asList(policyRuleAction));
List<String> deviceIds = Arrays.asList("device1", "device2");
policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, deviceIds);
}
@Test
void deviceListMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice = new PolicyRuleDevice(policyRuleBasic, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Device Ids must not be empty.");
policyService
.updatePolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void isPolicyRuleBasicValid() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleBasic policyRuleBasic =
new PolicyRuleBasic(
"policyId",
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
0,
new ArrayList<>(),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
new ArrayList<>());
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED, "Policy Rule conditions cannot be empty.");
policyService
.updatePolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void isUpdatedPolicyRuleIdValid()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1", "device2"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED,
String.format(
INVALID_MESSAGE, policyRuleDevice.getPolicyRuleBasic().getPolicyRuleId()));
Mockito.when(policyRuleConditionValidator.isUpdatedPolicyRuleIdValid(Mockito.anyString()))
.thenReturn(Uni.createFrom().item(Boolean.FALSE));
policyService
.updatePolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void successUpdatePolicyService()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
PolicyRuleDevice policyRuleDevice =
new PolicyRuleDevice(policyRuleBasic, Arrays.asList("device1"));
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_VALIDATED,
VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage());
Mockito.when(policyRuleConditionValidator.isUpdatedPolicyRuleIdValid(Mockito.anyString()))
.thenReturn(Uni.createFrom().item(Boolean.TRUE));
policyService
.updatePolicyDevice(policyRuleDevice)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
}
/*
* Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
*
* 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 org.etsi.tfs.policy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.etsi.tfs.policy.common.ApplicationProperties.INVALID_MESSAGE;
import static org.etsi.tfs.policy.common.ApplicationProperties.VALIDATED_POLICYRULE_STATE;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.smallrye.mutiny.Uni;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.inject.Inject;
import org.etsi.tfs.policy.context.ContextService;
import org.etsi.tfs.policy.context.model.Service;
import org.etsi.tfs.policy.context.model.ServiceId;
import org.etsi.tfs.policy.context.model.ServiceTypeEnum;
import org.etsi.tfs.policy.model.BooleanOperator;
import org.etsi.tfs.policy.model.NumericalOperator;
import org.etsi.tfs.policy.model.PolicyRuleAction;
import org.etsi.tfs.policy.model.PolicyRuleActionConfig;
import org.etsi.tfs.policy.model.PolicyRuleActionEnum;
import org.etsi.tfs.policy.model.PolicyRuleBasic;
import org.etsi.tfs.policy.model.PolicyRuleCondition;
import org.etsi.tfs.policy.model.PolicyRuleService;
import org.etsi.tfs.policy.model.PolicyRuleState;
import org.etsi.tfs.policy.model.PolicyRuleStateEnum;
import org.etsi.tfs.policy.monitoring.MonitoringService;
import org.etsi.tfs.policy.monitoring.model.IntegerKpiValue;
import org.etsi.tfs.policy.monitoring.model.KpiValue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
class PolicyUpdateServiceTest {
@Inject PolicyServiceImpl policyService;
@InjectMock PolicyRuleConditionValidator policyRuleConditionValidator;
@InjectMock ContextService contextService;
@InjectMock MonitoringService monitoringService;
static PolicyRuleBasic policyRuleBasic;
static PolicyRuleService policyRuleService;
@BeforeAll
static void init() {
String policyId = "policyRuleId";
KpiValue kpiValue = new IntegerKpiValue(100);
PolicyRuleCondition policyRuleCondition =
new PolicyRuleCondition(
"kpiId", NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN, kpiValue);
PolicyRuleActionConfig policyRuleActionConfig = new PolicyRuleActionConfig("key", "value");
PolicyRuleAction policyRuleAction =
new PolicyRuleAction(
PolicyRuleActionEnum.POLICY_RULE_ACTION_NO_ACTION,
Arrays.asList(policyRuleActionConfig));
policyRuleBasic =
new PolicyRuleBasic(
policyId,
new PolicyRuleState(PolicyRuleStateEnum.POLICY_INSERTED, "Failed due to some errors"),
1,
Arrays.asList(policyRuleCondition),
BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_OR,
Arrays.asList(policyRuleAction));
ServiceId serviceId = new ServiceId("contextId", "serviceId");
Service service = new Service(serviceId, ServiceTypeEnum.UNKNOWN, null, null, null, null, 0.0);
List<String> deviceIds = Arrays.asList("device1", "device2");
policyRuleService = new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
}
@Test
void contextOrServiceIdMustNotBeEmpty()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("", "");
List<String> deviceIds = Arrays.asList("device1", "device2");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED, "Context Id of Service Id must not be empty.");
policyService
.updatePolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void serviceIdMustNotBeEmpty() throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("sdf", "");
List<String> deviceIds = Arrays.asList("device1", "device2");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, deviceIds);
PolicyRuleState expectedResult =
new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, "Service Id must not be empty.");
policyService
.updatePolicyService(policyRuleService)
.subscribe()
.with(item -> message.complete(item));
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void checkMessageIfServiceIsNotValid()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("contextId", "serviceId");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_FAILED, String.format(INVALID_MESSAGE, serviceId));
Mockito.when(
policyRuleConditionValidator.isPolicyRuleServiceValid(
Mockito.anyString(), Mockito.any(ServiceId.class)))
.thenReturn(Uni.createFrom().item(Boolean.FALSE));
policyService
.updatePolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
@Test
void successUpdatePolicyService()
throws ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<PolicyRuleState> message = new CompletableFuture<>();
ServiceId serviceId = new ServiceId("contextId", "serviceId");
PolicyRuleService policyRuleService =
new PolicyRuleService(policyRuleBasic, serviceId, new ArrayList<>());
PolicyRuleState expectedResult =
new PolicyRuleState(
PolicyRuleStateEnum.POLICY_VALIDATED,
VALIDATED_POLICYRULE_STATE.getPolicyRuleStateMessage());
Mockito.when(
policyRuleConditionValidator.isPolicyRuleServiceValid(
Mockito.anyString(), Mockito.any(ServiceId.class)))
.thenReturn(Uni.createFrom().item(Boolean.TRUE));
policyService
.updatePolicyService(policyRuleService)
.subscribe()
.with(
item -> {
message.complete(item);
});
assertThat(message.get(5, TimeUnit.SECONDS).getPolicyRuleStateMessage())
.isEqualTo(expectedResult.getPolicyRuleStateMessage().toString());
}
}
......@@ -3,8 +3,8 @@ apiVersion: v1
kind: Service
metadata:
annotations:
app.quarkus.io/commit-id: 5f8866be9cb91871607627819258b0b375410467
app.quarkus.io/build-timestamp: 2024-01-26 - 16:40:15 +0000
app.quarkus.io/commit-id: 47e6691312515be37e2d9ffa85a1ee165a66c9db
app.quarkus.io/build-timestamp: 2024-02-09 - 14:52:23 +0000
prometheus.io/scrape: "true"
prometheus.io/path: /q/metrics
prometheus.io/port: "8080"
......@@ -29,8 +29,8 @@ apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
app.quarkus.io/commit-id: 5f8866be9cb91871607627819258b0b375410467
app.quarkus.io/build-timestamp: 2024-01-26 - 16:40:15 +0000
app.quarkus.io/commit-id: 47e6691312515be37e2d9ffa85a1ee165a66c9db
app.quarkus.io/build-timestamp: 2024-02-09 - 14:52:23 +0000
prometheus.io/scrape: "true"
prometheus.io/path: /q/metrics
prometheus.io/port: "8080"
......@@ -47,8 +47,8 @@ spec:
template:
metadata:
annotations:
app.quarkus.io/commit-id: 5f8866be9cb91871607627819258b0b375410467
app.quarkus.io/build-timestamp: 2024-01-26 - 16:40:15 +0000
app.quarkus.io/commit-id: 47e6691312515be37e2d9ffa85a1ee165a66c9db
app.quarkus.io/build-timestamp: 2024-02-09 - 14:52:23 +0000
prometheus.io/scrape: "true"
prometheus.io/path: /q/metrics
prometheus.io/port: "8080"
......@@ -63,12 +63,12 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CONTEXT_SERVICE_HOST
value: contextservice
- name: SERVICE_SERVICE_HOST
value: serviceservice
- name: MONITORING_SERVICE_HOST
value: monitoringservice
- name: CONTEXT_SERVICE_HOST
value: contextservice
image: labs.etsi.org:5050/tfs/controller/policy:0.1.0
imagePullPolicy: Always
livenessProbe:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment