78ac8536602be38e1f9402cfa678c362621d4d09
[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.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import java.util.Properties;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
32 import org.onap.policy.apex.context.parameters.DistributorParameters;
33 import org.onap.policy.apex.context.parameters.LockManagerParameters;
34 import org.onap.policy.apex.context.parameters.PersistorParameters;
35 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
36 import org.onap.policy.apex.core.engine.event.EnEvent;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
39 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
40 import org.onap.policy.apex.model.policymodel.concepts.AxState;
41 import org.onap.policy.common.parameters.ParameterService;
42
43 /**
44  * Test the JavaTaskSelectExecutor class.
45  *
46  */
47 public class JavascriptTaskSelectExecutorTest {
48     /**
49      * Initiate Parameters.
50      */
51     @Before
52     public void initiateParameters() {
53         ParameterService.register(new DistributorParameters());
54         ParameterService.register(new LockManagerParameters());
55         ParameterService.register(new PersistorParameters());
56     }
57
58     /**
59      * Clear Parameters.
60      */
61     @After
62     public void clearParameters() {
63         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
64         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
65         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
66     }
67
68     @Test
69     public void testJavascriptTaskSelectExecutor() throws Exception {
70         JavascriptTaskSelectExecutor jtse = new JavascriptTaskSelectExecutor();
71
72         assertThatThrownBy(() -> {
73             jtse.prepare();
74             fail("test should throw an exception here");
75         }).isInstanceOf(NullPointerException.class);
76
77         AxState state = new AxState();
78         ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel());
79         jtse.setContext(null, state, internalContext);
80
81         assertThatThrownBy(() -> {
82             jtse.prepare();
83         }).hasMessage("no logic specified for NULL:0.0.0:NULL:NULL");
84
85         state.getTaskSelectionLogic().setLogic("java.lang.String");
86         jtse.prepare();
87
88         assertThatThrownBy(() -> {
89             jtse.execute(-1, new Properties(), null);
90         }).isInstanceOf(NullPointerException.class);
91
92         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
93         EnEvent event = new EnEvent(axEvent);
94
95         assertThatThrownBy(() -> {
96             jtse.execute(-1, new Properties(), event);
97         }).hasMessage(
98             "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]");
99
100         state.getTaskSelectionLogic().setLogic("var x=1;\n" + "false; ");
101
102         assertThatThrownBy(() -> {
103             jtse.prepare();
104             jtse.execute(-1, new Properties(), event);
105         }).hasMessage("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"");
106
107         state.getTaskSelectionLogic().setLogic("var x = 1\n" + "true; ");
108
109         jtse.prepare();
110         AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
111         assertEquals("NULL:0.0.0", taskKey.getId());
112         jtse.cleanUp();
113     }
114 }