24c5048222f8f3077b7107a6cf10372e99b61609
[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.HashMap;
30 import java.util.LinkedHashMap;
31 import java.util.LinkedHashSet;
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
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;
48
49 /**
50  * Test Task Execution Context.
51  */
52 public class TaskExecutionContextTest {
53     @Mock
54     private TaskExecutor taskExecutorMock;
55
56     @Mock
57     private TaskExecutor parentExecutorMock;
58
59     @Mock
60     private AxTask axTaskMock;
61
62     @Mock
63     private ApexInternalContext internalContextMock;
64
65     /**
66      * Set up mocking.
67      */
68     @Before
69     public void startMocking() {
70         MockitoAnnotations.initMocks(this);
71
72         Set<AxArtifactKey> contextAlbumReferences = new LinkedHashSet<>();
73         contextAlbumReferences.add(new AxArtifactKey(("AlbumKey0:0.0.1")));
74         contextAlbumReferences.add(new AxArtifactKey(("AlbumKey1:0.0.1")));
75
76         Mockito.doReturn(contextAlbumReferences).when(axTaskMock).getContextAlbumReferences();
77
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();
82
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");
86
87         contextAlbumMap.put(album0Key, new DummyContextAlbum(album0Key));
88         contextAlbumMap.put(album1Key, new DummyContextAlbum(album1Key));
89
90         Mockito.doReturn(contextAlbumMap).when(internalContextMock).getContextAlbums();
91
92         Mockito.doReturn(parentExecutorMock).when(taskExecutorMock).getParent();
93         Mockito.doReturn(new AxArtifactKey("Parent:0.0.1")).when(parentExecutorMock).getKey();
94     }
95
96     @Test
97     public void test() {
98         final Map<String, Object> inFields = new LinkedHashMap<>();
99         final List<Map<String, Object>> outFieldsList = new LinkedList<>();
100
101         TaskExecutionContext tec = new TaskExecutionContext(taskExecutorMock, 0, null, axTaskMock, inFields,
102             outFieldsList, internalContextMock);
103
104         assertNotNull(tec);
105         tec.setMessage("TEC Message");
106         assertEquals("TEC Message", tec.getMessage());
107
108         ContextAlbum contextAlbum = tec.getContextAlbum("AlbumKey0");
109         assertEquals("AlbumKey0:0.0.1", contextAlbum.getKey().getId());
110
111         Map<String, String> parameters = tec.getParameters();
112         assertEquals("parameterValue1", parameters.get("parameterKey1"));
113         assertEquals("parameterValue2", parameters.get("parameterKey2"));
114
115         assertThatThrownBy(() -> tec.getContextAlbum("AlbumKeyNonExistant"))
116             .hasMessageContaining("cannot find definition of context album \"AlbumKeyNonExistant\" on task \"null\"");
117     }
118 }