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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.examples.myfirstpolicy;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import java.util.LinkedHashMap;
29 import java.util.Map.Entry;
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;
42 * The Class TestMfpLogic.
44 public class TestMfpLogic {
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", ""));
84 * Check logic for MyFirstPolicyAlt#1.
87 public void testMfp1AltTaskLogic() {
88 final AxPolicyModel apexPolicyModel = new MFPDomainModelFactory().getMFP1AltPolicyModel();
89 assertNotNull(apexPolicyModel);
91 final Map<String, String> logics = new LinkedHashMap<>();
92 logics.putAll(getTslLogics(apexPolicyModel));
93 logics.putAll(getTaskLogics(apexPolicyModel));
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);
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", ""));
109 * Check logic for MyFirstPolicy2.
112 public void testMfp2TaskLogic() {
113 final AxPolicyModel apexPolicyModel = new MFPDomainModelFactory().getMFP2PolicyModel();
114 assertNotNull(apexPolicyModel);
116 final Map<String, String> logics = new LinkedHashMap<>();
117 logics.putAll(getTslLogics(apexPolicyModel));
118 logics.putAll(getTaskLogics(apexPolicyModel));
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);
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", ""));
134 * Gets the TSL logics.
136 * @param apexPolicyModel the apex policy model
137 * @return the TSL logics
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()
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) {
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);
162 * Gets the task logics.
164 * @param apexPolicyModel the apex policy model
165 * @return the task logics
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);