Skip to content

Commit

Permalink
added the first test for mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
p-zalejko committed Oct 11, 2019
1 parent f1a5a1a commit e9fe16d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.gmail.pzalejko.myplanner.event;

import com.mongodb.client.MongoClient
import io.vertx.axle.core.Vertx
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import javax.inject.Inject
import javax.ws.rs.*
Expand All @@ -27,9 +28,10 @@ class EventController {
}

@POST
fun addNew(name: EventDto): CompletionStage<Void> {
fun addNew(name: EventDto): CompletionStage<String> {
val event = Event(name = name.name)
return service.addNew(event)
val addNew = service.addNew(event)
return addNew.thenCompose { d -> CompletableFuture.completedFuture("OK") }
}

class EventDto {
Expand Down
72 changes: 72 additions & 0 deletions src/test/kotlin/org/acme/quickstart/EventControllerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.acme.quickstart;

import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured.given
import org.hamcrest.CoreMatchers.`is`
import org.junit.jupiter.api.Test
import javax.ws.rs.core.MediaType
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import de.flapdoodle.embed.mongo.MongodExecutable
import de.flapdoodle.embed.mongo.MongodStarter
import de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder
import de.flapdoodle.embed.process.runtime.Network;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import org.hamcrest.CoreMatchers.hasItems

@QuarkusTest
open class EventControllerTest {

companion object {

@JvmStatic
private var MONGO: MongodExecutable? = null

@BeforeAll
@JvmStatic
internal fun beforeAll() {
val version = Version.Main.V4_0
val port = 27018
val config = MongodConfigBuilder()
.version(version)
.net(Net(port, Network.localhostIsIPv6()))
.cmdOptions(MongoCmdOptionsBuilder().useNoJournal(false).build())
.build()
MONGO = MongodStarter.getDefaultInstance().prepare(config)
MONGO!!.start()
}

@AfterAll
@JvmStatic
internal fun afterAll() {
MONGO!!.stop();
}
}

@Test
fun test_add() {
given()
.`when`().get("/events")
.then()
.statusCode(200)
.body("$.size()", `is`(0))

given()
.body("{\"name\": \"foo\"}")
.header("Content-Type", MediaType.APPLICATION_JSON)
.`when`()
.post("/events")
.then()
.statusCode(200)

given()
.`when`().get("/events")
.then()
.statusCode(200)
.body("", hasItems("foo"));

}

}
4 changes: 4 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Configuration file
# key = value
# configure the mongoDB client for a single instance on localhost
quarkus.mongodb.connection-string = mongodb://localhost:27018

0 comments on commit e9fe16d

Please sign in to comment.