fa494ced905dea57796d9c21ff0ca0ef01788631
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-javascript / src / test / java / org / onap / policy / apex / plugins / executor / javascript / JavascriptTaskSelectExecutorTest.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 import static org.junit.Assert.fail;
28
29 import java.util.Properties;
30
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.context.ApexInternalContext;
39 import org.onap.policy.apex.core.engine.event.EnEvent;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
41 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
42 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
43 import org.onap.policy.apex.model.policymodel.concepts.AxState;
44 import org.onap.policy.common.parameters.ParameterService;
45
46 /**
47  * Test the JavaTaskSelectExecutor class.
48  *
49  */
50 public class JavascriptTaskSelectExecutorTest {
51     /**
52      * Initiate Parameters.
53      */
54     @Before
55     public void initiateParameters() {
56         ParameterService.register(new DistributorParameters());
57         ParameterService.register(new LockManagerParameters());
58         ParameterService.register(new PersistorParameters());
59     }
60
61     /**
62      * Clear Parameters.
63      */
64     @After
65     public void clearParameters() {
66         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
67         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
68         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
69     }
70
71     @Test
72     public void testJavascriptTaskSelectExecutor() throws Exception {
73         JavascriptTaskSelectExecutor jtse = new JavascriptTaskSelectExecutor();
74         assertNotNull(jtse);
75
76         assertThatThrownBy(() -> {
77             jtse.prepare();
78             fail("test should throw an exception here");
79         }).isInstanceOf(NullPointerException.class);
80
81         AxState state = new AxState();
82         ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
83         jtse.setContext(null, state, internalContext);
84
85         assertThatThrownBy(() -> {
86             jtse.prepare();
87         }).hasMessage("initiation failed, no logic specified for executor NULL:0.0.0:NULL:NULL");
88
89         AxEvent axEvent1 = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
90         EnEvent event1 = new EnEvent(axEvent1);
91
92         assertThatThrownBy(() -> {
93             jtse.execute(-1, new Properties(), event1);
94         }).hasMessage("execution failed, executor NULL:0.0.0:NULL:NULL is not initialized");
95
96         assertThatThrownBy(() -> {
97             jtse.cleanUp();
98         }).hasMessage("cleanup failed, executor NULL:0.0.0:NULL:NULL is not initialized");
99
100         state.getTaskSelectionLogic().setLogic("java.lang.String");
101         jtse.prepare();
102
103         assertThatThrownBy(() -> {
104             jtse.execute(-1, new Properties(), null);
105         }).isInstanceOf(NullPointerException.class);
106
107         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
108         EnEvent event = new EnEvent(axEvent);
109
110         assertThatThrownBy(() -> {
111             jtse.execute(-1, new Properties(), event);
112         }).hasMessage(
113             "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]");
114
115         state.getTaskSelectionLogic().setLogic("var x=1;\n" + "false;");
116
117         assertThatThrownBy(() -> {
118             jtse.execute(-1, new Properties(), event);
119         }).hasMessage(
120             "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]");
121
122         assertThatThrownBy(() -> {
123             jtse.prepare();
124         }).hasMessage(
125             "initiation failed, executor NULL:0.0.0:NULL:NULL already initialized, run cleanUp to clear executor");
126
127         assertThatCode(() -> {
128             jtse.cleanUp();
129         }).doesNotThrowAnyException();
130
131         JavascriptTaskSelectExecutor jtse1 = new JavascriptTaskSelectExecutor();
132         jtse1.setContext(null, state, internalContext);
133         state.getTaskSelectionLogic().setLogic("var x = 1\n" + "true;");
134
135         assertThatCode(() -> {
136             jtse1.prepare();
137             AxArtifactKey taskKey = jtse1.execute(0, new Properties(), event);
138             assertEquals("NULL:0.0.0", taskKey.getId());
139             jtse1.cleanUp();
140         }).doesNotThrowAnyException();
141     }
142 }