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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.examples.myfirstpolicy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import java.util.LinkedHashMap;
30 import java.util.Map.Entry;
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;
42 * The Class TestMfpLogic.
44 public class MfpLogicTest {
46 private static final Map<String, String> LOGICEXTENSIONS = new LinkedHashMap<>();
52 public static void testMfpUseCaseSetup() {
53 LOGICEXTENSIONS.put("MVEL", "mvel");
54 LOGICEXTENSIONS.put("JAVASCRIPT", "js");
58 * Check logic for MyFirstPolicy#1.
61 public void testMfp1TaskLogic() {
62 final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1PolicyModel();
63 assertNotNull(apexPolicyModel);
65 final Map<String, String> logics = new LinkedHashMap<>();
66 logics.putAll(getTslLogics(apexPolicyModel));
67 logics.putAll(getTaskLogics(apexPolicyModel));
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);
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", ""));
83 * Check logic for MyFirstPolicyAlt#1.
86 public void testMfp1AltTaskLogic() {
87 final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1AltPolicyModel();
88 assertNotNull(apexPolicyModel);
90 final Map<String, String> logics = new LinkedHashMap<>();
91 logics.putAll(getTslLogics(apexPolicyModel));
92 logics.putAll(getTaskLogics(apexPolicyModel));
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);
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", ""));
108 * Check logic for MyFirstPolicy2.
111 public void testMfp2TaskLogic() {
112 final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp2PolicyModel();
113 assertNotNull(apexPolicyModel);
115 final Map<String, String> logics = new LinkedHashMap<>();
116 logics.putAll(getTslLogics(apexPolicyModel));
117 logics.putAll(getTaskLogics(apexPolicyModel));
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);
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", ""));
133 * Gets the TSL logics.
135 * @param apexPolicyModel the apex policy model
136 * @return the TSL logics
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()
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) {
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);
161 * Gets the task logics.
163 * @param apexPolicyModel the apex policy model
164 * @return the task logics
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);