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.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Paths;
29 import java.util.List;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.drools.domain.models.Metadata;
36 import org.onap.policy.drools.policies.DomainMaker;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 public class OperationalPolicyTest {
43 private static final String OPERATIONAL_DROOLS_POLICY_TYPE = "onap.policies.controlloop.operational.common.Drools";
45 // Operational vCPE Policies
46 private static final String OP_POLICY_NAME_VCPE = "operational.restart";
47 private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
48 "policies/vCPE.policy.operational.input.tosca.json";
50 private DomainMaker domainMaker;
51 private StandardCoder nonValCoder;
55 domainMaker = new DomainMaker();
56 nonValCoder = new StandardCoder();
60 public void testToscaCompliantOperationalPolicyType() throws CoderException {
61 String rawVcpeToscaPolicy = getExamplesPolicyString(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
63 // valid "known" policy type with implicit schema
64 ToscaPolicyTypeIdentifier operationalCompliantType =
65 new ToscaPolicyTypeIdentifier(OPERATIONAL_DROOLS_POLICY_TYPE, "1.0.0");
66 assertTrue(domainMaker.isConformant(operationalCompliantType, rawVcpeToscaPolicy));
67 assertNotNull(domainMaker.convertTo(operationalCompliantType, rawVcpeToscaPolicy, OperationalPolicy.class));
71 public void testOperationalCompliantModel() {
73 OperationalPolicy policy =
74 OperationalPolicy.builder()
75 .metadata(Metadata.builder().policyId(OP_POLICY_NAME_VCPE).build())
76 .name(OP_POLICY_NAME_VCPE)
77 .type(OPERATIONAL_DROOLS_POLICY_TYPE)
81 OperationalProperties.builder()
82 .id("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e")
84 .trigger("unique-policy-id-1-restart")
86 List.of(Operation.builder()
87 .id("unique-policy-id-1-restart")
88 .description("Restart the VM")
91 .actorOperation(ActorOperation.builder()
94 .target(OperationalTarget.builder().targetType("VNF").build())
97 .controllerName("usecases")
102 assertNotNull(policy);
105 private String getJsonFromFile(String filePath) throws IOException {
106 return Files.readString(Paths.get(filePath));
109 private String getJsonFromResource(String resourcePath) {
110 return ResourceUtils.getResourceAsString(resourcePath);
113 private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
114 String policyJson = getJsonFromResource(resourcePath);
115 ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
116 return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
119 private String getExamplesPolicyString(String resourcePath, String policyName) throws CoderException {
120 return nonValCoder.encode(getExamplesPolicy(resourcePath, policyName));