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