Skip to content

Commit a785128

Browse files
committed
added integration test for product controller and unit test for product repository when product code exists and when the code is invalid
1 parent 32b461a commit a785128

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

.github/workflows/catalog-service.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ jobs:
2828
java-version: '21'
2929
cache: 'maven'
3030

31+
- name: Applying Spotless Plugin
32+
run: ./mvnw spotless:apply
33+
3134
- name: Make Maven wrapper executable
3235
run: chmod +x ./mvnw
3336

catalog-service/src/test/java/com/eyepatch/works/catalogservice/domain/ProductRepositoryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
import java.math.BigDecimal;
56
import java.util.List;
67
import org.junit.jupiter.api.Test;
78
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,4 +26,18 @@ void shouldGetAllProducts() {
2526
List<ProductEntity> productList = productRepository.findAll();
2627
assertThat(productList).hasSize(15);
2728
}
29+
30+
@Test
31+
void shouldGetProductByCode() {
32+
ProductEntity product = productRepository.findByCode("P100").orElseThrow();
33+
assertThat(product.getCode()).isEqualTo("P100");
34+
assertThat(product.getName()).isEqualTo("The Hunger Games");
35+
assertThat(product.getDescription()).isEqualTo("Winning will make you famous. Losing means certain death...");
36+
assertThat(product.getPrice()).isEqualTo(new BigDecimal("34.0"));
37+
}
38+
39+
@Test
40+
void shouldReturnEmptyWhenProductCodeNotExists(){
41+
assertThat(productRepository.findByCode("invalid_product_code")).isEmpty();
42+
}
2843
}

catalog-service/src/test/java/com/eyepatch/works/catalogservice/web/controllers/ProductControllerTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.eyepatch.works.catalogservice.web.controllers;
22
import static io.restassured.RestAssured.given;
3+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
34
import static org.hamcrest.Matchers.hasSize;
45
import static org.hamcrest.Matchers.is;
56

67
import com.eyepatch.works.catalogservice.AbstractIT;
8+
import com.eyepatch.works.catalogservice.domain.Product;
79
import io.restassured.http.ContentType;
810
import org.junit.jupiter.api.Test;
911
import org.springframework.test.context.jdbc.Sql;
1012

13+
import java.math.BigDecimal;
14+
1115
@Sql("/test-data.sql")
1216
public class ProductControllerTest extends AbstractIT {
1317
@Test
@@ -29,4 +33,36 @@ public void shouldReturnProducts(){
2933

3034
}
3135

36+
@Test
37+
void shouldGetProductByCode(){
38+
Product product = given().contentType(ContentType.JSON)
39+
.when()
40+
.get("/api/products/{code}","P100")
41+
.then()
42+
.statusCode(200)
43+
.assertThat()
44+
.extract()
45+
.body()
46+
.as(Product.class);
47+
48+
assertThat(product.code()).isEqualTo("P100");
49+
assertThat(product.name()).isEqualTo("The Hunger Games");
50+
assertThat(product.description()).isEqualTo("Winning will make you famous. Losing means certain death...");
51+
assertThat(product.price()).isEqualTo(new BigDecimal("34.0"));
52+
}
53+
54+
@Test
55+
void shouldReturnEmptyWhenProductCodeNotExists(){
56+
String code = "invalid_product_code";
57+
given().contentType(ContentType.JSON)
58+
.when()
59+
.get("/api/products/{code}",code)
60+
.then()
61+
.statusCode(404)
62+
.body("status",is(404))
63+
.body("title",is("Product Not Found"))
64+
.body("detail",is("Product with code: " + code + " not found"));
65+
66+
}
67+
3268
}

0 commit comments

Comments
 (0)