5857e0513fbdb73d6208ef089abf5222e9ada18c
[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.LinkedHashMap;
29 import java.util.LinkedHashSet;
30 import java.util.Map;
31 import java.util.Set;
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.event.EnEvent;
40 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
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.AxState;
44
45 /**
46  * Test Task Execution Context.
47  */
48 public class TaskSelectionExecutionContextTest {
49     @Mock
50     private TaskSelectExecutor taskSelectExecutorMock;
51
52     @Mock
53     private TaskSelectExecutor parentExecutorMock;
54
55     @Mock
56     private AxState axStateMock;
57
58     @Mock
59     private ApexInternalContext internalContextMock;
60
61     @Mock
62     private EnEvent incomingEventMock;
63
64     /**
65      * Set up mocking.
66      */
67     @Before
68     public void startMocking() {
69         MockitoAnnotations.initMocks(this);
70
71         Set<AxArtifactKey> contextAlbumReferences = new LinkedHashSet<>();
72         contextAlbumReferences.add(new AxArtifactKey(("AlbumKey0:0.0.1")));
73         contextAlbumReferences.add(new AxArtifactKey(("AlbumKey1:0.0.1")));
74
75         Mockito.doReturn(contextAlbumReferences).when(axStateMock).getContextAlbumReferences();
76         Mockito.doReturn(new AxReferenceKey("Parent:0.0.1:ParentName:StateName")).when(axStateMock).getKey();
77
78         Map<AxArtifactKey, ContextAlbum> contextAlbumMap = new LinkedHashMap<>();
79         AxArtifactKey album0Key = new AxArtifactKey("AlbumKey0:0.0.1");
80         AxArtifactKey album1Key = new AxArtifactKey("AlbumKey1:0.0.1");
81
82         contextAlbumMap.put(album0Key, new DummyContextAlbum(album0Key));
83         contextAlbumMap.put(album1Key, new DummyContextAlbum(album1Key));
84
85         Mockito.doReturn(contextAlbumMap).when(internalContextMock).getContextAlbums();
86
87         Mockito.doReturn(parentExecutorMock).when(taskSelectExecutorMock).getParent();
88         Mockito.doReturn(new AxReferenceKey("Parent:0.0.1:ParentName:LocalName")).when(parentExecutorMock).getKey();
89     }
90
91     @Test
92     public void test() {
93         final AxArtifactKey outgoingEventKey = new AxArtifactKey("OutEvent:0.0.1");
94
95         TaskSelectionExecutionContext tsec = new TaskSelectionExecutionContext(taskSelectExecutorMock, 0, axStateMock,
96                         incomingEventMock, outgoingEventKey, internalContextMock);
97
98         assertNotNull(tsec);
99         tsec.setMessage("TSEC Message");
100         assertEquals("TSEC Message", tsec.getMessage());
101
102         ContextAlbum contextAlbum = tsec.getContextAlbum("AlbumKey0");
103         assertEquals("AlbumKey0:0.0.1", contextAlbum.getKey().getId());
104
105         assertThatThrownBy(() -> tsec.getContextAlbum("AlbumKeyNonExistant"))
106             .hasMessage("cannot find definition of context album \"AlbumKeyNonExistant\" "
107                             + "on state \"Parent:0.0.1:ParentName:StateName\"");
108     }
109 }