9a705020c90dad4d4c991f5a65e10686e51ddbc7
[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 java.util.Properties;
28
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
34 import org.onap.policy.apex.context.parameters.DistributorParameters;
35 import org.onap.policy.apex.context.parameters.LockManagerParameters;
36 import org.onap.policy.apex.context.parameters.PersistorParameters;
37 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
38 import org.onap.policy.apex.core.engine.event.EnEvent;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
41 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
42 import org.onap.policy.apex.model.policymodel.concepts.AxState;
43 import org.onap.policy.common.parameters.ParameterService;
44
45 /**
46  * Test the JavaTaskSelectExecutor class.
47  *
48  */
49 public class JavascriptTaskSelectExecutorTest {
50     /**
51      * Initiate Parameters.
52      */
53     @Before
54     public void initiateParameters() {
55         ParameterService.register(new DistributorParameters());
56         ParameterService.register(new LockManagerParameters());
57         ParameterService.register(new PersistorParameters());
58     }
59
60     /**
61      * Clear Parameters.
62      */
63     @After
64     public void clearParameters() {
65         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
66         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
67         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
68     }
69
70     @Test
71     public void testJavascriptTaskSelectExecutor() {
72         JavascriptTaskSelectExecutor jtse = new JavascriptTaskSelectExecutor();
73         assertNotNull(jtse);
74
75         try {
76             jtse.prepare();
77             fail("test should throw an exception here");
78         } catch (Exception jtseException) {
79             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
80         }
81
82         AxState state = new AxState();
83         ApexInternalContext internalContext = null;
84         try {
85             internalContext = new ApexInternalContext(new AxPolicyModel());
86         } catch (ContextException e) {
87             fail("test should not throw an exception here");
88         }
89         jtse.setContext(null, state, internalContext);
90
91         state.getTaskSelectionLogic().setLogic("x!0");
92         try {
93             jtse.prepare();
94             fail("test should throw an exception here");
95         } catch (Exception jtseException) {
96             assertEquals("task selection logic failed to compile for state  \"NULL:0.0.0:NULL:NULL\"",
97                     jtseException.getMessage());
98         }
99
100         AxEvent axEvent1 = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
101         EnEvent event1 = new EnEvent(axEvent1);
102         try {
103             jtse.execute(-1, new Properties(), event1);
104             fail("test should throw an exception here");
105         } catch (Exception jtseException) {
106             assertEquals(
107                     "task selection logic failed to run for state  \"NULL:0.0.0:NULL:NULL\"",
108                     jtseException.getMessage());
109         }
110
111         state.getTaskSelectionLogic().setLogic("java.lang.String");
112
113         try {
114             jtse.prepare();
115         } catch (Exception jtseException) {
116             fail("test should not throw an exception here");
117         }
118
119         try {
120             jtse.execute(-1, new Properties(), null);
121             fail("test should throw an exception here");
122         } catch (Exception jtseException) {
123             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
124         }
125
126         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
127         EnEvent event = new EnEvent(axEvent);
128         try {
129             jtse.execute(-1, new Properties(), event);
130             fail("test should throw an exception here");
131         } catch (Exception jtseException) {
132             assertEquals(
133                     "execute: task selection logic failed to set a return value for state  \"NULL:0.0.0:NULL:NULL\"",
134                     jtseException.getMessage());
135         }
136
137         state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
138                 + "var returnValue = new returnValueType(false); ");
139         try {
140             jtse.prepare();
141             jtse.execute(-1, new Properties(), event);
142             fail("test should throw an exception here");
143         } catch (Exception jtseException) {
144             assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",
145                     jtseException.getMessage());
146         }
147
148         state.getTaskSelectionLogic().setLogic("var returnValueType = Java.type(\"java.lang.Boolean\");\r\n"
149                 + "var returnValue = new returnValueType(true); ");
150         try {
151             jtse.prepare();
152             AxArtifactKey taskKey = jtse.execute(0, new Properties(), event);
153             assertEquals("NULL:0.0.0", taskKey.getId());
154             jtse.cleanUp();
155         } catch (Exception jtseException) {
156             fail("test should not throw an exception here");
157         }
158     }
159 }