9c6aa2f07256708f52e90d7cc851246ce6921c40
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020, 2023 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.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.junit.MockitoJUnitRunner;
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.StateFinalizerExecutor;
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 @RunWith(MockitoJUnitRunner.class)
49 public class StateFinalizerExecutionContextTest {
50     @Mock
51     private StateFinalizerExecutor stateFinalizerExecutorMock;
52
53     @Mock
54     private StateFinalizerExecutor parentExecutorMock;
55
56     @Mock
57     private AxState axStateMock;
58
59     @Mock
60     private ApexInternalContext internalContextMock;
61
62     /**
63      * Set up mocking.
64      */
65     @Before
66     public void startMocking() {
67
68         Set<AxArtifactKey> contextAlbumReferences = new LinkedHashSet<>();
69         contextAlbumReferences.add(new AxArtifactKey(("AlbumKey0:0.0.1")));
70         contextAlbumReferences.add(new AxArtifactKey(("AlbumKey1:0.0.1")));
71
72         Mockito.doReturn(contextAlbumReferences).when(axStateMock).getContextAlbumReferences();
73         Mockito.doReturn(new AxReferenceKey("Parent:0.0.1:ParentName:StateName")).when(axStateMock).getKey();
74
75         Map<AxArtifactKey, ContextAlbum> contextAlbumMap = new LinkedHashMap<>();
76         AxArtifactKey album0Key = new AxArtifactKey("AlbumKey0:0.0.1");
77         AxArtifactKey album1Key = new AxArtifactKey("AlbumKey1:0.0.1");
78
79         contextAlbumMap.put(album0Key, new DummyContextAlbum(album0Key));
80         contextAlbumMap.put(album1Key, new DummyContextAlbum(album1Key));
81
82         Mockito.doReturn(contextAlbumMap).when(internalContextMock).getContextAlbums();
83
84         Mockito.doReturn(parentExecutorMock).when(stateFinalizerExecutorMock).getParent();
85         Mockito.doReturn(new AxReferenceKey("Parent:0.0.1:ParentName:LocalName")).when(parentExecutorMock).getKey();
86     }
87
88     @Test
89     public void test() {
90         final Map<String, Object> fields = new LinkedHashMap<>();
91         final Set<String> stateOutputNames = new LinkedHashSet<>();
92
93         StateFinalizerExecutionContext sfec = new StateFinalizerExecutionContext(stateFinalizerExecutorMock, 0, null,
94                         axStateMock, fields, stateOutputNames, internalContextMock);
95
96         assertNotNull(sfec);
97         sfec.setMessage("SFEC Message");
98         assertEquals("SFEC Message", sfec.getMessage());
99
100         sfec.setSelectedStateOutputName("SomeOutput");
101         assertEquals("SomeOutput", sfec.getSelectedStateOutputName());
102
103         ContextAlbum contextAlbum = sfec.getContextAlbum("AlbumKey0");
104         assertEquals("AlbumKey0:0.0.1", contextAlbum.getKey().getId());
105
106         assertThatThrownBy(() -> sfec.getContextAlbum("AlbumKeyNonExistant"))
107             .hasMessage("cannot find definition of context album \"AlbumKeyNonExistant\" "
108                             + "on state \"Parent:0.0.1:ParentName:StateName\"");
109     }
110 }