2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.domain.models.operational;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
26 import java.util.List;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.common.utils.coder.CoderException;
30 import org.onap.policy.common.utils.coder.StandardCoder;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.drools.domain.models.Metadata;
33 import org.onap.policy.drools.policies.DomainMaker;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
38 public class OperationalPolicyTest {
40 private static final String OPERATIONAL_DROOLS_POLICY_TYPE = "onap.policies.controlloop.operational.common.Drools";
42 // Operational vCPE Policies
43 private static final String OP_POLICY_NAME_VCPE = "operational.restart";
44 private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
45 "policies/vCPE.policy.operational.input.tosca.json";
47 private DomainMaker domainMaker;
48 private StandardCoder nonValCoder;
52 domainMaker = new DomainMaker();
53 nonValCoder = new StandardCoder();
57 public void testToscaCompliantOperationalPolicyType() throws CoderException {
58 String rawVcpeToscaPolicy = getExamplesPolicyString(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
60 // valid "known" policy type with implicit schema
61 ToscaPolicyTypeIdentifier operationalCompliantType =
62 new ToscaPolicyTypeIdentifier(OPERATIONAL_DROOLS_POLICY_TYPE, "1.0.0");
63 assertTrue(domainMaker.isConformant(operationalCompliantType, rawVcpeToscaPolicy));
64 assertNotNull(domainMaker.convertTo(operationalCompliantType, rawVcpeToscaPolicy, OperationalPolicy.class));
68 public void testOperationalCompliantModel() {
70 OperationalPolicy policy =
71 OperationalPolicy.builder()
72 .metadata(Metadata.builder().policyId(OP_POLICY_NAME_VCPE).build())
73 .name(OP_POLICY_NAME_VCPE)
74 .type(OPERATIONAL_DROOLS_POLICY_TYPE)
78 OperationalProperties.builder()
79 .id("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e")
81 .trigger("unique-policy-id-1-restart")
83 List.of(Operation.builder()
84 .id("unique-policy-id-1-restart")
85 .description("Restart the VM")
88 .actorOperation(ActorOperation.builder()
91 .target(OperationalTarget.builder().targetType("VNF").build())
94 .controllerName("usecases")
99 assertNotNull(policy);
102 private String getJsonFromResource(String resourcePath) {
103 return ResourceUtils.getResourceAsString(resourcePath);
106 private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
107 String policyJson = getJsonFromResource(resourcePath);
108 ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
109 return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
112 private String getExamplesPolicyString(String resourcePath, String policyName) throws CoderException {
113 return nonValCoder.encode(getExamplesPolicy(resourcePath, policyName));