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;
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;
41 * The Class TestMfpLogic.
43 public class MfpLogicTest {
45 private static final Map<String, String> LOGICEXTENSIONS = new LinkedHashMap<>();
51 public static void testMfpUseCaseSetup() {
52 LOGICEXTENSIONS.put("MVEL", "mvel");
53 LOGICEXTENSIONS.put("JAVASCRIPT", "js");
57 * Check logic for MyFirstPolicy#1.
60 public void testMfp1TaskLogic() {
61 final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1PolicyModel();
62 assertNotNull(apexPolicyModel);
64 final Map<String, String> logics = new LinkedHashMap<>();
65 logics.putAll(getTslLogics(apexPolicyModel));
66 logics.putAll(getTaskLogics(apexPolicyModel));
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);
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", ""));
82 * Check logic for MyFirstPolicyAlt#1.
85 public void testMfp1AltTaskLogic() {
86 final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp1AltPolicyModel();
87 assertNotNull(apexPolicyModel);
89 final Map<String, String> logics = new LinkedHashMap<>();
90 logics.putAll(getTslLogics(apexPolicyModel));
91 logics.putAll(getTaskLogics(apexPolicyModel));
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);
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", ""));
107 * Check logic for MyFirstPolicy2.
110 public void testMfp2TaskLogic() {
111 final AxPolicyModel apexPolicyModel = new MfpDomainModelFactory().getMfp2PolicyModel();
112 assertNotNull(apexPolicyModel);
114 final Map<String, String> logics = new LinkedHashMap<>();
115 logics.putAll(getTslLogics(apexPolicyModel));
116 logics.putAll(getTaskLogics(apexPolicyModel));
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);
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", ""));
132 * Gets the TSL logics.
134 * @param apexPolicyModel the apex policy model
135 * @return the TSL logics
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()
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) {
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);
160 * Gets the task logics.
162 * @param apexPolicyModel the apex policy model
163 * @return the task logics
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);