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