Point to snapshot versions
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / controlloop / ControlLoopEventContextTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.controlloop;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Map;
35 import java.util.UUID;
36 import java.util.concurrent.CompletableFuture;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
42
43 public class ControlLoopEventContextTest {
44     private static final String MY_KEY = "def";
45     private static final UUID REQ_ID = UUID.randomUUID();
46     private static final String ITEM_KEY = "obtain-C";
47
48     private Map<String, String> enrichment;
49     private VirtualControlLoopEvent event;
50     private ControlLoopEventContext context;
51
52     /**
53      * Initializes data, including {@link #context}.
54      */
55     @Before
56     public void setUp() {
57         enrichment = Map.of("abc", "one", MY_KEY, "two");
58
59         event = new VirtualControlLoopEvent();
60         event.setRequestId(REQ_ID);
61         event.setAai(enrichment);
62
63         context = new ControlLoopEventContext(event);
64     }
65
66     @Test
67     public void testControlLoopEventContext() {
68         assertSame(event, context.getEvent());
69         assertSame(REQ_ID, context.getRequestId());
70         assertEquals(enrichment, context.getEnrichment());
71
72         // null event
73         assertThatThrownBy(() -> new ControlLoopEventContext(null));
74
75         // no request id, no enrichment data
76         event.setRequestId(null);
77         event.setAai(null);
78         context = new ControlLoopEventContext(event);
79         assertSame(event, context.getEvent());
80         assertNotNull(context.getRequestId());
81         assertEquals(Map.of(), context.getEnrichment());
82     }
83
84     @Test
85     public void testContains_testGetProperty_testSetProperty_testRemoveProperty() {
86         context.setProperty("abc", "a string");
87         context.setProperty(MY_KEY, 100);
88
89         assertTrue(context.contains(MY_KEY));
90         assertFalse(context.contains("ghi"));
91
92         String strValue = context.getProperty("abc");
93         assertEquals("a string", strValue);
94
95         int intValue = context.getProperty(MY_KEY);
96         assertEquals(100, intValue);
97
98         context.removeProperty(MY_KEY);
99         assertFalse(context.contains(MY_KEY));
100     }
101
102     @Test
103     public void testObtain() {
104         final ControlLoopOperationParams params = mock(ControlLoopOperationParams.class);
105
106         // property is already loaded
107         context.setProperty("obtain-A", "value-A");
108         assertNull(context.obtain("obtain-A", params));
109
110         // new property - should retrieve
111         CompletableFuture<OperationOutcome> future = new CompletableFuture<>();
112         when(params.start()).thenReturn(future);
113         assertSame(future, context.obtain("obtain-B", params));
114
115         // repeat - should get the same future, without invoking start() again
116         assertSame(future, context.obtain("obtain-B", params));
117         verify(params).start();
118
119         // arrange for another invoker to start while this one is starting
120         CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
121
122         when(params.start()).thenAnswer(args -> {
123
124             ControlLoopOperationParams params2 = mock(ControlLoopOperationParams.class);
125             when(params2.start()).thenReturn(future2);
126
127             assertSame(future2, context.obtain(ITEM_KEY, params2));
128             return future;
129         });
130
131         assertSame(future2, context.obtain(ITEM_KEY, params));
132
133         // should have canceled the interrupted future
134         assertTrue(future.isCancelled());
135
136         // return a new future next time start() is called
137         CompletableFuture<OperationOutcome> future3 = new CompletableFuture<>();
138         when(params.start()).thenReturn(future3);
139
140         // repeat - should get the same future
141         assertSame(future2, context.obtain(ITEM_KEY, params));
142         assertSame(future2, context.obtain(ITEM_KEY, params));
143
144         // future2 should still be active
145         assertFalse(future2.isCancelled());
146
147         // cancel it - now we should get the new future
148         future2.cancel(false);
149         assertSame(future3, context.obtain(ITEM_KEY, params));
150     }
151 }