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