bab22d9ba516cc13c08fcb3b9838943b238d3b16
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.models.domain.operational;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import com.openpojo.reflection.PojoClass;
27 import com.openpojo.reflection.filters.FilterChain;
28 import com.openpojo.reflection.filters.FilterClassName;
29 import com.openpojo.reflection.filters.FilterNonConcrete;
30 import com.openpojo.reflection.impl.PojoClassFactory;
31 import com.openpojo.validation.Validator;
32 import com.openpojo.validation.ValidatorBuilder;
33 import com.openpojo.validation.test.impl.GetterTester;
34 import com.openpojo.validation.test.impl.SetterTester;
35 import java.io.IOException;
36 import java.nio.file.Files;
37 import java.nio.file.Paths;
38 import java.util.List;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.resources.ResourceUtils;
44 import org.onap.policy.drools.domain.models.DroolsPolicy;
45 import org.onap.policy.drools.domain.models.Metadata;
46 import org.onap.policy.drools.policies.DomainMaker;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50
51 public class OperationalPolicyTest {
52     // Policy Types
53     private static final String OPERATIONAL_DROOLS_POLICY_TYPE = "onap.policies.controlloop.operational.common.Drools";
54     private static final String OPERATIONAL_LEGACY_POLICY_TYPE = "onap.policies.controlloop.Operational";
55
56     // Operational vCPE Policies
57     private static final String OP_POLICY_NAME_VCPE = "operational.restart";
58     private static final String VCPE_OPERATIONAL_DROOLS_POLICY_JSON =
59                                 "policies/vCPE.policy.operational.input.tosca.json";
60     public static final String VCPE_OPERATIONAL_LEGACY_POLICY_JSON = "src/test/resources/tosca-legacy-vcpe.json";
61
62     private DomainMaker domainMaker;
63     private StandardCoder nonValCoder;
64
65     @Before
66     public void setUp() {
67         domainMaker = new DomainMaker();
68         nonValCoder = new StandardCoder();
69     }
70
71
72     @Test
73     public void testToscaCompliantOperationalPolicyType() throws CoderException {
74         String rawVcpeToscaPolicy = getExamplesPolicyString(VCPE_OPERATIONAL_DROOLS_POLICY_JSON, OP_POLICY_NAME_VCPE);
75
76         // valid "known" policy type with implicit schema
77         assertTrue(domainMaker
78             .isConformant(
79                 new ToscaPolicyTypeIdentifier(OPERATIONAL_DROOLS_POLICY_TYPE, "1.0.0"), rawVcpeToscaPolicy));
80
81         OperationalPolicy policy = domainMaker.convertTo(
82                 new ToscaPolicyTypeIdentifier("OPERATIONAL_LEGACY_POLICY_TYPE", "1.0.0"),
83                     rawVcpeToscaPolicy, OperationalPolicy.class);
84
85         assertNotNull(policy);
86     }
87
88     @Test
89     public void testToscaLegacyOperationalPolicyType() throws CoderException, IOException {
90         String rawVcpeToscaPolicy = getJsonFromFile(VCPE_OPERATIONAL_LEGACY_POLICY_JSON);
91
92         // valid "known" policy type with implicit schema
93         assertTrue(domainMaker
94             .isConformant(
95                 new ToscaPolicyTypeIdentifier(OPERATIONAL_LEGACY_POLICY_TYPE, "1.0.0"), rawVcpeToscaPolicy));
96     }
97
98     @Test
99     public void testOperationalCompliantModel() {
100         // @formatter:off
101         OperationalPolicy policy =
102                 OperationalPolicy.builder()
103                     .metadata(Metadata.builder().policyId(OP_POLICY_NAME_VCPE).build())
104                     .name(OP_POLICY_NAME_VCPE)
105                     .type(OPERATIONAL_DROOLS_POLICY_TYPE)
106                     .typeVersion("1.0.0")
107                     .version("1.0.0")
108                     .properties(
109                             OperationalProperties.builder()
110                                     .id("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e")
111                                     .abatement(true)
112                                     .trigger("unique-policy-id-1-restart")
113                                     .operations(
114                                             List.of(Operation.builder()
115                                                     .id("unique-policy-id-1-restart")
116                                                     .description("Restart the VM")
117                                                     .timeout(60)
118                                                     .retries(3)
119                                                     .actorOperation(ActorOperation.builder()
120                                                         .operation("Restart")
121                                                         .actor("APPC")
122                                                         .target(OperationalTarget.builder().type("VNF").build())
123                                                         .build())
124                                                     .build()))
125                                     .controllerName("usecases")
126                                     .build())
127                     .build();
128         // @formatter:on
129
130         assertNotNull(policy);
131     }
132
133     @Test
134     public void testPackage() {
135         /* validate model pojos */
136         List<PojoClass> pojoClasses =
137                 PojoClassFactory
138                         .getPojoClassesRecursively("org.onap.policy.drools.models.domain.operational",
139                             new FilterChain(new FilterNonConcrete(),
140                                     new FilterClassName(DroolsPolicy.class.getName())));
141
142         Validator validator = ValidatorBuilder.create()
143                                       .with(new SetterTester(), new GetterTester()).build();
144         validator.validate(pojoClasses);
145     }
146
147     private String getJsonFromFile(String filePath) throws IOException {
148         return new String(Files.readAllBytes(Paths.get(filePath)));
149     }
150
151     private String getJsonFromResource(String resourcePath) {
152         return ResourceUtils.getResourceAsString(resourcePath);
153     }
154
155     private String getPolicyFromFileString(String filePath, String policyName) throws CoderException, IOException {
156         String policyJson = getJsonFromFile(filePath);
157         ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
158         return nonValCoder.encode(serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName));
159     }
160
161     private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
162         String policyJson = getJsonFromResource(resourcePath);
163         ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
164         return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
165     }
166
167     private String getExamplesPolicyString(String resourcePath, String policyName) throws CoderException {
168         return nonValCoder.encode(getExamplesPolicy(resourcePath, policyName));
169     }
170 }