Skip to content

Commit 7de0591

Browse files
committed
test(JdbcCategoryDaoTest): add tests for getStatisticsOf() method.
Prerequisite for #1143
1 parent 4f2d8c7 commit 7de0591

10 files changed

+241
-0
lines changed

pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,30 @@
122122
<artifactId>spring-boot-starter-security</artifactId>
123123
</dependency>
124124

125+
<!-- https://github.com/spring-projects/spring-boot/blob/v1.5.22.RELEASE/spring-boot-starters/spring-boot-starter-test/pom.xml -->
126+
<dependency>
127+
<groupId>org.springframework.boot</groupId>
128+
<artifactId>spring-boot-starter-test</artifactId>
129+
<exclusions>
130+
<exclusion>
131+
<groupId>com.jayway.jsonpath</groupId>
132+
<artifactId>json-path</artifactId>
133+
</exclusion>
134+
<exclusion>
135+
<groupId>org.mockito</groupId>
136+
<artifactId>mockito-core</artifactId>
137+
</exclusion>
138+
<exclusion>
139+
<groupId>org.skyscreamer</groupId>
140+
<artifactId>jsonassert</artifactId>
141+
</exclusion>
142+
<exclusion>
143+
<groupId>org.hamcrest</groupId>
144+
<artifactId>hamcrest-library</artifactId>
145+
</exclusion>
146+
</exclusions>
147+
</dependency>
148+
125149
<!-- https://github.com/spring-projects/spring-boot/blob/v1.5.22.RELEASE/spring-boot-starters/spring-boot-starter-thymeleaf/pom.xml -->
126150
<dependency>
127151
<groupId>org.springframework.boot</groupId>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright (C) 2009-2019 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
package ru.mystamps.web.feature.category;
20+
21+
import org.assertj.core.api.WithAssertions;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
26+
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.test.context.TestPropertySource;
28+
import org.springframework.test.context.jdbc.Sql;
29+
import org.springframework.test.context.junit4.SpringRunner;
30+
import ru.mystamps.web.tests.Random;
31+
32+
import java.util.Map;
33+
34+
@JdbcTest
35+
// @todo #1143 Introduce a dedicated config for DAO classes
36+
@ContextConfiguration(classes = CategoryConfig.Services.class)
37+
@TestPropertySource(
38+
properties = {
39+
// don't load test data, start with an empty database
40+
"liquibase.contexts=scheme,init-data",
41+
// overrides settings from application-test.properties to keep the console clean,
42+
// comment this out when you need to debug tests. See also logback-test.xml
43+
"logging.level.=WARN", "logging.level.ru.mystamps=WARN"
44+
},
45+
// @todo #1143 Improve a way of importing properties files in the tests
46+
locations = "/sql/category_dao_queries.properties")
47+
@RunWith(SpringRunner.class)
48+
public class JdbcCategoryDaoTest implements WithAssertions {
49+
50+
@Autowired
51+
private CategoryDao categoryDao;
52+
53+
//
54+
// Tests for getStatisticsOf()
55+
//
56+
57+
@Test
58+
public void getStatisticsOfWithEmptyCollection() {
59+
// given
60+
// when
61+
Map<String, Integer> statistics = categoryDao.getStatisticsOf(Random.id(), Random.lang());
62+
// then
63+
assertThat(statistics).isEmpty();
64+
}
65+
66+
// LATER: extract all "scripts" to a class level. Requires @SqlMergeMode from Spring 5.2
67+
@Test
68+
@Sql(
69+
scripts = {
70+
"/db/users-coder.sql",
71+
"/db/collections-coder.sql",
72+
"/db/categories-sport.sql",
73+
"/db/categories-fauna.sql",
74+
"/db/series-1-fauna-qty5.sql",
75+
"/db/series-2-sport-qty3.sql",
76+
"/db/series-3-sport-qty7.sql"
77+
},
78+
statements = {
79+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
80+
+ "VALUES (1, 1, 5), (1, 2, 3), (1, 3, 7)"
81+
}
82+
)
83+
public void getStatisticsOfWithSeriesWithAllStamps() {
84+
// given
85+
final Integer expectedStampsInFaunaCategory = 5;
86+
final Integer expectedStampsInSportCategory = 10;
87+
// when
88+
Map<String, Integer> statistics = categoryDao.getStatisticsOf(1, "en");
89+
// then
90+
assertThat(statistics)
91+
.containsEntry("Fauna", expectedStampsInFaunaCategory)
92+
.containsEntry("Sport", expectedStampsInSportCategory)
93+
.hasSize(2);
94+
}
95+
96+
@Test
97+
@Sql(
98+
scripts = {
99+
"/db/users-coder.sql",
100+
"/db/collections-coder.sql",
101+
"/db/categories-fauna.sql",
102+
"/db/series-1-fauna-qty5.sql"
103+
},
104+
statements = {
105+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
106+
+ "VALUES (1, 1, 5)"
107+
}
108+
)
109+
public void getStatisticsOfInRussian() {
110+
// given
111+
// when
112+
Map<String, Integer> statisticsInRussian = categoryDao.getStatisticsOf(1, "ru");
113+
// then
114+
assertThat(statisticsInRussian).containsKeys("Фауна");
115+
}
116+
117+
@Test
118+
@Sql(
119+
scripts = {
120+
"/db/users-coder.sql",
121+
"/db/collections-coder.sql",
122+
"/db/categories-sport.sql",
123+
"/db/series-2-sport-qty3.sql",
124+
},
125+
statements = {
126+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
127+
+ "VALUES (1, 2, 3)"
128+
}
129+
)
130+
public void getStatisticsOfInRussianWithFallbackToEnglish() {
131+
// given
132+
// when
133+
Map<String, Integer> statistics = categoryDao.getStatisticsOf(1, "ru");
134+
// then
135+
assertThat(statistics).containsKey("Sport");
136+
}
137+
138+
@Test
139+
@Sql(
140+
scripts = {
141+
"/db/users-coder.sql",
142+
"/db/collections-coder.sql",
143+
"/db/categories-fauna.sql",
144+
"/db/series-1-fauna-qty5.sql",
145+
},
146+
statements = {
147+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
148+
+ "VALUES (1, 1, 5)"
149+
}
150+
)
151+
public void getStatisticsOfInUnsupportedLanguageWithFallbackToEnglish() {
152+
// given
153+
// when
154+
Map<String, Integer> statistics = categoryDao.getStatisticsOf(1, "fr");
155+
// then
156+
assertThat(statistics).containsKey("Fauna");
157+
}
158+
159+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--
2+
-- creates category "Fauna" with id=2 and name in Russian
3+
--
4+
-- depends on: users-coder.sql
5+
--
6+
INSERT INTO categories(id, name, name_ru, slug, created_at, created_by, updated_at, updated_by)
7+
SELECT 2, 'Fauna', 'Фауна', 'fauna', CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--
2+
-- creates category "Sport" with id=1
3+
--
4+
-- depends on: users-coder.sql
5+
--
6+
INSERT INTO categories(id, name, slug, created_at, created_by, updated_at, updated_by)
7+
SELECT 1, 'Sport', 'sport', CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--
2+
-- creates collection with id=1 for user "coder"
3+
--
4+
-- depends on: users-coder.sql
5+
--
6+
INSERT INTO collections(id, user_id, slug, updated_at, updated_by)
7+
SELECT 1, id, 'coder', CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--
2+
-- creates a series with id=1, in Fauna category and 5 stamps
3+
--
4+
-- depends on: users-coder.sql
5+
-- depends on: categories-fauna.sql
6+
--
7+
INSERT INTO series(id, quantity, perforated, category_id, created_at, created_by, updated_at, updated_by)
8+
SELECT 1, 5, TRUE, (SELECT id FROM categories WHERE slug = 'fauna'), CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--
2+
-- creates a series with id=2, in Sport category and 3 stamps
3+
--
4+
-- depends on: users-coder.sql
5+
-- depends on: categories-sport.sql
6+
--
7+
INSERT INTO series(id, quantity, perforated, category_id, created_at, created_by, updated_at, updated_by)
8+
SELECT 2, 3, TRUE, (SELECT id FROM categories WHERE slug = 'sport'), CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--
2+
-- creates a series with id=3, in Sport category and 7 stamps
3+
--
4+
-- depends on: users-coder.sql
5+
-- depends on: categories-sport.sql
6+
--
7+
INSERT INTO series(id, quantity, perforated, category_id, created_at, created_by, updated_at, updated_by)
8+
SELECT 3, 7, TRUE, (SELECT id FROM categories WHERE slug = 'sport'), CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';

src/test/resources/db/users-coder.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--
2+
-- creates user "coder" with id=1
3+
--
4+
INSERT INTO users(id, login, role, name, email, hash, registered_at, activated_at)
5+
VALUES(1, 'coder', 'USER', 'Coder', 'coder@example.com', '<password-hash>', CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP());

src/test/resources/logback-test.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@
2222
</root>
2323
<!-- End of org/springframework/boot/logging/logback/base.xml content -->
2424

25+
<logger name="org.springframework.test.context.support.AbstractContextLoader" level="WARN">
26+
<appender-ref ref="CONSOLE" />
27+
</logger>
28+
29+
<logger name="org.springframework.boot.test.context.SpringBootTestContextBootstrapper" level="WARN">
30+
<appender-ref ref="CONSOLE" />
31+
</logger>
32+
2533
</configuration>

0 commit comments

Comments
 (0)