replace test sleep() with awaitality package 21/102721/6
authorHenry.Sun <henry.a.sun@est.tech>
Mon, 2 Mar 2020 07:47:09 +0000 (15:47 +0800)
committerHenry.Sun <henry.a.sun@est.tech>
Wed, 4 Mar 2020 04:02:42 +0000 (12:02 +0800)
Signed-off-by: Henry.Sun <henry.a.sun@est.tech>
Change-Id: I305771ddef42bd3032ad52f4c5ecd55b01ed5a1a
Issue-ID: POLICY-1914
Signed-off-by: Henry.Sun <henry.a.sun@est.tech>
23 files changed:
client/client-deployment/src/test/java/org/onap/policy/apex/client/deployment/rest/DeploymentRestMainTest.java
client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/ApexEditorStartupTest.java
client/client-editor/src/test/java/org/onap/policy/apex/client/editor/rest/RestInterfaceTest.java
client/client-full/src/test/java/org/onap/policy/apex/client/full/rest/ServicesRestMainTest.java
client/client-monitoring/src/test/java/org/onap/policy/apex/client/monitoring/rest/MonitoringRestMainTest.java
core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DeploymentClientTest.java
core/core-deployment/src/test/java/org/onap/policy/apex/core/deployment/DummyDeploymentClient.java
core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/ApexEngineImplTest.java
core/core-engine/src/test/java/org/onap/policy/apex/core/engine/engine/impl/DummySlowEnEventListener.java
core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/EndToEndStringMessagingTest.java
core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/StringTestServer.java
core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTest.java
core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/threading/ThreadingTestThread.java
examples/examples-aadm/src/test/java/org/onap/policy/apex/examples/aadm/TestApexActionListener.java
examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionTslUseCaseTest.java
examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AutoLearnTslUseCaseTest.java
examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/TestApexActionListener.java
examples/examples-myfirstpolicy/src/test/java/org/onap/policy/apex/examples/myfirstpolicy/TestSaleAuthListener.java
plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java
plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java
plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java
pom.xml
services/services-engine/src/test/java/org/onap/policy/apex/service/engine/main/ApexMainTest.java

index 12fe8e5..245900d 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,14 +23,16 @@ package org.onap.policy.apex.client.deployment.rest;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.catchThrowable;
+import static org.awaitility.Awaitility.await;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.util.concurrent.TimeUnit;
+
 import org.junit.After;
 import org.junit.Test;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 
 /**
  * Test the periodic event manager utility.
@@ -193,10 +195,10 @@ public class DeploymentRestMainTest {
 
         assertThatCode(() -> {
             monThread.start();
-            ThreadUtilities.sleep(2000);
+            await().atMost(2, TimeUnit.SECONDS).until(
+                    () -> monRestMain.getState().equals(ApexDeploymentRestMain.ServicesState.RUNNING));
             monRestMain.shutdown();
         }).doesNotThrowAnyException();
-
     }
 
     @After
index 54e40ae..ac1c605 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.client.editor.rest;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.PrintStream;
+import java.util.concurrent.TimeUnit;
 
 import org.junit.Test;
 import org.onap.policy.apex.client.editor.rest.ApexEditorMain.EditorState;
@@ -444,11 +447,8 @@ public class ApexEditorStartupTest {
             }
         };
         new Thread(testThread).start();
-        while (editorMain.getState().equals(EditorState.READY)
-                || editorMain.getState().equals(EditorState.INITIALIZING)) {
-            Thread.sleep(100);
-        }
-
+        await().atMost(15000, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
+                || editorMain.getState().equals(EditorState.INITIALIZING)));
         editorMain.shutdown();
         final String outString = outBaStream.toString();
         System.out.println(outString);
index 260a898..c95a04a 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +21,7 @@
 
 package org.onap.policy.apex.client.editor.rest;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -27,6 +29,7 @@ import static org.junit.Assert.assertTrue;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.concurrent.TimeUnit;
 
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
@@ -87,16 +90,11 @@ public class RestInterfaceTest {
         };
         new Thread(testThread).start();
         // wait until editorMain is in state RUNNING
-        final long startwait = System.currentTimeMillis();
-        while (editorMain.getState().equals(EditorState.STOPPED) || editorMain.getState().equals(EditorState.READY)
-                || editorMain.getState().equals(EditorState.INITIALIZING)) {
-            if (editorMain.getState().equals(EditorState.STOPPED)) {
-                Assert.fail("Rest endpoint (" + editorMain + ") shut down before it could be used");
-            }
-            if (System.currentTimeMillis() - startwait > MAX_WAIT) {
-                Assert.fail("Rest endpoint (" + editorMain + ") for test failed to start fast enough");
-            }
-            Thread.sleep(100);
+        await().atMost(MAX_WAIT, TimeUnit.MILLISECONDS).until(() -> !(editorMain.getState().equals(EditorState.READY)
+                || editorMain.getState().equals(EditorState.INITIALIZING)));
+
+        if (editorMain.getState().equals(EditorState.STOPPED)) {
+            Assert.fail("Rest endpoint (" + editorMain + ") shut down before it could be used");
         }
 
         // create the client
@@ -124,13 +122,7 @@ public class RestInterfaceTest {
     public static void cleanUpStreams() throws IOException, InterruptedException {
         editorMain.shutdown();
         // wait until editorMain is in state STOPPED
-        final long startwait = System.currentTimeMillis();
-        while (!editorMain.getState().equals(EditorState.STOPPED)) {
-            if (System.currentTimeMillis() - startwait > MAX_WAIT) {
-                Assert.fail("Rest endpoint (" + editorMain + ") for test failed to shutdown fast enough");
-            }
-            Thread.sleep(50);
-        }
+        await().atMost(MAX_WAIT, TimeUnit.MILLISECONDS).until(() -> editorMain.getState().equals(EditorState.STOPPED));
         System.setIn(SYSIN);
     }
 
index 1c5c440..3e21ee5 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,14 +23,16 @@ package org.onap.policy.apex.client.full.rest;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.assertj.core.api.Assertions.catchThrowable;
+import static org.awaitility.Awaitility.await;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.util.concurrent.TimeUnit;
+
 import org.junit.After;
 import org.junit.Test;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 
 /**
  * Test the periodic event manager utility.
@@ -189,7 +191,8 @@ public class ServicesRestMainTest {
 
         assertThatCode(() -> {
             monThread.start();
-            ThreadUtilities.sleep(2000);
+            await().atMost(6, TimeUnit.SECONDS).until(
+                    () -> monRestMain.getState().equals(ApexServicesRestMain.EditorState.RUNNING));
             monRestMain.shutdown();
         }).doesNotThrowAnyException();
     }
index 0c929f5..71ca21d 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +21,7 @@
 
 package org.onap.policy.apex.client.monitoring.rest;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
@@ -27,9 +29,9 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.util.concurrent.TimeUnit;
 
 import org.junit.Test;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 
 /**
  * Test the periodic event manager utility.
@@ -231,7 +233,8 @@ public class MonitoringRestMainTest {
 
         try {
             monThread.start();
-            ThreadUtilities.sleep(2000);
+            await().atMost(6, TimeUnit.SECONDS).until(
+                    () -> monRestMain.getState().equals(ApexMonitoringRestMain.ServicesState.RUNNING));
             monRestMain.shutdown();
         } catch (Exception ex) {
             fail("test should not throw an exception");
index d5b34a0..f51b233 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +21,7 @@
 
 package org.onap.policy.apex.core.deployment;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
@@ -29,6 +31,7 @@ import static org.mockito.Matchers.anyObject;
 import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -41,7 +44,6 @@ import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.onap.policy.apex.core.protocols.Message;
 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus;
 import org.onap.policy.apex.core.protocols.engdep.messages.Response;
@@ -81,7 +83,7 @@ public class DeploymentClientTest {
         Thread clientThread = new Thread(deploymentClient);
         clientThread.start();
 
-        ThreadUtilities.sleep(100);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> deploymentClient.isStarted());
 
         assertTrue(deploymentClient.isStarted());
         assertTrue(clientThread.isAlive());
@@ -90,7 +92,6 @@ public class DeploymentClientTest {
         GetEngineStatus getEngineStatus = new GetEngineStatus(engineKey);
         deploymentClient.sendMessage(new GetEngineStatus(engineKey));
 
-        ThreadUtilities.sleep(20);
         Response response = new Response(engineKey, true, getEngineStatus);
         List<Message> messageList = new ArrayList<>();
         messageList.add(response);
@@ -105,8 +106,7 @@ public class DeploymentClientTest {
             assertEquals("String mesages are not supported on the EngDep protocol", use.getMessage());
         }
 
-        ThreadUtilities.sleep(300);
-        assertEquals(1, deploymentClient.getMessagesSent());
+        await().atMost(300, TimeUnit.MILLISECONDS).until(() -> deploymentClient.getMessagesReceived() == 2);
         assertEquals(2, deploymentClient.getMessagesReceived());
         
         deploymentClient.stopClient();
@@ -128,14 +128,12 @@ public class DeploymentClientTest {
         Thread clientThread = new Thread(deploymentClient);
         clientThread.start();
 
-        ThreadUtilities.sleep(50);
+        await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !deploymentClient.isStarted());
 
         assertFalse(deploymentClient.isStarted());
         assertFalse(clientThread.isAlive());
         assertEquals(0, deploymentClient.getReceiveQueue().size());
 
-        ThreadUtilities.sleep(100);
-        
         deploymentClient.stopClient();
     }
 }
index 5bbe181..965013a 100644 (file)
@@ -26,8 +26,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
 
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.onap.policy.apex.core.protocols.Message;
 import org.onap.policy.apex.core.protocols.engdep.messages.EngineServiceInfoResponse;
 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo;
@@ -42,6 +42,8 @@ import org.onap.policy.apex.core.protocols.engdep.messages.UpdateModel;
 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
 import org.onap.policy.common.utils.resources.TextFileUtils;
 
+import static org.awaitility.Awaitility.await;
+
 /**
  * Dummy deployment client.
  */
@@ -81,10 +83,7 @@ public class DummyDeploymentClient extends DeploymentClient implements Runnable
         started = true;
 
         // Loop forever, sending messages as they appear on the queue
-        while (started && !thisThread.isInterrupted()) {
-            ThreadUtilities.sleep(50);
-        }
-
+        await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !(started && !thisThread.isInterrupted()));
         // Thread has been interrupted
         thisThread = null;
         started = false;
index 2663dfc..f91b58b 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
 package org.onap.policy.apex.core.engine.engine.impl;
 
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
@@ -32,6 +33,8 @@ import static org.junit.Assert.fail;
 import java.io.IOException;
 import java.lang.reflect.Field;
 import java.util.HashMap;
+import java.util.concurrent.TimeUnit;
+
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -312,8 +315,7 @@ public class ApexEngineImplTest {
                 assertEquals(AxEngineState.STOPPED, engine.getState());
             }
         }).start();
-
-        Thread.sleep(50);
+        await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> engine.getState().equals(AxEngineState.EXECUTING));
         assertEquals(AxEngineState.EXECUTING, engine.getState());
 
         assertFalse(engine.handleEvent(event));
@@ -343,7 +345,7 @@ public class ApexEngineImplTest {
             }
         }).start();
 
-        Thread.sleep(50);
+        await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> engine.getState().equals(AxEngineState.EXECUTING));
         assertEquals(AxEngineState.EXECUTING, engine.getState());
         try {
             engine.stop();
index 9b6fc39..178e16a 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -41,7 +42,7 @@ public class DummySlowEnEventListener implements EnEventListener {
             Thread.sleep(waitTime);
         }
         catch (InterruptedException ie) {
-            // Do nothing
+            //Do nothing
         }
     }
 
index c9d56ef..d9691f1 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +21,7 @@
 
 package org.onap.policy.apex.core.infrastructure.messaging;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
@@ -27,10 +29,11 @@ import org.junit.Test;
 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageClient;
 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageListener;
 import org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WsStringMessageServer;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.slf4j.ext.XLogger;
 import org.slf4j.ext.XLoggerFactory;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * The Class EndToEndMessagingTest.
  *
@@ -59,9 +62,8 @@ public class EndToEndStringMessagingTest {
 
             client.sendString("Hello, client here");
 
-            while (!finished) {
-                ThreadUtilities.sleep(50);
-            }
+            await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> finished);
+
         } finally {
             if (client != null) {
                 client.stop();
index 1471bf3..9cf99fe 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -47,9 +48,8 @@ public class StringTestServer {
 
         System.out.println("StringTestServer started on port " + port + " for " + timeToLive + " seconds");
 
-        for (; timeToLive > 0; timeToLive--) {
-            ThreadUtilities.sleep(1000);
-        }
+        // convert to milliSeconds
+        ThreadUtilities.sleep(1000 * timeToLive);
 
         server.stop();
         System.out.println("StringTestServer completed");
index 055a76f..23f458a 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.core.infrastructure.threading;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import org.junit.Test;
 import org.slf4j.ext.XLogger;
@@ -77,10 +80,11 @@ public class ThreadingTest {
             final Thread thread = threadFactory.newThread(runnable);
             thread.start();
 
-        }
+            if (i == 4) {
+                await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> thread.isAlive());
+            }
 
-        // Threads should need a little more than 300ms to count to 3
-        ThreadUtilities.sleep(380);
+        }
 
         for (int i = 0; i < 5; i++) {
             threadList.get(i).interrupt();
index b504780..a2c5153 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,6 +24,8 @@ package org.onap.policy.apex.core.infrastructure.threading;
 import org.slf4j.ext.XLogger;
 import org.slf4j.ext.XLoggerFactory;
 
+import java.util.concurrent.CountDownLatch;
+
 /**
  * The Class ThreadingTestThread.
  *
@@ -39,7 +42,9 @@ public class ThreadingTestThread implements Runnable {
 
     private String threadName;
 
-    /**
+    private CountDownLatch latch = new CountDownLatch(1);
+
+   /**
      * {@inheritDoc}.
      */
     @Override
@@ -54,11 +59,11 @@ public class ThreadingTestThread implements Runnable {
             if (logger.isDebugEnabled()) {
                 logger.debug("in threading test thread \"" + threadName + "\", counter=" + counter + " . . .");
             }
-
-            if (!ThreadUtilities.sleep(50)) {
+            if(!ThreadUtilities.sleep(50)) {
                 interrupted = true;
             }
         }
+
         if (logger.isDebugEnabled()) {
             logger.debug("stopped threading test thread \"" + threadName + "\"");
         }
index 03372d7..bca901e 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,10 +23,11 @@ package org.onap.policy.apex.examples.aadm;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import org.onap.policy.apex.core.engine.engine.EnEventListener;
 import org.onap.policy.apex.core.engine.event.EnEvent;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
+import static org.awaitility.Awaitility.await;
 
 /**
  * The listener interface for receiving testApexAction events. The class that is interested in processing a
@@ -55,9 +57,7 @@ public class TestApexActionListener implements EnEventListener {
      * @return the result
      */
     public EnEvent getResult() {
-        while (resultEvents.isEmpty()) {
-            ThreadUtilities.sleep(100);
-        }
+        await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> !resultEvents.isEmpty());
         return resultEvents.remove(0);
     }
 
@@ -66,12 +66,9 @@ public class TestApexActionListener implements EnEventListener {
      */
     @Override
     public void onEnEvent(final EnEvent actionEvent) {
-        ThreadUtilities.sleep(100);
-
-        if (actionEvent != null) {
-            System.out.println("Action event from engine:" + actionEvent.getName());
-            resultEvents.add(actionEvent);
-        }
+        await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> actionEvent != null);
+        System.out.println("Action event from engine:" + actionEvent.getName());
+        resultEvents.add(actionEvent);
     }
 
     /**
index 7ada6a7..d1b2aaa 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.examples.adaptive;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 import java.util.Random;
+import java.util.concurrent.TimeUnit;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -38,11 +41,11 @@ import org.onap.policy.apex.core.engine.EngineParameters;
 import org.onap.policy.apex.core.engine.engine.ApexEngine;
 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
 import org.onap.policy.apex.core.engine.event.EnEvent;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory;
 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
+import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
 import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters;
 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
@@ -149,7 +152,7 @@ public class AnomalyDetectionTslUseCaseTest {
         assertEquals("ExecutionIDs are different", triggerEvent.getExecutionId(), result.getExecutionId());
         triggerEvent.clear();
         result.clear();
-        ThreadUtilities.sleep(1);
+        await().atLeast(1, TimeUnit.MILLISECONDS).until(() -> result.isEmpty());
         apexEngine1.stop();
     }
 
@@ -216,7 +219,7 @@ public class AnomalyDetectionTslUseCaseTest {
             result.clear();
         }
         apexEngine1.stop();
-        ThreadUtilities.sleep(1000);
+        await().atLeast(1000, TimeUnit.MILLISECONDS).until(() -> apexEngine1.getState().equals(AxEngineState.STOPPED));
     }
 
     /**
index d40dcc6..ab0bf30 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.examples.adaptive;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 import java.util.Random;
+import java.util.concurrent.TimeUnit;
 
 import org.junit.After;
 import org.junit.Before;
@@ -39,11 +41,11 @@ import org.onap.policy.apex.core.engine.EngineParameters;
 import org.onap.policy.apex.core.engine.engine.ApexEngine;
 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
 import org.onap.policy.apex.core.engine.event.EnEvent;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.onap.policy.apex.examples.adaptive.model.AdaptiveDomainModelFactory;
 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
+import org.onap.policy.apex.model.enginemodel.concepts.AxEngineState;
 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
 import org.onap.policy.apex.plugins.executor.java.JavaExecutorParameters;
 import org.onap.policy.apex.plugins.executor.mvel.MvelExecutorParameters;
@@ -148,7 +150,7 @@ public class AutoLearnTslUseCaseTest {
         assertEquals("ExecutionIDs are different", triggerEvent.getExecutionId(), result.getExecutionId());
         triggerEvent.clear();
         result.clear();
-        ThreadUtilities.sleep(10);
+        await().atLeast(10, TimeUnit.MILLISECONDS).until(() -> triggerEvent.isEmpty() && result.isEmpty());
         apexEngine1.stop();
     }
 
@@ -236,11 +238,11 @@ public class AutoLearnTslUseCaseTest {
             LOGGER.info("Iteration " + iteration + ": \tpreval\t" + prevval + "\tval\t" + val + "\tavval\t" + avval);
 
             result.clear();
-            ThreadUtilities.sleep(10);
+            await().atLeast(10, TimeUnit.MILLISECONDS).until(() -> !result.isEmpty());
         }
 
         apexEngine1.stop();
-        ThreadUtilities.sleep(1000);
+        await().atMost(1000, TimeUnit.MILLISECONDS).until(() -> apexEngine1.getState().equals(AxEngineState.STOPPED));
     }
 
     /**
index cd13271..2892472 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,10 +23,12 @@ package org.onap.policy.apex.examples.adaptive;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import org.onap.policy.apex.core.engine.engine.EnEventListener;
 import org.onap.policy.apex.core.engine.event.EnEvent;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
+
+import static org.awaitility.Awaitility.await;
 
 /**
  * The listener interface for receiving testApexAction events. The class that is interested in processing a
@@ -55,9 +58,7 @@ public class TestApexActionListener implements EnEventListener {
      * @return the result
      */
     public EnEvent getResult() {
-        while (resultEvents.isEmpty()) {
-            ThreadUtilities.sleep(100);
-        }
+        await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> !resultEvents.isEmpty());
         return resultEvents.remove(0);
     }
 
@@ -66,8 +67,6 @@ public class TestApexActionListener implements EnEventListener {
      */
     @Override
     public void onEnEvent(final EnEvent actionEvent) {
-        ThreadUtilities.sleep(100);
-
         if (actionEvent != null) {
             System.out.println("Action event from engine:" + actionEvent.getName());
             resultEvents.add(actionEvent);
index 3fce59c..0169d14 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,10 +23,12 @@ package org.onap.policy.apex.examples.myfirstpolicy;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import org.onap.policy.apex.core.engine.engine.EnEventListener;
 import org.onap.policy.apex.core.engine.event.EnEvent;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
+
+import static org.awaitility.Awaitility.await;
 
 /**
  * The listener interface for receiving SaleAuth events. The class that is interested in processing a SaleAuth event
@@ -57,9 +60,7 @@ public class TestSaleAuthListener implements EnEventListener {
      * @return the result
      */
     public EnEvent getResult() {
-        while (resultEvents.isEmpty()) {
-            ThreadUtilities.sleep(100);
-        }
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> !resultEvents.isEmpty());
         return resultEvents.remove(0);
     }
 
@@ -68,8 +69,6 @@ public class TestSaleAuthListener implements EnEventListener {
      */
     @Override
     public void onEnEvent(final EnEvent saleauthEvent) {
-        ThreadUtilities.sleep(100);
-
         if (saleauthEvent != null) {
             System.out.println("SaleAuth event from engine:" + saleauthEvent.getName());
             resultEvents.add(saleauthEvent);
index 6face9e..3341082 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
 
 package org.onap.policy.apex.plugins.event.carrier.restclient;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -28,6 +29,7 @@ import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
+import java.util.concurrent.TimeUnit;
 
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.Invocation.Builder;
@@ -39,7 +41,6 @@ import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.onap.policy.apex.service.engine.event.ApexEventException;
 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
@@ -119,7 +120,7 @@ public class ApexRestClientConusmerTest {
 
         // We have not set the URL, this test should not receive any events
         arcc.start();
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> incomingEventReceiver.getEventCount() == 0);
         arcc.stop();
         assertEquals(0, incomingEventReceiver.getEventCount());
 
@@ -127,7 +128,7 @@ public class ApexRestClientConusmerTest {
 
         // We have not set the URL, this test should not receive any events
         arcc.start();
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> incomingEventReceiver.getEventCount() == 0);
         arcc.stop();
         assertEquals(0, incomingEventReceiver.getEventCount());
     }
@@ -169,7 +170,7 @@ public class ApexRestClientConusmerTest {
 
         // We have not set the URL, this test should not receive any events
         arcc.start();
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> incomingEventReceiver.getEventCount() == 0);
         arcc.stop();
         assertEquals(0, incomingEventReceiver.getEventCount());
     }
@@ -214,14 +215,10 @@ public class ApexRestClientConusmerTest {
 
         // We have not set the URL, this test should not receive any events
         arcc.start();
-        ThreadUtilities.sleep(200);
+        await().atMost(400, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
+                        .contains("received an empty event from URL \"http://some.place.that.does.not/exist\""));
         arcc.stop();
-
         assertEquals(0, incomingEventReceiver.getEventCount());
-
-        final String outString = outContent.toString();
-
-        assertTrue(outString.contains("received an empty event from URL \"http://some.place.that.does.not/exist\""));
     }
 
     @Test
@@ -261,7 +258,8 @@ public class ApexRestClientConusmerTest {
 
         // We have not set the URL, this test should not receive any events
         arcc.start();
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
+                        .contains("received an empty event from URL \"http://some.place.that.does.not/exist\""));
         arcc.stop();
 
         assertEquals(0, incomingEventReceiver.getEventCount());
@@ -305,10 +303,9 @@ public class ApexRestClientConusmerTest {
 
         // We have not set the URL, this test should not receive any events
         arcc.start();
-        ThreadUtilities.sleep(200);
+        await().atMost(400, TimeUnit.MILLISECONDS).until(
+                () -> incomingEventReceiver.getLastEvent().equals("This is an event"));
         arcc.stop();
-
-        assertEquals("This is an event", incomingEventReceiver.getLastEvent());
     }
 
     @Test
@@ -349,7 +346,7 @@ public class ApexRestClientConusmerTest {
         try {
             // We have not set the URL, this test should not receive any events
             arcc.start();
-            ThreadUtilities.sleep(200);
+            await().atMost(200, TimeUnit.MILLISECONDS).until(() -> incomingEventReceiver.getEventCount() == 0);
             arcc.stop();
         } catch (Exception e) {
             // test invalid status code
index 46390f9..0c6067a 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 import java.util.Properties;
+import java.util.concurrent.TimeUnit;
 
 import org.junit.Test;
 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
@@ -126,7 +128,7 @@ public class ApexRestRequestorConsumerTest {
         consumer.start();
         ApexRestRequest request = new ApexRestRequest(123, null, EVENT_NAME, EVENT_BODY);
         consumer.processRestRequest(request);
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> consumer.getEventsReceived() == 0);
         consumer.stop();
         assertEquals(0, consumer.getEventsReceived());
     }
@@ -152,7 +154,7 @@ public class ApexRestRequestorConsumerTest {
         consumer.start();
         ApexRestRequest request = new ApexRestRequest(123, properties, EVENT_NAME, EVENT_BODY);
         consumer.processRestRequest(request);
-        ThreadUtilities.sleep(2000);
+        await().atMost(2000, TimeUnit.MILLISECONDS).until(() -> consumer.getEventsReceived() == 0);
         consumer.stop();
         assertEquals(0, consumer.getEventsReceived());
     }
index a650d79..55baf3c 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
- *  Modifications Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -365,8 +365,6 @@ public class RestRequestorTest {
         client.close();
 
         assertEquals(Double.valueOf(8.0), getsSoFar);
-
-        ThreadUtilities.sleep(1000);
     }
 
     /**
diff --git a/pom.xml b/pom.xml
index 4de7315..95cee17 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+            <scope>test</scope>
+        </dependency>
         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-ext</artifactId>
index 86ae99e..4b05c52 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2018 Ericsson. All rights reserved.
+ *  Modifications Copyright (C) 2020 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +21,7 @@
 
 package org.onap.policy.apex.service.engine.main;
 
+import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
@@ -28,9 +30,10 @@ import java.io.OutputStream;
 import java.io.PrintStream;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
 import org.junit.After;
 import org.junit.Test;
-import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 import org.onap.policy.apex.service.parameters.ApexParameters;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
@@ -57,11 +60,8 @@ public class ApexMainTest {
         System.setOut(new PrintStream(outContent));
 
         ApexMain.main(null);
-        ThreadUtilities.sleep(200);
-
-        final String outString = outContent.toString();
-
-        assertTrue(outString.contains("Apex configuration file was not specified as an argument"));
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
+                .contains("Apex configuration file was not specified as an argument"));
     }
 
     @Test
@@ -72,12 +72,9 @@ public class ApexMainTest {
         String[] args = { "-whee" };
 
         final ApexMain apexMain = new ApexMain(args);
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
+                .contains("invalid command line arguments specified : Unrecognized option: -whee"));
         apexMain.shutdown();
-
-        final String outString = outContent.toString();
-
-        assertTrue(outString.contains("invalid command line arguments specified : Unrecognized option: -whee"));
     }
 
     @Test
@@ -88,12 +85,9 @@ public class ApexMainTest {
         String[] args = { "-h" };
 
         final ApexMain apexMain = new ApexMain(args);
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
+                .contains("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
         apexMain.shutdown();
-
-        final String outString = outContent.toString();
-
-        assertTrue(outString.contains("usage: org.onap.policy.apex.service.engine.main.ApexMain [options...]"));
     }
 
     @Test
@@ -104,12 +98,9 @@ public class ApexMainTest {
         String[] args = { "-c", "src/test/resources/parameters/badParams.json" };
 
         final ApexMain apexMain = new ApexMain(args);
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
+                .contains("parameter group has status INVALID"));
         apexMain.shutdown();
-
-        final String outString = outContent.toString();
-
-        assertTrue(outString.contains("parameter group has status INVALID"));
     }
 
     @Test
@@ -122,12 +113,9 @@ public class ApexMainTest {
         final ApexMain apexMain = new ApexMain(args);
         assertEquals("MyApexEngine",
             apexMain.getApexParametersMap().values().iterator().next().getEngineServiceParameters().getName());
-        ThreadUtilities.sleep(200);
+        await().atMost(200, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
+                .contains("Added the action listener to the engine"));
         apexMain.shutdown();
-
-        final String outString = outContent.toString();
-
-        assertTrue(outString.contains("Added the action listener to the engine"));
     }
 
     @Test
@@ -143,13 +131,9 @@ public class ApexMainTest {
 
         assertEquals("trust-store-file", System.getProperty("javax.net.ssl.trustStore"));
         assertEquals("Pol1cy_0nap", System.getProperty("javax.net.ssl.trustStorePassword"));
-        ThreadUtilities.sleep(200);
+        await().atMost(10000, TimeUnit.MILLISECONDS).until(() -> outContent.toString()
+                .contains("Added the action listener to the engine"));
         apexMain.shutdown();
-
-        ThreadUtilities.sleep(10000);
-        final String outString = outContent.toString();
-
-        assertTrue(outString.contains("Added the action listener to the engine"));
     }
 
     @Test