8efbc6241b93c583cf3ce44f86e492a755fb3d10
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.ContextException;
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() {
70         JavascriptTaskSelectExecutor jtse = new JavascriptTaskSelectExecutor();
71         assertNotNull(jtse);
72
73         try {
74             jtse.prepare();
75             fail("test should throw an exception here");
76         } catch (Exception jtseException) {
77             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
78         }
79
80         AxState state = new AxState();
81         ApexInternalContext internalContext = null;
82         try {
83             internalContext = new ApexInternalContext(new AxPolicyModel());
84         } catch (ContextException e) {
85             fail("test should not throw an exception here");
86         }
87         jtse.setContext(null, state, internalContext);
88
89         state.getTaskSelectionLogic().setLogic("x!0");
90         try {
91             jtse.prepare();
92             fail("test should throw an exception here");
93         } catch (Exception jtseException) {
94             assertEquals("task selection logic failed to compile for state  \"NULL:0.0.0:NULL:NULL\"",
95                     jtseException.getMessage());
96         }
97
98         AxEvent axEvent1 = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
99         EnEvent event1 = new EnEvent(axEvent1);
100         try {
101             jtse.execute(-1, null, event1);
102             fail("test should throw an exception here");
103         } catch (Exception jtseException) {
104             assertEquals(
105                     "task selection logic failed to run for state  \"NULL:0.0.0:NULL:NULL\"",
106                     jtseException.getMessage());
107         }
108
109         state.getTaskSelectionLogic().setLogic("java.lang.String");
110
111         try {
112             jtse.prepare();
113         } catch (Exception jtseException) {
114             fail("test should not throw an exception here");
115         }
116
117         try {
118             jtse.execute(-1, null, null);
119             fail("test should throw an exception here");
120         } catch (Exception jtseException) {
121             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
122         }
123
124         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
125         EnEvent event = new EnEvent(axEvent);
126         try {
127             jtse.execute(-1, null, event);
128             fail("test should throw an exception here");
129         } catch (Exception jtseException) {
130             assertEquals(
131                     "execute: task selection logic failed to set a return value for state  \"NULL:0.0.0:NULL:NULL\"",
132                     jtseException.getMessage());
133         }
134
135         state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
136                 + "var returnValue = new returnValueType(false); ");
137         try {
138             jtse.prepare();
139             jtse.execute(-1, null, event);
140             fail("test should throw an exception here");
141         } catch (Exception jtseException) {
142             assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",
143                     jtseException.getMessage());
144         }
145
146         state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
147                 + "var returnValue = new returnValueType(true); ");
148         try {
149             jtse.prepare();
150             AxArtifactKey taskKey = jtse.execute(0, null, event);
151             assertEquals("NULL:0.0.0", taskKey.getId());
152             jtse.cleanUp();
153         } catch (Exception jtseException) {
154             fail("test should not throw an exception here");
155         }
156     }
157 }