blob: cd09819bf5f9485efd7712afe5fec7599bea9787 [file] [log] [blame]
* Copyright 2015-2016 IBM Corporation
* 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
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.
package catalog.cloudant
import org.junit.runner.RunWith
import org.scalatest.FlatSpec
import org.scalatest.junit.JUnitRunner
import catalog.CloudantUtil
import common.TestHelpers
import common.Wsk
import common.WskProps
import common.WhiskProperties
import common.WskTestHelpers
import spray.json.DefaultJsonProtocol.IntJsonFormat
import spray.json.DefaultJsonProtocol.StringJsonFormat
import spray.json.pimpAny
import common.WskActorSystem
* Tests for Cloudant trigger service
class CloudantTriggerPersistencyTest
extends FlatSpec
with TestHelpers
with WskTestHelpers
with WskActorSystem {
val dbUsername = WhiskProperties.getProperty("db.username")
val dbPassword = WhiskProperties.getProperty("db.password")
val dbPrefix = WhiskProperties.getProperty("db.prefix")
val wskprops = WskProps()
val wsk = new Wsk
val myCloudantCreds = CloudantUtil.Credential.makeFromVCAPFile("cloudantNoSQLDB", this.getClass.getSimpleName)
val cloudantTriggerDBCreds = new CloudantUtil.Credential(dbUsername, dbPassword, dbPrefix + "cloudanttrigger")
behavior of "Cloudant trigger service"
ignore should "persist trigger into Cloudant" in withAssetCleaner(wskprops) {
(wp, assetHelper) =>
implicit val wskprops = wp // shadow global props and make implicit
val namespace = wsk.namespace.list().stdout.trim.split("\n").last
val triggerName = s"dummyCloudantTrigger-${System.currentTimeMillis}"
val trigger = "/" + namespace + "/" + triggerName
val packageName = "/" + namespace + "/dummyCloudantPackage"
val feed = "changes"
try {
val packageGetResult = wsk.pkg.get("/whisk.system/cloudant")
println("Fetching cloudant package.")
packageGetResult.stdout should include("ok")
println("Creating cloudant package binding.")
assetHelper.withCleaner(wsk.pkg, packageName) {
(pkg, name) => pkg.bind("/whisk.system/cloudant", name)
println("Creating cloudant trigger feed.")
val feedCreationResult = wsk.trigger.create(trigger,
feed = Some(s"$packageName/$feed"),
parameters = Map(
"username" -> myCloudantCreds.user.toJson,
"password" -> myCloudantCreds.password.toJson,
"host" ->,
"dbname" -> myCloudantCreds.dbname.toJson,
"maxTriggers" -> 1.toJson))
feedCreationResult.stdout should include("ok")
println("Getting cloudanttrigger doc from the cloudant")
val docId = s":${namespace}:${triggerName}"
val persistedResponse = CloudantUtil.getDocument(cloudantTriggerDBCreds, docId)
println("Deleting cloudant trigger feed.")
val feedDeletionResult = wsk.trigger.delete(trigger)
feedDeletionResult.stdout should include("ok")
println("Getting cloudanttrigger doc from the cloudant")
val removedResponse = CloudantUtil.getDocument(cloudantTriggerDBCreds, docId)
persistedResponse.get("id").getAsString() should be(docId)
removedResponse.get("error").getAsString() should be("not_found")
} finally {