From bdbc0f68e22a1d26ae559435849aebdcade4fda1 Mon Sep 17 00:00:00 2001 From: Wellyson Freitas Date: Tue, 21 May 2024 02:07:03 +0200 Subject: [PATCH] Add BDD --- README.md | 8 ++ pom.xml | 63 +++++++++- .../adapter/gateway/ComponentGateway.kt | 2 - .../adapter/gateway/ProductGateway.kt | 2 - .../gateway/impl/ComponentGatewayImpl.kt | 4 - .../gateway/impl/ProductGatewayImpl.kt | 4 - src/test/kotlin/IntegrationTestFixtures.kt | 36 ------ .../stock/application}/TestAnnotations.kt | 10 +- .../fiap/stock/application}/TestFixtures.kt | 2 + .../controller/ComponentControllerTest.kt | 4 +- .../adapter/controller/MenuControllerTest.kt | 4 +- .../controller/ProductControllerTest.kt | 4 +- .../adapter/gateway/ComponentGatewayTest.kt | 15 +-- .../adapter/gateway/ProductGatewayTest.kt | 13 +- .../adapter/gateway/StockGatewayTest.kt | 4 +- .../fiap/stock/application/it/CommonSteps.kt | 32 +++++ .../stock/application/it/ComponentCreation.kt | 67 +++++++++++ .../stock/application/it/IntegrationTest.kt | 12 ++ .../application/it/IntegrationTestFixtures.kt | 32 +++++ .../it}/PostgreSQLContainerInitializer.kt | 8 +- .../stock/application/it/ProductCreation.kt | 111 ++++++++++++++++++ .../stock/application/it/RunCucumberIT.kt | 16 +++ .../services/ComponentServiceTest.kt | 4 +- .../services/ProductServiceTest.kt | 4 +- .../application/services/StockServiceTest.kt | 2 +- src/test/resources/features/Component.feature | 7 ++ src/test/resources/features/Product.feature | 7 ++ 27 files changed, 376 insertions(+), 101 deletions(-) delete mode 100644 src/test/kotlin/IntegrationTestFixtures.kt rename src/test/kotlin/{ => com/fiap/stock/application}/TestAnnotations.kt (63%) rename src/test/kotlin/{ => com/fiap/stock/application}/TestFixtures.kt (97%) create mode 100644 src/test/kotlin/com/fiap/stock/application/it/CommonSteps.kt create mode 100644 src/test/kotlin/com/fiap/stock/application/it/ComponentCreation.kt create mode 100644 src/test/kotlin/com/fiap/stock/application/it/IntegrationTest.kt create mode 100644 src/test/kotlin/com/fiap/stock/application/it/IntegrationTestFixtures.kt rename src/test/kotlin/{ => com/fiap/stock/application/it}/PostgreSQLContainerInitializer.kt (86%) create mode 100644 src/test/kotlin/com/fiap/stock/application/it/ProductCreation.kt create mode 100644 src/test/kotlin/com/fiap/stock/application/it/RunCucumberIT.kt create mode 100644 src/test/resources/features/Component.feature create mode 100644 src/test/resources/features/Product.feature diff --git a/README.md b/README.md index 46b5ae0..9c3b96a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,13 @@ # Microsserviço de estoque +### BDD + +Este microsserviço contém testes de integração implementados com Cucumber. + +### Análise estática + +Projeto no SonarCloud: [https://sonarcloud.io/project/overview?id=FIAP-3SOAT-G15_stock-api](https://sonarcloud.io/project/overview?id=FIAP-3SOAT-G15_stock-api) + [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=FIAP-3SOAT-G15_stock-api&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=FIAP-3SOAT-G15_stock-api) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=FIAP-3SOAT-G15_stock-api&metric=coverage)](https://sonarcloud.io/summary/new_code?id=FIAP-3SOAT-G15_stock-api) diff --git a/pom.xml b/pom.xml index 2cc0eca..e1e88aa 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,7 @@ 17 1.5.3.Final 1.19.1 + false true true fiap-3soat-g15 @@ -92,6 +93,36 @@ spring-boot-starter-test test + + io.cucumber + cucumber-java + test + + + io.cucumber + cucumber-junit + test + + + io.cucumber + cucumber-spring + test + + + org.junit.jupiter + junit-jupiter + test + + + org.junit.platform + junit-platform-suite + test + + + io.cucumber + cucumber-junit-platform-engine + test + org.springframework.boot spring-boot-starter-data-jpa @@ -143,6 +174,20 @@ pom import + + io.cucumber + cucumber-bom + 7.15.0 + pom + import + + + org.junit + junit-bom + 5.10.1 + pom + import + @@ -337,26 +382,32 @@ org.apache.maven.plugins maven-surefire-plugin + 3.2.5 - IntegrationTest + ${skipUnitTests} org.apache.maven.plugins maven-failsafe-plugin + 3.1.2 - - **/* - - IntegrationTest - ${skipITs} + + + cucumber.junit-platform.naming-strategy=long + + + integration-tests integration-test verify + + ${skipITs} + diff --git a/src/main/kotlin/com/fiap/stock/application/adapter/gateway/ComponentGateway.kt b/src/main/kotlin/com/fiap/stock/application/adapter/gateway/ComponentGateway.kt index 2a664c5..95a4e02 100644 --- a/src/main/kotlin/com/fiap/stock/application/adapter/gateway/ComponentGateway.kt +++ b/src/main/kotlin/com/fiap/stock/application/adapter/gateway/ComponentGateway.kt @@ -14,6 +14,4 @@ interface ComponentGateway { fun update(component: Component): Component fun delete(component: Component): Component - - fun deleteAll() } diff --git a/src/main/kotlin/com/fiap/stock/application/adapter/gateway/ProductGateway.kt b/src/main/kotlin/com/fiap/stock/application/adapter/gateway/ProductGateway.kt index e4601c2..cbed5bd 100644 --- a/src/main/kotlin/com/fiap/stock/application/adapter/gateway/ProductGateway.kt +++ b/src/main/kotlin/com/fiap/stock/application/adapter/gateway/ProductGateway.kt @@ -19,6 +19,4 @@ interface ProductGateway { fun update(product: Product): Product fun delete(productNumber: Long): Product - - fun deleteAll() } diff --git a/src/main/kotlin/com/fiap/stock/application/adapter/gateway/impl/ComponentGatewayImpl.kt b/src/main/kotlin/com/fiap/stock/application/adapter/gateway/impl/ComponentGatewayImpl.kt index ba729de..07e65bf 100644 --- a/src/main/kotlin/com/fiap/stock/application/adapter/gateway/impl/ComponentGatewayImpl.kt +++ b/src/main/kotlin/com/fiap/stock/application/adapter/gateway/impl/ComponentGatewayImpl.kt @@ -67,10 +67,6 @@ class ComponentGatewayImpl( return component } - override fun deleteAll() { - componentJpaRepository.deleteAll() - } - private fun persist(component: Component): Component = component .let(mapper::toEntity) diff --git a/src/main/kotlin/com/fiap/stock/application/adapter/gateway/impl/ProductGatewayImpl.kt b/src/main/kotlin/com/fiap/stock/application/adapter/gateway/impl/ProductGatewayImpl.kt index de80f04..2871f3f 100644 --- a/src/main/kotlin/com/fiap/stock/application/adapter/gateway/impl/ProductGatewayImpl.kt +++ b/src/main/kotlin/com/fiap/stock/application/adapter/gateway/impl/ProductGatewayImpl.kt @@ -58,10 +58,6 @@ class ProductGatewayImpl( ) } - override fun deleteAll() { - productJpaRepository.deleteAll() - } - private fun persist(product: Product): Product = product .let(mapper::toEntity) diff --git a/src/test/kotlin/IntegrationTestFixtures.kt b/src/test/kotlin/IntegrationTestFixtures.kt deleted file mode 100644 index 1715a8f..0000000 --- a/src/test/kotlin/IntegrationTestFixtures.kt +++ /dev/null @@ -1,36 +0,0 @@ -import com.fiap.stock.application.domain.valueobjects.ProductCategory -import com.fiap.stock.application.driver.web.request.ComponentRequest -import com.fiap.stock.application.driver.web.request.ProductRequest -import java.math.BigDecimal - -fun createProductRequest( - name: String = "Big Mac", - category: String = ProductCategory.MAIN.name, - price: BigDecimal = BigDecimal("10.00"), - description: String = "Dois hambúrgueres, alface, queijo, molho especial, cebola, picles, num pão com gergelim", - minSub: Int = 3, - maxSub: Int = 3, - components: List = listOf(1, 2, 3, 4, 5, 6, 7), -): ProductRequest { - return ProductRequest( - name = name, - category = category, - price = price, - description = description, - minSub = minSub, - maxSub = maxSub, - components = components, - ) -} - -fun createNewInputRequests(): List { - return listOf( - ComponentRequest("Hambúrguer", 100), - ComponentRequest("Alface", 100), - ComponentRequest("Queijo", 100), - ComponentRequest("Molho especial", 100), - ComponentRequest("Cebola", 100), - ComponentRequest("Picles", 100), - ComponentRequest("Pão com gergelim", 100), - ) -} diff --git a/src/test/kotlin/TestAnnotations.kt b/src/test/kotlin/com/fiap/stock/application/TestAnnotations.kt similarity index 63% rename from src/test/kotlin/TestAnnotations.kt rename to src/test/kotlin/com/fiap/stock/application/TestAnnotations.kt index 3f44809..0815c09 100644 --- a/src/test/kotlin/TestAnnotations.kt +++ b/src/test/kotlin/com/fiap/stock/application/TestAnnotations.kt @@ -1,13 +1,9 @@ -import org.junit.jupiter.api.Tag +package com.fiap.stock.application + +import com.fiap.stock.application.it.PostgreSQLContainerInitializer import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase -import org.springframework.test.context.ActiveProfiles import org.springframework.test.context.ContextConfiguration -@Tag("IntegrationTest") -@ActiveProfiles("test") -@Target(AnnotationTarget.CLASS, AnnotationTarget.FILE) -annotation class IntegrationTest - @ContextConfiguration(initializers = [PostgreSQLContainerInitializer::class]) @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @Target(AnnotationTarget.CLASS, AnnotationTarget.FILE) diff --git a/src/test/kotlin/TestFixtures.kt b/src/test/kotlin/com/fiap/stock/application/TestFixtures.kt similarity index 97% rename from src/test/kotlin/TestFixtures.kt rename to src/test/kotlin/com/fiap/stock/application/TestFixtures.kt index 11beee3..466800b 100644 --- a/src/test/kotlin/TestFixtures.kt +++ b/src/test/kotlin/com/fiap/stock/application/TestFixtures.kt @@ -1,3 +1,5 @@ +package com.fiap.stock.application + import com.fiap.stock.application.domain.entities.Component import com.fiap.stock.application.domain.entities.Product import com.fiap.stock.application.domain.entities.Stock diff --git a/src/test/kotlin/com/fiap/stock/application/adapter/controller/ComponentControllerTest.kt b/src/test/kotlin/com/fiap/stock/application/adapter/controller/ComponentControllerTest.kt index 9ade2b3..e2be7f4 100644 --- a/src/test/kotlin/com/fiap/stock/application/adapter/controller/ComponentControllerTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/adapter/controller/ComponentControllerTest.kt @@ -4,7 +4,7 @@ import com.fiap.stock.application.driver.web.request.ComponentRequest import com.fiap.stock.application.usecases.CreateComponentUseCase import com.fiap.stock.application.usecases.LoadComponentUseCase import com.fiap.stock.application.usecases.SearchComponentUseCase -import createComponent +import com.fiap.stock.application.createComponent import io.mockk.every import io.mockk.mockk import org.assertj.core.api.Assertions.assertThat @@ -101,4 +101,4 @@ class ComponentControllerTest { -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fiap/stock/application/adapter/controller/MenuControllerTest.kt b/src/test/kotlin/com/fiap/stock/application/adapter/controller/MenuControllerTest.kt index ebcbbb8..dcc6c31 100644 --- a/src/test/kotlin/com/fiap/stock/application/adapter/controller/MenuControllerTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/adapter/controller/MenuControllerTest.kt @@ -3,7 +3,7 @@ package com.fiap.stock.application.adapter.controller import com.fiap.stock.application.domain.valueobjects.ProductCategory import com.fiap.stock.application.usecases.LoadProductUseCase import com.fiap.stock.application.usecases.SearchProductUseCase -import createProduct +import com.fiap.stock.application.createProduct import io.mockk.every import io.mockk.mockk import io.mockk.unmockkAll @@ -106,4 +106,4 @@ class MenuControllerTest { } -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fiap/stock/application/adapter/controller/ProductControllerTest.kt b/src/test/kotlin/com/fiap/stock/application/adapter/controller/ProductControllerTest.kt index d3ef8a4..219c080 100644 --- a/src/test/kotlin/com/fiap/stock/application/adapter/controller/ProductControllerTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/adapter/controller/ProductControllerTest.kt @@ -7,8 +7,8 @@ import com.fiap.stock.application.usecases.AdjustStockUseCase import com.fiap.stock.application.usecases.AssembleProductsUseCase import com.fiap.stock.application.usecases.LoadProductUseCase import com.fiap.stock.application.usecases.SearchProductUseCase -import createProduct -import createProductRequest +import com.fiap.stock.application.createProduct +import com.fiap.stock.application.it.createProductRequest import io.mockk.every import io.mockk.mockk import org.assertj.core.api.Assertions.assertThat diff --git a/src/test/kotlin/com/fiap/stock/application/adapter/gateway/ComponentGatewayTest.kt b/src/test/kotlin/com/fiap/stock/application/adapter/gateway/ComponentGatewayTest.kt index 3860817..8328577 100644 --- a/src/test/kotlin/com/fiap/stock/application/adapter/gateway/ComponentGatewayTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/adapter/gateway/ComponentGatewayTest.kt @@ -4,7 +4,7 @@ import com.fiap.stock.application.adapter.gateway.impl.ComponentGatewayImpl import com.fiap.stock.application.domain.errors.SelfOrderManagementException import com.fiap.stock.application.driver.database.persistence.jpa.ComponentJpaRepository import com.fiap.stock.application.driver.database.persistence.mapper.ComponentMapper -import createComponent +import com.fiap.stock.application.createComponent import io.mockk.every import io.mockk.mockk import org.assertj.core.api.Assertions.assertThat @@ -106,17 +106,6 @@ class ComponentGatewayTest { assertThat(result).isNotNull() } - - @Test - fun `deleteAll should return nothing`() { - - every { componentJpaRepository.deleteAll() } returns Unit - - gateway.deleteAll() - } - - - } @Nested @@ -170,4 +159,4 @@ class ComponentGatewayTest { } -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fiap/stock/application/adapter/gateway/ProductGatewayTest.kt b/src/test/kotlin/com/fiap/stock/application/adapter/gateway/ProductGatewayTest.kt index f33e138..448308c 100644 --- a/src/test/kotlin/com/fiap/stock/application/adapter/gateway/ProductGatewayTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/adapter/gateway/ProductGatewayTest.kt @@ -5,7 +5,7 @@ import com.fiap.stock.application.domain.errors.SelfOrderManagementException import com.fiap.stock.application.domain.valueobjects.ProductCategory import com.fiap.stock.application.driver.database.persistence.jpa.ProductJpaRepository import com.fiap.stock.application.driver.database.persistence.mapper.ProductMapper -import createProduct +import com.fiap.stock.application.createProduct import io.mockk.every import io.mockk.mockk import org.assertj.core.api.Assertions.assertThat @@ -118,15 +118,6 @@ class ProductGatewayTest { assertThat(response).isNotNull() } - - @Test - fun `deleteAll should return nothing`() { - - every { productJpaRepository.deleteAll() } returns Unit - - gateway.deleteAll() - } - } @Nested @@ -191,4 +182,4 @@ class ProductGatewayTest { } -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fiap/stock/application/adapter/gateway/StockGatewayTest.kt b/src/test/kotlin/com/fiap/stock/application/adapter/gateway/StockGatewayTest.kt index 749015c..718b243 100644 --- a/src/test/kotlin/com/fiap/stock/application/adapter/gateway/StockGatewayTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/adapter/gateway/StockGatewayTest.kt @@ -4,7 +4,7 @@ import com.fiap.stock.application.adapter.gateway.impl.StockGatewayImpl import com.fiap.stock.application.domain.errors.SelfOrderManagementException import com.fiap.stock.application.driver.database.persistence.jpa.StockJpaRepository import com.fiap.stock.application.driver.database.persistence.mapper.StockMapper -import createStock +import com.fiap.stock.application.createStock import io.mockk.every import io.mockk.mockk import org.assertj.core.api.Assertions.assertThat @@ -106,4 +106,4 @@ class StockGatewayTest { } -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/fiap/stock/application/it/CommonSteps.kt b/src/test/kotlin/com/fiap/stock/application/it/CommonSteps.kt new file mode 100644 index 0000000..eea1f31 --- /dev/null +++ b/src/test/kotlin/com/fiap/stock/application/it/CommonSteps.kt @@ -0,0 +1,32 @@ +package com.fiap.stock.application.it + +import io.cucumber.java.Before +import io.restassured.RestAssured +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.web.server.LocalServerPort +import org.springframework.jdbc.core.JdbcTemplate + +class CommonSteps: IntegrationTest() { + + @Autowired + lateinit var jdbcTemplate: JdbcTemplate + + @LocalServerPort + private val port: Int? = null + + @Before + fun setup() { + RestAssured.baseURI = "http://localhost:$port" + RestAssured.enableLoggingOfRequestAndResponseIfValidationFails() + } + + @Before("@database") + fun setupDatabase() { + jdbcTemplate.execute(""" + TRUNCATE TABLE product RESTART IDENTITY CASCADE; + TRUNCATE TABLE stock RESTART IDENTITY; + TRUNCATE TABLE component RESTART IDENTITY CASCADE; + """.trimIndent() + ) + } +} diff --git a/src/test/kotlin/com/fiap/stock/application/it/ComponentCreation.kt b/src/test/kotlin/com/fiap/stock/application/it/ComponentCreation.kt new file mode 100644 index 0000000..da8726b --- /dev/null +++ b/src/test/kotlin/com/fiap/stock/application/it/ComponentCreation.kt @@ -0,0 +1,67 @@ +package com.fiap.stock.application.it + +import com.fiap.stock.application.adapter.gateway.ComponentGateway +import com.fiap.stock.application.adapter.gateway.StockGateway +import com.fiap.stock.application.domain.entities.Component +import com.fiap.stock.application.domain.entities.Stock +import com.fiap.stock.application.driver.web.request.ComponentRequest +import io.cucumber.java.en.Given +import io.cucumber.java.en.Then +import io.cucumber.java.en.When +import io.restassured.RestAssured.given +import io.restassured.http.ContentType +import io.restassured.response.Response +import org.assertj.core.api.Assertions.assertThat +import org.hamcrest.Matchers.equalTo +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.http.HttpStatus + +class ComponentCreation: IntegrationTest() { + + @Autowired + lateinit var componentRepository: ComponentGateway + + @Autowired + lateinit var stockRepository: StockGateway + + private lateinit var componentRequest: ComponentRequest + private lateinit var response: Response + + @Given("valid data for product component") + fun validDataForProductComponent() { + componentRequest = createComponentRequest() + } + + @When("request to create product component") + fun requestToCreateProductComponent() { + response = given() + .contentType(ContentType.JSON) + .body(componentRequest) + .`when`() + .post("/admin/components") + } + + @Then("product component should be created") + fun productComponentShouldBeCreated() { + val persistedProduct = componentRepository.findAll()[0] + + assertThat(persistedProduct).isEqualTo(Component( + number = 1, + name = componentRequest.name, + )) + + response.then() + .statusCode(HttpStatus.OK.value()) + .body( + "number", equalTo(persistedProduct.number!!.toInt()), + "name", equalTo(componentRequest.name), + ) + + val stock = stockRepository.findByComponentNumber(persistedProduct.number as Long) + + assertThat(stock).isEqualTo(Stock( + componentNumber = persistedProduct.number as Long, + quantity = componentRequest.initialQuantity, + )) + } +} diff --git a/src/test/kotlin/com/fiap/stock/application/it/IntegrationTest.kt b/src/test/kotlin/com/fiap/stock/application/it/IntegrationTest.kt new file mode 100644 index 0000000..0c24224 --- /dev/null +++ b/src/test/kotlin/com/fiap/stock/application/it/IntegrationTest.kt @@ -0,0 +1,12 @@ +package com.fiap.stock.application.it + +import com.fiap.stock.application.WithPostgreSQL +import io.cucumber.spring.CucumberContextConfiguration +import org.junit.jupiter.api.Tag +import org.springframework.boot.test.context.SpringBootTest + +@CucumberContextConfiguration +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@WithPostgreSQL +@Tag("IntegrationTest") +class IntegrationTest diff --git a/src/test/kotlin/com/fiap/stock/application/it/IntegrationTestFixtures.kt b/src/test/kotlin/com/fiap/stock/application/it/IntegrationTestFixtures.kt new file mode 100644 index 0000000..c502ec5 --- /dev/null +++ b/src/test/kotlin/com/fiap/stock/application/it/IntegrationTestFixtures.kt @@ -0,0 +1,32 @@ +package com.fiap.stock.application.it + +import com.fiap.stock.application.domain.valueobjects.ProductCategory +import com.fiap.stock.application.driver.web.request.ComponentRequest +import com.fiap.stock.application.driver.web.request.ProductRequest +import java.math.BigDecimal + +fun createComponentRequest( + name: String = "Hamburger", + initialQuantity: Long = 100L, +) = ComponentRequest( + name = name, + initialQuantity = initialQuantity, +) + +fun createProductRequest( + name: String = "Big Mac", + category: String = ProductCategory.MAIN.name, + price: BigDecimal = BigDecimal("10.00"), + description: String = "Dois hambúrgueres, alface, queijo, molho especial, cebola, picles, num pão com gergelim", + minSub: Int = 3, + maxSub: Int = 3, + components: List = listOf(1, 2, 3, 4, 5, 6, 7), +) = ProductRequest( + name = name, + category = category, + price = price, + description = description, + minSub = minSub, + maxSub = maxSub, + components = components, +) diff --git a/src/test/kotlin/PostgreSQLContainerInitializer.kt b/src/test/kotlin/com/fiap/stock/application/it/PostgreSQLContainerInitializer.kt similarity index 86% rename from src/test/kotlin/PostgreSQLContainerInitializer.kt rename to src/test/kotlin/com/fiap/stock/application/it/PostgreSQLContainerInitializer.kt index b9eee55..4858fab 100644 --- a/src/test/kotlin/PostgreSQLContainerInitializer.kt +++ b/src/test/kotlin/com/fiap/stock/application/it/PostgreSQLContainerInitializer.kt @@ -1,3 +1,5 @@ +package com.fiap.stock.application.it + import org.springframework.boot.test.util.TestPropertyValues import org.springframework.context.ApplicationContextInitializer import org.springframework.context.ConfigurableApplicationContext @@ -10,9 +12,9 @@ class PostgreSQLContainerInitializer : companion object { private val instance: PostgreSQLContainerInitializer = PostgreSQLContainerInitializer() - .withDatabaseName("selforder") - .withUsername("selforder") - .withPassword("self@Order123!") + .withDatabaseName("stockdb") + .withUsername("stock") + .withPassword("stock") .waitingFor(forListeningPort()) } diff --git a/src/test/kotlin/com/fiap/stock/application/it/ProductCreation.kt b/src/test/kotlin/com/fiap/stock/application/it/ProductCreation.kt new file mode 100644 index 0000000..4e4f7aa --- /dev/null +++ b/src/test/kotlin/com/fiap/stock/application/it/ProductCreation.kt @@ -0,0 +1,111 @@ +package com.fiap.stock.application.it + +import com.fiap.stock.application.adapter.gateway.ComponentGateway +import com.fiap.stock.application.adapter.gateway.ProductGateway +import com.fiap.stock.application.adapter.gateway.StockGateway +import com.fiap.stock.application.domain.entities.Component +import com.fiap.stock.application.domain.entities.Product +import com.fiap.stock.application.domain.entities.Stock +import com.fiap.stock.application.domain.valueobjects.ProductCategory +import com.fiap.stock.application.driver.web.request.ComponentRequest +import com.fiap.stock.application.driver.web.request.ProductRequest +import io.cucumber.java.en.Given +import io.cucumber.java.en.Then +import io.cucumber.java.en.When +import io.restassured.RestAssured.given +import io.restassured.http.ContentType +import io.restassured.response.Response +import org.assertj.core.api.Assertions.assertThat +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.Matchers.hasSize +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.http.HttpStatus + +class ProductCreation: IntegrationTest() { + + @Autowired + lateinit var productRepository: ProductGateway + + @Autowired + lateinit var componentRepository: ComponentGateway + + @Autowired + lateinit var stockRepository: StockGateway + + private lateinit var productRequest: ProductRequest + private lateinit var response: Response + private lateinit var componentRequests: List + + @Given("valid data for product") + fun validDataForProduct() { + componentRequests = listOf( + ComponentRequest("Hambúrguer", 100), + ComponentRequest("Alface", 100), + ComponentRequest("Queijo", 100), + ComponentRequest("Molho especial", 100), + ComponentRequest("Cebola", 100), + ComponentRequest("Picles", 100), + ComponentRequest("Pão com gergelim", 100), + ) + + productRequest = createProductRequest( + components = persistComponents() + ) + } + + @When("request to create product") + fun requestToCreateProduct() { + response = given() + .contentType(ContentType.JSON) + .body(productRequest) + .`when`() + .post("/admin/products") + } + + @Then("product should be created") + fun productShouldBeCreated() { + val persistedProduct = productRepository.findAll()[0] + + assertThat(persistedProduct).isEqualTo(Product( + number = 1, + name = productRequest.name, + price = productRequest.price, + description = productRequest.description, + category = ProductCategory.fromString(productRequest.category), + minSub = productRequest.minSub, + maxSub = productRequest.maxSub, + subItems = emptyList(), + components = componentRequests.mapIndexed { index, request -> + Component(number = index + 1L, name = request.name) + }, + )) + + response + .then() + .statusCode(HttpStatus.OK.value()) + .body( + "number", equalTo(productRepository.findAll()[0].number!!.toInt()), + "name", equalTo(productRequest.name), + "price", equalTo(productRequest.price.toString()), + "description", equalTo(productRequest.description), + "category", equalTo(productRequest.category), + "minSub", equalTo(productRequest.minSub), + "maxSub", equalTo(productRequest.maxSub), + "subItems", hasSize(0), + "components", hasSize(7), + ) + } + + private fun persistComponents(): List { + return componentRequests.map { componentRequest -> + val savedComponent = componentRepository.create(Component(name = componentRequest.name)) + stockRepository.create( + Stock( + componentNumber = savedComponent.number!!, + quantity = componentRequest.initialQuantity, + ), + ) + savedComponent + }.map { it.number!! } + } +} diff --git a/src/test/kotlin/com/fiap/stock/application/it/RunCucumberIT.kt b/src/test/kotlin/com/fiap/stock/application/it/RunCucumberIT.kt new file mode 100644 index 0000000..a0aa05c --- /dev/null +++ b/src/test/kotlin/com/fiap/stock/application/it/RunCucumberIT.kt @@ -0,0 +1,16 @@ +package com.fiap.stock.application.it + +import io.cucumber.junit.platform.engine.Constants +import org.junit.platform.suite.api.ConfigurationParameter +import org.junit.platform.suite.api.IncludeEngines +import org.junit.platform.suite.api.SelectClasspathResource +import org.junit.platform.suite.api.Suite + +@Suite +@IncludeEngines("cucumber") +@SelectClasspathResource("features") +@ConfigurationParameter(key = Constants.GLUE_PROPERTY_NAME, value = "com.fiap.stock.application.it") +@ConfigurationParameter(key = Constants.PLUGIN_PROPERTY_NAME, value = "pretty") +@ConfigurationParameter(key = Constants.PLUGIN_PUBLISH_QUIET_PROPERTY_NAME, value = "true") +@ConfigurationParameter(key = Constants.JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, value = "long") +class RunCucumberIT diff --git a/src/test/kotlin/com/fiap/stock/application/services/ComponentServiceTest.kt b/src/test/kotlin/com/fiap/stock/application/services/ComponentServiceTest.kt index 01a7b3f..526e3f3 100644 --- a/src/test/kotlin/com/fiap/stock/application/services/ComponentServiceTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/services/ComponentServiceTest.kt @@ -5,8 +5,8 @@ import com.fiap.stock.application.adapter.gateway.ProductGateway import com.fiap.stock.application.adapter.gateway.StockGateway import com.fiap.stock.application.domain.errors.ErrorType import com.fiap.stock.application.domain.errors.SelfOrderManagementException -import createComponent -import createProduct +import com.fiap.stock.application.createComponent +import com.fiap.stock.application.createProduct import io.mockk.every import io.mockk.mockk import io.mockk.unmockkAll diff --git a/src/test/kotlin/com/fiap/stock/application/services/ProductServiceTest.kt b/src/test/kotlin/com/fiap/stock/application/services/ProductServiceTest.kt index 54327c5..aa8adef 100644 --- a/src/test/kotlin/com/fiap/stock/application/services/ProductServiceTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/services/ProductServiceTest.kt @@ -4,8 +4,8 @@ import com.fiap.stock.application.adapter.gateway.ProductGateway import com.fiap.stock.application.domain.errors.ErrorType import com.fiap.stock.application.domain.errors.SelfOrderManagementException import com.fiap.stock.application.usecases.LoadComponentUseCase -import createComponent -import createProduct +import com.fiap.stock.application.createComponent +import com.fiap.stock.application.createProduct import io.mockk.every import io.mockk.mockk import io.mockk.unmockkAll diff --git a/src/test/kotlin/com/fiap/stock/application/services/StockServiceTest.kt b/src/test/kotlin/com/fiap/stock/application/services/StockServiceTest.kt index d96aac4..ddbb5da 100644 --- a/src/test/kotlin/com/fiap/stock/application/services/StockServiceTest.kt +++ b/src/test/kotlin/com/fiap/stock/application/services/StockServiceTest.kt @@ -5,7 +5,7 @@ import com.fiap.stock.application.adapter.gateway.TransactionalGateway import com.fiap.stock.application.domain.errors.ErrorType import com.fiap.stock.application.domain.errors.SelfOrderManagementException import com.fiap.stock.application.usecases.LoadProductUseCase -import createStock +import com.fiap.stock.application.createStock import io.mockk.every import io.mockk.mockk import io.mockk.unmockkAll diff --git a/src/test/resources/features/Component.feature b/src/test/resources/features/Component.feature new file mode 100644 index 0000000..912c6d5 --- /dev/null +++ b/src/test/resources/features/Component.feature @@ -0,0 +1,7 @@ +Feature: Component + + @database + Scenario: Creation of product component + Given valid data for product component + When request to create product component + Then product component should be created diff --git a/src/test/resources/features/Product.feature b/src/test/resources/features/Product.feature new file mode 100644 index 0000000..d26da1a --- /dev/null +++ b/src/test/resources/features/Product.feature @@ -0,0 +1,7 @@ +Feature: Product + + @database + Scenario: Creation of product + Given valid data for product + When request to create product + Then product should be created