Skip to content

Commit

Permalink
Add BDD
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyfrs committed May 21, 2024
1 parent 6490c59 commit b8af149
Show file tree
Hide file tree
Showing 27 changed files with 368 additions and 101 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Microsserviço de estoque

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)

Expand Down
63 changes: 57 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
<testcontainers.version>1.19.1</testcontainers.version>
<skipUnitTests>false</skipUnitTests>
<skipITs>true</skipITs>
<skipOpenAPIGen>true</skipOpenAPIGen>
<sonar.organization>fiap-3soat-g15</sonar.organization>
Expand Down Expand Up @@ -92,6 +93,36 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down Expand Up @@ -143,6 +174,20 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-bom</artifactId>
<version>7.15.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
Expand Down Expand Up @@ -337,26 +382,32 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<excludedGroups>IntegrationTest</excludedGroups>
<skipTests>${skipUnitTests}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<includes>
<include>**/*</include>
</includes>
<groups>IntegrationTest</groups>
<skipITs>${skipITs}</skipITs>
<properties>
<configurationParameters>
cucumber.junit-platform.naming-strategy=long
</configurationParameters>
</properties>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skipITs}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ interface ComponentGateway {
fun update(component: Component): Component

fun delete(component: Component): Component

fun deleteAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ interface ProductGateway {
fun update(product: Product): Product

fun delete(productNumber: Long): Product

fun deleteAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ class ComponentGatewayImpl(
return component
}

override fun deleteAll() {
componentJpaRepository.deleteAll()
}

private fun persist(component: Component): Component =
component
.let(mapper::toEntity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ class ProductGatewayImpl(
)
}

override fun deleteAll() {
productJpaRepository.deleteAll()
}

private fun persist(product: Product): Product =
product
.let(mapper::toEntity)
Expand Down
36 changes: 0 additions & 36 deletions src/test/kotlin/IntegrationTestFixtures.kt

This file was deleted.

32 changes: 32 additions & 0 deletions src/test/kotlin/com/fiap/stock/application/CommonSteps.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fiap.stock.application

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()
)
}
}
67 changes: 67 additions & 0 deletions src/test/kotlin/com/fiap/stock/application/ComponentCreation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.fiap.stock.application

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,
))
}
}
11 changes: 11 additions & 0 deletions src/test/kotlin/com/fiap/stock/application/IntegrationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fiap.stock.application

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fiap.stock.application

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<Long> = listOf(1, 2, 3, 4, 5, 6, 7),
) = ProductRequest(
name = name,
category = category,
price = price,
description = description,
minSub = minSub,
maxSub = maxSub,
components = components,
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.fiap.stock.application

import org.springframework.boot.test.util.TestPropertyValues
import org.springframework.context.ApplicationContextInitializer
import org.springframework.context.ConfigurableApplicationContext
Expand All @@ -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())
}

Expand Down
Loading

0 comments on commit b8af149

Please sign in to comment.