a18718cc5af1b900cfdc6e9d129ca7da69eae425
[policy/apex-pdp.git] / core / core-engine / src / test / java / org / onap / policy / apex / core / engine / executor / context / AxTaskFacadeTest.java
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.core.engine.executor.context;
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.LinkedHashMap;
28 import java.util.Map;
29
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
37 import org.onap.policy.apex.context.parameters.SchemaParameters;
38 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
40 import org.onap.policy.apex.model.basicmodel.service.ModelService;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
43 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
44 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
45 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
46 import org.onap.policy.common.parameters.ParameterService;
47
48 /**
49  * Test the state facade.
50  */
51 public class AxTaskFacadeTest {
52     @Mock
53     private AxTask axTaskMock;
54
55     @Mock
56     private AxInputField axInputFieldMock;
57
58     @Mock
59     private AxInputField axInputFieldBadMock;
60
61     @Mock
62     private AxOutputField axOutputFieldMock;
63
64     @Mock
65     private AxOutputField axOutputFieldBadMock;
66
67     /**
68      * Set up mocking.
69      */
70     @Before
71     public void startMocking() {
72         AxContextSchemas schemas = new AxContextSchemas();
73
74         AxArtifactKey stringTypeKey = new AxArtifactKey("StringType:0.0.1");
75         AxContextSchema stringType = new AxContextSchema(stringTypeKey, "Java", "java.lang.String");
76         schemas.getSchemasMap().put(stringTypeKey, stringType);
77
78         ModelService.registerModel(AxContextSchemas.class, schemas);
79
80         MockitoAnnotations.initMocks(this);
81
82         AxArtifactKey task0Key = new AxArtifactKey("Task0:0.0.1");
83         Mockito.doReturn(task0Key).when(axTaskMock).getKey();
84         Mockito.doReturn(task0Key.getId()).when(axTaskMock).getId();
85
86         Map<String, AxInputField> inFieldMap = new LinkedHashMap<>();
87         Map<String, AxOutputField> outFieldMap = new LinkedHashMap<>();
88
89         inFieldMap.put("InField0", axInputFieldMock);
90         inFieldMap.put("InFieldBad", axInputFieldBadMock);
91         outFieldMap.put("OutField0", axOutputFieldMock);
92         outFieldMap.put("OutFieldBad", axOutputFieldBadMock);
93
94         Mockito.doReturn(inFieldMap).when(axTaskMock).getInputFields();
95         Mockito.doReturn(outFieldMap).when(axTaskMock).getOutputFields();
96
97         Mockito.doReturn(new AxReferenceKey(task0Key, "InField0")).when(axInputFieldMock).getKey();
98         Mockito.doReturn(stringTypeKey).when(axInputFieldMock).getSchema();
99
100         Mockito.doReturn(new AxReferenceKey(task0Key, "OutField0")).when(axOutputFieldMock).getKey();
101         Mockito.doReturn(stringTypeKey).when(axOutputFieldMock).getSchema();
102
103         ParameterService.register(new SchemaParameters());
104     }
105
106     @After
107     public void teardown() {
108         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
109         ModelService.clear();
110     }
111
112     @Test
113     public void testAxStateFacade() {
114         AxTaskFacade taskFacade = new AxTaskFacade(axTaskMock);
115
116         assertEquals("Task0", taskFacade.getTaskName());
117         assertEquals("Task0:0.0.1", taskFacade.getId());
118
119         try {
120             taskFacade.getInFieldSchemaHelper("InFieldDoesntExist");
121             fail("test should throw an exception");
122         } catch (Exception exc) {
123             assertEquals("no incoming field with name \"InFieldDoesntExist\" " + "defined on task \"Task0:0.0.1\"",
124                             exc.getMessage());
125         }
126
127         try {
128             taskFacade.getOutFieldSchemaHelper("OutFieldDoesntExist");
129             fail("test should throw an exception");
130         } catch (Exception exc) {
131             assertEquals("no outgoing field with name \"OutFieldDoesntExist\" " + "defined on task \"Task0:0.0.1\"",
132                             exc.getMessage());
133         }
134
135         assertNotNull(taskFacade.getInFieldSchemaHelper("InField0"));
136         assertNotNull(taskFacade.getOutFieldSchemaHelper("OutField0"));
137
138         try {
139             taskFacade.getInFieldSchemaHelper("InFieldBad");
140             fail("test should throw an exception");
141         } catch (Exception exc) {
142             assertEquals("schema helper cannot be created for task field \"InFieldBad\" "
143                             + "with key \"null\" with schema \"null\"", exc.getMessage());
144         }
145
146         try {
147             taskFacade.getOutFieldSchemaHelper("OutFieldBad");
148             fail("test should throw an exception");
149         } catch (Exception exc) {
150             assertEquals("schema helper cannot be created for task field \"OutFieldBad\" "
151                             + "with key \"null\" with schema \"null\"", exc.getMessage());
152         }
153
154     }
155 }