pheymann / rest-refactoring-test   2.2.0

MIT License GitHub

Create small and simple refactoring tests for your REST service

Scala versions: 2.11 2.10
sbt plugins: 0.13

Build Status Chat

rrt-core: Maven Central rrt-play: Maven Central rrt-plugin: Maven Central

Currently in release process: 2.2.1

[DEPRECATED | replacement artie] rest-refactoring-test (rrt)

Often you have to refactor or fix REST services which are not covered by extensive automated test and are hard to test at all. But you want to make sure that these services haven't changed their behaviour after you have done your changes.

This library gives you the tools to test your services fast and simple. You only describe the data you need and how the request is built and let the library do the work. See this small example:

import com.github.pheymann.rrt._

// GET /rest/hello/:name?age: Int
val config = newConfig("my-test", ServiceConfig("refactored-rest.com", 8080), ServiceConfig("old-rest.com", 8081))
              .withRepetitions(100)
              .withIgnoreJsonKeys(List("service-tracking-id"))

val testCase = for {
  userNames <- genStaticData("Luke", "Anakin", "Yoda")
  ages      <- genInts(Some(900))
  result    <- testGet { _ =>
    // selects randomly one name out of the static list
    val uri = s"/rest/hello/${userNames()}"
    // adds a query parameter `age` with a random `Int` between 0 and 900
    val params = Params().add("age", ages)
    
    uri |+| params
  }
} yield result

assert(checkAndLog(testCase.runSeq(config)))

Output:

[GET] start my-test

[####################] 100%

test case my-test succeeded
  succeeded tries: 100
  failed tries     0

Here, we create a test for a GET endpoint /rest/hello/:name which is currently provided by the refactored REST service on refactored-rest.com and the old version on old-rest.com. The library will create a request with random name and age and send it to both services. It will then remove the "service-tracing-id" key with value from the response Json and compare the responses and log possible differences. This step is repeated 100 times as configured.

Besides the random generation or selection of values you are also able to load data from a database, e.g. if you need existing user ids.

For more information have a look into the Wiki.

Get The Libraries

You can get the core library by adding the following dependency:

libraryDependencies += "com.github.pheymann" %% "rrt-core" % "2.1.x" % Test

Furthermore you can add a Play dependency which adds the ability to read Play database configs.

libraryDependencies += "com.github.pheymann" %% "rrt-play" % "2.1.x" % Test

Both libs are built for Scala 2.11.x.

SBT Plugin

If you want to have a rrt task and don't want to manually define all modules you need you can use the SBT plugin. It is built for SBT version 0.13.x and can be used by adding the following line to your plugins.sbt file:

addSbtPlugin("com.github.pheymann" % "rrt-plugin" % "2.1.x")

With that you can add the dependencies as follows:

import com.github.pheymann.rrt.plugin._

libraryDependencies ++= Seq(
  rrtCore % RestRefactoringTest,
  rrtPlay % RestRefactoringTest
)

And run your refactoring tests with the following task: rrt:test. As this task extends Test you also have access to all sub-tasks.

Dependecies

This library is build with:

  • Free Mondas provided by cats to build the library api
  • the http client by akka-http for the REST calls
  • the db api from scalike-jdbc for the database interactions

Examples

In this early phase you can only find some running examples in the integration tests. You can also have a look into the Wiki.