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:58 UTC

[07/10] incubator-iota git commit: Adding tests to Utils.scala

Adding tests to Utils.scala


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

Branch: refs/heads/master
Commit: 28e0b81941087b3f97141391e2e0778f5f65763b
Parents: e0cad79
Author: Barbara Gomes <ba...@gmail.com>
Authored: Wed Jul 20 18:41:19 2016 -0700
Committer: Barbara Gomes <ba...@gmail.com>
Committed: Wed Jul 20 18:41:19 2016 -0700

----------------------------------------------------------------------
 .../main/scala/org/apache/iota/fey/Utils.scala  |   2 +
 fey-core/src/test/resources/fey-test-actor.jar  | Bin 7595 -> 12703 bytes
 .../src/test/resources/json/invalid-json.json   |  35 ++++
 .../src/test/resources/json/valid-json.json     |  35 ++++
 .../org/apache/iota/fey/BaseAkkaSpec.scala      |   7 +-
 .../scala/org/apache/iota/fey/TestSetup.scala   |  16 +-
 .../scala/org/apache/iota/fey/UtilsSpec.scala   | 176 +++++++++++++++++++
 7 files changed, 266 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/28e0b819/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala b/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala
index 8402485..fb62f37 100644
--- a/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala
+++ b/fey-core/src/main/scala/org/apache/iota/fey/Utils.scala
@@ -160,6 +160,8 @@ protected object Utils {
               log.info(s"Orchestration ${orchestrationID} saved.")
           }
       }
+    }else{
+      log.debug("Checkpoint not enabled")
     }
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/28e0b819/fey-core/src/test/resources/fey-test-actor.jar
----------------------------------------------------------------------
diff --git a/fey-core/src/test/resources/fey-test-actor.jar b/fey-core/src/test/resources/fey-test-actor.jar
index b074d76..d440cd1 100644
Binary files a/fey-core/src/test/resources/fey-test-actor.jar and b/fey-core/src/test/resources/fey-test-actor.jar differ

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/28e0b819/fey-core/src/test/resources/json/invalid-json.json
----------------------------------------------------------------------
diff --git a/fey-core/src/test/resources/json/invalid-json.json b/fey-core/src/test/resources/json/invalid-json.json
new file mode 100644
index 0000000..b41189d
--- /dev/null
+++ b/fey-core/src/test/resources/json/invalid-json.json
@@ -0,0 +1,35 @@
+{
+  "guid" : "FEY-TEST-ORCHESTRATION",
+  "command" : "CREATE",
+  "timestamp": "213263914979",
+  "name" : "ORCHESTRATION FOR TEST",
+  "ensembles" : [
+    {
+      "guid":"MY-ENSEMBLE-0001",
+      "command": "DELETE",
+      "performers":[
+        {
+          "guid": "TEST-0001",
+          "schedule": 0,
+          "backoff": 0,
+          "source": {
+            "name": "fey-test-actor.jar",
+            "classPath": "org.apache.iota.fey.TestActor",
+            "parameters": {}
+          }
+        },
+        {
+          "guid": "TEST-0002",
+          "schedule": 0,
+          "backoff": 0,
+          "source": {
+            "name": "fey-test-actor.jar",
+            "classPath": "org.apache.iota.fey.TestActor",
+            "parameters": {}
+          }
+        }
+      ],
+      "connections":
+    }
+  ]
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/28e0b819/fey-core/src/test/resources/json/valid-json.json
----------------------------------------------------------------------
diff --git a/fey-core/src/test/resources/json/valid-json.json b/fey-core/src/test/resources/json/valid-json.json
new file mode 100644
index 0000000..9dfde8c
--- /dev/null
+++ b/fey-core/src/test/resources/json/valid-json.json
@@ -0,0 +1,35 @@
+{
+  "guid" : "FEY-TEST-ORCHESTRATION",
+  "command" : "CREATE",
+  "timestamp": "213263914979",
+  "name" : "ORCHESTRATION FOR TEST",
+  "ensembles" : [
+    {
+      "guid":"MY-ENSEMBLE-0001",
+      "command": "DELETE",
+      "performers":[
+        {
+          "guid": "TEST-0001",
+          "schedule": 0,
+          "backoff": 0,
+          "source": {
+            "name": "fey-test-actor.jar",
+            "classPath": "org.apache.iota.fey.TestActor",
+            "parameters": {}
+          }
+        },
+        {
+          "guid": "TEST-0002",
+          "schedule": 0,
+          "backoff": 0,
+          "source": {
+            "name": "fey-test-actor.jar",
+            "classPath": "org.apache.iota.fey.TestActor",
+            "parameters": {}
+          }
+        }
+      ],
+      "connections":[]
+    }
+  ]
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/28e0b819/fey-core/src/test/scala/org/apache/iota/fey/BaseAkkaSpec.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/test/scala/org/apache/iota/fey/BaseAkkaSpec.scala b/fey-core/src/test/scala/org/apache/iota/fey/BaseAkkaSpec.scala
index fbf80f9..d77d28b 100644
--- a/fey-core/src/test/scala/org/apache/iota/fey/BaseAkkaSpec.scala
+++ b/fey-core/src/test/scala/org/apache/iota/fey/BaseAkkaSpec.scala
@@ -21,7 +21,7 @@ package org.apache.iota.fey
 import java.nio.file.Paths
 
 import akka.actor.{ActorIdentity, ActorRef, ActorSystem, Identify, Props}
-import akka.testkit.{EventFilter, TestActorRef, TestEvent, TestProbe}
+import akka.testkit.{EventFilter, TestEvent, TestProbe}
 import com.typesafe.config.ConfigFactory
 import org.scalatest.BeforeAndAfterAll
 import play.api.libs.json._
@@ -29,9 +29,10 @@ import play.api.libs.json._
 import scala.concurrent.duration.{DurationInt, FiniteDuration}
 import scala.concurrent.Await
 
-class BaseAkkaSpec extends BaseSpec with BeforeAndAfterAll{
+class BaseAkkaSpec extends BaseSpec with BeforeAndAfterAll with LoggingTest{
 
   //Load default configuration for Fey when running tests
+  resetCapturedLogs()
   CONFIG.loadUserConfiguration(Paths.get(TestSetup.configTest.toURI()).toFile().getAbsolutePath)
   TestSetup.setup()
 
@@ -46,6 +47,8 @@ class BaseAkkaSpec extends BaseSpec with BeforeAndAfterAll{
   val globalIdentifierRef = system.actorOf(Props[IdentifyFeyActors],globalIdentifierName)
 
   override protected def afterAll(): Unit = {
+    //Force reload of GenericActor's jar
+    Utils.loadedJars.remove("fey-test-actor.jar")
     Monitor.events.removeAllNodes()
     Await.ready(system.terminate(), 20.seconds)
   }

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/28e0b819/fey-core/src/test/scala/org/apache/iota/fey/TestSetup.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/test/scala/org/apache/iota/fey/TestSetup.scala b/fey-core/src/test/scala/org/apache/iota/fey/TestSetup.scala
index 7f25490..2db22a1 100644
--- a/fey-core/src/test/scala/org/apache/iota/fey/TestSetup.scala
+++ b/fey-core/src/test/scala/org/apache/iota/fey/TestSetup.scala
@@ -34,14 +34,24 @@ object TestSetup {
       println("SETTING UP ...")
       createFeyTmpDirectoriesForTest()
       copyTestActorToTmp()
+      copyJSONstoTmp()
       runSetup = false
     }
   }
 
   private def copyTestActorToTmp(): Unit = {
-    val jarTest = getClass.getResource("/fey-test-actor.jar")
-    val dest = new File(s"${CONFIG.JAR_REPOSITORY}/fey-test-actor.jar")
-    FileUtils.copyURLToFile(jarTest, dest)
+    copyResourceFileToLocal("/fey-test-actor.jar",s"${CONFIG.JAR_REPOSITORY}/fey-test-actor.jar")
+  }
+
+  private def copyJSONstoTmp(): Unit = {
+    copyResourceFileToLocal("/json/valid-json.json",s"${CONFIG.JSON_REPOSITORY}/valid-json.json.not")
+    copyResourceFileToLocal("/json/invalid-json.json",s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not")
+  }
+
+  private def copyResourceFileToLocal(resourcePath: String, destination: String): Unit = {
+    val resourceFile = getClass.getResource(resourcePath)
+    val dest = new File(destination)
+    FileUtils.copyURLToFile(resourceFile, dest)
   }
 
   private def createFeyTmpDirectoriesForTest(): Unit = {

http://git-wip-us.apache.org/repos/asf/incubator-iota/blob/28e0b819/fey-core/src/test/scala/org/apache/iota/fey/UtilsSpec.scala
----------------------------------------------------------------------
diff --git a/fey-core/src/test/scala/org/apache/iota/fey/UtilsSpec.scala b/fey-core/src/test/scala/org/apache/iota/fey/UtilsSpec.scala
new file mode 100644
index 0000000..8a4b73b
--- /dev/null
+++ b/fey-core/src/test/scala/org/apache/iota/fey/UtilsSpec.scala
@@ -0,0 +1,176 @@
+
+/*
+ * 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.io.File
+
+import akka.actor.{ActorRef, PoisonPill, Props}
+import akka.testkit.{EventFilter, TestActorRef, TestProbe}
+import ch.qos.logback.classic.Level
+import play.api.libs.json._
+import java.nio.file.{Files, Paths}
+
+import scala.io.Source
+import scala.concurrent.duration._
+
+class UtilsSpec extends BaseAkkaSpec{
+
+  "Global variable loadedJars" should{
+    "be empty when starting" in {
+      Utils.loadedJars shouldBe empty
+    }
+  }
+
+  "Executing getFilesInDirectory" should {
+    "return a list of all Files in the directory" in {
+      val files = Utils.getFilesInDirectory(CONFIG.JAR_REPOSITORY)
+      files should not be empty
+      files should have size(2)
+      val filepath = files.map(_.getAbsolutePath)
+      filepath should contain(s"${CONFIG.JAR_REPOSITORY}/fey-test-actor.jar")
+      filepath should contain(s"${CONFIG.JAR_REPOSITORY}/dynamic")
+    }
+  }
+
+  "Executing loadActorClassFromJar with not yet loaded jar" should {
+    "result in new entry to global variable loadedJars" in {
+      Utils.loadActorClassFromJar(s"${CONFIG.JAR_REPOSITORY}/fey-test-actor.jar", "org.apache.iota.fey.TestActor","fey-test-actor.jar")
+      Utils.loadedJars should have size(1)
+      Utils.loadedJars should contain key("fey-test-actor.jar")
+      Utils.loadedJars.get("fey-test-actor.jar").get._2 should have size(1)
+      Utils.loadedJars.get("fey-test-actor.jar").get._2 should contain key("org.apache.iota.fey.TestActor")
+    }
+  }
+
+
+  "Executing loadActorClassFromJar with loaded jar but a different class" should {
+    "not add new entry to loadedJars" in {
+      val loader = Utils.loadedJars.get("fey-test-actor.jar").get._1
+      Utils.loadActorClassFromJar(s"${CONFIG.JAR_REPOSITORY}/fey-test-actor.jar", "org.apache.iota.fey.TestActor_2","fey-test-actor.jar")
+      Utils.loadedJars should have size(1)
+      Utils.loadedJars should contain key("fey-test-actor.jar")
+      Utils.loadedJars.get("fey-test-actor.jar").get._1 should equal(loader)
+    }
+    "add a new classpath to the loadedJars value map" in{
+      Utils.loadedJars.get("fey-test-actor.jar").get._2 should have size(2)
+      Utils.loadedJars.get("fey-test-actor.jar").get._2 should contain key("org.apache.iota.fey.TestActor")
+      Utils.loadedJars.get("fey-test-actor.jar").get._2 should contain key("org.apache.iota.fey.TestActor_2")
+    }
+  }
+
+  "Executing loadActorClassFromJar with loaded jar and class" should {
+    "not reload the jar" in {
+      val loader = Utils.loadedJars.get("fey-test-actor.jar").get._1
+      Utils.loadActorClassFromJar(s"${CONFIG.JAR_REPOSITORY}/fey-test-actor.jar", "org.apache.iota.fey.TestActor","fey-test-actor.jar")
+      Utils.loadedJars.get("fey-test-actor.jar").get._1 should equal(loader)
+    }
+  }
+
+  var actorRef: ActorRef = _
+
+  "Initializing an actor from a clazz returned by loadActorClassFromJar" should {
+    "result in creation of a GenericActor" in {
+      val clazz = Utils.loadActorClassFromJar(s"${CONFIG.JAR_REPOSITORY}/fey-test-actor.jar", "org.apache.iota.fey.TestActor_2","fey-test-actor.jar")
+      val props = Props(clazz, Map("TEST" -> "TESTED"), 0.seconds, Map.empty, 0.seconds, "MY-ORCH", "ORCH", false)
+      val parent = TestProbe("UTILS-PARENT")
+      actorRef = TestActorRef[FeyGenericActor](props, parent.ref, "TESTING-UTILS")
+    }
+    "running GenericActor actor" in{
+      val respTB = TestProbe()
+      TestProbe().expectActor(actorRef.path.toString)
+      actorRef ! (respTB.ref)
+      actorRef ! "TEST_ACTOR"
+      respTB.expectMsg(Some("TESTED"))
+    }
+    "respond normally to stop message" in {
+      actorRef ! PoisonPill
+      TestProbe().verifyActorTermination(actorRef)
+      TestProbe().notExpectActor(actorRef.path.toString)
+    }
+  }
+
+  "Executing loadJsonFromFile with a valid JSON" should {
+    "return JsValue" in {
+      val json = Utils.loadJsonFromFile(new File(s"${CONFIG.JSON_REPOSITORY}/valid-json.json.not"))
+      json shouldBe defined
+    }
+  }
+
+  "Executing loadJsonFromFile with a invalid JSON" should {
+    "return None" in {
+      val json = Utils.loadJsonFromFile(new File(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not"))
+      json should not be defined
+    }
+    "Log message at Error Level" in {
+      "Could not parse JSON" should beLoggedAt(Level.ERROR)
+    }
+  }
+
+  "Executing renameProcessedFile when CHECKPOINT is disabled" should {
+    "not concatenated extension to the file" in {
+      Utils.renameProcessedFile(new File(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not"), "processed")
+      Utils.getFilesInDirectory(CONFIG.JSON_REPOSITORY).map(_.getAbsolutePath) should contain(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not")
+      Utils.getFilesInDirectory(CONFIG.JSON_REPOSITORY).map(_.getAbsolutePath) should not contain(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not.processed")
+    }
+  }
+
+  "Executing renameProcessedFile when CHECKPOINT is enabled" should {
+    "concatenated extension to the file" in {
+      CONFIG.CHEKPOINT_ENABLED = true
+      Utils.renameProcessedFile(new File(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not"), "processed")
+      Utils.getFilesInDirectory(CONFIG.JSON_REPOSITORY).map(_.getAbsolutePath) should not contain(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not")
+      Utils.getFilesInDirectory(CONFIG.JSON_REPOSITORY).map(_.getAbsolutePath) should contain(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not.processed")
+      new File(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not.processed").renameTo(new File(s"${CONFIG.JSON_REPOSITORY}/invalid-json.json.not"))
+      CONFIG.CHEKPOINT_ENABLED = false
+    }
+  }
+
+  val jsonObj = getJSValueFromString(Utils_JSONTest.orchestration_update2_test_json).as[JsObject]
+
+  "Executing updateOrchestrationState" should {
+    "result in log message at Debug when Checkpoint is disables" in {
+      Utils.updateOrchestrationState("TEST-15")
+      "Checkpoint not enabled" should beLoggedAt(Level.DEBUG)
+    }
+    "result in log message at warn when Orchestration to be updated is not cached" in {
+      CONFIG.CHEKPOINT_ENABLED = true
+      Utils.updateOrchestrationState("MY-TEST-UPDATE")
+      "Could not save state for Orchestration MY-TEST-UPDATE. It is not active on Fey." should beLoggedAt(Level.WARN)
+      CONFIG.CHEKPOINT_ENABLED = false
+    }
+    "result in creating a new file at Checkpoint dir" in {
+      CONFIG.CHEKPOINT_ENABLED = true
+      FEY_CACHE.activeOrchestrations.put("TEST_ORCHESTRATION_FOR_UTILS", ("", null))
+      ORCHESTRATION_CACHE.orchestration_metadata.put("TEST_ORCHESTRATION_FOR_UTILS",
+        Map("ENSEMBLE-UTILS" -> jsonObj))
+      Utils.updateOrchestrationState("TEST_ORCHESTRATION_FOR_UTILS")
+      Files.exists(Paths.get(s"${CONFIG.CHECKPOINT_DIR}/TEST_ORCHESTRATION_FOR_UTILS.json")) should be(true)
+      CONFIG.CHEKPOINT_ENABLED = false
+    }
+    "result in correct file created" in {
+      val file = Source.fromFile(s"${CONFIG.CHECKPOINT_DIR}/TEST_ORCHESTRATION_FOR_UTILS.json").getLines.mkString("")
+      val jsonFile = getJSValueFromString(file)
+      val ensembles = (jsonFile \ JSON_PATH.ENSEMBLES).as[List[JsObject]]
+      ensembles should have size(1)
+      Json.stringify(ensembles(0).as[JsValue]) should equal(Json.stringify(jsonObj))
+      new File(s"${CONFIG.CHECKPOINT_DIR}/TEST_ORCHESTRATION_FOR_UTILS.json").delete()
+    }
+  }
+
+}