More changes to actor code
[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 UUID REQ_ID = UUID.randomUUID();
45     private static final String ITEM_KEY = "obtain-C";
46
47     private Map<String, String> enrichment;
48     private VirtualControlLoopEvent event;
49     private ControlLoopEventContext context;
50
51     /**
52      * Initializes data, including {@link #context}.
53      */
54     @Before
55     public void setUp() {
56         enrichment = Map.of("abc", "one", "def", "two");
57
58         event = new VirtualControlLoopEvent();
59         event.setRequestId(REQ_ID);
60         event.setAai(enrichment);
61
62         context = new ControlLoopEventContext(event);
63     }
64
65     @Test
66     public void testControlLoopEventContext() {
67         assertSame(event, context.getEvent());
68         assertSame(REQ_ID, context.getRequestId());
69         assertEquals(enrichment, context.getEnrichment());
70
71         // null event
72         assertThatThrownBy(() -> new ControlLoopEventContext(null));
73
74         // no request id, no enrichment data
75         event.setRequestId(null);
76         event.setAai(null);
77         context = new ControlLoopEventContext(event);
78         assertSame(event, context.getEvent());
79         assertNotNull(context.getRequestId());
80         assertEquals(Map.of(), context.getEnrichment());
81     }
82
83     @Test
84     public void testContains_testGetProperty_testSetProperty() {
85         context.setProperty("abc", "a string");
86         context.setProperty("def", 100);
87
88         assertFalse(context.contains("ghi"));
89
90         String strValue = context.getProperty("abc");
91         assertEquals("a string", strValue);
92
93         int intValue = context.getProperty("def");
94         assertEquals(100, intValue);
95     }
96
97     @Test
98     public void testObtain() {
99         final ControlLoopOperationParams params = mock(ControlLoopOperationParams.class);
100
101         // property is already loaded
102         context.setProperty("obtain-A", "value-A");
103         assertNull(context.obtain("obtain-A", params));
104
105         // new property - should retrieve
106         CompletableFuture<OperationOutcome> future = new CompletableFuture<>();
107         when(params.start()).thenReturn(future);
108         assertSame(future, context.obtain("obtain-B", params));
109
110         // repeat - should get the same future, without invoking start() again
111         assertSame(future, context.obtain("obtain-B", params));
112         verify(params).start();
113
114         // arrange for another invoker to start while this one is starting
115         CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
116
117         when(params.start()).thenAnswer(args -> {
118
119             ControlLoopOperationParams params2 = mock(ControlLoopOperationParams.class);
120             when(params2.start()).thenReturn(future2);
121
122             assertSame(future2, context.obtain(ITEM_KEY, params2));
123             return future;
124         });
125
126         assertSame(future2, context.obtain(ITEM_KEY, params));
127
128         // should have canceled the interrupted future
129         assertTrue(future.isCancelled());
130
131         // return a new future next time start() is called
132         CompletableFuture<OperationOutcome> future3 = new CompletableFuture<>();
133         when(params.start()).thenReturn(future3);
134
135         // repeat - should get the same future
136         assertSame(future2, context.obtain(ITEM_KEY, params));
137         assertSame(future2, context.obtain(ITEM_KEY, params));
138
139         // future2 should still be active
140         assertFalse(future2.isCancelled());
141
142         // cancel it - now we should get the new future
143         future2.cancel(false);
144         assertSame(future3, context.obtain(ITEM_KEY, params));
145     }
146 }