Skip to content

Commit

Permalink
Create package com.headstorm and Test directory #9
Browse files Browse the repository at this point in the history
  • Loading branch information
plee committed Mar 20, 2020
1 parent 4302da7 commit e3dabf2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ val silencerVersion = "1.4.2"

libraryDependencies += "com.github.ghik" % "silencer-plugin_2.12" % "1.4.2"
libraryDependencies += "net.liftweb" %% "lift-json" % "3.4.1"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.headstorm

import RouteGenerator._
import akka.actor.ActorSystem
import akka.http.scaladsl.Http

import sttp.tapir.docs.openapi._
import sttp.tapir.openapi.OpenAPI
import sttp.tapir.openapi.circe.yaml._
import sttp.tapir.swagger.akkahttp.SwaggerAkka

import scala.concurrent.duration._
import scala.concurrent.Await

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.headstorm

import sttp.tapir.Endpoint

import scala.concurrent.Future
Expand Down Expand Up @@ -69,4 +71,4 @@ object RouteGenerator {
val streamingRoute: Route = streamingEndpoint.toRoute(createStream _)

}
}
}
80 changes: 80 additions & 0 deletions src/test/scala/MainTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import com.headstorm.RouteGenerator._
import org.scalatest.FunSuite
import sttp.tapir.docs.openapi._
import sttp.tapir.openapi.OpenAPI
import sttp.tapir.openapi.circe.yaml._
import sttp.tapir.swagger.akkahttp.SwaggerAkka

import scala.concurrent.duration._
import scala.concurrent.Await

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import sttp.client._

class MainTest extends FunSuite {

// the endpoints' routes
val helloWorldInit = new Endpoints("name", "hello", "get")
val helloWorldEndpoint = helloWorldInit.aEndpoint
val helloWorldRoute = helloWorldInit.aRoute

val byeWorldInit = new Endpoints("name", "bye", "post")
val byeWorldEndpoint = byeWorldInit.aEndpoint
val byeWorldRoute = byeWorldInit.aRoute

val streamInit = new StreamEndpoints("text", "stream", "get")
val streamEndpoint = streamInit.streamingEndpoint
val streamRoute = streamInit.streamingRoute

// generating the documentation in yml; extension methods come from imported packages
val openApiDocs: OpenAPI = List(helloWorldEndpoint, byeWorldEndpoint, streamEndpoint).toOpenAPI("The tapir library", "1.0.0")
val openApiYml: String = openApiDocs.toYaml

// starting the server
implicit val actorSystem: ActorSystem = ActorSystem()
import actorSystem.dispatcher

val routes = {
import akka.http.scaladsl.server.Directives._
helloWorldRoute ~ byeWorldRoute ~ streamRoute ~ new SwaggerAkka(openApiYml).routes
}

val bindAndCheck = Http().bindAndHandle(routes, "localhost", 8080).map { _ =>
implicit val backend: SttpBackend[Identity, Nothing, NothingT] = HttpURLConnectionBackend()

//test hello GET endpoint
test("hello GET Endpoint"){
val result: String = basicRequest.response(asStringAlways).get(uri"http://localhost:8080/hello?name=Frodo").send().body
// println("Got result: " + result)
assert(result == "{\"code\":\"200\",\"msg\":\"hello : name : Frodo\"}")
}

test("bye POST Endpoint") {
//test bye POST endpoint
val result2: String = basicRequest.response(asStringAlways).post(uri"http://localhost:8080/bye?name=Frodo").send().body
// println("Got result: " + result2)
assert(result2 == "{\"code\":\"200\",\"msg\":\"bye : name : Frodo\"}")
}

test("test streaming GET endpoint") {
//test streaming GET endpoint
val result3: String = basicRequest.response(asStringAlways).get(uri"http://localhost:8080/stream?text=Frodo").send().body
// println("Got result: " + result3)
assert(result3 == "{\n \"code\" : \"200\",\n \"msg\" : \"stream : text : Frodo\"\n}")
}

test("test Not Found") {
//test Not Found
val result4: String = basicRequest.response(asStringAlways).get(uri"http://localhost:8080/stream2?text=Frodo").send().statusText
// println("Got result: " + result4)
assert(result4 == "Not Found")
}

//running server
println("Go to: http://localhost:8080/docs")
println("Press any key to exit ...")
scala.io.StdIn.readLine()
}

}

0 comments on commit e3dabf2

Please sign in to comment.