You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iota.apache.org by to...@apache.org on 2016/07/21 23:37:59 UTC

[08/10] incubator-iota git commit: Adding tests for JsonReceiver trait

Adding tests for JsonReceiver trait


Project: http://git-wip-us.apache.org/repos/asf/incubator-iota/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-iota/commit/e892b5d8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-iota/tree/e892b5d8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-iota/diff/e892b5d8

Branch: refs/heads/master
Commit: e892b5d87c5c23a463607ebbb3a5b913a72ef159
Parents: 28e0b81
Author: Barbara Gomes <ba...@gmail.com>
Authored: Thu Jul 21 11:09:53 2016 -0700
Committer: Barbara Gomes <ba...@gmail.com>
Committed: Thu Jul 21 11:09:53 2016 -0700

----------------------------------------------------------------------
 .../apache/iota/fey/CheckpointProcessor.scala   |   3 +
 .../org/apache/iota/fey/JsonReceiver.scala      |   6 +-
 .../org/apache/iota/fey/JsonReceiverSpec.scala  | 100 +++++++++++++++++++
 .../org/apache/iota/fey/Utils_JSONTest.scala    |  58 +++++++++++
 4 files changed, 165 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e892b5d8/fey-core/src/main/scala/org/apache/iota/fey/CheckpointProcessor.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/main/scala/org/apache/iota/fey/CheckpointProcessor.scala b/fey-core/src/main/scala/org/apache/iota/fey/CheckpointProcessor.scala
index 987d9e6..c66be1c 100644
--- a/fey-core/src/main/scala/org/apache/iota/fey/CheckpointProcessor.scala
+++ b/fey-core/src/main/scala/org/apache/iota/fey/CheckpointProcessor.scala
@@ -78,4 +78,7 @@ class CheckpointProcessor(receiverActor: ActorRef) extends JsonReceiver{
         processJson(file.getAbsolutePath, file)
       })
   }
+
+  override def execute(): Unit = {}
+  override def exceptionOnRun(e: Exception): Unit = {}
 }

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e892b5d8/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala b/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala
index d455ed5..6781f01 100644
--- a/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala
+++ b/fey-core/src/main/scala/org/apache/iota/fey/JsonReceiver.scala
@@ -103,6 +103,8 @@ trait JsonReceiver extends Runnable{
 
             downloadJAR(url, jarName, credentials)
           }
+        }else{
+          log.debug("Location not defined in JSON")
         }
       })
     })
@@ -171,7 +173,7 @@ trait JsonReceiver extends Runnable{
   /**
     * Called inside run method
     */
-  def execute(): Unit = {}
+  def execute(): Unit
 
   /**
     * Called when occurs an exception inside Run.
@@ -179,7 +181,7 @@ trait JsonReceiver extends Runnable{
     *
     * @param e
     */
-  def exceptionOnRun(e: Exception): Unit = {}
+  def exceptionOnRun(e: Exception): Unit
 }
 
 object HttpBasicAuth {

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e892b5d8/fey-core/src/test/scala/org/apache/iota/fey/JsonReceiverSpec.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/test/scala/org/apache/iota/fey/JsonReceiverSpec.scala b/fey-core/src/test/scala/org/apache/iota/fey/JsonReceiverSpec.scala
new file mode 100644
index 0000000..5904f2b
--- /dev/null
+++ b/fey-core/src/test/scala/org/apache/iota/fey/JsonReceiverSpec.scala
@@ -0,0 +1,100 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.iota.fey
+
+import java.nio.file.{Files, Paths}
+
+import akka.actor.ActorRef
+import akka.testkit.{EventFilter, TestProbe}
+import ch.qos.logback.classic.Level
+import scala.concurrent.duration.{DurationInt, FiniteDuration}
+
+class JsonReceiverSpec extends BaseAkkaSpec with LoggingTest{
+
+
+  class ReceiverTest(verifyActor: ActorRef) extends JsonReceiver{
+
+    override def execute(): Unit = {
+      verifyActor ! "EXECUTED"
+      Thread.sleep(500)
+    }
+
+    override def exceptionOnRun(e: Exception): Unit = {
+      verifyActor ! "INTERRUPTED"
+    }
+
+  }
+
+  val verifyTB = TestProbe("RECEIVER-TEST")
+  val receiver = new ReceiverTest(verifyTB.ref)
+
+  "Executing validJson in JsonReceiver" should {
+    "return false when json schema is not right" in {
+      receiver.validJson(getJSValueFromString(Utils_JSONTest.test_json_schema_invalid)) should be(false)
+    }
+    "log message to Error" in {
+      ("Incorrect JSON schema \n/ensembles/0 \n\tErrors: Property command missing") should beLoggedAt(Level.ERROR)
+    }
+    "return true when Json schema is valid" in {
+      receiver.validJson(getJSValueFromString(Utils_JSONTest.create_json_test)) should be(true)
+    }
+  }
+
+  "Executing checkForLocation in JsonReceiver" should {
+    "log message at Debug level" in {
+      receiver.checkForLocation(getJSValueFromString(Utils_JSONTest.test_json_schema_invalid))
+      "Location not defined in JSON" should beLoggedAt(Level.DEBUG)
+    }
+    "download jar dynamically from URL" in {
+      receiver.checkForLocation(getJSValueFromString(Utils_JSONTest.location_test))
+      Files.exists(Paths.get(s"${CONFIG.DYNAMIC_JAR_REPO}/fey-stream.jar"))
+    }
+  }
+
+  var watchThread: Thread = _
+  "Start a Thread with the JSON receiver" should {
+    "Start Thread" in {
+      watchThread = new Thread(receiver, "TESTING-RECEIVERS-IN-THREAD")
+      watchThread.setDaemon(true)
+      watchThread.start()
+      TestProbe().isThreadRunning("TESTING-RECEIVERS-IN-THREAD") should be(true)
+    }
+    "execute execute() method inside run" in {
+      verifyTB.expectMsgAllOf(600.milliseconds,"EXECUTED","EXECUTED")
+    }
+  }
+
+  "Interrupting the receiver Thread" should {
+    "Throw Interrupted exception" in {
+      EventFilter[InterruptedException]() intercept {
+        watchThread.interrupt()
+        watchThread.join()
+      }
+    }
+    "execute exceptionOnRun method" in {
+      verifyTB.receiveWhile(1200.milliseconds) {
+        case "EXECUTED" =>
+      }
+      verifyTB.expectMsg("INTERRUPTED")
+    }
+  }
+
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/e892b5d8/fey-core/src/test/scala/org/apache/iota/fey/Utils_JSONTest.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/test/scala/org/apache/iota/fey/Utils_JSONTest.scala b/fey-core/src/test/scala/org/apache/iota/fey/Utils_JSONTest.scala
index c6dfbc7..417ba54 100644
--- a/fey-core/src/test/scala/org/apache/iota/fey/Utils_JSONTest.scala
+++ b/fey-core/src/test/scala/org/apache/iota/fey/Utils_JSONTest.scala
@@ -359,4 +359,62 @@ object Utils_JSONTest {
        ]
      }
     """
+
+  val test_json_schema_invalid =
+    """{
+       "guid" : "TEST-ACTOR",
+       "command" : "CREATE",
+       "timestamp": "213263914979",
+       "name" : "ORCHESTRATION FOR TEST",
+       "ensembles" : [
+         {
+           "guid":"MY-ENSEMBLE-0001",
+           "performers":[
+             {
+               "guid": "TEST-0001",
+               "schedule": 0,
+               "backoff": 0,
+               "source": {
+                 "name": "fey-test-actor.jar",
+                 "classPath": "org.apache.iota.fey.TestActor",
+                 "parameters": {}
+               }
+             }
+           ],
+           "connections":[]
+         }
+       ]
+     }"""
+
+  val location_test =
+    """{
+        "guid": "Orch2",
+        "command": "CREATE",
+        "timestamp": "591997890",
+        "name": "DESCRIPTION",
+        "ensembles": [
+          {
+            "guid": "En2",
+            "command": "NONE",
+            "performers": [
+              {
+                "guid": "S2",
+                "schedule": 1000,
+                "backoff": 0,
+                "source": {
+                  "name": "fey-stream.jar",
+                  "classPath": "org.apache.iota.fey.performer.Timestamp",
+                  "location" :{
+                    "url" : "https://github.com/apache/incubator-iota/raw/master/fey-examples/active-jar-repo"
+                  },
+                  "parameters": {
+                  }
+                }
+              }
+            ],
+            "connections": [
+            ]
+          }
+        ]
+      }"""
 }