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.math.BigDecimal;
21  import java.util.Comparator;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.extension.ExtendWith;
28  import org.mybatis.jpetstore.domain.Item;
29  import org.springframework.beans.factory.annotation.Autowired;
30  import org.springframework.jdbc.core.JdbcTemplate;
31  import org.springframework.test.context.ContextConfiguration;
32  import org.springframework.test.context.junit.jupiter.SpringExtension;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  @ExtendWith(SpringExtension.class)
36  @ContextConfiguration(classes = MapperTestContext.class)
37  @Transactional
38  class ItemMapperTest {
39  
40    @Autowired
41    private ItemMapper mapper;
42  
43    @Autowired
44    private JdbcTemplate jdbcTemplate;
45  
46    @Test
47    void getItemListByProduct() {
48      // given
49      String productId = "FI-SW-01";
50  
51      // when
52      List<Item> items = mapper.getItemListByProduct(productId);
53  
54      // then
55      items.sort(Comparator.comparing(Item::getItemId));
56      assertThat(items).hasSize(2);
57      assertThat(items.get(0).getItemId()).isEqualTo("EST-1");
58      assertThat(items.get(0).getListPrice()).isEqualTo(new BigDecimal("16.50"));
59      assertThat(items.get(0).getUnitCost()).isEqualTo(new BigDecimal("10.00"));
60      assertThat(items.get(0).getSupplierId()).isEqualTo(1);
61      assertThat(items.get(0).getStatus()).isEqualTo("P");
62      assertThat(items.get(0).getAttribute1()).isEqualTo("Large");
63      assertThat(items.get(0).getAttribute2()).isNull();
64      assertThat(items.get(0).getAttribute3()).isNull();
65      assertThat(items.get(0).getAttribute4()).isNull();
66      assertThat(items.get(0).getAttribute5()).isNull();
67      assertThat(items.get(0).getProduct().getProductId()).isEqualTo("FI-SW-01");
68      assertThat(items.get(0).getProduct().getName()).isEqualTo("Angelfish");
69      assertThat(items.get(0).getProduct().getDescription())
70          .isEqualTo("<image src=\"../images/fish1.gif\">Salt Water fish from Australia");
71      assertThat(items.get(0).getProduct().getCategoryId()).isEqualTo("FISH");
72      assertThat(items.get(1).getItemId()).isEqualTo("EST-2");
73      assertThat(items.get(1).getListPrice()).isEqualTo(new BigDecimal("16.50"));
74      assertThat(items.get(1).getUnitCost()).isEqualTo(new BigDecimal("10.00"));
75      assertThat(items.get(1).getSupplierId()).isEqualTo(1);
76      assertThat(items.get(1).getStatus()).isEqualTo("P");
77      assertThat(items.get(1).getAttribute1()).isEqualTo("Small");
78      assertThat(items.get(1).getAttribute2()).isNull();
79      assertThat(items.get(1).getAttribute3()).isNull();
80      assertThat(items.get(1).getAttribute4()).isNull();
81      assertThat(items.get(1).getAttribute5()).isNull();
82      assertThat(items.get(1).getProduct().getProductId()).isEqualTo("FI-SW-01");
83      assertThat(items.get(1).getProduct().getName()).isEqualTo("Angelfish");
84      assertThat(items.get(1).getProduct().getDescription())
85          .isEqualTo("<image src=\"../images/fish1.gif\">Salt Water fish from Australia");
86      assertThat(items.get(1).getProduct().getCategoryId()).isEqualTo("FISH");
87    }
88  
89    @Test
90    void getItem() {
91      // given
92      String itemId = "EST-1";
93  
94      // when
95      Item item = mapper.getItem(itemId);
96  
97      // then
98      assertThat(item.getItemId()).isEqualTo("EST-1");
99      assertThat(item.getListPrice()).isEqualTo(new BigDecimal("16.50"));
100     assertThat(item.getUnitCost()).isEqualTo(new BigDecimal("10.00"));
101     assertThat(item.getSupplierId()).isEqualTo(1);
102     assertThat(item.getStatus()).isEqualTo("P");
103     assertThat(item.getAttribute1()).isEqualTo("Large");
104     assertThat(item.getAttribute2()).isNull();
105     assertThat(item.getAttribute3()).isNull();
106     assertThat(item.getAttribute4()).isNull();
107     assertThat(item.getAttribute5()).isNull();
108     assertThat(item.getProduct().getProductId()).isEqualTo("FI-SW-01");
109     assertThat(item.getProduct().getName()).isEqualTo("Angelfish");
110     assertThat(item.getProduct().getDescription())
111         .isEqualTo("<image src=\"../images/fish1.gif\">Salt Water fish from Australia");
112     assertThat(item.getProduct().getCategoryId()).isEqualTo("FISH");
113   }
114 
115   @Test
116   void getInventoryQuantity() {
117     // given
118     String itemId = "EST-1";
119 
120     // when
121     int quantity = mapper.getInventoryQuantity(itemId);
122 
123     // then
124     assertThat(quantity).isEqualTo(10000);
125 
126   }
127 
128   @Test
129   void updateInventoryQuantity() {
130     // given
131     String itemId = "EST-1";
132     Map<String, Object> params = new HashMap<>();
133     params.put("itemId", itemId);
134     params.put("increment", 10);
135 
136     // when
137     mapper.updateInventoryQuantity(params);
138 
139     // then
140     Integer quantity = jdbcTemplate.queryForObject("SELECT QTY FROM inventory WHERE itemid = ?", Integer.class, itemId);
141     assertThat(quantity).isEqualTo(9990);
142 
143   }
144 
145 }