Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-domains / src / test / java / org / onap / policy / drools / domain / models / operational / OperationalPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.domain.models.operational;
23
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26
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;
38
39 class OperationalPolicyTest {
40     // Policy Types
41     private static final String OPERATIONAL_DROOLS_POLICY_TYPE = "onap.policies.controlloop.operational.common.Drools";
42
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";
47
48     private DomainMaker domainMaker;
49     private StandardCoder nonValCoder;
50
51     @BeforeEach
52     public void setUp() {
53         domainMaker = new DomainMaker();
54         nonValCoder = new StandardCoder();
55     }
56
57     @Test
58     void testToscaCompliantOperationalPolicyType() throws CoderException {
59         String rawVcpeToscaPolicy = getExamplesPolicyString();
60
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));
66     }
67
68     @Test
69     void testOperationalCompliantModel() {
70         // @formatter:off
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)
76                     .typeVersion("1.0.0")
77                     .version("1.0.0")
78                     .properties(
79                             OperationalProperties.builder()
80                                     .id("ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e")
81                                     .abatement(true)
82                                     .trigger("unique-policy-id-1-restart")
83                                     .operations(
84                                             List.of(Operation.builder()
85                                                     .id("unique-policy-id-1-restart")
86                                                     .description("Restart the VM")
87                                                     .timeout(60)
88                                                     .retries(3)
89                                                     .actorOperation(ActorOperation.builder()
90                                                         .operation("Restart")
91                                                         .actor("APPC")
92                                                         .target(OperationalTarget.builder().targetType("VNF").build())
93                                                         .build())
94                                                     .build()))
95                                     .controllerName("usecases")
96                                     .build())
97                     .build();
98         // @formatter:on
99
100         assertNotNull(policy);
101     }
102
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);
109     }
110
111     private String getExamplesPolicyString() throws CoderException {
112         return nonValCoder.encode(getExamplesPolicy());
113     }
114 }