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