Stc scenario for testing distributed primitives and onos-execute-expect command

Change-Id: Iac949099bf072b71aedd85dbc2ee8cf613b01807
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java
index 204471c..2d1aa0b 100644
--- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java
@@ -23,7 +23,7 @@
 import org.onosproject.store.service.Serializer;
 import org.onosproject.store.service.StorageService;
 
-import java.util.HashSet;
+import java.util.Arrays;
 import java.util.Set;
 
 /**
@@ -44,7 +44,6 @@
     String[] values = null;
 
     Set<String> set;
-    Set<String> toAdd = new HashSet<>();
 
 
     Serializer serializer = Serializer.using(
@@ -68,13 +67,10 @@
             }
         } else if (values.length >= 1) {
             // Add multiple elements to a set
-            for (String value : values) {
-                toAdd.add(value);
-            }
-            if (set.addAll(toAdd)) {
-                print("%s was added to the set %s", toAdd, setName);
+            if (set.addAll(Arrays.asList(values))) {
+                print("%s was added to the set %s", Arrays.asList(values), setName);
             } else {
-                print("%s was already in set %s", toAdd, setName);
+                print("%s was already in set %s", Arrays.asList(values), setName);
             }
         }
     }
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java
index fb36a06..74c52c1 100644
--- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java
@@ -24,7 +24,7 @@
 import org.onosproject.store.service.Serializer;
 import org.onosproject.store.service.StorageService;
 
-import java.util.HashSet;
+import java.util.Arrays;
 import java.util.Set;
 
 /**
@@ -49,7 +49,6 @@
     String[] values = null;
 
     Set<String> set;
-    Set<String> toCheck = new HashSet<>();
     String output = "";
 
     Serializer serializer = Serializer.using(
@@ -95,13 +94,10 @@
             }
         } else if (values.length > 1) {
             //containsAll
-            for (String value : values) {
-                toCheck.add(value);
-            }
-            if (set.containsAll(toCheck)) {
-                print("Set %s contains the the subset %s", setName, toCheck);
+            if (set.containsAll(Arrays.asList(values))) {
+                print("Set %s contains the the subset %s", setName, Arrays.asList(values));
             } else {
-                print("Set %s did not contain the the subset %s", setName, toCheck);
+                print("Set %s did not contain the the subset %s", setName, Arrays.asList(values));
             }
         }
     }
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java
index d1f81e4..1fa073f3 100644
--- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java
@@ -24,7 +24,7 @@
 import org.onosproject.store.service.Serializer;
 import org.onosproject.store.service.StorageService;
 
-import java.util.HashSet;
+import java.util.Arrays;
 import java.util.Set;
 
 /**
@@ -54,7 +54,6 @@
     String[] values = null;
 
     Set<String> set;
-    Set<String> givenValues = new HashSet<>();
     Serializer serializer = Serializer.using(
             new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
 
@@ -79,13 +78,10 @@
         }
 
         if (retain) { // Keep only the given values
-            for (String value : values) {
-                givenValues.add(value);
-            }
-            if (set.retainAll(givenValues)) {
-                print("%s was pruned to contain only elements of set %s", setName, givenValues);
+            if (set.retainAll(Arrays.asList(values))) {
+                print("%s was pruned to contain only elements of set %s", setName, Arrays.asList(values));
             } else {
-                print("%s was not changed by retaining only elements of the set %s", setName, givenValues);
+                print("%s was not changed by retaining only elements of the set %s", setName, Arrays.asList(values));
             }
         } else if (values.length == 1) {
             // Remove a single element from the set
@@ -94,15 +90,12 @@
             } else {
                 print("[%s] was not in set %s", values[0], setName);
             }
-        } else if (values.length >= 1) {
+        } else if (values.length > 1) {
             // Remove multiple elements from a set
-            for (String value : values) {
-                givenValues.add(value);
-            }
-            if (set.removeAll(givenValues)) {
-                print("%s was removed from the set %s", givenValues, setName);
+            if (set.removeAll(Arrays.asList(values))) {
+                print("%s was removed from the set %s", Arrays.asList(values), setName);
             } else {
-                print("No element of %s was in set %s", givenValues, setName);
+                print("No element of %s was in set %s", Arrays.asList(values), setName);
             }
         }
     }
diff --git a/tools/test/bin/onos-execute-expect b/tools/test/bin/onos-execute-expect
new file mode 100755
index 0000000..6ad9569
--- /dev/null
+++ b/tools/test/bin/onos-execute-expect
@@ -0,0 +1,27 @@
+#!/bin/bash
+# -----------------------------------------------------------------------------
+# Executes a command on the given ONOS instance and matches the output
+# to the passed one.
+# First argument is the IP address of the machine to run the command on,
+# then you pass the command and it's arguments if needed, then --expect and
+# after it the string of what the output should be.
+# Example:
+# onos-execute-expect 1.1.1.1 fooCommand fooParamenter --expect fooOutputString
+# -----------------------------------------------------------------------------
+
+[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
+. $ONOS_ROOT/tools/build/envDefaults
+
+
+aux=/tmp/stc-$$.log
+trap "rm -f $aux 2>/dev/null" EXIT
+ip=$1
+cmd=""
+for a in ${*:2}; do shift; if [ "$a" = "--expect" ]; then break; fi; cmd="$cmd $a"; done
+expect="${@: -1}"
+onos $ip $cmd > $aux
+cat $aux
+grep -q $expect $aux && echo "expected value found" && exit 0
+exit 1
+
+
diff --git a/tools/test/scenarios/dist-test-seq.xml b/tools/test/scenarios/dist-test-seq.xml
new file mode 100644
index 0000000..2e99fa2
--- /dev/null
+++ b/tools/test/scenarios/dist-test-seq.xml
@@ -0,0 +1,67 @@
+<!--
+  ~ Copyright 20${OCI}5 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="dist-test"
+          description="ONOS distributed primitives setup">
+    <group name="Distributed-Primitives">
+        <!--<import file="${ONOS_SCENARIOS}/prerequisites.xml"/>-->
+
+        <!--<import file="${ONOS_SCENARIOS}/setup.xml"/>
+        <dependency name="Setup" requires="Prerequisites"/>-->
+
+        <sequential var="${OC#}" starts="Distributed-App-${#}" ends="Check-Distributed-Exceptions-${#-1}">
+            <step name="Distributed-App-${#}"
+                  requires="Setup"
+                  exec="onos ${OC#} app activate org.onosproject.distributedprimitives"/>
+
+            <step name="Test-Counter-Increment-${#}"
+                  requires="Distributed-App-${#}"
+                  exec="onos-execute-expect ${OC#} counter-test-increment fooCounter 5 --expect updated"/>
+
+            <step name="Test-Add-${#}"
+                  requires="Distributed-App-${#}"
+                  exec="onos-execute-expect ${OC#} set-test-add fooSet foo --expect added"/>
+
+            <step name="Test-Get-${#}"
+                  requires="Test-Add-${#}"
+                  exec="onos-execute-expect ${OC#} set-test-get fooSet foo --expect contains"/>
+
+            <step name="Test-Remove-${#}"
+                  requires="Test-Get-${#}"
+                  exec="onos-execute-expect ${OC#} set-test-remove fooSet foo --expect removed"/>
+
+            <step name="Test-Add-Multiple-${#}"
+                  requires="Test-Remove-${#}"
+                  exec="onos-execute-expect ${OC#} set-test-add fooSet foo foo2 foo3 --expect added"/>
+
+            <step name="Test-Get-Multiple-${#}"
+                  requires="Test-Add-Multiple-${#}"
+                  exec="onos-execute-expect ${OC#} set-test-get fooSet foo foo2 foo3 --expect contains"/>
+
+            <step name="Test-Remove-Multiple-${#}"
+                  requires="Test-Get-Multiple-${#}"
+                  exec="onos-execute-expect ${OC#} set-test-remove fooSet foo foo2 foo3 --expect removed"/>
+
+            <step name="Sleep-${#}"
+                  exec="sleep 2"
+                  requires="Test-Remove-Multiple-${#}"/>
+            <!--Check with check logs-->
+            <step name="Check-Distributed-Exceptions-${#}"
+                  exec="onos-check-logs ${OC#}"
+                  requires="Sleep-${#}"/>
+        </sequential>
+    </group>
+</scenario>
\ No newline at end of file
diff --git a/tools/test/scenarios/dist-test.xml b/tools/test/scenarios/dist-test.xml
new file mode 100644
index 0000000..5e7dab0
--- /dev/null
+++ b/tools/test/scenarios/dist-test.xml
@@ -0,0 +1,70 @@
+<!--
+  ~ Copyright 2015 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="dist-test"
+          description="ONOS distributed primitives setup">
+    <group name="Distributed-Primitives">
+
+        <!--<import file="${ONOS_SCENARIOS}/setup.xml"/>
+        <dependency name="Setup" requires="Prerequisites"/>-->
+
+        <step name="Distributed-App"
+              requires="Setup"
+              exec="onos ${OCI} app activate org.onosproject.distributedprimitives"/>
+
+        <step name="Test-Counter-Increment"
+              requires="Distributed-App"
+              exec="onos-execute-expect ${OCI} counter-test-increment fooCounter 5 --expect updated"/>
+
+        <step name="Test-Add"
+              requires="Distributed-App"
+              exec="onos-execute-expect ${OCI} set-test-add fooSet foo --expect added"/>
+
+        <step name="Test-Get"
+              requires="Test-Add"
+              exec="onos-execute-expect ${OCI} set-test-get fooSet foo --expect contains"/>
+
+        <step name="Test-Remove"
+              requires="Test-Get"
+              exec="onos-execute-expect ${OCI} set-test-remove fooSet foo --expect removed"/>
+
+        <step name="Test-Add-Multiple"
+              requires="Test-Remove"
+              exec="onos-execute-expect ${OCI} set-test-add fooSet foo foo2 foo3 --expect added"/>
+
+        <step name="Test-Get-Multiple"
+              requires="Test-Add-Multiple"
+              exec="onos-execute-expect ${OCI} set-test-get fooSet foo foo2 foo3 --expect contains"/>
+
+        <step name="Test-Remove-Multiple"
+              requires="Test-Get-Multiple"
+              exec="onos-execute-expect ${OCI} set-test-remove fooSet foo foo2 foo3 --expect removed"/>
+
+        <step name="Test-Map-Put"
+              requires="Distributed-App"
+              exec="onos-execute-expect ${OCI} transactional-map-test-put 1 foo --expect Created"/>
+
+        <step name="Test-Map-Get"
+              requires="Test-Map-Put"
+              exec="onos-execute-expect ${OCI} transactional-map-test-get Key1 --expect Key-value"/>
+
+        <!--Check with check logs-->
+        <step name="Check-Distributed-Exceptions"
+              exec="onos-check-logs ${OCI}"
+              requires="Test-Map-Get"/>
+    </group>
+</scenario>
+