Fix testUninstallAndInstallNonDisruptive test

Change-Id: Id5f8cd5a3b270231ccadfcde5ad4934c5ee407e0
(cherry picked from commit 9f832343ebd760246928e54c86080227a1ebfdf4)
diff --git a/core/net/src/test/java/org/onosproject/net/intent/impl/installer/FlowRuleIntentInstallerTest.java b/core/net/src/test/java/org/onosproject/net/intent/impl/installer/FlowRuleIntentInstallerTest.java
index fdef627..e1e186a 100644
--- a/core/net/src/test/java/org/onosproject/net/intent/impl/installer/FlowRuleIntentInstallerTest.java
+++ b/core/net/src/test/java/org/onosproject/net/intent/impl/installer/FlowRuleIntentInstallerTest.java
@@ -31,6 +31,7 @@
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperation;
 import org.onosproject.net.flow.FlowRuleOperations;
 import org.onosproject.net.flow.FlowRuleServiceAdapter;
 import org.onosproject.net.flow.TrafficSelector;
@@ -54,6 +55,8 @@
 import static org.easymock.EasyMock.mock;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.onosproject.net.flow.FlowRuleOperation.Type.ADD;
+import static org.onosproject.net.flow.FlowRuleOperation.Type.REMOVE;
 
 /**
  * Tests for flow rule Intent installer.
@@ -61,14 +64,14 @@
 public class FlowRuleIntentInstallerTest extends AbstractIntentInstallerTest {
 
     private TestFlowRuleService flowRuleService;
-    private TestFlowRuleServiceNonDisruptive flowRuleServiceNonDisruptive;
+    private final TestFlowRuleServiceNonDisruptive flowRuleServiceNonDisruptive =
+            new TestFlowRuleServiceNonDisruptive();
     private FlowRuleIntentInstaller installer;
 
     @Before
     public void setup() {
         super.setup();
         flowRuleService = new TestFlowRuleService();
-        flowRuleServiceNonDisruptive = new TestFlowRuleServiceNonDisruptive();
         installer = new FlowRuleIntentInstaller();
         installer.flowRuleService = flowRuleService;
         installer.store = new SimpleIntentStore();
@@ -381,6 +384,14 @@
                 .makePermanent()
                 .build();
 
+        // We need to wait a bit in order to avoid
+        // race conditions and failing builds
+        synchronized (flowRuleServiceNonDisruptive) {
+            while (!verifyFlowRule(ADD, firstStageInstalledRule)) {
+                flowRuleServiceNonDisruptive.wait();
+            }
+        }
+
         assertTrue(flowRuleServiceNonDisruptive.flowRulesAdd.contains(firstStageInstalledRule));
 
         selector = DefaultTrafficSelector.builder()
@@ -399,6 +410,12 @@
                 .makePermanent()
                 .build();
 
+        synchronized (flowRuleServiceNonDisruptive) {
+            while (!verifyFlowRule(REMOVE, secondStageUninstalledRule)) {
+                flowRuleServiceNonDisruptive.wait();
+            }
+        }
+
         assertTrue(flowRuleServiceNonDisruptive.flowRulesRemove.contains(secondStageUninstalledRule));
 
         selector = DefaultTrafficSelector.builder()
@@ -417,6 +434,12 @@
                 .makePermanent()
                 .build();
 
+        synchronized (flowRuleServiceNonDisruptive) {
+            while (!verifyFlowRule(ADD, thirdStageInstalledRule)) {
+                flowRuleServiceNonDisruptive.wait();
+            }
+        }
+
         assertTrue(flowRuleServiceNonDisruptive.flowRulesAdd.contains(thirdStageInstalledRule));
 
         selector = DefaultTrafficSelector.builder()
@@ -435,12 +458,23 @@
                 .makePermanent()
                 .build();
 
+        synchronized (flowRuleServiceNonDisruptive) {
+            while (!verifyFlowRule(REMOVE, lastStageUninstalledRule)) {
+                flowRuleServiceNonDisruptive.wait();
+            }
+        }
+
         assertTrue(flowRuleServiceNonDisruptive.flowRulesRemove.contains(lastStageUninstalledRule));
 
         IntentOperationContext successContext = intentInstallCoordinator.successContext;
         assertEquals(successContext, operationContext);
     }
 
+    private boolean verifyFlowRule(FlowRuleOperation.Type type, FlowRule flowRule) {
+        return type == ADD ? flowRuleServiceNonDisruptive.flowRulesAdd.contains(flowRule) :
+                flowRuleServiceNonDisruptive.flowRulesRemove.contains(flowRule);
+    }
+
     /**
      * Generates FlowRuleIntents for test.
      *
@@ -734,6 +768,9 @@
         public void apply(FlowRuleOperations ops) {
             record(ops);
             ops.callback().onSuccess(ops);
+            synchronized (this) {
+                this.notify();
+            }
         }
     }