f19e2c8c32f3d29a62e71ea4739bff4b1f6d3650
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.examples.myfirstpolicy;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import java.util.Map.Entry;
30
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.apex.examples.myfirstpolicy.model.MfpDomainModelFactory;
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     /**
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 filename = "examples/models/MyFirstPolicy/2/" + logicvalue.getKey();
122             final String logic = logicvalue.getValue();
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 }