Skip to content

Commit

Permalink
add GenericRoutesTest
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed Apr 26, 2018
1 parent 59634a2 commit f3e27ca
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 40 deletions.
21 changes: 21 additions & 0 deletions src/main/scala/com/fluency03/blockchain/Util.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.fluency03.blockchain

import java.security.MessageDigest
import java.time.Instant

import akka.http.scaladsl.model.{StatusCode, StatusCodes}
import com.fluency03.blockchain.api.{FailureMsg, Message, SuccessMsg}
import org.bouncycastle.util.encoders.Base64

object Util {
Expand Down Expand Up @@ -60,4 +63,22 @@ object Util {
*/
def fromBase64(base64: String): String = new String(Base64.decode(base64), "UTF-8")

/**
* Return either SuccessMsg (if fun successfully returned a String) or FailureMsg (if fun failed).
*/
def failsafeMsg(fun: => String): Message =
try { SuccessMsg(fun) }
catch {
case e: Exception => FailureMsg(e.getMessage)
}

/**
* Return either SuccessMsg (if fun successfully returned a String) or FailureMsg (if fun failed).
*/
def failsafeResp(fun: => String): (StatusCode, Message) =
try { (StatusCodes.OK, SuccessMsg(fun)) }
catch {
case e: Exception => (StatusCodes.InternalServerError, FailureMsg(e.getMessage))
}

}
4 changes: 2 additions & 2 deletions src/main/scala/com/fluency03/blockchain/api/Message.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.fluency03.blockchain.api
final case class Input(content: String)

sealed trait Message
final case class SuccessMsg(content: String) extends Message
final case class FailureMsg(content: String) extends Message
final case class SuccessMsg(message: String) extends Message
final case class FailureMsg(error: String) extends Message
3 changes: 3 additions & 0 deletions src/main/scala/com/fluency03/blockchain/api/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ package object api {

val PARENT_UP = "../"




}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ trait BlockRoutes extends RoutesSupport {
entity(as[Block]) { block =>
val blockCreated: Future[Message] = (blocksActor ? CreateBlock(block)).mapTo[Message]
onSuccess(blockCreated) {
case SuccessMsg(content) => complete((StatusCodes.Created, content))
case FailureMsg(content) => complete((StatusCodes.Conflict, content))
case s: SuccessMsg => complete((StatusCodes.Created, s))
case f: FailureMsg => complete((StatusCodes.Conflict, f))
}
}
}
Expand All @@ -47,8 +47,8 @@ trait BlockRoutes extends RoutesSupport {
delete {
val blockDeleted: Future[Message] = (blocksActor ? DeleteBlock(hash)).mapTo[Message]
onSuccess(blockDeleted) {
case SuccessMsg(content) => complete((StatusCodes.OK, content))
case FailureMsg(content) => complete((StatusCodes.NotFound, content))
case s: SuccessMsg => complete((StatusCodes.OK, s))
case f: FailureMsg => complete((StatusCodes.NotFound, f))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ trait BlockchainRoutes extends RoutesSupport {
entity(as[JValue]) { _ =>
val blockchainCreated: Future[Message] = (blockchainActor ? CreateBlockchain).mapTo[Message]
onSuccess(blockchainCreated) {
case SuccessMsg(content) => complete((StatusCodes.Created, content))
case FailureMsg(content) => complete((StatusCodes.Conflict, content))
case s: SuccessMsg => complete((StatusCodes.Created, s))
case f: FailureMsg => complete((StatusCodes.Conflict, f))
}
}
} ~
delete {
val blockchainDeleted: Future[Message] = (blockchainActor ? DeleteBlockchain).mapTo[Message]
onSuccess(blockchainDeleted) {
case SuccessMsg(content) => complete((StatusCodes.OK, content))
case FailureMsg(content) => complete((StatusCodes.NotFound, content))
case s: SuccessMsg => complete((StatusCodes.OK, s))
case f: FailureMsg => complete((StatusCodes.NotFound, f))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,41 @@ import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.MethodDirectives.post
import akka.http.scaladsl.server.directives.RouteDirectives.complete
import com.fluency03.blockchain.Util._
import com.fluency03.blockchain.api.Input
import com.fluency03.blockchain.api.{FailureMsg, Input, SuccessMsg}

trait GenericRoutes extends RoutesSupport {
lazy val log = Logging(system, classOf[GenericRoutes])



lazy val genericRoutes: Route =
pathSingleSlash {
get {
complete("Welcome to Blockchain in Scala!")
complete(SuccessMsg(SLOGAN))
}
} ~
pathPrefix("generic") {
path("toSha256") {
pathPrefix(GENERIC) {
path(TO_SHA256) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, in.content.toSha256)) }
entity(as[Input]) { in => complete( failsafeResp { in.content.toSha256 } ) }
}
} ~
path("toBase64") {
path(TO_BASE64) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, in.content.toBase64)) }
entity(as[Input]) { in => complete( failsafeResp { in.content.toBase64 } ) }
}
} ~
path("fromBase64") {
path(FROM_BASE64) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, fromBase64(in.content))) }
entity(as[Input]) { in => complete( failsafeResp { fromBase64(in.content) } ) }
}
} ~
path("epoch-time") {
path(TO_EPOCH_TIME) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, epochTimeOf(in.content))) }
entity(as[Input]) { in => complete( failsafeResp { epochTimeOf(in.content).toString } ) }
}
} ~
path("time-of-epoch") {
path(TIME_FROM_EPOCH) {
post {
entity(as[Input]) { in => complete((StatusCodes.Created, Instant.ofEpochSecond(in.content.toLong))) }
entity(as[Input]) { in => complete( failsafeResp { Instant.ofEpochSecond(in.content.toLong).toString } ) }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ trait NetworkRoutes extends RoutesSupport {
entity(as[PeerSimple]) { peer =>
val peerCreated: Future[Message] = (networkActor ? CreatePeer(peer.name)).mapTo[Message]
onSuccess(peerCreated) {
case SuccessMsg(content) => complete((StatusCodes.Created, content))
case FailureMsg(content) => complete((StatusCodes.Conflict, content))
case s: SuccessMsg => complete((StatusCodes.Created, s))
case f: FailureMsg => complete((StatusCodes.Conflict, f))
}
}
}
Expand All @@ -47,8 +47,8 @@ trait NetworkRoutes extends RoutesSupport {
delete {
val peerDeleted: Future[Message] = (networkActor ? DeletePeer(name)).mapTo[Message]
onSuccess(peerDeleted) {
case SuccessMsg(content) => complete((StatusCodes.OK, content))
case FailureMsg(content) => complete((StatusCodes.NotFound, content))
case s: SuccessMsg => complete((StatusCodes.OK, s))
case f: FailureMsg => complete((StatusCodes.NotFound, f))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ trait TransactionRoutes extends RoutesSupport {
entity(as[Transaction]) { tx =>
val txCreated: Future[Message] = (transActor ? CreateTransaction(tx)).mapTo[Message]
onSuccess(txCreated) {
case SuccessMsg(content) => complete((StatusCodes.Created, content))
case FailureMsg(content) => complete((StatusCodes.Conflict, content))
case s: SuccessMsg => complete((StatusCodes.Created, s))
case f: FailureMsg => complete((StatusCodes.Conflict, f))
}
}
}
Expand All @@ -47,8 +47,8 @@ trait TransactionRoutes extends RoutesSupport {
delete {
val txDeleted: Future[Message] = (transActor ? DeleteTransaction(id)).mapTo[Message]
onSuccess(txDeleted) {
case SuccessMsg(content) => complete((StatusCodes.OK, content))
case FailureMsg(content) => complete((StatusCodes.NotFound, content))
case s: SuccessMsg => complete((StatusCodes.OK, s))
case f: FailureMsg => complete((StatusCodes.NotFound, f))
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/com/fluency03/blockchain/api/routes/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.fluency03.blockchain.api

package object routes {

// generics
val SLASH = "/"
val GENERIC = "generic"
val TO_SHA256 = "to-sha256"
val TO_BASE64 = "to-base64"
val FROM_BASE64 = "from-base64"
val TO_EPOCH_TIME = "to-epoch-time"
val TIME_FROM_EPOCH = "time-from-epoch"

def pathOf(seg: String*): String = SLASH + seg.mkString(SLASH)

}
4 changes: 2 additions & 2 deletions src/test/scala/com/fluency03/blockchain/api/MessageTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class MessageTest extends FlatSpec with Matchers {

"A Message" should "contain valid content." in {
SuccessMsg("Response test.") shouldBe a[Message]
SuccessMsg("Response test.").content shouldEqual "Response test."
SuccessMsg("Response test.").message shouldEqual "Response test."
}

"A Input" should "contain valid content." in {
Expand All @@ -15,7 +15,7 @@ class MessageTest extends FlatSpec with Matchers {

"A Fail" should "contain valid content." in {
FailureMsg("Error") shouldBe a[Message]
FailureMsg("Error").content shouldEqual "Error"
FailureMsg("Error").error shouldEqual "Error"
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.fluency03.blockchain.api.routes

class BlockRoutesTest {
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.{Matchers, WordSpec}

class BlockRoutesTest extends WordSpec with Matchers with ScalatestRouteTest {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.fluency03.blockchain.api.routes

class BlockchainRoutesTest {
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.{Matchers, WordSpec}

class BlockchainRoutesTest extends WordSpec with Matchers with ScalatestRouteTest {





}
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
package com.fluency03.blockchain.api.routes

class GenericRoutesTest {
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest
import com.fluency03.blockchain.api.{Input, JsonSupport, SuccessMsg}
import org.scalatest.{Matchers, WordSpec}
import com.fluency03.blockchain.api.Server.genericRoutes

class GenericRoutesTest extends WordSpec with Matchers with ScalatestRouteTest with JsonSupport {

"GenericRoutes" should {
"return a greeting for GET requests to the root path." in {
Get() ~> genericRoutes ~> check {
status shouldEqual StatusCodes.OK
responseAs[SuccessMsg] shouldEqual SuccessMsg("Welcome to Blockchain in Scala!")
}
}

"return corresponding value." in {
Post(pathOf(GENERIC, TO_SHA256), Input("open sesame")) ~> genericRoutes ~> check {
status shouldEqual StatusCodes.OK
responseAs[SuccessMsg] shouldEqual SuccessMsg("41ef4bb0b23661e66301aac36066912dac037827b4ae63a7b1165a5aa93ed4eb")
}

Post(pathOf(GENERIC, TO_SHA256), Input("")) ~> genericRoutes ~> check {
status shouldEqual StatusCodes.OK
responseAs[SuccessMsg] shouldEqual SuccessMsg("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
}

Post(pathOf(GENERIC, TO_BASE64), Input("open sesame")) ~> genericRoutes ~> check {
status shouldEqual StatusCodes.OK
responseAs[SuccessMsg] shouldEqual SuccessMsg("b3BlbiBzZXNhbWU=")
}

Post(pathOf(GENERIC, FROM_BASE64), Input("b3BlbiBzZXNhbWU=")) ~> genericRoutes ~> check {
status shouldEqual StatusCodes.OK
responseAs[SuccessMsg] shouldEqual SuccessMsg("open sesame")
}

Post(pathOf(GENERIC, TO_EPOCH_TIME), Input("2018-04-11T18:52:01Z")) ~> genericRoutes ~> check {
status shouldEqual StatusCodes.OK
responseAs[SuccessMsg] shouldEqual SuccessMsg("1523472721")
}

Post(pathOf(GENERIC, TIME_FROM_EPOCH), Input("1523472721")) ~> genericRoutes ~> check {
status shouldEqual StatusCodes.OK
responseAs[SuccessMsg] shouldEqual SuccessMsg("2018-04-11T18:52:01Z")
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.fluency03.blockchain.api.routes

class NetworkRoutesTest {
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.{Matchers, WordSpec}

class NetworkRoutesTest extends WordSpec with Matchers with ScalatestRouteTest {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.fluency03.blockchain.api.routes

class TransactionRoutesTest {
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.{Matchers, WordSpec}

class TransactionRoutesTest extends WordSpec with Matchers with ScalatestRouteTest {

}

0 comments on commit f3e27ca

Please sign in to comment.