6f8402e55cd477a65c7e3dc58361196a7fb4a440
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.core.engine.executor.context;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import java.util.Map;
30 import java.util.TreeMap;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
38 import org.onap.policy.apex.context.parameters.SchemaParameters;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
41 import org.onap.policy.apex.model.basicmodel.service.ModelService;
42 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
43 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
44 import org.onap.policy.apex.model.eventmodel.concepts.AxEvent;
45 import org.onap.policy.apex.model.eventmodel.concepts.AxField;
46 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
47 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
48 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
49 import org.onap.policy.common.parameters.ParameterService;
50
51 /**
52  * Test the state facade.
53  */
54 public class AxTaskFacadeTest {
55     @Mock
56     private AxTask axTaskMock;
57
58     @Mock
59     private AxInputField axInputFieldMock;
60
61     @Mock
62     private AxInputField axInputFieldBadMock;
63
64     @Mock
65     private AxOutputField axOutputFieldMock;
66
67     @Mock
68     private AxOutputField axOutputFieldBadMock;
69
70     /**
71      * Set up mocking.
72      */
73     @Before
74     public void startMocking() {
75         AxContextSchemas schemas = new AxContextSchemas();
76
77         AxArtifactKey stringTypeKey = new AxArtifactKey("StringType:0.0.1");
78         AxContextSchema stringType = new AxContextSchema(stringTypeKey, "Java", "java.lang.String");
79         schemas.getSchemasMap().put(stringTypeKey, stringType);
80
81         ModelService.registerModel(AxContextSchemas.class, schemas);
82
83         MockitoAnnotations.initMocks(this);
84
85         AxArtifactKey task0Key = new AxArtifactKey("Task0:0.0.1");
86         Mockito.doReturn(task0Key).when(axTaskMock).getKey();
87         Mockito.doReturn(task0Key.getId()).when(axTaskMock).getId();
88
89         Map<String, AxField> inFieldMap = Map.of("InField0", axInputFieldMock, "InFieldBad", axInputFieldBadMock);
90         Map<String, AxField> outFieldMap = Map.of("OutField0", axOutputFieldMock, "OutFieldBad", axOutputFieldBadMock);
91
92         AxEvent inEvent = new AxEvent();
93         inEvent.setParameterMap(inFieldMap);
94         AxEvent outEvent = new AxEvent(new AxArtifactKey("outputEvent:1.0.0"));
95         outEvent.setParameterMap(outFieldMap);
96         Map<String, AxEvent> outEvents = new TreeMap<>();
97         outEvents.put(outEvent.getKey().getName(), outEvent);
98
99         Mockito.doReturn(inEvent).when(axTaskMock).getInputEvent();
100         Mockito.doReturn(outEvents).when(axTaskMock).getOutputEvents();
101
102         Mockito.doReturn(inFieldMap).when(axTaskMock).getInputFields();
103         Mockito.doReturn(outFieldMap).when(axTaskMock).getOutputFields();
104
105         Mockito.doReturn(new AxReferenceKey(task0Key, "InField0")).when(axInputFieldMock).getKey();
106         Mockito.doReturn(stringTypeKey).when(axInputFieldMock).getSchema();
107
108         Mockito.doReturn(new AxReferenceKey(task0Key, "OutField0")).when(axOutputFieldMock).getKey();
109         Mockito.doReturn(stringTypeKey).when(axOutputFieldMock).getSchema();
110
111         ParameterService.register(new SchemaParameters());
112     }
113
114     @After
115     public void teardown() {
116         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
117         ModelService.clear();
118     }
119
120     @Test
121     public void testAxStateFacade() {
122         AxTaskFacade taskFacade = new AxTaskFacade(axTaskMock);
123
124         assertEquals("Task0", taskFacade.getTaskName());
125         assertEquals("Task0:0.0.1", taskFacade.getId());
126
127         assertThatThrownBy(() -> taskFacade.getInFieldSchemaHelper("InFieldDoesntExist"))
128             .hasMessage("no incoming field with name \"InFieldDoesntExist\" " + "defined on task "
129                     + "\"Task0:0.0.1\"");
130         assertThatThrownBy(() -> taskFacade.getOutFieldSchemaHelper("OutFieldDoesntExist"))
131             .hasMessage("no outgoing field with name \"OutFieldDoesntExist\" " + "defined on task "
132                     + "\"Task0:0.0.1\"");
133         assertNotNull(taskFacade.getInFieldSchemaHelper("InField0"));
134         assertNotNull(taskFacade.getOutFieldSchemaHelper("OutField0"));
135
136         assertThatThrownBy(() -> taskFacade.getInFieldSchemaHelper("InFieldBad"))
137             .hasMessage("schema helper cannot be created for task field \"InFieldBad\" "
138                     + "with key \"null\" with schema \"null\"");
139         assertThatThrownBy(() -> taskFacade.getOutFieldSchemaHelper("OutFieldBad"))
140             .hasMessage("schema helper cannot be created for task field \"OutFieldBad\" "
141                     + "with key \"null\" with schema \"null\"");
142     }
143 }