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 org.junit.jupiter.api.Test;
21  import org.junit.jupiter.api.extension.ExtendWith;
22  import org.mybatis.jpetstore.domain.Sequence;
23  import org.springframework.beans.factory.annotation.Autowired;
24  import org.springframework.jdbc.core.JdbcTemplate;
25  import org.springframework.test.context.ContextConfiguration;
26  import org.springframework.test.context.junit.jupiter.SpringExtension;
27  import org.springframework.transaction.annotation.Transactional;
28  
29  @ExtendWith(SpringExtension.class)
30  @ContextConfiguration(classes = MapperTestContext.class)
31  @Transactional
32  class SequenceMapperTest {
33  
34    @Autowired
35    private SequenceMapper mapper;
36  
37    @Autowired
38    private JdbcTemplate jdbcTemplate;
39  
40    @Test
41    void getSequence() {
42      // given
43  
44      // when
45      Sequence sequence = mapper.getSequence(new Sequence("ordernum", -1));
46  
47      // then
48      assertThat(sequence.getName()).isEqualTo("ordernum");
49      assertThat(sequence.getNextId()).isEqualTo(1000);
50    }
51  
52    @Test
53    void updateSequence() {
54      // given
55      Sequence sequence = new Sequence("ordernum", 1001);
56  
57      // when
58      mapper.updateSequence(sequence);
59  
60      // then
61      Integer id = jdbcTemplate.queryForObject("SELECT nextid FROM sequence WHERE name = ?", Integer.class, "ordernum");
62      assertThat(id).isEqualTo(1001);
63    }
64  
65  }