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.List;
22  import java.util.Map;
23  
24  import org.junit.jupiter.api.Test;
25  import org.junit.jupiter.api.extension.ExtendWith;
26  import org.mybatis.jpetstore.domain.LineItem;
27  import org.springframework.beans.factory.annotation.Autowired;
28  import org.springframework.jdbc.core.JdbcTemplate;
29  import org.springframework.test.context.ContextConfiguration;
30  import org.springframework.test.context.junit.jupiter.SpringExtension;
31  import org.springframework.transaction.annotation.Transactional;
32  
33  @ExtendWith(SpringExtension.class)
34  @ContextConfiguration(classes = MapperTestContext.class)
35  @Transactional
36  class LineItemMapperTest {
37  
38    @Autowired
39    private LineItemMapper mapper;
40  
41    @Autowired
42    private JdbcTemplate jdbcTemplate;
43  
44    @Test
45    void insertLineItem() {
46      // given
47      LineItem lineItem = new LineItem();
48      lineItem.setOrderId(1);
49      lineItem.setLineNumber(1);
50      lineItem.setItemId("EST-1");
51      lineItem.setQuantity(4);
52      lineItem.setUnitPrice(BigDecimal.valueOf(100));
53  
54      // when
55      mapper.insertLineItem(lineItem);
56  
57      // then
58      Map<String, Object> record = jdbcTemplate.queryForMap("SELECT * FROM lineitem WHERE orderid = ? AND linenum = ?", 1,
59          1);
60      assertThat(record).hasSize(5).containsEntry("ORDERID", lineItem.getOrderId())
61          .containsEntry("LINENUM", lineItem.getLineNumber()).containsEntry("ITEMID", lineItem.getItemId())
62          .containsEntry("QUANTITY", lineItem.getQuantity()).containsEntry("UNITPRICE", new BigDecimal("100.00"));
63  
64    }
65  
66    @Test
67    void getLineItemsByOrderId() {
68      // given
69      LineItem lineItem = new LineItem();
70      lineItem.setOrderId(1);
71      lineItem.setLineNumber(1);
72      lineItem.setItemId("EST-1");
73      lineItem.setQuantity(4);
74      lineItem.setUnitPrice(BigDecimal.valueOf(100));
75      mapper.insertLineItem(lineItem);
76  
77      // when
78      List<LineItem> lineItems = mapper.getLineItemsByOrderId(1);
79  
80      // then
81      assertThat(lineItems).hasSize(1);
82      assertThat(lineItems.get(0).getOrderId()).isEqualTo(lineItem.getOrderId());
83      assertThat(lineItems.get(0).getLineNumber()).isEqualTo(lineItem.getOrderId());
84      assertThat(lineItems.get(0).getItemId()).isEqualTo(lineItem.getItemId());
85      assertThat(lineItems.get(0).getQuantity()).isEqualTo(lineItem.getQuantity());
86      assertThat(lineItems.get(0).getUnitPrice()).isEqualTo(new BigDecimal("100.00"));
87  
88    }
89  
90  }