Skip to content

Commit

Permalink
Feature/add category unit tests (#23)
Browse files Browse the repository at this point in the history
* bean validation get message improvements show field and message

* category unit tests

* Remove order status CONFIRMED

---------

Co-authored-by: Myller Sakaguchi Lobo <myller.lobo@omni.com.br>
  • Loading branch information
msakaguchi and Myller Sakaguchi Lobo authored Aug 5, 2024
1 parent 63b8716 commit f97be57
Show file tree
Hide file tree
Showing 26 changed files with 422 additions and 153 deletions.
55 changes: 8 additions & 47 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,16 @@
<spring-security-oauth2.version>2.2.4.RELEASE</spring-security-oauth2.version>
<spring-security-jwt.version>1.0.9.RELEASE</spring-security-jwt.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<jacoco.version>0.8.6</jacoco.version>
<jacoco.version>0.8.12</jacoco.version>
<fixture-factory.version>3.1.0</fixture-factory.version>
<org.reflections>0.9.10</org.reflections>
<com.thoughtworks.paranamer>2.8</com.thoughtworks.paranamer>
<lombok.version>1.18.22</lombok.version>
<springfox-swagger2.version>3.0.0</springfox-swagger2.version>

<embedded-redis.version>0.6</embedded-redis.version>
<redisson.version>3.13.1</redisson.version>
<jedis.version>3.3.0</jedis.version>
<epoll.version>4.1.50.Final</epoll.version>
<caelum.stella.version>2.1.5</caelum.stella.version>
<querydsl-apt.version>4.4.0</querydsl-apt.version>
<google.zxing.version>3.5.2</google.zxing.version>
<sonar.organization>jcmdsbr</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>


</properties>

<dependencies>
Expand Down Expand Up @@ -175,22 +168,6 @@
<version>${caelum.stella.version}</version>
</dependency>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>${querydsl-apt.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -199,35 +176,19 @@
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.2</version>
<version>${google.zxing.version}</version>
</dependency>

<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.2</version>
<version>${google.zxing.version}</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down Expand Up @@ -258,8 +219,8 @@
<version>${jacoco.version}</version>
<configuration>
<includes>
<include>com/fiap/tc/usecase/service/**/*</include>
<include>com/fiap/tc/usecase/*</include>
<include>com/fiap/tc/core/usecase/**/*</include>
<include>com/fiap/tc/adapter/repository/output/**</include>
</includes>
</configuration>
<executions>
Expand All @@ -269,8 +230,8 @@
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>verify</phase>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import java.util.UUID;

@Repository
public interface CategoryRepository extends JpaRepository<CategoryEntity, Integer> {
CategoryEntity findByNameOrDescription(String name, String description);
Optional<CategoryEntity> findByName(String name);

CategoryEntity findByUuid(UUID uuid);
Optional<CategoryEntity> findByUuid(UUID uuid);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.fiap.tc.adapter.repository.entity.OrderEntity;
import com.fiap.tc.adapter.repository.entity.OrderHistoricEntity;
import com.fiap.tc.adapter.repository.mapper.base.MapperEntity;
import com.fiap.tc.core.domain.model.Order;
import com.fiap.tc.core.domain.model.enums.OrderStatus;
import com.fiap.tc.core.domain.response.OrderListResponse;
import org.mapstruct.Mapper;
Expand All @@ -14,7 +13,6 @@
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.fiap.tc.common.constants.OrderConstants.WAIT_TIME_STATUS;

Expand Down Expand Up @@ -45,7 +43,7 @@ default Optional<LocalDateTime> getPreparingRegisterDate(OrderEntity orderEntity
List<LocalDateTime> preparingDates = orderEntity.getOrderHistoric().stream()
.filter(historic -> historic.getStatus().equals(OrderStatus.PREPARING))
.map(OrderHistoricEntity::getRegisterDate)
.collect(Collectors.toList());
.toList();

return preparingDates.stream().findFirst();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import static com.fiap.tc.adapter.repository.mapper.base.MapperConstants.CATEGORY_MAPPER;
import static java.lang.String.format;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

@Service
public class CategoryOutputAdapter implements SaveCategoryOutputPort, LoadCategoryOutputPort,
Expand All @@ -32,10 +30,8 @@ public CategoryOutputAdapter(CategoryRepository categoryPersistenceRepository) {

@Override
public void delete(UUID uuid) {
var category = categoryRepository.findByUuid(uuid);
if (nonNull(category)) {
categoryRepository.delete(category);
}
var categoryEntityOptional = categoryRepository.findByUuid(uuid);
categoryEntityOptional.ifPresent(categoryRepository::delete);
}

@Override
Expand All @@ -46,17 +42,18 @@ public Page<Category> list(Pageable pageable) {

@Override
public Category load(UUID uuid) {
var category = categoryRepository.findByUuid(uuid);
if (isNull(category)) {
var categoryEntityOptional = categoryRepository.findByUuid(uuid);
if (categoryEntityOptional.isEmpty()) {
throw new NotFoundException(format("Category with uuid %s not found!", uuid));
}
return CATEGORY_MAPPER.fromEntity(category);
return CATEGORY_MAPPER.fromEntity(categoryEntityOptional.get());
}

@Override
public Category saveOrUpdate(String name, String description, boolean active) {
var categoryEntity = categoryRepository.findByNameOrDescription(name, description);
if (nonNull(categoryEntity)) {
var categoryEntityOptional = categoryRepository.findByName(name);
if (categoryEntityOptional.isPresent()) {
var categoryEntity = categoryEntityOptional.get();
categoryEntity.setName(name);
categoryEntity.setDescription(description);
categoryEntity.getAudit().setActive(active);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,17 @@ public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
.secret(clientConfig.getSecret())
.scopes("read", "write")
.authorizedGrantTypes("password", "refresh_token")
.accessTokenValiditySeconds(86400)
.refreshTokenValiditySeconds(3600 * 24);
.accessTokenValiditySeconds(clientConfig.getTokenExpirationTime())
.refreshTokenValiditySeconds(clientConfig.getRefreshTokenExpirationTime());
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));

endpoints

.tokenStore(tokenStore())
endpoints.tokenStore(tokenStore())
.tokenEnhancer(tokenEnhancerChain)
.reuseRefreshTokens(false)
.userDetailsService(userDetailsService)
.authenticationManager(authenticationManager);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/fiap/tc/common/config/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public class ClientConfig {
private String name;
private String secret;
private String signKey;
private Integer tokenExpirationTime;
private Integer refreshTokenExpirationTime;
}
14 changes: 12 additions & 2 deletions src/main/java/com/fiap/tc/common/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package com.fiap.tc.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration
@EnableScheduling
public class WebConfig {

@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ public abstract class OrderStatusValidatorTemplate implements OrderStatusValidat
public void validate(OrderStatus status) {
var allowedNextStatus = next();
if (!allowedNextStatus.contains(status)) {
throw new BadRequestException(format("Status %s is a invalid next state!", status.name()));
throw new BadRequestException(format("Status %s is in a invalid next state!", status.name()));
}

log.info("status {} is valid!", status.name());

}

public abstract List<OrderStatus> next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
public class PendingOrderStatusValidator extends OrderStatusValidatorTemplate {
@Override
public List<OrderStatus> next() {
return List.of(OrderStatus.CONFIRMED, OrderStatus.PENDING, OrderStatus.CANCELED);
return List.of(OrderStatus.PREPARING, OrderStatus.PENDING, OrderStatus.CANCELED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
public class ReceivedOrderStatusValidator extends OrderStatusValidatorTemplate {
@Override
public List<OrderStatus> next() {
return List.of(OrderStatus.CONFIRMED, OrderStatus.PENDING, OrderStatus.CANCELED);
return List.of(OrderStatus.PREPARING, OrderStatus.PENDING, OrderStatus.CANCELED);
}
}
Loading

0 comments on commit f97be57

Please sign in to comment.