import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import java.util.Map;
import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
+import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
public class ControlLoopEventContextTest {
+ private static final String MY_KEY = "def";
private static final UUID REQ_ID = UUID.randomUUID();
+ private static final String ITEM_KEY = "obtain-C";
private Map<String, String> enrichment;
private VirtualControlLoopEvent event;
*/
@Before
public void setUp() {
- enrichment = Map.of("abc", "one", "def", "two");
+ enrichment = Map.of("abc", "one", MY_KEY, "two");
event = new VirtualControlLoopEvent();
event.setRequestId(REQ_ID);
}
@Test
- public void testContains_testGetProperty_testSetProperty() {
+ public void testContains_testGetProperty_testSetProperty_testRemoveProperty() {
context.setProperty("abc", "a string");
- context.setProperty("def", 100);
+ context.setProperty(MY_KEY, 100);
+ assertTrue(context.contains(MY_KEY));
assertFalse(context.contains("ghi"));
String strValue = context.getProperty("abc");
assertEquals("a string", strValue);
- int intValue = context.getProperty("def");
+ int intValue = context.getProperty(MY_KEY);
assertEquals(100, intValue);
+
+ context.removeProperty(MY_KEY);
+ assertFalse(context.contains(MY_KEY));
+ }
+
+ @Test
+ public void testObtain() {
+ final ControlLoopOperationParams params = mock(ControlLoopOperationParams.class);
+
+ // property is already loaded
+ context.setProperty("obtain-A", "value-A");
+ assertNull(context.obtain("obtain-A", params));
+
+ // new property - should retrieve
+ CompletableFuture<OperationOutcome> future = new CompletableFuture<>();
+ when(params.start()).thenReturn(future);
+ assertSame(future, context.obtain("obtain-B", params));
+
+ // repeat - should get the same future, without invoking start() again
+ assertSame(future, context.obtain("obtain-B", params));
+ verify(params).start();
+
+ // arrange for another invoker to start while this one is starting
+ CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
+
+ when(params.start()).thenAnswer(args -> {
+
+ ControlLoopOperationParams params2 = mock(ControlLoopOperationParams.class);
+ when(params2.start()).thenReturn(future2);
+
+ assertSame(future2, context.obtain(ITEM_KEY, params2));
+ return future;
+ });
+
+ assertSame(future2, context.obtain(ITEM_KEY, params));
+
+ // should have canceled the interrupted future
+ assertTrue(future.isCancelled());
+
+ // return a new future next time start() is called
+ CompletableFuture<OperationOutcome> future3 = new CompletableFuture<>();
+ when(params.start()).thenReturn(future3);
+
+ // repeat - should get the same future
+ assertSame(future2, context.obtain(ITEM_KEY, params));
+ assertSame(future2, context.obtain(ITEM_KEY, params));
+
+ // future2 should still be active
+ assertFalse(future2.isCancelled());
+
+ // cancel it - now we should get the new future
+ future2.cancel(false);
+ assertSame(future3, context.obtain(ITEM_KEY, params));
}
}