dbb250f8a40477c69f546c7452e001b428b221a6
[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     @Before
49     public void initiateParameters() {
50         ParameterService.register(new DistributorParameters());
51         ParameterService.register(new LockManagerParameters());
52         ParameterService.register(new PersistorParameters());
53     }
54
55     @After
56     public void clearParameters() {
57         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
58         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
59         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
60     }
61
62     @Test
63     public void testJavaTaskSelectExecutor() {
64         JavaTaskSelectExecutor jtse = new JavaTaskSelectExecutor();
65         assertNotNull(jtse);
66
67         try {
68             jtse.prepare();
69             fail("test should throw an exception here");
70         } catch (Exception jtseException) {
71             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
72         }
73
74         AxState state = new AxState();
75         ApexInternalContext internalContext = null;
76         try {
77             internalContext = new ApexInternalContext(new AxPolicyModel());
78         } catch (ContextException e) {
79             fail("test should not throw an exception here");
80         }
81         jtse.setContext(null, state, internalContext);
82
83         try {
84             jtse.prepare();
85             fail("test should throw an exception here");
86         } catch (Exception jtseException) {
87             assertEquals("instantiation error on Java class \"\"", jtseException.getMessage());
88         }
89
90         state.getTaskSelectionLogic().setLogic("java.lang.String");
91
92         try {
93             jtse.prepare();
94         } catch (Exception jtseException) {
95             fail("test should not throw an exception here");
96         }
97
98         try {
99             jtse.execute(-1, null);
100             fail("test should throw an exception here");
101         } catch (Exception jtseException) {
102             assertEquals(java.lang.NullPointerException.class, jtseException.getClass());
103         }
104
105         AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1"));
106         EnEvent event = new EnEvent(axEvent);
107         try {
108             jtse.execute(-1, event);
109             fail("test should throw an exception here");
110         } catch (Exception jtseException) {
111             assertEquals("task selection logic failed to run for state  \"NULL:0.0.0:NULL:NULL\"",
112                             jtseException.getMessage());
113         }
114
115         state.getTaskSelectionLogic()
116                         .setLogic("org.onap.policy.apex.plugins.executor.java.DummyJavaTaskSelectionLogic");
117         try {
118             jtse.prepare();
119             jtse.execute(-1, event);
120             fail("test should throw an exception here");
121         } catch (Exception jtseException) {
122             assertEquals("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\"",
123                             jtseException.getMessage());
124         }
125
126         try {
127             jtse.prepare();
128             AxArtifactKey taskKey = jtse.execute(0, event);
129             assertEquals("NULL:0.0.0", taskKey.getId());
130             jtse.cleanUp();
131         } catch (Exception jtseException) {
132             fail("test should not throw an exception here");
133         }
134     }
135 }