f8bdc4bddc19bfadfc7a63d40039ac5030dd064c
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.executor.context;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.LinkedHashSet;
31 import java.util.Map;
32 import java.util.Set;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.apex.context.ContextAlbum;
39 import org.onap.policy.apex.core.engine.context.ApexInternalContext;
40 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
43 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
44 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
45
46 /**
47  * Test Task Execution Context.
48  */
49 public class TaskExecutionContextTest {
50     @Mock
51     private TaskExecutor taskExecutorMock;
52
53     @Mock
54     private TaskExecutor parentExecutorMock;
55
56     @Mock
57     private AxTask axTaskMock;
58
59     @Mock
60     private ApexInternalContext internalContextMock;
61
62     /**
63      * Set up mocking.
64      */
65     @Before
66     public void startMocking() {
67         MockitoAnnotations.initMocks(this);
68
69         Set<AxArtifactKey> contextAlbumReferences = new LinkedHashSet<>();
70         contextAlbumReferences.add(new AxArtifactKey(("AlbumKey0:0.0.1")));
71         contextAlbumReferences.add(new AxArtifactKey(("AlbumKey1:0.0.1")));
72
73         Mockito.doReturn(contextAlbumReferences).when(axTaskMock).getContextAlbumReferences();
74
75         Map<String, AxTaskParameter> taskParameters = new HashMap<>();
76         taskParameters.put("parameterKey1", new AxTaskParameter(new AxReferenceKey(), "parameterValue1"));
77         taskParameters.put("parameterKey2", new AxTaskParameter(new AxReferenceKey(), "parameterValue2"));
78         Mockito.doReturn(taskParameters).when(axTaskMock).getTaskParameters();
79
80         Map<AxArtifactKey, ContextAlbum> contextAlbumMap = new LinkedHashMap<>();
81         AxArtifactKey album0Key = new AxArtifactKey("AlbumKey0:0.0.1");
82         AxArtifactKey album1Key = new AxArtifactKey("AlbumKey1:0.0.1");
83
84         contextAlbumMap.put(album0Key, new DummyContextAlbum(album0Key));
85         contextAlbumMap.put(album1Key, new DummyContextAlbum(album1Key));
86
87         Mockito.doReturn(contextAlbumMap).when(internalContextMock).getContextAlbums();
88
89         Mockito.doReturn(parentExecutorMock).when(taskExecutorMock).getParent();
90         Mockito.doReturn(new AxArtifactKey("Parent:0.0.1")).when(parentExecutorMock).getKey();
91     }
92
93     @Test
94     public void test() {
95         final Map<String, Object> inFields = new LinkedHashMap<>();
96         final Map<String, Object> outFields = new LinkedHashMap<>();
97
98         TaskExecutionContext tec = new TaskExecutionContext(taskExecutorMock, 0, null, axTaskMock, inFields, outFields,
99                         internalContextMock);
100
101         assertNotNull(tec);
102         tec.setMessage("TEC Message");
103         assertEquals("TEC Message", tec.getMessage());
104
105         ContextAlbum contextAlbum = tec.getContextAlbum("AlbumKey0");
106         assertEquals("AlbumKey0:0.0.1", contextAlbum.getKey().getId());
107
108         Map<String, String> parameters = tec.getParameters();
109         assertEquals("parameterValue1", parameters.get("parameterKey1"));
110         assertEquals("parameterValue2", parameters.get("parameterKey2"));
111
112         assertThatThrownBy(() -> tec.getContextAlbum("AlbumKeyNonExistant"))
113             .hasMessageContaining("cannot find definition of context album \"AlbumKeyNonExistant\" on task \"null\"");
114     }
115 }