Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blaz-cerpnjak committed Mar 20, 2024
1 parent 2e86346 commit 49f28d3
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 5 deletions.
1 change: 1 addition & 0 deletions RestaurantManagementAPI/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
testImplementation 'io.quarkus:quarkus-junit5'
implementation 'io.quarkus:quarkus-smallrye-openapi'
implementation 'org.jboss.logging:jboss-logging'
testImplementation 'io.rest-assured:rest-assured:4.3.3'
}

group 'com.blazc'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.blazc.model

import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntity
import org.bson.types.ObjectId

class Product: ReactivePanacheMongoEntity() {
class Product {
var id: ObjectId? = null
lateinit var restaurantId: ObjectId
lateinit var name: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.blazc.model

import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntity
import org.bson.types.ObjectId

class Restaurant: ReactivePanacheMongoEntity() {
class Restaurant {
var id: ObjectId? = null
lateinit var name: String
lateinit var address: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.bson.types.ObjectId
class ProductRepository: ReactivePanacheMongoRepository<Product> {

fun create(product: Product): Uni<Product> {
return persist(product)
return persist(product).onItem().transform { product }
}

fun getAllByRestaurantId(restaurantId: ObjectId): Uni<List<Product>> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.blazc

import com.blazc.model.Product
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured.given
import jakarta.json.Json
import org.bson.types.ObjectId
import org.hamcrest.CoreMatchers.`is`
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test


@QuarkusTest
class ProductControllerTest {

private val restaurantId: ObjectId = ObjectId.get()
private lateinit var createdProduct: Product

@BeforeEach
fun createProduct() {
val productJson = Json.createObjectBuilder()
.add("id", ObjectId.get().toString())
.add("restaurantId", restaurantId.toString())
.add("name", "Hamburger")
.add("price", 599)
.build()

val response = given()
.contentType("application/json")
.body(productJson.toString())
.`when`().post("/product")
.then()
.statusCode(200)
.contentType("application/json")
.body("name", `is`("Hamburger"))
.extract()
.response()

createdProduct = response.body.`as`(Product::class.java)
}

@Test
fun testUpdateProduct() {
val productJson = Json.createObjectBuilder()
.add("id", createdProduct.id.toString())
.add("restaurantId", createdProduct.restaurantId.toString())
.add("name", "Cheeseburger")
.add("price", 699)
.build()

given()
.contentType("application/json")
.body(productJson.toString())
.`when`().put("/product")
.then()
.statusCode(200)
.contentType("application/json")
.body("name", `is`("Cheeseburger"))
.body("price", `is`(699))
}

@Test
fun testGetProductById() {
given()
.contentType("application/json")
.`when`().get("/product/${createdProduct.id}")
.then()
.statusCode(200)
.contentType("application/json")
.body("id", `is`(createdProduct.id.toString()))
}

@Test
fun testDeleteProduct() {
given()
.`when`().delete("/product/${createdProduct.id}")
.then()
.statusCode(200)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.blazc

import com.blazc.model.Restaurant
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import jakarta.json.Json
import org.bson.types.ObjectId
import org.hamcrest.CoreMatchers
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

@QuarkusTest
class RestaurantControllerTest {

private lateinit var createdRestaurant: Restaurant

@BeforeEach
fun createRestaurant() {
val json = Json.createObjectBuilder()
.add("id", ObjectId.get().toString())
.add("name", "Burger King")
.add("address", "1234 Main St")
.add("contact", "555-555-5555")
.build()

val response = RestAssured.given()
.contentType("application/json")
.body(json.toString())
.`when`().post("/restaurant")
.then()
.statusCode(200)
.contentType("application/json")
.body("name", CoreMatchers.`is`("Burger King"))
.extract()
.response()

createdRestaurant = response.body.`as`(Restaurant::class.java)
}

@Test
fun testUpdateRestaurant() {
val productJson = Json.createObjectBuilder()
.add("id", createdRestaurant.id.toString())
.add("name", "McDonalds")
.add("address", "1234 Main St")
.add("contact", "555-555-5556")
.build()

RestAssured.given()
.contentType("application/json")
.body(productJson.toString())
.`when`().put("/restaurant")
.then()
.statusCode(200)
.contentType("application/json")
.body("contact", CoreMatchers.`is`("555-555-5556"))
}

@Test
fun testGetRestaurantById() {
RestAssured.given()
.contentType("application/json")
.`when`().get("/restaurant/${createdRestaurant.id}")
.then()
.statusCode(200)
.contentType("application/json")
.body("id", CoreMatchers.`is`(createdRestaurant.id.toString()))
}

@Test
fun testDeleteRestaurant() {
RestAssured.given()
.`when`().delete("/restaurant/${createdRestaurant.id}")
.then()
.statusCode(200)
}

}

0 comments on commit 49f28d3

Please sign in to comment.