Changes for checkstyle 8.32
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-javascript / src / test / java / org / onap / policy / apex / plugins / executor / javascript / JavascriptStateFinalizerExecutorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
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.plugins.executor.javascript;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Properties;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
35 import org.onap.policy.apex.context.parameters.DistributorParameters;
36 import org.onap.policy.apex.context.parameters.LockManagerParameters;
37 import org.onap.policy.apex.context.parameters.PersistorParameters;
38 import org.onap.policy.apex.core.engine.EngineParameterConstants;
39 import org.onap.policy.apex.core.engine.EngineParameters;
40 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
41 import org.onap.policy.apex.core.engine.event.EnEvent;
42 import org.onap.policy.apex.core.engine.executor.StateExecutor;
43 import org.onap.policy.apex.core.engine.executor.impl.ExecutorFactoryImpl;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
46 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
47 import org.onap.policy.apex.model.policymodel.concepts.AxState;
48 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
49 import org.onap.policy.common.parameters.ParameterService;
50
51 /**
52  * Test the JavascriptStateFinalizerExecutor class.
53  *
54  */
55 public class JavascriptStateFinalizerExecutorTest {
56     /**
57      * Initiate Parameters.
58      */
59     @Before
60     public void initiateParameters() {
61         ParameterService.register(new DistributorParameters());
62         ParameterService.register(new LockManagerParameters());
63         ParameterService.register(new PersistorParameters());
64         ParameterService.register(new EngineParameters());
65     }
66
67     /**
68      * Clear down Parameters.
69      */
70     @After
71     public void clearParameters() {
72         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
73         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
74         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
75         ParameterService.deregister(EngineParameterConstants.MAIN_GROUP_NAME);
76     }
77
78     @Test
79     public void testJavaStateFinalizerExecutor() throws Exception {
80         JavascriptStateFinalizerExecutor jsfe = new JavascriptStateFinalizerExecutor();
81         assertNotNull(jsfe);
82
83         assertThatThrownBy(() -> {
84             jsfe.prepare();
85         }).isInstanceOf(java.lang.NullPointerException.class);
86
87         ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
88         StateExecutor parentStateExcutor = new StateExecutor(new ExecutorFactoryImpl());
89
90         AxState state = new AxState();
91         parentStateExcutor.setContext(null, state, internalContext);
92         AxStateFinalizerLogic stateFinalizerLogic = new AxStateFinalizerLogic();
93         jsfe.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
94
95         stateFinalizerLogic.setLogic("return false");
96         assertThatThrownBy(() -> {
97             jsfe.prepare();
98         }).hasMessage("logic failed to compile for NULL:0.0.0:NULL:NULL "
99             + "with message: invalid return (NULL:0.0.0:NULL:NULL#1)");
100
101         Map<String, Object> incomingParameters1 = new HashMap<>();
102         assertThatThrownBy(() -> {
103             jsfe.execute(-1, new Properties(), incomingParameters1);
104         }).hasMessage("execution failed, executor NULL:0.0.0:NULL:NULL is not running, "
105             + "run cleanUp to clear executor and init to restart executor");
106
107         assertThatThrownBy(() -> {
108             jsfe.prepare();
109         }).hasMessage(
110             "initiation failed, executor NULL:0.0.0:NULL:NULL already initialized, run cleanUp to clear executor");
111
112         assertThatCode(() -> {
113             jsfe.cleanUp();
114         }).doesNotThrowAnyException();
115
116         JavascriptStateFinalizerExecutor jsfe1 = new JavascriptStateFinalizerExecutor();
117         stateFinalizerLogic.setLogic("java.lang.String");
118         jsfe1.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
119         jsfe1.prepare();
120
121         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
122         EnEvent event = new EnEvent(axEvent);
123
124         assertThatThrownBy(() -> {
125             jsfe1.execute(-1, new Properties(), event);
126         }).hasMessage(
127             "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]");
128
129         assertThatThrownBy(() -> {
130             jsfe1.execute(-1, new Properties(), event);
131         }).hasMessage(
132             "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]");
133
134         assertThatCode(() -> {
135             jsfe1.cleanUp();
136         }).doesNotThrowAnyException();
137
138         JavascriptStateFinalizerExecutor jsfe2 = new JavascriptStateFinalizerExecutor();
139
140         stateFinalizerLogic.setLogic("executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n true;");
141
142         jsfe2.setContext(parentStateExcutor, stateFinalizerLogic, internalContext);
143         assertThatCode(() -> {
144             jsfe2.prepare();
145         }).doesNotThrowAnyException();
146
147         state.getStateOutputs().put("SelectedOutputIsMe", null);
148
149         String stateOutput = jsfe2.execute(0, new Properties(), event);
150         assertEquals("SelectedOutputIsMe", stateOutput);
151
152         assertThatCode(() -> {
153             jsfe2.cleanUp();
154         }).doesNotThrowAnyException();
155     }
156 }