diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index afec2d78d035c90dd456120dfb07488cb39c12fe..8e9b37961b60a50193e31b6f101e75447abd77e4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -26,7 +26,6 @@ variables: # Improve performance with overlayfs. DOCKER_DRIVER: overlay2 # Publish reports to this collection by defining an environment variable before running Cucumber: - CUCUMBER_PUBLISH_TOKEN: 605dec34-3c89-47e8-8b3d-ebd7484e128a stages: # List of stages for jobs, and their order of execution - build @@ -41,11 +40,38 @@ build-job: # This job runs in the build stage, which runs first. - mvn compile - echo "Compile complete." -unit-test-job: # This job runs in the test stage. +prod-unit-test-job: # This job runs in the test stage. image: maven:3.8-openjdk-17 stage: test # It only starts when the job in the build stage completes successfully. + variables: + CUCUMBER_PUBLISH_TOKEN: 605dec34-3c89-47e8-8b3d-ebd7484e128a + only: + - main + script: + - echo "Running unit tests..." + - echo "$CUCUMBER_PUBLISH_TOKEN" + - mvn test + - cat target/site/jacoco/index.html + artifacts: + when: always + reports: + junit: + - target/surefire-reports/TEST-*.xml + - target/failsafe-reports/TEST-*.xml + paths: + - target/site/jacoco/index.html + - target/*.jar + +dev-unit-test-job: # This job runs in the test stage. + image: maven:3.8-openjdk-17 + stage: test # It only starts when the job in the build stage completes successfully. + variables: + CUCUMBER_PUBLISH_TOKEN: 43c12368-e346-460f-b5c4-f541e743b64b + only: + - dev script: - echo "Running unit tests..." + - echo "$CUCUMBER_PUBLISH_TOKEN" - mvn test - cat target/site/jacoco/index.html artifacts: diff --git a/README.md b/README.md index ff19c0b22268b02ec973954c1163db7f35b020a6..5604e7e98a6db4de93f285092d1aeab8c9f24052 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,13 @@ - [x] Documentação [https://springdoc.org/] - [ ] Consulta de apis externas com feign client - [ ] Enums -- [ ] Executar os testes de unidade e integração(cucumber) em apenas um comando (mvn test) +- [x] Executar os testes de unidade e integração(cucumber) em apenas um comando (mvn test) - [ ] Executar os testes de unidade e integração(cucumber) em comandos separados - [x] CD (continuous delivery) com heroku [https://www.heroku.com/home] - [ ] Adicionar instruções ao readme +- [ ] Logs centralizados +- [ ] Design pattern - strategy [https://refactoring.guru/pt-br/design-patterns/strategy/java/example] + diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000000000000000000000000000000000000..7a21e88040d4ef0658232b3e196ee4db630dc65e --- /dev/null +++ b/lombok.config @@ -0,0 +1 @@ +lombok.addLombokGeneratedAnnotation = true diff --git a/pom.xml b/pom.xml index 24731c12a40d40f3566ce36c0194718e03ac4d11..0dfb068b90b885e5fc57fe0275d0d376d0fadbff 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ 17 2021.0.0 1.16.2 - 7.2.2 + 7.2.3 3.0.0 3.0.0 3.1.2 @@ -99,20 +99,20 @@ io.cucumber - cucumber-junit + cucumber-junit-platform-engine ${cucumber.version} test io.cucumber - cucumber-junit-platform-engine + cucumber-spring ${cucumber.version} test - io.cucumber - cucumber-spring - ${cucumber.version} + org.junit.platform + junit-platform-suite + 1.8.2 test @@ -146,7 +146,7 @@ com.puppycrawl.tools checkstyle - 8.27 + 8.30 diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml new file mode 100644 index 0000000000000000000000000000000000000000..11348583a9c0815dad4ff8820c25915ca644f06f --- /dev/null +++ b/src/main/resources/application-prod.yml @@ -0,0 +1,15 @@ +spring: + datasource: + driverClassName: com.mysql.cj.jdbc.Driver + url: ${DATABASE_URL_PROD:jdbc:mysql://localhost:3306/riot_champion} + username: ${DATABASE_USERNAME_PROD:riot_champion} + password: ${DATABASE_PASSWORD_PROD:riot_champion} + jpa: + hibernate.ddl-auto: none + generate-ddl: false + show-sql: false + flyway: + enabled: true + +server: + port: 8083 \ No newline at end of file diff --git a/src/test/java/com/vitu/riot/champion/cucumber/CucumberRunnerTest.java b/src/test/java/com/vitu/riot/champion/cucumber/CucumberRunnerTest.java index fd0fcd23d4d41fb77dc3ca28653cb7923fd61c70..89f09aa31067f5dcd28696d49b1c9f7c8d63e03a 100644 --- a/src/test/java/com/vitu/riot/champion/cucumber/CucumberRunnerTest.java +++ b/src/test/java/com/vitu/riot/champion/cucumber/CucumberRunnerTest.java @@ -1,20 +1,25 @@ package com.vitu.riot.champion.cucumber; import com.vitu.riot.champion.AbstractDatabaseTest; -import io.cucumber.junit.Cucumber; -import io.cucumber.junit.CucumberOptions; import io.cucumber.spring.CucumberContextConfiguration; -import org.junit.runner.RunWith; +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; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; +import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME; + +@Suite @SpringBootTest @AutoConfigureMockMvc -@RunWith(Cucumber.class) +@IncludeEngines("cucumber") @CucumberContextConfiguration @ActiveProfiles(profiles = "test") -@CucumberOptions(plugin = {"pretty", "html:target/cucumber"}, features = "src/test/resources/features", publish = true) +@SelectClasspathResource("features") +@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.vitu.riot.champion.cucumber") public class CucumberRunnerTest extends AbstractDatabaseTest { } diff --git a/src/test/java/com/vitu/riot/champion/cucumber/steps/CriarCampeaoStepdefs.java b/src/test/java/com/vitu/riot/champion/cucumber/steps/CriarCampeaoStepdefs.java index 642ae6dcc88588e0a27a531628f9c1e91b8a5794..6bb6758d4abc1f0b91117f308b128cac93f2cb32 100644 --- a/src/test/java/com/vitu/riot/champion/cucumber/steps/CriarCampeaoStepdefs.java +++ b/src/test/java/com/vitu/riot/champion/cucumber/steps/CriarCampeaoStepdefs.java @@ -69,7 +69,7 @@ public class CriarCampeaoStepdefs extends BasicStepDefs { log.info(championDtos.toString()); - ChampionDto expectedBody = championDtos.stream().findFirst().get(); + ChampionDto expectedBody = championDtos.stream().findFirst().orElseThrow(); log.info("Expected body: {}", expectedBody); diff --git a/src/test/java/com/vitu/riot/champion/service/ChampionServiceTest.java b/src/test/java/com/vitu/riot/champion/service/ChampionServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e0b8af540b24e1fd123ec06ea08db02e3206fcff --- /dev/null +++ b/src/test/java/com/vitu/riot/champion/service/ChampionServiceTest.java @@ -0,0 +1,76 @@ +package com.vitu.riot.champion.service; + +import com.vitu.riot.champion.domain.Champion; +import com.vitu.riot.champion.repository.ChampionRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.List; + +@ExtendWith(SpringExtension.class) +class ChampionServiceTest { + + @Mock + ChampionRepository championRepository; + + ChampionService championService; + + @BeforeEach + void setUp() { + championService = new ChampionService(championRepository); + } + + @Test + void findAll() { + + Champion championOne = Champion.builder() + .name("draven") + .difficulty("alta") + .description("arrombado") + .build(); + + + Champion championTwo = Champion.builder() + .name("draven") + .difficulty("alta") + .description("arrombado") + .build(); + + List expectedChampions = List.of(championOne, championTwo); + + Mockito.when(championRepository.findAll()).thenReturn(expectedChampions); + + List champions = championService.findAll(); + + Assertions.assertEquals(expectedChampions, champions); + + } + + @Test + void save() { + Champion champion = Champion.builder() + .name("draven") + .difficulty("alta") + .description("arrombado") + .build(); + + Champion expectedChampion = Champion.builder() + .id(1L) + .name("draven") + .difficulty("alta") + .description("arrombado") + .build(); + + Mockito.when(championRepository.save(champion)).thenReturn(expectedChampion); + + Champion save = championService.save(champion); + + Assertions.assertNotNull(save.getId()); + Assertions.assertEquals(champion.getName(), save.getName()); + } +} diff --git a/src/test/resources/features/create_champion.feature b/src/test/resources/features/create_champion.feature index 0603493aee767e349d5ee0c8f7f19e964abf559a..d21dd19e6b7ed14decb381cfe0b5222cbfbbf870 100644 --- a/src/test/resources/features/create_champion.feature +++ b/src/test/resources/features/create_champion.feature @@ -1,5 +1,6 @@ #language:pt +@Done Funcionalidade: Criar campeão Cenário: Criar novo campeão com sucesso diff --git a/src/test/resources/junit-platform.properties b/src/test/resources/junit-platform.properties new file mode 100644 index 0000000000000000000000000000000000000000..044e7f92af4b5c21d68b880445f27f4f8fcea20e --- /dev/null +++ b/src/test/resources/junit-platform.properties @@ -0,0 +1 @@ +cucumber.publish.enabled=true