Skip to content

Commit

Permalink
complete test of Get product API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwell-balla committed Nov 24, 2024
1 parent 93ca36b commit d4f74d6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.maxwell.catalog_service.domain;

public class ProductNotFoundException extends RuntimeException {
public ProductNotFoundException(String message) {
super(message);
}
public static ProductNotFoundException forCode(String code) {
return new ProductNotFoundException("Product not found with code: " + code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.maxwell.catalog_service.web.exception;

import com.maxwell.catalog_service.domain.ProductNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.net.URI;
import java.time.Instant;

@RestControllerAdvice
class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
private static final URI NOT_FOUND_TYPE = URI.create("https://api.bookstore.com/errors/not-found");
private static final URI ISE_FOUND_TYPE = URI.create("https://api.bookstore.com/errors/server-error");
private static final String SERVICE_NAME = "catalog-service";

@ExceptionHandler(Exception.class)
ProblemDetail handleUnhandledException(Exception e) {
ProblemDetail problemDetail =
ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage());
problemDetail.setTitle("Internal Server Error");
problemDetail.setType(ISE_FOUND_TYPE);
problemDetail.setProperty("service", SERVICE_NAME);
problemDetail.setProperty("error_category", "Generic");
problemDetail.setProperty("timestamp", Instant.now());
return problemDetail;
}

@ExceptionHandler(ProductNotFoundException.class)
ProblemDetail handleProductNotFoundException(ProductNotFoundException e) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage());
problemDetail.setTitle("Product Not Found");
problemDetail.setType(NOT_FOUND_TYPE);
problemDetail.setProperty("service", SERVICE_NAME);
problemDetail.setProperty("error_category", "Generic");
problemDetail.setProperty("timestamp", Instant.now());
return problemDetail;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.maxwell.catalog_service.domain;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.jdbc.Sql;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.assertj.core.api.Assertions.assertThat;

@DataJpaTest
@Testcontainers
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Sql("/test-data.sql")
class ProductRepositoryTest {

@Container
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@Autowired
private ProductRepository productRepository;

@Test
void shouldGetProductByCode() {
ProductEntity product = productRepository.findByCode("P100").orElseThrow();
assertThat(product.getCode()).isEqualTo("P100");
}

@Test
void shouldReturnEmptyWhenProductCodeNotExists() {
assertThat(productRepository.findByCode("invalid_product_code")).isEmpty();
}
}

0 comments on commit d4f74d6

Please sign in to comment.