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