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;
29 import java.util.HashMap;
30 import java.util.LinkedHashMap;
31 import java.util.LinkedHashSet;
32 import java.util.LinkedList;
33 import java.util.List;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.policy.apex.context.ContextAlbum;
42 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
43 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
46 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
47 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
50 * Test Task Execution Context.
52 public class TaskExecutionContextTest {
54 private TaskExecutor taskExecutorMock;
57 private TaskExecutor parentExecutorMock;
60 private AxTask axTaskMock;
63 private ApexInternalContext internalContextMock;
69 public void startMocking() {
70 MockitoAnnotations.initMocks(this);
72 Set<AxArtifactKey> contextAlbumReferences = new LinkedHashSet<>();
73 contextAlbumReferences.add(new AxArtifactKey(("AlbumKey0:0.0.1")));
74 contextAlbumReferences.add(new AxArtifactKey(("AlbumKey1:0.0.1")));
76 Mockito.doReturn(contextAlbumReferences).when(axTaskMock).getContextAlbumReferences();
78 Map<String, AxTaskParameter> taskParameters = new HashMap<>();
79 taskParameters.put("parameterKey1", new AxTaskParameter(new AxReferenceKey(), "parameterValue1"));
80 taskParameters.put("parameterKey2", new AxTaskParameter(new AxReferenceKey(), "parameterValue2"));
81 Mockito.doReturn(taskParameters).when(axTaskMock).getTaskParameters();
83 Map<AxArtifactKey, ContextAlbum> contextAlbumMap = new LinkedHashMap<>();
84 AxArtifactKey album0Key = new AxArtifactKey("AlbumKey0:0.0.1");
85 AxArtifactKey album1Key = new AxArtifactKey("AlbumKey1:0.0.1");
87 contextAlbumMap.put(album0Key, new DummyContextAlbum(album0Key));
88 contextAlbumMap.put(album1Key, new DummyContextAlbum(album1Key));
90 Mockito.doReturn(contextAlbumMap).when(internalContextMock).getContextAlbums();
92 Mockito.doReturn(parentExecutorMock).when(taskExecutorMock).getParent();
93 Mockito.doReturn(new AxArtifactKey("Parent:0.0.1")).when(parentExecutorMock).getKey();
98 final Map<String, Object> inFields = new LinkedHashMap<>();
99 final List<Map<String, Object>> outFieldsList = new LinkedList<>();
101 TaskExecutionContext tec = new TaskExecutionContext(taskExecutorMock, 0, null, axTaskMock, inFields,
102 outFieldsList, internalContextMock);
105 tec.setMessage("TEC Message");
106 assertEquals("TEC Message", tec.getMessage());
108 ContextAlbum contextAlbum = tec.getContextAlbum("AlbumKey0");
109 assertEquals("AlbumKey0:0.0.1", contextAlbum.getKey().getId());
111 Map<String, String> parameters = tec.getParameters();
112 assertEquals("parameterValue1", parameters.get("parameterKey1"));
113 assertEquals("parameterValue2", parameters.get("parameterKey2"));
115 assertThatThrownBy(() -> tec.getContextAlbum("AlbumKeyNonExistant"))
116 .hasMessageContaining("cannot find definition of context album \"AlbumKeyNonExistant\" on task \"null\"");