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