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