View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.jpetstore.mapper;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  
20  import java.util.Comparator;
21  import java.util.List;
22  
23  import org.junit.jupiter.api.Test;
24  import org.junit.jupiter.api.extension.ExtendWith;
25  import org.mybatis.jpetstore.domain.Product;
26  import org.springframework.beans.factory.annotation.Autowired;
27  import org.springframework.test.context.ContextConfiguration;
28  import org.springframework.test.context.junit.jupiter.SpringExtension;
29  import org.springframework.transaction.annotation.Transactional;
30  
31  @ExtendWith(SpringExtension.class)
32  @ContextConfiguration(classes = MapperTestContext.class)
33  @Transactional
34  class ProductMapperTest {
35  
36    @Autowired
37    private ProductMapper mapper;
38  
39    @Test
40    void getProductListByCategory() {
41      // given
42      String categoryId = "FISH";
43  
44      // when
45      List<Product> products = mapper.getProductListByCategory(categoryId);
46  
47      // then
48      products.sort(Comparator.comparing(Product::getProductId));
49      assertThat(products).hasSize(4);
50      assertThat(products.get(0).getProductId()).isEqualTo("FI-FW-01");
51      assertThat(products.get(0).getName()).isEqualTo("Koi");
52      assertThat(products.get(0).getCategoryId()).isEqualTo("FISH");
53      assertThat(products.get(0).getDescription())
54          .isEqualTo("<image src=\"../images/fish3.gif\">Fresh Water fish from Japan");
55      assertThat(products.get(1).getProductId()).isEqualTo("FI-FW-02");
56      assertThat(products.get(1).getName()).isEqualTo("Goldfish");
57      assertThat(products.get(1).getCategoryId()).isEqualTo("FISH");
58      assertThat(products.get(1).getDescription())
59          .isEqualTo("<image src=\"../images/fish2.gif\">Fresh Water fish from China");
60      assertThat(products.get(2).getProductId()).isEqualTo("FI-SW-01");
61      assertThat(products.get(2).getName()).isEqualTo("Angelfish");
62      assertThat(products.get(2).getCategoryId()).isEqualTo("FISH");
63      assertThat(products.get(2).getDescription())
64          .isEqualTo("<image src=\"../images/fish1.gif\">Salt Water fish from Australia");
65      assertThat(products.get(3).getProductId()).isEqualTo("FI-SW-02");
66      assertThat(products.get(3).getName()).isEqualTo("Tiger Shark");
67      assertThat(products.get(3).getCategoryId()).isEqualTo("FISH");
68      assertThat(products.get(3).getDescription())
69          .isEqualTo("<image src=\"../images/fish4.gif\">Salt Water fish from Australia");
70    }
71  
72    @Test
73    void getProduct() {
74      // given
75      String productId = "FI-FW-01";
76  
77      // when
78      Product product = mapper.getProduct(productId);
79  
80      // then
81      assertThat(product.getProductId()).isEqualTo("FI-FW-01");
82      assertThat(product.getName()).isEqualTo("Koi");
83      assertThat(product.getCategoryId()).isEqualTo("FISH");
84      assertThat(product.getDescription()).isEqualTo("<image src=\"../images/fish3.gif\">Fresh Water fish from Japan");
85    }
86  
87    @Test
88    void searchProductList() {
89      // given
90      String keywords = "%o%";
91  
92      // when
93      List<Product> products = mapper.searchProductList(keywords);
94  
95      // then
96      products.sort(Comparator.comparing(Product::getProductId));
97      System.out.println(products);
98      assertThat(products).hasSize(8);
99      assertThat(products.get(0).getProductId()).isEqualTo("AV-CB-01");
100     assertThat(products.get(0).getName()).isEqualTo("Amazon Parrot");
101     assertThat(products.get(0).getCategoryId()).isEqualTo("BIRDS");
102     assertThat(products.get(0).getDescription())
103         .isEqualTo("<image src=\"../images/bird2.gif\">Great companion for up to 75 years");
104     assertThat(products.get(1).getName()).isEqualTo("Koi");
105     assertThat(products.get(2).getName()).isEqualTo("Goldfish");
106     assertThat(products.get(3).getName()).isEqualTo("Bulldog");
107     assertThat(products.get(4).getName()).isEqualTo("Dalmation");
108     assertThat(products.get(5).getName()).isEqualTo("Poodle");
109     assertThat(products.get(6).getName()).isEqualTo("Golden Retriever");
110     assertThat(products.get(7).getName()).isEqualTo("Labrador Retriever");
111   }
112 
113 }