1 package guru.mikelue.jdut.example;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import java.sql.Date;
6 import java.sql.SQLException;
7 import java.time.LocalDate;
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import org.apache.commons.lang3.RandomUtils;
12 import org.junit.jupiter.api.AfterEach;
13 import org.junit.jupiter.api.BeforeAll;
14 import org.junit.jupiter.api.BeforeEach;
15 import org.junit.jupiter.api.Test;
16 import org.junit.jupiter.api.TestInfo;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import guru.mikelue.jdut.DuetConductor;
21 import guru.mikelue.jdut.annotation.IfDatabaseVendor;
22 import guru.mikelue.jdut.assertion.ResultSetAssert;
23 import guru.mikelue.jdut.datagrain.DataGrain;
24 import guru.mikelue.jdut.jdbc.JdbcTemplateFactory;
25 import guru.mikelue.jdut.jdbc.function.DbResultSet;
26 import guru.mikelue.jdut.operation.DefaultOperators;
27 import guru.mikelue.jdut.test.AbstractDataSourceTestBase;
28 import guru.mikelue.jdut.vendor.DatabaseVendor;
29 import guru.mikelue.jdut.yaml.YamlConductorFactory;
30
31 @IfDatabaseVendor(match=DatabaseVendor.H2)
32 public class YamlExampleTest extends AbstractDataSourceTestBase {
33 private static ExampleDao testedDao;
34
35 private static YamlConductorFactory yamlConductor;
36
37 private final String INSERT_ARTIST_NAME = "Miles Davis";
38 private final String UPDATE_ARTIST_NAME = "John Coltrane";
39 private final static String REMOVE_ARTIST_NAME = "Blue Mountain";
40
41 private static Map<String, DuetConductor> duetConductors = new HashMap<>(6);
42
43 public YamlExampleTest() {}
44
45 @BeforeEach
46 private void buildData(TestInfo testInfo)
47 {
48 String methodName = testInfo.getTestMethod().get().getName();
49
50 duetConductors.put(
51 methodName,
52 yamlConductor.conductResource(
53 String.format("guru/mikelue/jdut/example/YamlExampleTest-%s.yaml", methodName)
54 )
55 );
56
57 duetConductors.get(methodName).build();
58 }
59 @AfterEach
60 private void cleanData(TestInfo testInfo)
61 {
62 String methodName = testInfo.getTestMethod().get().getName();
63
64 duetConductors.get(methodName).clean();
65 }
66
67
68
69
70 @Test
71 public void addArtist() throws SQLException
72 {
73 testedDao.addArtist(INSERT_ARTIST_NAME);
74
75 JdbcTemplateFactory.buildRunnable(
76 () -> getDataSource().getConnection(),
77 conn -> DbResultSet.buildRunnable(
78 conn,
79 "SELECT COUNT(*) FROM ex_artist WHERE at_name = '" + INSERT_ARTIST_NAME + "'",
80 rs -> new ResultSetAssert(rs)
81 .assertNextTrue()
82 .assertInt(1, 1)
83 ).runJdbc()
84 ).runJdbc();
85 }
86
87
88
89
90 @Test
91 public void updateArtistName() throws SQLException
92 {
93 testedDao.updateArtistName(1001, UPDATE_ARTIST_NAME);
94
95 JdbcTemplateFactory.buildRunnable(
96 () -> getDataSource().getConnection(),
97 conn -> DbResultSet.buildRunnable(
98 conn,
99 "SELECT COUNT(*) FROM ex_artist WHERE at_name = '" + UPDATE_ARTIST_NAME + "'",
100 rs -> new ResultSetAssert(rs)
101 .assertNextTrue()
102 .assertInt(1, 1)
103 ).runJdbc()
104 ).runJdbc();
105 }
106
107
108
109
110 @Test
111 public void removeArtistByName() throws SQLException
112 {
113 testedDao.removeArtistByName(REMOVE_ARTIST_NAME);
114
115 JdbcTemplateFactory.buildRunnable(
116 () -> getDataSource().getConnection(),
117 conn -> DbResultSet.buildRunnable(
118 conn,
119 "SELECT COUNT(*) FROM ex_artist WHERE at_name = '" + REMOVE_ARTIST_NAME + "'",
120 rs -> new ResultSetAssert(rs)
121 .assertNextTrue()
122 .assertInt(1, 0)
123 ).runJdbc()
124 ).runJdbc();
125 }
126
127 @Test
128 public void countAlbumsByType() throws SQLException
129 {
130 assertEquals(
131 2,
132 testedDao.countAlbumsByType(1)
133 );
134 }
135
136 @BeforeAll
137 void setupDatabaseSchema()
138 {
139 SchemaSetup.buildSchema(getDataSource());
140
141 final Logger logger = LoggerFactory.getLogger("guru.mikelue.jdut.example.INSERT_AND_LOG");
142
143 testedDao = new ExampleDao(getDataSource());
144
145 yamlConductor = YamlConductorFactory.build(
146 getDataSource(),
147 builder -> builder
148 .namedSupplier(
149 "random_date", YamlExampleTest::randomDate
150 )
151 .namedSupplier(
152 "random_duration", YamlExampleTest::randomDuration
153 )
154 .namedOperator(
155 "insert_and_log",
156 (connection, dataGrain) -> {
157 logger.info("@@@ BEFORE BUILDING DATA @@@");
158
159 DataGrain result = DefaultOperators.insert(connection, dataGrain);
160
161 logger.info("@@@ AFTER BUILDING DATA @@@");
162
163 return result;
164 }
165 )
166 .namedDecorator(
167 "decorator_album",
168 (dataRowBuilder) -> {
169 dataRowBuilder.fieldOfValue(
170 "ab_name",
171 dataRowBuilder.getData("ab_name").get() + "(BlueNote)"
172 );
173 }
174 )
175 );
176 }
177
178 private static Date randomDate()
179 {
180 return Date.valueOf(
181 LocalDate.of(
182 RandomUtils.nextInt(1950, 1961),
183 RandomUtils.nextInt(1, 13),
184 RandomUtils.nextInt(1, 26)
185 )
186 );
187 }
188
189 private static int randomDuration()
190 {
191 return RandomUtils.nextInt(1800, 10801);
192 }
193 }