2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021, 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.domain.models.operational;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import java.util.List;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
33 import org.onap.policy.drools.domain.models.Metadata;
34 import org.onap.policy.drools.policies.DomainMaker;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 class OperationalPolicyTest {
41 private static final String OPERATIONAL_DROOLS_POLICY_TYPE = "onap.policies.controlloop.operational.common.Drools";
43 // Operational vCPE Policies
44 private static final String OP_POLICY_NAME_VCPE = "operational.restart";
45 private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
46 "policies/vCPE.policy.operational.input.tosca.json";
48 private DomainMaker domainMaker;
49 private StandardCoder nonValCoder;
53 domainMaker = new DomainMaker();
54 nonValCoder = new StandardCoder();
58 void testToscaCompliantOperationalPolicyType() throws CoderException {
59 String rawVcpeToscaPolicy = getExamplesPolicyString();
61 // valid "known" policy type with implicit schema
62 ToscaConceptIdentifier operationalCompliantType =
63 new ToscaConceptIdentifier(OPERATIONAL_DROOLS_POLICY_TYPE, "1.0.0");
64 assertTrue(domainMaker.isConformant(operationalCompliantType, rawVcpeToscaPolicy));
65 assertNotNull(domainMaker.convertTo(operationalCompliantType, rawVcpeToscaPolicy, OperationalPolicy.class));
69 void testOperationalCompliantModel() {
71 OperationalPolicy policy =
72 OperationalPolicy.builder()
73 .metadata(Metadata.builder().policyId(OP_POLICY_NAME_VCPE).build())
74 .name(OP_POLICY_NAME_VCPE)
75 .type(OPERATIONAL_DROOLS_POLICY_TYPE)
79 OperationalProperties.builder()
80 .id("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e")
82 .trigger("unique-policy-id-1-restart")
84 List.of(Operation.builder()
85 .id("unique-policy-id-1-restart")
86 .description("Restart the VM")
89 .actorOperation(ActorOperation.builder()
92 .target(OperationalTarget.builder().targetType("VNF").build())
95 .controllerName("usecases")
100 assertNotNull(policy);
103 private ToscaPolicy getExamplesPolicy() throws CoderException {
104 String policyJson = ResourceUtils
105 .getResourceAsString(OperationalPolicyTest.VCPE_OPERATIONAL_DROOLS_POLICY_JSON);
106 ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
107 return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(
108 OperationalPolicyTest.OP_POLICY_NAME_VCPE);
111 private String getExamplesPolicyString() throws CoderException {
112 return nonValCoder.encode(getExamplesPolicy());