c470e9356ab30edd63401e8b1d5b967f4ca80119
[policy/apex-pdp.git] / examples / examples-myfirstpolicy / src / test / java / org / onap / policy / apex / examples / myfirstpolicy / MfpLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.examples.myfirstpolicy;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
36 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
37 import org.onap.policy.apex.model.policymodel.concepts.AxState;
38 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40
41 /**
42  * The Class TestMfpLogic.
43  */
44 public class MfpLogicTest {
45
46     private static final Map<String, String> LOGICEXTENSIONS = new LinkedHashMap<>();
47
48     /**
49      * Test setup.
50      */
51     @BeforeClass
52     public static void testMfpUseCaseSetup() {
53         LOGICEXTENSIONS.put("MVEL", "mvel");
54         LOGICEXTENSIONS.put("JAVASCRIPT", "js");
55     }
56
57     /**
58      * Check logic for MyFirstPolicy#1.
59      */
60     @Test
61     public void testMfp1TaskLogic() {
62         final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1PolicyModel();
63         assertNotNull(apexPolicyModel);
64
65         final Map<String, String> logics = new LinkedHashMap<>();
66         logics.putAll(getTslLogics(apexPolicyModel));
67         logics.putAll(getTaskLogics(apexPolicyModel));
68
69         for (final Entry<String, String> logicvalue : logics.entrySet()) {
70             final String filename = "examples/models/MyFirstPolicy/1/" + logicvalue.getKey();
71             final String logic = logicvalue.getValue();
72             final String expectedlogic = ResourceUtils.getResourceAsString(filename);
73             assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel "
74                     + apexPolicyModel.getKey(), expectedlogic);
75             assertEquals(
76                     "The task in " + filename + " is not the same as the relevant logic in PolicyModel "
77                             + apexPolicyModel.getKey(),
78                     expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", ""));
79         }
80     }
81
82     /**
83      * Check logic for MyFirstPolicyAlt#1.
84      */
85     @Test
86     public void testMfp1AltTaskLogic() {
87         final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1AltPolicyModel();
88         assertNotNull(apexPolicyModel);
89
90         final Map<String, String> logics = new LinkedHashMap<>();
91         logics.putAll(getTslLogics(apexPolicyModel));
92         logics.putAll(getTaskLogics(apexPolicyModel));
93
94         for (final Entry<String, String> logicvalue : logics.entrySet()) {
95             final String filename = "examples/models/MyFirstPolicy/1/" + logicvalue.getKey();
96             final String logic = logicvalue.getValue();
97             final String expectedlogic = ResourceUtils.getResourceAsString(filename);
98             assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel "
99                     + apexPolicyModel.getKey(), expectedlogic);
100             assertEquals(
101                     "The task in " + filename + " is not the same as the relevant logic in PolicyModel "
102                             + apexPolicyModel.getKey(),
103                     expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", ""));
104         }
105     }
106
107     /**
108      * Check logic for MyFirstPolicy2.
109      */
110     @Test
111     public void testMfp2TaskLogic() {
112         final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp2PolicyModel();
113         assertNotNull(apexPolicyModel);
114
115         final Map<String, String> logics = new LinkedHashMap<>();
116         logics.putAll(getTslLogics(apexPolicyModel));
117         logics.putAll(getTaskLogics(apexPolicyModel));
118
119         for (final Entry<String, String> logicvalue : logics.entrySet()) {
120             final String logic = logicvalue.getValue();
121             final String filename = "examples/models/MyFirstPolicy/2/" + logicvalue.getKey();
122             final String expectedlogic = ResourceUtils.getResourceAsString(filename);
123             assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel "
124                     + apexPolicyModel.getKey(), expectedlogic);
125             assertEquals(
126                     "The task in " + filename + " is not the same as the relevant logic in PolicyModel "
127                             + apexPolicyModel.getKey(),
128                     expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", ""));
129         }
130     }
131
132     /**
133      * Gets the TSL logics.
134      *
135      * @param apexPolicyModel the apex policy model
136      * @return the TSL logics
137      */
138     private Map<String, String> getTslLogics(final AxPolicyModel apexPolicyModel) {
139         final Map<String, String> ret = new LinkedHashMap<>();
140         for (final Entry<AxArtifactKey, AxPolicy> policyentry : apexPolicyModel.getPolicies().getPolicyMap()
141                 .entrySet()) {
142             for (final Entry<String, AxState> statesentry : policyentry.getValue().getStateMap().entrySet()) {
143                 final AxState state = statesentry.getValue();
144                 final String tsllogic = state.getTaskSelectionLogic().getLogic();
145                 final String tsllogicflavour = state.getTaskSelectionLogic().getLogicFlavour();
146                 if (tsllogic != null && tsllogic.trim().length() > 0) {
147                     assertNotNull(
148                             "Logic Type \"" + tsllogicflavour + "\" in state " + statesentry.getKey() + " in policy "
149                                     + policyentry.getKey() + " is not supported in this test",
150                             LOGICEXTENSIONS.get(tsllogicflavour.toUpperCase()));
151                     final String filename = policyentry.getKey().getName() + "_" + statesentry.getKey() + "TSL."
152                             + LOGICEXTENSIONS.get(tsllogicflavour.toUpperCase());
153                     ret.put(filename, tsllogic);
154                 }
155             }
156         }
157         return ret;
158     }
159
160     /**
161      * Gets the task logics.
162      *
163      * @param apexPolicyModel the apex policy model
164      * @return the task logics
165      */
166     private Map<String, String> getTaskLogics(final AxPolicyModel apexPolicyModel) {
167         final Map<String, String> ret = new LinkedHashMap<>();
168         for (final Entry<AxArtifactKey, AxTask> taskentry : apexPolicyModel.getTasks().getTaskMap().entrySet()) {
169             final AxTask task = taskentry.getValue();
170             final String tasklogic = task.getTaskLogic().getLogic();
171             final String tasklogicflavour = task.getTaskLogic().getLogicFlavour();
172             assertTrue("No/Blank logic found in task " + taskentry.getKey(),
173                     (tasklogic != null && tasklogic.trim().length() > 0));
174             assertNotNull("Logic Type \"" + tasklogicflavour + "\" in task " + taskentry.getKey()
175                     + " is not supported in this test", LOGICEXTENSIONS.get(tasklogicflavour.toUpperCase()));
176             final String filename =
177                     taskentry.getKey().getName() + "." + LOGICEXTENSIONS.get(tasklogicflavour.toUpperCase());
178             ret.put(filename, tasklogic);
179         }
180         return ret;
181     }
182 }