Skip to content

Instantly share code, notes, and snippets.

@tjlee
Last active November 17, 2016 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjlee/7f5a09498b69b06d75c6 to your computer and use it in GitHub Desktop.
Save tjlee/7f5a09498b69b06d75c6 to your computer and use it in GitHub Desktop.
example
package net.admoment.tests.functional.services
import org.scalatest._
import scalaj.http.{HttpOptions, Http}
import net.admoment.tests.WebTest
import com.google.gson.Gson
import net.admoment.db.model._
import org.springframework.beans.factory.annotation.Autowired
import com.typesafe.config.Config
import org.springframework.test.context.{TestContextManager, ContextConfiguration}
import net.admoment.spec.configuration.CommonConfiguration
/*
REST API Tests for 'As authenticated actor (USER or ADMIN) I wanna switch between available campaigns in tabs'
https://wikiadmoment.atlassian.net/wiki/display/AAP/A.+As+authenticated+actor+%28USER+or+ADMIN%29+I+wanna+switch+between+available+campaigns+in+tabs
*/
@ContextConfiguration(classes = Array(classOf[CommonConfiguration]))
class CampaignAvailabilitySpec extends FunSpec {
val tag = WebTest
val connectionTimeout = 5000
val serviceWeb = "services.web.url"
val authorizationHeader = "Authorization"
val campaignsUrlChunk = "/campaigns"
@Autowired var config: Config = null
new TestContextManager(this.getClass).prepareTestInstance(this)
describe("As authenticated USER I wanna switch between available campaigns in tabs") {
val serviceUrl = config.getString(serviceWeb)
it(s"Service '$serviceUrl' should return campaign list", tag) {
val campaigns = getAvailableCampaignList(serviceUrl, TestValues.User.base64EncodedCredentials, 200, tag)
withClue("Should no duplicates in returned campaign list") {
expectResult(false) {
// according to campaign duplication bug
isItemsInArrayDuplicating(campaigns.map(el => el.id))
}
}
}
}
def getAvailableCampaignList(service: AnyRef, encodedCredentials: String, expectedResponseCode: Int, tag: Tag): Array[Campaign] = {
val (responseCode, headersMap, resultString) = Http(s"http://$service$campaignsUrlChunk")
.option(HttpOptions.connTimeout(connectionTimeout))
.header(authorizationHeader, "Basic " + encodedCredentials)
.asHeadersAndParse(Http.readString)
expectResult(expectedResponseCode) {
responseCode
}
val map = (new Gson).fromJson(resultString, classOf[Array[Campaign]])
withClue("Returned campaign list should be not empty") {
expectResult(true) {
map.size > 0
}
}
return map
}
def getRandomCampaign(service: AnyRef, encodedCredentials: String, tag: Tag): Campaign = {
val map = getAvailableCampaignList(service, encodedCredentials, 200, tag)
val anyCampaignId = map(scala.util.Random.nextInt(map.size)).id
val (responseCode, headersMap, resultString) = Http(s"http://$service$campaignsUrlChunk/$anyCampaignId")
.option(HttpOptions.connTimeout(connectionTimeout))
.header(authorizationHeader, "Basic " + encodedCredentials)
.asHeadersAndParse(Http.readString)
expectResult(200) {
responseCode
}
validateCampaignJson(resultString)
(new Gson).fromJson(resultString, classOf[Campaign])
}
def validateCampaignJson(resultString: String) {
val campaignMap = (new Gson).fromJson(resultString, classOf[Campaign])
assert(!campaignMap.name.isEmpty())
}
def isItemsInArrayDuplicating(array: Array[String]): Boolean = {
val res = array.toSeq.diff(array.toSeq.distinct)
res.length > 0
}
def getCampaignById(service: AnyRef, encodedCredentials: String, campaignId: String, tag: Tag) {
val (responseCode, headersMap, resultString) = Http(s"http://$service$campaignsUrlChunk/$campaignId")
.option(HttpOptions.connTimeout(connectionTimeout))
.header(authorizationHeader, "Basic " + encodedCredentials)
.asHeadersAndParse(Http.readString)
expectResult(200) {
responseCode
}
validateCampaignJson(resultString)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment