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