Skip to content

Commit

Permalink
[#24] test: 상품 목록 조회에 대한 test code 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
chung committed Jun 28, 2023
1 parent 91d6ddf commit 38800ef
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

@Repository
public class ProductRepository {
Expand All @@ -18,9 +19,8 @@ public ProductRepository(final DataSource dataSource) {
this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}


public List<Product> findProductsBy(final String category) {
String sql = "SELECT p.*, SUM(sl.count) total_quantity"
String sql = "SELECT p.*, SUM(sl.count) total_quantity"
+ " FROM product p"
+ " JOIN category c ON p.category_id = c.id"
+ " JOIN sales_log sl ON p.id = sl.product_id"
Expand All @@ -40,4 +40,9 @@ public List<Product> findProductsBy(final String category) {
rs.getString("image_url")
));
}

public boolean isExistCategory(final String category) {
String sql = "SELECT EXISTS (SELECT 1 FROM category WHERE category.name = :name)";
return Boolean.TRUE.equals(jdbcTemplate.queryForObject(sql, Map.of("name", category), Boolean.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ public class ProductService {
private final ProductRepository productRepository;

public List<ResponseProduct> getProducts(final String category) {
checkCategory(category);

List<Product> products = productRepository.findProductsBy(category);

return products.stream()
.map(ResponseProduct::from)
.collect(Collectors.toList());
}

private void checkCategory(final String category) {
if (!productRepository.isExistCategory(category)) {
throw new IllegalStateException("카테고리 명이 잘못되었습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package team.five.kiosk.service;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;
import team.five.kiosk.dto.ResponseProduct;
import team.five.kiosk.repository.ProductRepository;

import java.util.List;

@SpringBootTest
@Transactional
class ProductServiceTest {

@Autowired ProductService productService;
@Autowired ProductRepository productRepository;
@Autowired NamedParameterJdbcTemplate jdbcTemplate;


@Test
@DisplayName("카테고리명이 주어졌을 때 해당하는 메뉴의 개수가 옳게 나오는지 테스트")
public void checkProductSizeByCategory() throws Exception {
//given
createSalesLog(2, 12);

//when
List<ResponseProduct> products = productService.getProducts("coffee");

//then
Assertions.assertThat(products.size()).isEqualTo(4);
}

@Test
@DisplayName("주어진 카테고리와 db의 카테고리의 이름이 맞는지 확인하는 테스트")
public void checkCategoryName() throws Exception {
//given
String wrongName = "coff";

//when
Assertions.assertThatThrownBy(() -> productService.getProducts(wrongName))
.isInstanceOf(IllegalStateException.class);
}

private void createSalesLog(final int productId, final int count) {
String sql = "insert into sales_log (product_id, count) values(:productId, :count)";
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("productId", productId);
params.addValue("count", count);
jdbcTemplate.update(sql, params);
}
}

0 comments on commit 38800ef

Please sign in to comment.