stc scenario for testing work queue distributed primitive

Change-Id: Ib548cef733a7d1f6418d3c318aa41d5e2cd1f400
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/WorkQueueTestCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/WorkQueueTestCommand.java
new file mode 100644
index 0000000..62aefc5
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/WorkQueueTestCommand.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.distributedprimitives.cli;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Task;
+import org.onosproject.store.service.WorkQueue;
+import org.onosproject.store.service.WorkQueueStats;
+
+import com.google.common.base.Throwables;
+
+/**
+ * CLI command to test a distributed work queue.
+ */
+@Command(scope = "onos", name = "work-queue-test",
+        description = "Test a distributed work queue")
+public class WorkQueueTestCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "name",
+            description = "Work Queue name",
+            required = true, multiValued = false)
+    String name = null;
+
+    @Argument(index = 1, name = "operation",
+            description = "operation name. One of {add, addMutiple, "
+                    + "takeAndComplete, totalPending, totalInProgress, totalCompleted}",
+            required = true, multiValued = false)
+    String operation = null;
+
+    @Argument(index = 2, name = "value1",
+            description = "first arg",
+            required = false, multiValued = false)
+    String value1 = null;
+
+    @Argument(index = 3, name = "value2",
+            description = "second arg",
+            required = false, multiValued = false)
+    String value2 = null;
+
+    WorkQueue<String> queue;
+
+    @Override
+    protected void execute() {
+        StorageService storageService = get(StorageService.class);
+        Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
+        queue = storageService.getWorkQueue(name, serializer);
+        if (operation.equals("add")) {
+            if (value1 == null) {
+                print("Usage: add <value1>");
+            } else {
+                get(queue.addOne(value1));
+                print("Done");
+            }
+        } else if (operation.equals("addMultiple")) {
+            if (value1 == null || value2 == null) {
+                print("Usage: addMultiple <value1> <value2>");
+            } else {
+                get(queue.addMultiple(Arrays.asList(value1, value2)));
+                print("Done");
+            }
+        } else if (operation.equals("takeAndComplete")) {
+            int maxItems = value1 != null ? Integer.parseInt(value1) : 1;
+            Collection<Task<String>> tasks = get(queue.take(maxItems));
+            tasks.forEach(task -> get(queue.complete(task.taskId())));
+            print("Done");
+        } else if (operation.equals("totalPending")) {
+            WorkQueueStats stats = get(queue.stats());
+            print("%d", stats.totalPending());
+        } else if (operation.equals("totalInProgress")) {
+            WorkQueueStats stats = get(queue.stats());
+            print("%d", stats.totalInProgress());
+        } else if (operation.equals("totalCompleted")) {
+            WorkQueueStats stats = get(queue.stats());
+            print("%d", stats.totalCompleted());
+        } else {
+            print("Invalid operation name. Valid operations names are:"
+                    + " [add, addMultiple takeAndComplete, totalPending, totalInProgress, totalCompleted]");
+        }
+    }
+
+    private <T> T get(CompletableFuture<T> future) {
+        try {
+            return future.get(1, TimeUnit.SECONDS);
+        } catch (Exception e) {
+            throw Throwables.propagate(e);
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 9fa828f..310e8da 100644
--- a/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -38,6 +38,9 @@
             <action class="org.onosproject.distributedprimitives.cli.CounterTestCommand"/>
         </command>
         <command>
+            <action class="org.onosproject.distributedprimitives.cli.WorkQueueTestCommand"/>
+        </command>
+        <command>
             <action class="org.onosproject.distributedprimitives.cli.ConsistentMapTestCommand"/>
         </command>
         <command>
diff --git a/tools/test/scenarios/dist-work-queue.xml b/tools/test/scenarios/dist-work-queue.xml
new file mode 100644
index 0000000..ce3ed36
--- /dev/null
+++ b/tools/test/scenarios/dist-work-queue.xml
@@ -0,0 +1,55 @@
+<!--
+  ~ Copyright 2016-present Open Networking Laboratory
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+-->
+
+<scenario name="distributed-work-queue-test"
+          description="ONOS WorkQueue distributed primitive Test">
+    <group name="Distributed-Primitive-WorkQueue">
+
+        <step name="Distributed-Primitive-WorkQueue.Activate-Distributed-Primitives-App"
+            exec="onos ${OCI} app activate org.onosproject.distributedprimitives"/>
+
+        <step name="Distributed-Primitive-WorkQueue.Test-Queue-AddOne" requires="^"
+              exec="onos-execute-expect ${OCI} work-queue-test stc-test-work-queue add foo --expect Done"/>
+
+        <step name="Distributed-Primitive-WorkQueue.Test-Queue-Check-Pending-1" requires="^"
+              exec="onos-cluster-execute-expect work-queue-test stc-test-work-queue totalPending --expect 1"/>
+
+        <step name="Distributed-Primitive-WorkQueue.Test-Queue-Check-InProgress-1" requires="^"
+              exec="onos-cluster-execute-expect work-queue-test stc-test-work-queue totalInProgress --expect 0"/>
+
+        <step name="Distributed-Primitive-WorkQueue.Test-Queue-AddMultiple" requires="^"
+              exec="onos-execute-expect ${OCI} work-queue-test stc-test-work-queue addMultiple bar car --expect Done"/>
+
+        <step name="Distributed-Primitive-WorkQueue.Test-Queue-TakeAndComplete" requires="^"
+              exec="onos-execute-expect ${OCI} work-queue-test stc-test-work-queue takeAndComplete 3 --expect Done"/>
+
+        <step name="Distributed-Primitive-WorkQueue.Test-Queue-Check-Pending-2" requires="^"
+              exec="onos-cluster-execute-expect work-queue-test stc-test-work-queue totalPending --expect 0"/>
+
+        <step name="Distributed-Primitive-WorkQueue.Test-Queue-Check-InProgress-2" requires="^"
+              exec="onos-cluster-execute-expect work-queue-test stc-test-work-queue totalInProgress --expect 0"/>
+
+        <!-- Since totalCompleted is a additive quantity, testing its value breaks when the test is run in a loop -->
+
+        <!--Check with check logs-->
+        <step name="Distributed-Primitive-WorkQueue.Check-Log-Exceptions" requires="^"
+              exec="onos-check-logs ${OCI}"/>
+
+        <step name="Distributed-Primitive-WorkQueue.Teardown-Distributed-Primitives-Test-App" requires="^"
+              exec="onos ${OCI} app deactivate org.onosproject.distributedprimitives"/>
+    </group>
+</scenario>
+