import java.time.Instant;
import java.util.ArrayDeque;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import org.onap.policy.controlloop.actorserviceprovider.ActorService;
import org.onap.policy.controlloop.actorserviceprovider.Operation;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.Operator;
import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
private static final Logger logger = (Logger) LoggerFactory.getLogger(OperationPartial.class);
private static final ExtractAppender appender = new ExtractAppender();
+ private static final List<String> PROP_NAMES = List.of("hello", "world");
+
@Mock
private ActorService service;
@Mock
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
- /**
+ /*
* Attach appender to the logger.
*/
appender.setContext(logger.getLoggerContext());
assertNull(future.get(5, TimeUnit.SECONDS));
}
+ @Test
+ public void testGetPropertyNames() {
+ assertThat(oper.getPropertyNames()).isEqualTo(PROP_NAMES);
+ }
+
+ @Test
+ public void testGetProperty_testSetProperty() {
+ oper.setProperty("propertyA", "valueA");
+ oper.setProperty("propertyB", "valueB");
+ oper.setProperty("propertyC", 20);
+
+ assertEquals("valueA", oper.getProperty("propertyA"));
+ assertEquals("valueB", oper.getProperty("propertyB"));
+ assertEquals(Integer.valueOf(20), oper.getProperty("propertyC"));
+ }
+
@Test
public void testStart() {
verifyRun("testStart", 1, 1, PolicyResult.SUCCESS);
assertEquals(oper.makeGuardPayload(), payload);
}
+ /**
+ * Tests startGuardAsync() when preprocessing is disabled.
+ */
+ @Test
+ public void testStartGuardAsyncDisabled() {
+ params = params.toBuilder().preprocessed(true).build();
+ assertNull(new MyOper().startGuardAsync());
+ }
+
@Test
public void testMakeGuardPayload() {
Map<String, Object> payload = oper.makeGuardPayload();
public void testIsActorFailed() {
assertFalse(oper.isActorFailed(null));
- OperationOutcome outcome = params.makeOutcome();
+ OperationOutcome outcome = params.makeOutcome(null);
// incorrect outcome
outcome.setResult(PolicyResult.SUCCESS);
/*
* Use an operation that doesn't override doOperation().
*/
- OperationPartial oper2 = new OperationPartial(params, config) {};
+ OperationPartial oper2 = new OperationPartial(params, config, Collections.emptyList()) {};
oper2.start();
assertTrue(executor.runAll(MAX_REQUESTS));
@Override
protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
- OperationOutcome outcome2 = params.makeOutcome();
+ OperationOutcome outcome2 = params.makeOutcome(null);
outcome2.setResult(PolicyResult.SUCCESS);
/*
}
};
- verifyRun("testSetRetryFlag_testRetryOnFailure_NullOutcome", 1, 1, PolicyResult.FAILURE, null, noop());
+ verifyRun("testSetRetryFlag_testRetryOnFailure_NullOutcome", 1, 1, PolicyResult.FAILURE, noop());
}
@Test
public void testIsSameOperation() {
assertFalse(oper.isSameOperation(null));
- OperationOutcome outcome = params.makeOutcome();
+ OperationOutcome outcome = params.makeOutcome(null);
// wrong actor - should be false
outcome.setActor(null);
// first task completes, others do not
List<Supplier<CompletableFuture<OperationOutcome>>> tasks = new LinkedList<>();
- final OperationOutcome outcome = params.makeOutcome();
+ final OperationOutcome outcome = params.makeOutcome(null);
tasks.add(() -> CompletableFuture.completedFuture(outcome));
tasks.add(() -> new CompletableFuture<>());
@Test
public void testAllOfArray() throws Exception {
- final OperationOutcome outcome = params.makeOutcome();
+ final OperationOutcome outcome = params.makeOutcome(null);
CompletableFuture<OperationOutcome> future1 = new CompletableFuture<>();
CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
@Test
public void testAllOfList() throws Exception {
- final OperationOutcome outcome = params.makeOutcome();
+ final OperationOutcome outcome = params.makeOutcome(null);
CompletableFuture<OperationOutcome> future1 = new CompletableFuture<>();
CompletableFuture<OperationOutcome> future2 = new CompletableFuture<>();
// null outcome - takes precedence over a success
List<Supplier<CompletableFuture<OperationOutcome>>> tasks = new LinkedList<>();
- tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome()));
+ tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome(null)));
tasks.add(() -> CompletableFuture.completedFuture(null));
- tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome()));
+ tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome(null)));
CompletableFuture<OperationOutcome> result = oper.allOf(tasks);
assertTrue(executor.runAll(MAX_REQUESTS));
IllegalStateException except = new IllegalStateException(EXPECTED_EXCEPTION);
tasks.clear();
- tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome()));
+ tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome(null)));
tasks.add(() -> CompletableFuture.failedFuture(except));
- tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome()));
+ tasks.add(() -> CompletableFuture.completedFuture(params.makeOutcome(null)));
result = oper.allOf(tasks);
assertTrue(executor.runAll(MAX_REQUESTS));
*/
@Test
public void testSequence() throws Exception {
- final OperationOutcome outcome = params.makeOutcome();
+ final OperationOutcome outcome = params.makeOutcome(null);
List<Supplier<CompletableFuture<OperationOutcome>>> tasks = new LinkedList<>();
tasks.add(() -> CompletableFuture.completedFuture(outcome));
assertSame(outcome, result.get());
// second task fails, third should not run
- OperationOutcome failure = params.makeOutcome();
+ OperationOutcome failure = params.makeOutcome(null);
failure.setResult(PolicyResult.FAILURE);
tasks.clear();
tasks.add(() -> CompletableFuture.completedFuture(outcome));
OperationOutcome expectedOutcome = null;
for (int count = 0; count < results.length; ++count) {
- OperationOutcome outcome = params.makeOutcome();
+ OperationOutcome outcome = params.makeOutcome(null);
outcome.setResult(results[count]);
tasks.add(() -> CompletableFuture.completedFuture(outcome));
public void testDetmPriority() throws CoderException {
assertEquals(1, oper.detmPriority(null));
- OperationOutcome outcome = params.makeOutcome();
+ OperationOutcome outcome = params.makeOutcome(null);
Map<PolicyResult, Integer> map = Map.of(PolicyResult.SUCCESS, 0, PolicyResult.FAILURE_GUARD, 2,
PolicyResult.FAILURE_RETRIES, 3, PolicyResult.FAILURE, 4, PolicyResult.FAILURE_TIMEOUT, 5,
assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+ oper.setOutcome(outcome, PolicyResult.SUCCESS);
+ assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
+ assertEquals(PolicyResult.SUCCESS, outcome.getResult());
+
for (PolicyResult result : FAILURE_RESULTS) {
outcome = new OperationOutcome();
oper.setOutcome(outcome, result);
@Test
public void testGetRetryWait() {
// need an operator that doesn't override the retry time
- OperationPartial oper2 = new OperationPartial(params, config) {};
+ OperationPartial oper2 = new OperationPartial(params, config, Collections.emptyList()) {};
assertEquals(OperationPartial.DEFAULT_RETRY_WAIT_MS, oper2.getRetryWaitMs());
}
+ @Test
+ public void testGetTargetEntity() {
+ // get it from the params
+ assertEquals(MY_TARGET_ENTITY, oper.getTargetEntity());
+
+ // now get it from the properties
+ oper.setProperty(OperationProperties.AAI_TARGET_ENTITY, "entityX");
+ assertEquals("entityX", oper.getTargetEntity());
+ }
+
@Test
public void testGetTimeOutMs() {
assertEquals(TIMEOUT * 1000, oper.getTimeoutMs(params.getTimeoutSec()));
}
private OperationOutcome makeSuccess() {
- OperationOutcome outcome = params.makeOutcome();
+ OperationOutcome outcome = params.makeOutcome(null);
outcome.setResult(PolicyResult.SUCCESS);
return outcome;
}
private OperationOutcome makeFailure() {
- OperationOutcome outcome = params.makeOutcome();
+ OperationOutcome outcome = params.makeOutcome(null);
outcome.setResult(PolicyResult.FAILURE);
return outcome;
private void verifyRun(String testName, int expectedCallbacks, int expectedOperations,
PolicyResult expectedResult) {
- String expectedSubRequestId =
- (expectedResult == PolicyResult.FAILURE_EXCEPTION ? null : String.valueOf(expectedOperations));
-
- verifyRun(testName, expectedCallbacks, expectedOperations, expectedResult, expectedSubRequestId, noop());
+ verifyRun(testName, expectedCallbacks, expectedOperations, expectedResult, noop());
}
/**
* @param expectedCallbacks number of callbacks expected
* @param expectedOperations number of operation invocations expected
* @param expectedResult expected outcome
- * @param expectedSubRequestId expected sub request ID
* @param manipulator function to modify the future returned by
* {@link OperationPartial#start(ControlLoopOperationParams)} before the tasks
* in the executor are run
*/
private void verifyRun(String testName, int expectedCallbacks, int expectedOperations, PolicyResult expectedResult,
- String expectedSubRequestId, Consumer<CompletableFuture<OperationOutcome>> manipulator) {
+ Consumer<CompletableFuture<OperationOutcome>> manipulator) {
tstart = null;
opstart = null;
}
if (expectedOperations > 0) {
- assertEquals(testName, expectedSubRequestId, opend.getSubRequestId());
+ assertNotNull(testName, oper.getSubRequestId());
+ assertEquals(testName + " op start", oper.getSubRequestId(), opstart.getSubRequestId());
+ assertEquals(testName + " op end", oper.getSubRequestId(), opend.getSubRequestId());
}
}
private void setOperCoderException() {
oper = new MyOper() {
@Override
- protected Coder makeCoder() {
+ protected Coder getCoder() {
return new StandardCoder() {
@Override
public String encode(Object object, boolean pretty) throws CoderException {
public MyOper() {
- super(OperationPartialTest.this.params, config);
+ super(OperationPartialTest.this.params, config, PROP_NAMES);
}
@Override