Adjust for Binding RPC codegen changes
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / DatastoreTestTask.java
index 8ac93b180437a7193bbe6e466ed0f1c7567aa9f3..ed17aa55cfb852fa5724272f87f8d298f1f694cb 100644 (file)
@@ -8,10 +8,13 @@
 package org.opendaylight.controller.md.sal.dom.store.impl;
 
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.SettableFuture;
 import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Future;
-
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
@@ -20,66 +23,65 @@ import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
-import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.SettableFuture;
-
 public class DatastoreTestTask {
 
     private final DOMStore store;
-    private AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> changeListener;
+    private AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> changeListener;
 
     private WriteTransactionCustomizer setup;
     private WriteTransactionCustomizer write;
     private ReadTransactionVerifier read;
     private WriteTransactionCustomizer cleanup;
-    private InstanceIdentifier changePath;
+    private YangInstanceIdentifier changePath;
     private DataChangeScope changeScope;
-    private boolean postSetup = false;
+    private volatile boolean postSetup = false;
     private final ChangeEventListener internalListener;
+    private final TestDCLExecutorService dclExecutorService;
 
-    public DatastoreTestTask(final DOMStore datastore) {
+    public DatastoreTestTask(final DOMStore datastore, final TestDCLExecutorService dclExecutorService) {
         this.store = datastore;
+        this.dclExecutorService = dclExecutorService;
         internalListener = new ChangeEventListener();
     }
 
-    public DatastoreTestTask changeListener(final InstanceIdentifier path, final DataChangeScope scope,
-            final AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> changeListener) {
-        this.changeListener = changeListener;
+    public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope,
+            final AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> listener) {
+        this.changeListener = listener;
         this.changePath = path;
         this.changeScope = scope;
         return this;
     }
 
-    public DatastoreTestTask changeListener(final InstanceIdentifier path, final DataChangeScope scope) {
+    public DatastoreTestTask changeListener(final YangInstanceIdentifier path, final DataChangeScope scope) {
         this.changePath = path;
         this.changeScope = scope;
         return this;
     }
 
-    public DatastoreTestTask setup(final WriteTransactionCustomizer setup) {
-        this.setup = setup;
+    public DatastoreTestTask setup(final WriteTransactionCustomizer customizer) {
+        this.setup = customizer;
         return this;
     }
 
-    public DatastoreTestTask test(final WriteTransactionCustomizer write) {
-        this.write = write;
+    public DatastoreTestTask test(final WriteTransactionCustomizer customizer) {
+        this.write = customizer;
         return this;
     }
 
-    public DatastoreTestTask read(final ReadTransactionVerifier read) {
-        this.read = read;
+    public DatastoreTestTask read(final ReadTransactionVerifier customizer) {
+        this.read = customizer;
         return this;
     }
 
-    public DatastoreTestTask cleanup(final WriteTransactionCustomizer cleanup) {
-        this.cleanup = cleanup;
+    public DatastoreTestTask cleanup(final WriteTransactionCustomizer customizer) {
+        this.cleanup = customizer;
         return this;
     }
 
-    public void run() throws InterruptedException, ExecutionException {
+    public void run() throws Exception {
         if (setup != null) {
             execute(setup);
         }
@@ -89,13 +91,17 @@ public class DatastoreTestTask {
         }
 
         Preconditions.checkState(write != null, "Write Transaction must be set.");
+
         postSetup = true;
+        dclExecutorService.afterTestSetup();
+
         execute(write);
         if (registration != null) {
             registration.close();
         }
+
         if (changeListener != null) {
-            changeListener.onDataChanged(internalListener.receivedChange.get());
+            changeListener.onDataChanged(getChangeEvent());
         }
         if (read != null) {
             read.verify(store.newReadOnlyTransaction());
@@ -105,8 +111,17 @@ public class DatastoreTestTask {
         }
     }
 
-    public Future<AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>>> getChangeEvent() {
-        return internalListener.receivedChange;
+    public AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> getChangeEvent() throws Exception {
+        return internalListener.receivedChange.get(10, TimeUnit.SECONDS);
+    }
+
+    public void verifyNoChangeEvent() throws Exception {
+        try {
+            Object unexpected = internalListener.receivedChange.get(500, TimeUnit.MILLISECONDS);
+            fail("Got unexpected AsyncDataChangeEvent from the Future: " + unexpected);
+        } catch (TimeoutException e) {
+            // Expected
+        }
     }
 
     private void execute(final WriteTransactionCustomizer writeCustomizer) throws InterruptedException,
@@ -120,55 +135,38 @@ public class DatastoreTestTask {
     }
 
     public interface WriteTransactionCustomizer {
-        public void customize(DOMStoreReadWriteTransaction tx);
+        void customize(DOMStoreReadWriteTransaction tx);
     }
 
     public interface ReadTransactionVerifier {
-        public void verify(DOMStoreReadTransaction tx);
+        void verify(DOMStoreReadTransaction tx);
     }
 
     private final class ChangeEventListener implements
-            AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>> {
+            AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> {
 
-        protected final SettableFuture<AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>>> receivedChange = SettableFuture
-                .create();
+        protected final SettableFuture<AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>>>
+                receivedChange = SettableFuture.create();
 
         @Override
-        public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change) {
+        public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
             if (postSetup) {
                 receivedChange.set(change);
             }
         }
     }
 
-    public static final WriteTransactionCustomizer simpleWrite(final InstanceIdentifier path,
+    public static final WriteTransactionCustomizer simpleWrite(final YangInstanceIdentifier path,
             final NormalizedNode<?, ?> data) {
-        return new WriteTransactionCustomizer() {
-
-            @Override
-            public void customize(final DOMStoreReadWriteTransaction tx) {
-                tx.write(path, data);
-            }
-        };
+        return tx -> tx.write(path, data);
     }
 
-    public static final WriteTransactionCustomizer simpleMerge(final InstanceIdentifier path,
+    public static final WriteTransactionCustomizer simpleMerge(final YangInstanceIdentifier path,
             final NormalizedNode<?, ?> data) {
-        return new WriteTransactionCustomizer() {
-
-            @Override
-            public void customize(final DOMStoreReadWriteTransaction tx) {
-                tx.merge(path, data);
-            }
-        };
+        return tx -> tx.merge(path, data);
     }
 
-    public static final WriteTransactionCustomizer simpleDelete(final InstanceIdentifier path) {
-        return new WriteTransactionCustomizer() {
-            @Override
-            public void customize(final DOMStoreReadWriteTransaction tx) {
-                tx.delete(path);
-            }
-        };
+    public static final WriteTransactionCustomizer simpleDelete(final YangInstanceIdentifier path) {
+        return tx -> tx.delete(path);
     }
 }