-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
complete test of Get product API endpoint
- Loading branch information
1 parent
93ca36b
commit d4f74d6
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...og-service/src/main/java/com/maxwell/catalog_service/domain/ProductNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...rvice/src/main/java/com/maxwell/catalog_service/web/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
catalog-service/src/test/java/com/maxwell/catalog_service/domain/ProductRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |