2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020-2021 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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.policy.controlloop.eventmanager;
 
  23 import static org.assertj.core.api.Assertions.assertThatCode;
 
  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.assertNotSame;
 
  28 import static org.junit.Assert.assertSame;
 
  29 import static org.junit.Assert.assertTrue;
 
  30 import static org.mockito.ArgumentMatchers.any;
 
  31 import static org.mockito.Mockito.doThrow;
 
  32 import static org.mockito.Mockito.never;
 
  33 import static org.mockito.Mockito.verify;
 
  35 import java.time.Instant;
 
  36 import java.util.UUID;
 
  37 import java.util.concurrent.CompletableFuture;
 
  38 import java.util.function.Consumer;
 
  39 import org.junit.Before;
 
  40 import org.junit.Test;
 
  41 import org.junit.runner.RunWith;
 
  42 import org.mockito.Mock;
 
  43 import org.mockito.junit.MockitoJUnitRunner;
 
  44 import org.onap.policy.controlloop.ControlLoopOperation;
 
  45 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
 
  46 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
 
  47 import org.onap.policy.drools.core.lock.Lock;
 
  49 @RunWith(MockitoJUnitRunner.class)
 
  50 public class LockDataTest {
 
  52     private static final String ENTITY = "my-entity";
 
  53     private static final UUID REQ_ID = UUID.randomUUID();
 
  58     private Consumer<OperationOutcome> callback1;
 
  60     private Consumer<OperationOutcome> callback2;
 
  62     private Consumer<OperationOutcome> callback3;
 
  64     private LockData data;
 
  71         data = new LockData(ENTITY, REQ_ID);
 
  75     public void testGetFuture() {
 
  76         CompletableFuture<OperationOutcome> future = data.getFuture();
 
  77         assertNotNull(future);
 
  78         assertFalse(future.isDone());
 
  82     public void testAddUnavailableCallback() {
 
  83         data.addUnavailableCallback(callback1);
 
  84         data.addUnavailableCallback(callback2);
 
  86         data.lockAvailable(lock);
 
  87         verify(callback1, never()).accept(any());
 
  88         verify(callback2, never()).accept(any());
 
  90         data.lockUnavailable(lock);
 
  91         verify(callback1).accept(any());
 
  92         verify(callback2).accept(any());
 
  96      * Tests addUnavailableCallback() when the lock never becomes available.
 
  99     public void testAddUnavailableCallbackNeverAvailable() {
 
 100         data.addUnavailableCallback(callback1);
 
 101         data.addUnavailableCallback(callback2);
 
 103         data.lockUnavailable(lock);
 
 104         verify(callback1).accept(any());
 
 105         verify(callback2).accept(any());
 
 107         data.addUnavailableCallback(callback3);
 
 108         verify(callback3).accept(any());
 
 112     public void testFree() {
 
 114         assertThatCode(() -> data.free()).doesNotThrowAnyException();
 
 117         data.lockAvailable(lock);
 
 123     public void testLockAvailable() throws Exception {
 
 124         data.addUnavailableCallback(callback1);
 
 125         data.addUnavailableCallback(callback2);
 
 127         CompletableFuture<OperationOutcome> future = data.getFuture();
 
 128         data.lockAvailable(lock);
 
 130         assertSame(future, data.getFuture());
 
 132         assertTrue(future.isDone());
 
 133         OperationOutcome outcome = future.get();
 
 134         assertEquals(ActorConstants.LOCK_ACTOR, outcome.getActor());
 
 135         assertEquals(ActorConstants.LOCK_OPERATION, outcome.getOperation());
 
 136         assertEquals(ENTITY, outcome.getTarget());
 
 137         assertEquals(OperationResult.SUCCESS, outcome.getResult());
 
 138         assertEquals(ControlLoopOperation.SUCCESS_MSG, outcome.getMessage());
 
 140         Instant start = outcome.getStart();
 
 141         assertNotNull(start);
 
 143         Instant end = outcome.getEnd();
 
 145         assertTrue(start.compareTo(end) <= 0);
 
 147         verify(callback1, never()).accept(any());
 
 148         verify(callback2, never()).accept(any());
 
 152     public void testLockUnavailable() throws Exception {
 
 153         data.addUnavailableCallback(callback1);
 
 154         data.addUnavailableCallback(callback2);
 
 155         data.addUnavailableCallback(callback3);
 
 157         // arrange for callback2 to throw an exception
 
 158         doThrow(new IllegalStateException("expected exception")).when(callback2).accept(any());
 
 160         CompletableFuture<OperationOutcome> future = data.getFuture();
 
 161         assertNotNull(future);
 
 162         data.lockUnavailable(lock);
 
 164         CompletableFuture<OperationOutcome> future2 = data.getFuture();
 
 165         assertNotNull(future2);
 
 167         assertNotSame(future, future2);
 
 169         assertTrue(future.isDone());
 
 170         OperationOutcome outcome = future.get();
 
 172         assertTrue(future2.isDone());
 
 173         assertSame(outcome, future2.get());
 
 175         assertEquals(ActorConstants.LOCK_ACTOR, outcome.getActor());
 
 176         assertEquals(ActorConstants.LOCK_OPERATION, outcome.getOperation());
 
 177         assertEquals(ENTITY, outcome.getTarget());
 
 178         assertEquals(OperationResult.FAILURE, outcome.getResult());
 
 179         assertEquals(ControlLoopOperation.FAILED_MSG, outcome.getMessage());
 
 181         Instant start = outcome.getStart();
 
 182         assertNotNull(start);
 
 184         Instant end = outcome.getEnd();
 
 186         assertTrue(start.compareTo(end) <= 0);
 
 188         verify(callback1).accept(outcome);
 
 189         verify(callback2).accept(outcome);
 
 190         verify(callback3).accept(outcome);