|
1 | 1 | package io.vertx.asyncsql.test
|
2 | 2 |
|
| 3 | +import java.nio.charset.StandardCharsets |
| 4 | + |
| 5 | +import org.vertx.java.core.Handler |
| 6 | +import org.vertx.java.core.json.impl.Base64 |
| 7 | +import org.vertx.scala.core.buffer.Buffer |
| 8 | + |
3 | 9 | import scala.concurrent.{Future, Promise}
|
4 | 10 | import org.vertx.scala.core.json.{JsonObject, Json, JsonArray}
|
5 | 11 | import org.vertx.testtools.VertxAssert._
|
@@ -455,10 +461,39 @@ trait BaseSqlTests {
|
455 | 461 | } yield {
|
456 | 462 | val receivedFields = reply.getArray("fields")
|
457 | 463 | assertEquals(Json.arr("test_date"), receivedFields)
|
458 |
| - logger.info("date is: " + reply.getArray("results").get[JsonArray](0).get[String](0)) |
459 |
| - assertEquals("2015-04-04T10:04:00.000", reply.getArray("results").get[JsonArray](0).get[String](0)) |
| 464 | + val date = reply.getArray("results").get[JsonArray](0).get[String](0) |
| 465 | + logger.info(s"date is: $date") |
| 466 | + assertEquals("2015-04-04T10:04:00.000", date) |
| 467 | + testComplete() |
| 468 | + }) recover failedTest |
| 469 | + |
| 470 | + @Test |
| 471 | + def blobUpload(): Unit = (for { |
| 472 | + image <- readFile("example.jpg") |
| 473 | + (msg, r0) <- sendOk(raw("DROP TABLE IF EXISTS blob_test")) |
| 474 | + (msg, r1) <- sendOk(raw(createBlobTable)) |
| 475 | + (msg, r2) <- sendOk(prepared("INSERT INTO blob_test (test_blob) VALUES (?)", Json.emptyArr().addBinary(image))) |
| 476 | + (msg, r3) <- sendOk(raw("SELECT test_blob FROM blob_test")) |
| 477 | + } yield { |
| 478 | + val receivedFields = r3.getArray("fields") |
| 479 | + assertEquals(Json.arr("test_blob"), receivedFields) |
| 480 | + logger.info(s"blob is: ${r3.getArray("results").get[JsonArray](0).get[Array[Byte]](0)}") |
| 481 | + val blob = r3.getArray("results").get[JsonArray](0).get[JsonArray](0).toArray.map(_.asInstanceOf[Byte]) |
| 482 | + val str = new String(Base64.decode(new String(blob))) |
| 483 | + logger.info(s"blob is2: ${blob.getClass}") |
| 484 | + assertEquals(new String(image), str) |
460 | 485 | testComplete()
|
461 | 486 | }) recover failedTest
|
462 | 487 |
|
| 488 | + private def readFile(file: String): Future[Array[Byte]] = { |
| 489 | + val p = Promise[Array[Byte]]() |
| 490 | + vertx.fileSystem.readFile(file, { |
| 491 | + case Success(buffer) => |
| 492 | + logger.info(s"read file buffer in ${StandardCharsets.UTF_8.name()} encoding") |
| 493 | + p.success(buffer.toString(StandardCharsets.UTF_8.name()).getBytes) |
| 494 | + case Failure(ex) => p.failure(ex) |
| 495 | + }: Try[Buffer] => Unit) |
| 496 | + p.future |
| 497 | + } |
463 | 498 | }
|
464 | 499 |
|
0 commit comments