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
ControlLoopOperationParams params2 = mock(ControlLoopOperationParams.class);
when(params2.start()).thenReturn(future2);
- assertSame(future2, context.obtain("obtain-C", params2));
+ assertSame(future2, context.obtain(ITEM_KEY, params2));
return future;
});
- assertSame(future2, context.obtain("obtain-C", params));
+ 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));
}
}