27eac53d38ab3d7255f36df9ec713810714d87ba
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 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.java;
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 JavaTaskSelectExecutorTest {
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 testJavaTaskSelectExecutor() {
70         JavaTaskSelectExecutor jtse = new JavaTaskSelectExecutor();
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         try {
90             jtse.prepare();
91             fail("test should throw an exception here");
92         } catch (Exception jtseException) {
93             assertEquals("instantiation error on Java class \"\"", jtseException.getMessage());
94         }
95
96         state.getTaskSelectionLogic().setLogic("java.lang.String");
97
98         try {
99             jtse.prepare();
100         } catch (Exception jtseException) {
101             fail("test should not throw an exception here");
102         }
103
104         try {
105             jtse.execute(-1, null, null);
106             fail("test should throw an exception here");
107         } catch (Exception jtseException) {
108             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
109         }
110
111         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
112         EnEvent event = new EnEvent(axEvent);
113         try {
114             jtse.execute(-1, null, event);
115             fail("test should throw an exception here");
116         } catch (Exception jtseException) {
117             assertEquals("task selection logic failed to run for state  \"NULL:0.0.0:NULL:NULL\"",
118                             jtseException.getMessage());
119         }
120
121         state.getTaskSelectionLogic()
122                         .setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaTaskSelectionLogic");
123         try {
124             jtse.prepare();
125             jtse.execute(-1, null, event);
126             fail("test should throw an exception here");
127         } catch (Exception jtseException) {
128             assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",
129                             jtseException.getMessage());
130         }
131
132         try {
133             jtse.prepare();
134             AxArtifactKey taskKey = jtse.execute(0, null, event);
135             assertEquals("NULL:0.0.0", taskKey.getId());
136             jtse.cleanUp();
137         } catch (Exception jtseException) {
138             fail("test should not throw an exception here");
139         }
140     }
141 }