3ebf8155e8d1d632e7373abb6aa269ab6c409408
[policy/apex-pdp.git] /
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.examples.myfirstpolicy.model.MfpDomainModelFactory;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
37 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
38 import org.onap.policy.apex.model.policymodel.concepts.AxState;
39 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
40 import org.onap.policy.common.utils.resources.ResourceUtils;
41
42 /**
43  * The Class TestMfpLogic.
44  */
45 public class MfpLogicTest {
46
47     private static final Map<String, String> LOGICEXTENSIONS = new LinkedHashMap<>();
48
49     /**
50      * Test setup.
51      */
52     @BeforeClass
53     public static void testMfpUseCaseSetup() {
54         LOGICEXTENSIONS.put("MVEL", "mvel");
55         LOGICEXTENSIONS.put("JAVASCRIPT", "js");
56     }
57
58     /**
59      * Check logic for MyFirstPolicy#1.
60      */
61     @Test
62     public void testMfp1TaskLogic() {
63         final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1PolicyModel();
64         assertNotNull(apexPolicyModel);
65
66         final Map<String, String> logics = new LinkedHashMap<>();
67         logics.putAll(getTslLogics(apexPolicyModel));
68         logics.putAll(getTaskLogics(apexPolicyModel));
69
70         for (final Entry<String, String> logicvalue : logics.entrySet()) {
71             final String filename = "examples/models/MyFirstPolicy/1/" + logicvalue.getKey();
72             final String logic = logicvalue.getValue();
73             final String expectedlogic = ResourceUtils.getResourceAsString(filename);
74             assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel "
75                     + apexPolicyModel.getKey(), expectedlogic);
76             assertEquals(
77                     "The task in " + filename + " is not the same as the relevant logic in PolicyModel "
78                             + apexPolicyModel.getKey(),
79                     expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", ""));
80         }
81     }
82
83     /**
84      * Check logic for MyFirstPolicyAlt#1.
85      */
86     @Test
87     public void testMfp1AltTaskLogic() {
88         final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1AltPolicyModel();
89         assertNotNull(apexPolicyModel);
90
91         final Map<String, String> logics = new LinkedHashMap<>();
92         logics.putAll(getTslLogics(apexPolicyModel));
93         logics.putAll(getTaskLogics(apexPolicyModel));
94
95         for (final Entry<String, String> logicvalue : logics.entrySet()) {
96             final String filename = "examples/models/MyFirstPolicy/1/" + logicvalue.getKey();
97             final String logic = logicvalue.getValue();
98             final String expectedlogic = ResourceUtils.getResourceAsString(filename);
99             assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel "
100                     + apexPolicyModel.getKey(), expectedlogic);
101             assertEquals(
102                     "The task in " + filename + " is not the same as the relevant logic in PolicyModel "
103                             + apexPolicyModel.getKey(),
104                     expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", ""));
105         }
106     }
107
108     /**
109      * Check logic for MyFirstPolicy2.
110      */
111     @Test
112     public void testMfp2TaskLogic() {
113         final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp2PolicyModel();
114         assertNotNull(apexPolicyModel);
115
116         final Map<String, String> logics = new LinkedHashMap<>();
117         logics.putAll(getTslLogics(apexPolicyModel));
118         logics.putAll(getTaskLogics(apexPolicyModel));
119
120         for (final Entry<String, String> logicvalue : logics.entrySet()) {
121             final String logic = logicvalue.getValue();
122             final String filename = "examples/models/MyFirstPolicy/2/" + logicvalue.getKey();
123             final String expectedlogic = ResourceUtils.getResourceAsString(filename);
124             assertNotNull("File " + filename + " was not found. It should contain logic for PolicyModel "
125                     + apexPolicyModel.getKey(), expectedlogic);
126             assertEquals(
127                     "The task in " + filename + " is not the same as the relevant logic in PolicyModel "
128                             + apexPolicyModel.getKey(),
129                     expectedlogic.replaceAll("\\s", ""), logic.replaceAll("\\s", ""));
130         }
131     }
132
133     /**
134      * Gets the TSL logics.
135      *
136      * @param apexPolicyModel the apex policy model
137      * @return the TSL logics
138      */
139     private Map<String, String> getTslLogics(final AxPolicyModel apexPolicyModel) {
140         final Map<String, String> ret = new LinkedHashMap<>();
141         for (final Entry<AxArtifactKey, AxPolicy> policyentry : apexPolicyModel.getPolicies().getPolicyMap()
142                 .entrySet()) {
143             for (final Entry<String, AxState> statesentry : policyentry.getValue().getStateMap().entrySet()) {
144                 final AxState state = statesentry.getValue();
145                 final String tsllogic = state.getTaskSelectionLogic().getLogic();
146                 final String tsllogicflavour = state.getTaskSelectionLogic().getLogicFlavour();
147                 if (tsllogic != null && tsllogic.trim().length() > 0) {
148                     assertNotNull(
149                             "Logic Type \"" + tsllogicflavour + "\" in state " + statesentry.getKey() + " in policy "
150                                     + policyentry.getKey() + " is not supported in this test",
151                             LOGICEXTENSIONS.get(tsllogicflavour.toUpperCase()));
152                     final String filename = policyentry.getKey().getName() + "_" + statesentry.getKey() + "TSL."
153                             + LOGICEXTENSIONS.get(tsllogicflavour.toUpperCase());
154                     ret.put(filename, tsllogic);
155                 }
156             }
157         }
158         return ret;
159     }
160
161     /**
162      * Gets the task logics.
163      *
164      * @param apexPolicyModel the apex policy model
165      * @return the task logics
166      */
167     private Map<String, String> getTaskLogics(final AxPolicyModel apexPolicyModel) {
168         final Map<String, String> ret = new LinkedHashMap<>();
169         for (final Entry<AxArtifactKey, AxTask> taskentry : apexPolicyModel.getTasks().getTaskMap().entrySet()) {
170             final AxTask task = taskentry.getValue();
171             final String tasklogic = task.getTaskLogic().getLogic();
172             final String tasklogicflavour = task.getTaskLogic().getLogicFlavour();
173             assertTrue("No/Blank logic found in task " + taskentry.getKey(),
174                     (tasklogic != null && tasklogic.trim().length() > 0));
175             assertNotNull("Logic Type \"" + tasklogicflavour + "\" in task " + taskentry.getKey()
176                     + " is not supported in this test", LOGICEXTENSIONS.get(tasklogicflavour.toUpperCase()));
177             final String filename =
178                     taskentry.getKey().getName() + "." + LOGICEXTENSIONS.get(tasklogicflavour.toUpperCase());
179             ret.put(filename, tasklogic);
180         }
181         return ret;
182     }
183 }