Test cases for file upload using akka-http in Scala

Table of contents
Reading Time: < 1 minute

Hello folks, In my previous blog, i explained how to upload a file using akka-http in Scala. Later, i got the queries about the test cases for the same. Therefore, in this blog I am going to explain the way of writing test cases for file upload.

First we need to add test dependency :

Full code :

Get full code here.

It’s done. Hope you enjoy it. In my next blog, I will create a basic application to handle multipart formdata (file + form fields) using akka-http in Scala. So Stay tuned !!

Happy Blogging !!!

Written by 

Rishi is a tech enthusiast with having around 10 years of experience who loves to solve complex problems with pure quality. He is a functional programmer and loves to learn new trending technologies. His leadership skill is well prooven and has delivered multiple distributed applications with high scalability and availability by keeping the Reactive principles in mind. He is well versed with Scala, Akka, Akka HTTP, Akka Streams, Java8, Reactive principles, Microservice architecture, Async programming, functional programming, distributed systems, AWS, docker.

4 thoughts on “Test cases for file upload using akka-http in Scala1 min read

  1. Hi

    I have an Akka HTTP service like

    https://stackoverflow.com/q/48762297

    path( “file-upload”) {
    extractClientIP { ip =>
    optionalHeaderValueByName(Constants.AUTH) { auth =>
    (post & extractRequestContext) { request =>
    extractRequestContext {
    ctx => {
    implicit val materializer = ctx.materializer
    implicit val ec = ctx.executionContext
    val currentTime = TimeObject.getCurrentTime()
    fileUpload(“fileUpload”) {
    case (fileInfo, fileStream) =>
    val localPath = Configuration.excelFilePath
    val uniqueidString = “12345”
    val filename = uniqueidString + fileInfo.fileName
    val sink = FileIO.toPath(Paths.get(localPath) resolve filename)
    val writeResult = fileStream.runWith(sink)
    onSuccess(writeResult) { result =>
    result.status match {
    case Success(_) =>
    var excelPath = localPath + File.separator + uniqueidString + fileInfo.fileName
    var doc_count = itemsExcelParse(excelPath, companyCode, subCompanyId, userId)
    val currentTime2 = TimeObject.getCurrentTime()
    var upload_time = currentTime2 – currentTime
    val resp: JsValue = Json.toJson(doc_count)
    complete {
    val json: JsValue = Json.obj(“status” -> Constants.SUCCESS,
    “status_details” -> “null”, “upload_details” -> resp)
    HttpResponse(status = StatusCodes.OK, entity = HttpEntity(ContentType(MediaTypes.`application/json`), json.toString))
    }

    case Failure(e) =>
    complete {
    val json: JsValue = Json.obj(“status” -> Constants.ERROR)
    HttpResponse(status = StatusCodes.BandwidthLimitExceeded, entity = HttpEntity(ContentType(MediaTypes.`application/json`), json.toString))
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }

    am using test code like

    it should “be able to upload file” in {
    val file = new File(getClass.getResource(“E:\\Excel\\1122 gr.xls”).toString)
    val formData = Multipart.FormData.fromFile(“fileUpload”, ContentTypes.`application/octet-stream`, file, 100000)
    Post(s”/api/file-upload”, formData).withHeaders(oauthHeader) ~> createPORoute -> check {
    status shouldBe StatusCodes.OK
    responseAs[String] contains “File successfully uploaded”
    }
    }

    but am getting the issue

    [info] – should be able to upload file *** FAILED ***
    [info] java.lang.NullPointerException:
    [info] at ExcelfileServiceSpec$$anonfun$1.apply$mcV$sp(ExcelfileServiceSpec.scala:34)
    [info] at ExcelfileServiceSpec$$anonfun$1.apply(ExcelfileServiceSpec.scala:33)
    [info] at ExcelfileServiceSpec$$anonfun$1.apply(ExcelfileServiceSpec.scala:33)
    [info] at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
    [info] at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
    [info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    [info] at org.scalatest.Transformer.apply(Transformer.scala:22)
    [info] at org.scalatest.Transformer.apply(Transformer.scala:20)
    [info] at org.scalatest.FlatSpecLike$$anon$1.apply(FlatSpecLike.scala:1647)
    [info] at org.scalatest.Suite$class.withFixture(Suite.scala:1122)

    how can I resolve the above issue

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading