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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.core.engine.executor.context;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
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;
52 * Test the state facade.
54 public class AxTaskFacadeTest {
56 private AxTask axTaskMock;
59 private AxInputField axInputFieldMock;
62 private AxInputField axInputFieldBadMock;
65 private AxOutputField axOutputFieldMock;
68 private AxOutputField axOutputFieldBadMock;
74 public void startMocking() {
75 AxContextSchemas schemas = new AxContextSchemas();
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);
81 ModelService.registerModel(AxContextSchemas.class, schemas);
83 MockitoAnnotations.initMocks(this);
85 AxArtifactKey task0Key = new AxArtifactKey("Task0:0.0.1");
86 Mockito.doReturn(task0Key).when(axTaskMock).getKey();
87 Mockito.doReturn(task0Key.getId()).when(axTaskMock).getId();
89 Map<String, AxField> inFieldMap = Map.of("InField0", axInputFieldMock, "InFieldBad", axInputFieldBadMock);
90 Map<String, AxField> outFieldMap = Map.of("OutField0", axOutputFieldMock, "OutFieldBad", axOutputFieldBadMock);
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);
99 Mockito.doReturn(inEvent).when(axTaskMock).getInputEvent();
100 Mockito.doReturn(outEvents).when(axTaskMock).getOutputEvents();
102 Mockito.doReturn(new AxReferenceKey(task0Key, "InField0")).when(axInputFieldMock).getKey();
103 Mockito.doReturn(stringTypeKey).when(axInputFieldMock).getSchema();
105 Mockito.doReturn(new AxReferenceKey(task0Key, "OutField0")).when(axOutputFieldMock).getKey();
106 Mockito.doReturn(stringTypeKey).when(axOutputFieldMock).getSchema();
108 ParameterService.register(new SchemaParameters());
112 public void teardown() {
113 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
114 ModelService.clear();
118 public void testAxStateFacade() {
119 AxTaskFacade taskFacade = new AxTaskFacade(axTaskMock);
121 assertEquals("Task0", taskFacade.getTaskName());
122 assertEquals("Task0:0.0.1", taskFacade.getId());
124 assertThatThrownBy(() -> taskFacade.getInFieldSchemaHelper("InFieldDoesntExist"))
125 .hasMessage("no incoming field with name \"InFieldDoesntExist\" " + "defined on task "
126 + "\"Task0:0.0.1\"");
127 assertThatThrownBy(() -> taskFacade.getOutFieldSchemaHelper("OutFieldDoesntExist"))
128 .hasMessage("no outgoing field with name \"OutFieldDoesntExist\" " + "defined on task "
129 + "\"Task0:0.0.1\"");
130 assertNotNull(taskFacade.getInFieldSchemaHelper("InField0"));
131 assertNotNull(taskFacade.getOutFieldSchemaHelper("OutField0"));
133 assertThatThrownBy(() -> taskFacade.getInFieldSchemaHelper("InFieldBad"))
134 .hasMessage("schema helper cannot be created for task field \"InFieldBad\" "
135 + "with key \"null\" with schema \"null\"");
136 assertThatThrownBy(() -> taskFacade.getOutFieldSchemaHelper("OutFieldBad"))
137 .hasMessage("schema helper cannot be created for task field \"OutFieldBad\" "
138 + "with key \"null\" with schema \"null\"");