Skip to content

Commit

Permalink
feat : Transition from MockMvc to RestAssured for integration testing…
Browse files Browse the repository at this point in the history
… & remove lombok (#1193)

* feat : use restAssured and remove lombok

* adds scope test
  • Loading branch information
rajadilipkolli authored Apr 13, 2024
1 parent 224d8f5 commit c78a978
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 169 deletions.
34 changes: 18 additions & 16 deletions jpa/boot-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand Down Expand Up @@ -100,6 +84,19 @@
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -120,6 +117,11 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,72 @@
package com.example.envers.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

@Data
@ConfigurationProperties("application")
public class ApplicationProperties {

@NestedConfigurationProperty
private Cors cors = new Cors();

@Data
public Cors getCors() {
return cors;
}

public void setCors(Cors cors) {
this.cors = cors;
}

public static class Cors {
private String pathPattern = "/api/**";
private String allowedMethods = "*";
private String allowedHeaders = "*";
private String allowedOriginPatterns = "*";
private boolean allowCredentials = true;

public String getPathPattern() {
return pathPattern;
}

public Cors setPathPattern(String pathPattern) {
this.pathPattern = pathPattern;
return this;
}

public String getAllowedMethods() {
return allowedMethods;
}

public Cors setAllowedMethods(String allowedMethods) {
this.allowedMethods = allowedMethods;
return this;
}

public String getAllowedHeaders() {
return allowedHeaders;
}

public Cors setAllowedHeaders(String allowedHeaders) {
this.allowedHeaders = allowedHeaders;
return this;
}

public String getAllowedOriginPatterns() {
return allowedOriginPatterns;
}

public Cors setAllowedOriginPatterns(String allowedOriginPatterns) {
this.allowedOriginPatterns = allowedOriginPatterns;
return this;
}

public boolean isAllowCredentials() {
return allowCredentials;
}

public Cors setAllowCredentials(boolean allowCredentials) {
this.allowCredentials = allowCredentials;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

import com.example.envers.entities.Customer;
import com.example.envers.repositories.CustomerRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.history.Revision;
import org.springframework.data.history.Revisions;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class Initializer implements CommandLineRunner {

private static final Logger LOGGER = LoggerFactory.getLogger(Initializer.class);

private final CustomerRepository customerRepository;

public Initializer(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

@Override
public void run(String... args) {
log.info("Running Initializer.....");
LOGGER.info("Running Initializer.....");
Customer customerObj = new Customer();
customerObj.setName("customerName");
customerObj.setAddress("customerAddress");
Expand All @@ -29,10 +33,10 @@ public void run(String... args) {
this.customerRepository.save(updatedCustomer);

Revisions<Integer, Customer> revisions = this.customerRepository.findRevisions(persistedCustomer.getId());
log.info("revisions ");
LOGGER.info("revisions ");
for (Revision<Integer, Customer> content : revisions.getContent()) {
log.info("Revision History Metadata :{}", content.getMetadata());
log.info("Revision History Entry :{}", content.getEntity());
LOGGER.info("Revision History Metadata :{}", content.getMetadata());
LOGGER.info("Revision History Entry :{}", content.getEntity());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.example.envers.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {

private final ApplicationProperties properties;

public WebMvcConfig(ApplicationProperties properties) {
this.properties = properties;
}

@Override
public void addCorsMappings(CorsRegistry registry) {
public void addCorsMappings(@NonNull CorsRegistry registry) {
ApplicationProperties.Cors propertiesCors = properties.getCors();
registry.addMapping(propertiesCors.getPathPattern())
.allowedMethods(propertiesCors.getAllowedMethods())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@Component
public class LoggingAspect {

private final Logger log = LoggerFactory.getLogger(this.getClass());
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingAspect.class);

private final Environment env;

Expand Down Expand Up @@ -47,7 +47,7 @@ public void applicationPackagePointcut() {
@AfterThrowing(pointcut = "applicationPackagePointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
if (env.acceptsProfiles(Profiles.of(AppConstants.PROFILE_NOT_PROD))) {
log.error(
LOGGER.error(
"Exception in {}.{}() with cause = '{}' and exception = '{}'",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
Expand All @@ -56,7 +56,7 @@ public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
e);

} else {
log.error(
LOGGER.error(
"Exception in {}.{}() with cause = {}",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
Expand All @@ -66,17 +66,17 @@ public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {

@Around("applicationPackagePointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
if (log.isTraceEnabled()) {
log.trace(
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(
"Enter: {}.{}()",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName());
}
long start = System.currentTimeMillis();
Object result = joinPoint.proceed();
long end = System.currentTimeMillis();
if (log.isTraceEnabled()) {
log.trace(
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(
"Exit: {}.{}(). Time taken: {} millis",
joinPoint.getSignature().getDeclaringTypeName(),
joinPoint.getSignature().getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@
import jakarta.persistence.Table;
import jakarta.persistence.Version;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.StringJoiner;
import org.hibernate.Hibernate;
import org.hibernate.envers.Audited;

@Entity
@Table(name = "customers")
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Audited
@ToString
public class Customer {

@Id
Expand All @@ -36,21 +29,37 @@ public class Customer {
@Version
Short version;

public Long getId() {
return id;
}

public Customer setId(Long id) {
this.id = id;
return this;
}

public String getName() {
return name;
}

public Customer setName(String name) {
this.name = name;
return this;
}

public String getAddress() {
return address;
}

public Customer setAddress(String address) {
this.address = address;
return this;
}

public Short getVersion() {
return version;
}

public Customer setVersion(Short version) {
this.version = version;
return this;
Expand All @@ -68,4 +77,14 @@ public boolean equals(Object o) {
public int hashCode() {
return getClass().hashCode();
}

@Override
public String toString() {
return new StringJoiner(", ", Customer.class.getSimpleName() + "[", "]")
.add("id=" + id)
.add("name='" + name + "'")
.add("address='" + address + "'")
.add("version=" + version)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -25,13 +24,21 @@

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class CustomerService {

private final CustomerRepository customerRepository;
private final CustomerRevisionToRevisionResultMapper customerRevisionToRevisionDTOMapper;
private final CustomerMapper customerMapper;

public CustomerService(
CustomerRepository customerRepository,
CustomerRevisionToRevisionResultMapper customerRevisionToRevisionDTOMapper,
CustomerMapper customerMapper) {
this.customerRepository = customerRepository;
this.customerRevisionToRevisionDTOMapper = customerRevisionToRevisionDTOMapper;
this.customerMapper = customerMapper;
}

public PagedResult<CustomerResponse> findAllCustomers(FindCustomersQuery findCustomersQuery) {

// create Pageable instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public final class AppConstants {
public static final String PROFILE_PROD = "prod";
public static final String PROFILE_NOT_PROD = "!" + PROFILE_PROD;
public static final String PROFILE_TEST = "test";
public static final String PROFILE_NOT_TEST = "!" + PROFILE_TEST;

public static final String DEFAULT_PAGE_NUMBER = "0";
public static final String DEFAULT_PAGE_SIZE = "10";
public static final String DEFAULT_SORT_BY = "id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import jakarta.validation.Valid;
import java.net.URI;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand All @@ -29,11 +28,14 @@

@RestController
@RequestMapping("/api/customers")
@RequiredArgsConstructor
public class CustomerController {

private final CustomerService customerService;

public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}

@GetMapping
public PagedResult<CustomerResponse> getAllCustomers(
@RequestParam(defaultValue = AppConstants.DEFAULT_PAGE_NUMBER, required = false) int pageNo,
Expand Down
Loading

0 comments on commit c78a978

Please sign in to comment.