-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLightAccessIntegrationTest.java
More file actions
246 lines (188 loc) · 9.43 KB
/
LightAccessIntegrationTest.java
File metadata and controls
246 lines (188 loc) · 9.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package integration;
import com.codurance.lightaccess.LightAccess;
import com.codurance.lightaccess.executables.DDLCommand;
import com.codurance.lightaccess.executables.SQLCommand;
import com.codurance.lightaccess.executables.SQLQuery;
import com.codurance.lightaccess.mapping.LAResultSet;
import integration.dtos.Item;
import integration.dtos.Product;
import integration.dtos.ProductID;
import org.h2.jdbcx.JdbcConnectionPool;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
public class LightAccessIntegrationTest {
private static final String CREATE_PRODUCTS_TABLE = "CREATE TABLE products (id VARCHAR(255) PRIMARY KEY, name VARCHAR(255), date TIMESTAMP)";
private static final String CREATE_ITEMS_TABLE = "CREATE TABLE items (id BIGINT PRIMARY KEY, name VARCHAR(255))";
private static final String CREATE_SEQUENCE_DDL = "CREATE SEQUENCE %s START WITH %s";
private static final String DROP_ALL_OBJECTS = "DROP ALL OBJECTS";
private static final String INSERT_PRODUCT_SQL = "insert into products (id, name, date) values (?, ?, ?)";
private static final String DELETE_PRODUCTS_SQL = "delete from products";
private static final String DELETE_PRODUCT_SQL = "delete from products where id = ?";
private static final String UPDATE_PRODUCT_NAME_SQL = "update products set name = ? where id = ?";
private static final String SELECT_ALL_PRODUCTS_SQL = "select * from products";
private static final String SELECT_PRODUCT_BY_ID_SQL = "select * from products where id = ?";
private static final String INSERT_ITEM_SQL = "insert into items (id, name) values (?, ?)";
private static final String SELECT_ITEM_BY_ID_SQL = "select id, name from items where id = ?";
private static final LocalDate TODAY = LocalDate.of(2017, 07, 27);
private static final LocalDate YESTERDAY = LocalDate.of(2017, 07, 26);
private static Product PRODUCT_ONE = new Product(1, "Product 1", YESTERDAY);
private static Product PRODUCT_TWO = new Product(2, "Product 2", TODAY);
private static Item SINGLE_ITEM = new Item(112345L, "9 Pound Hammer");
private static LightAccess lightAccess;
private static JdbcConnectionPool jdbcConnectionPool;
@BeforeClass
public static void before_all_tests() throws SQLException {
jdbcConnectionPool = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "user", "password");
lightAccess = new LightAccess(jdbcConnectionPool);
}
@Before
public void before_each_test() throws Exception {
lightAccess.executeDDLCommand(createProductsTable());
lightAccess.executeDDLCommand(createItemsTable());
}
@After
public void after_each_test() throws Exception {
lightAccess.executeDDLCommand(dropAllObjects());
}
@Test public void
close_connection_after_executing_a_query() {
lightAccess.executeQuery((conn) -> SELECT_ALL_PRODUCTS_SQL);
assertThat(jdbcConnectionPool.getActiveConnections()).isEqualTo(0);
}
@Test public void
close_connection_after_executing_a_command() {
lightAccess.executeCommand(deleteProducts());
assertThat(jdbcConnectionPool.getActiveConnections()).isEqualTo(0);
}
@Test public void
close_connection_after_executing_a_DDL_command() {
lightAccess.executeDDLCommand(dropAllObjects());
assertThat(jdbcConnectionPool.getActiveConnections()).isEqualTo(0);
}
@Test public void
insert_records() {
lightAccess.executeCommand(insertProduct(PRODUCT_ONE));
lightAccess.executeCommand(insertProduct(PRODUCT_TWO));
List<Product> products = lightAccess.executeQuery(retrieveAllProducts());
assertThat(products).containsExactlyInAnyOrder(PRODUCT_ONE, PRODUCT_TWO);
}
@Test public void
retrieve_a_single_record_and_map_it_to_an_object() {
lightAccess.executeCommand(insertProduct(PRODUCT_ONE));
lightAccess.executeCommand(insertProduct(PRODUCT_TWO));
Optional<Product> product = lightAccess.executeQuery(retrieveProductWithId(PRODUCT_TWO.id()));
assertThat(product.get()).isEqualTo(PRODUCT_TWO);
}
@Test public void
retrieve_an_empty_optional_when_not_record_is_found() {
lightAccess.executeCommand(insertProduct(PRODUCT_ONE));
Optional<Product> product = lightAccess.executeQuery(retrieveProductWithId(PRODUCT_TWO.id()));
assertThat(product.isPresent()).isEqualTo(false);
}
@Test public void
delete_a_record() {
lightAccess.executeCommand(insertProduct(PRODUCT_ONE));
lightAccess.executeCommand(insertProduct(PRODUCT_TWO));
lightAccess.executeCommand(delete(PRODUCT_ONE));
List<Product> products = lightAccess.executeQuery(retrieveAllProducts());
assertThat(products).containsExactlyInAnyOrder(PRODUCT_TWO);
}
@Test public void
update_a_record() {
lightAccess.executeCommand(insertProduct(PRODUCT_ONE));
lightAccess.executeCommand(updateProductName(1, "Another name"));
Optional<Product> product = lightAccess.executeQuery(retrieveProductWithId(PRODUCT_ONE.id()));
assertThat(product.get()).isEqualTo(new Product(PRODUCT_ONE.id(), "Another name", PRODUCT_ONE.date()));
}
@Test public void
return_next_integer_id_using_sequence() throws Exception {
lightAccess.executeDDLCommand(createSequence("id_sequence", "10"));
int firstId = lightAccess.nextId("id_sequence");
int secondId = lightAccess.nextId("id_sequence");
assertThat(firstId).isEqualTo(10);
assertThat(secondId).isEqualTo(11);
}
@Test public void
return_next_id_converted_to_a_different_type() throws Exception {
lightAccess.executeDDLCommand(createSequence("id_sequence", "10"));
String firstId = lightAccess.nextId("id_sequence", Object::toString);
ProductID secondId = lightAccess.nextId("id_sequence", ProductID::new);
assertThat(firstId).isEqualTo("10");
assertThat(secondId).isEqualTo(new ProductID(11));
}
@Test public void
return_item_when_exists() {
lightAccess.executeCommand(insertItem(SINGLE_ITEM));
Optional<Item> item = lightAccess.executeQuery(conn -> conn.prepareStatement(SELECT_ITEM_BY_ID_SQL).withParam(SINGLE_ITEM.id)
.executeQuery().onlyResult(this::toItem));
assertThat(item.get()).isEqualTo(SINGLE_ITEM);
}
private SQLCommand updateProductName(int id, String name) {
return conn -> conn.prepareStatement(UPDATE_PRODUCT_NAME_SQL)
.withParam(name)
.withParam(id)
.executeUpdate();
}
private SQLCommand delete(Product product) {
return conn -> conn.prepareStatement(DELETE_PRODUCT_SQL)
.withParam(product.id())
.executeUpdate();
}
private SQLCommand insertProduct(Product product) {
return conn -> conn.prepareStatement(INSERT_PRODUCT_SQL)
.withParam(product.id())
.withParam(product.name())
.withParam(product.date())
.executeUpdate();
}
private SQLCommand insertItem(Item item) {
return conn -> conn.prepareStatement(INSERT_ITEM_SQL)
.withParam(item.id)
.withParam(item.name)
.executeUpdate();
}
private SQLQuery<Optional<Product>> retrieveProductWithId(int id) {
return conn -> conn.prepareStatement(SELECT_PRODUCT_BY_ID_SQL)
.withParam(id)
.executeQuery()
.onlyResult(this::toProduct);
}
private SQLQuery<List<Product>> retrieveAllProducts() {
return conn -> conn.prepareStatement(SELECT_ALL_PRODUCTS_SQL)
.executeQuery()
.mapResults(this::toProduct);
}
private SQLCommand deleteProducts() {
return conn -> conn.prepareStatement(DELETE_PRODUCTS_SQL).executeUpdate();
}
private Product toProduct(LAResultSet laResultSet) {
return new Product(laResultSet.getInt(1),
laResultSet.getString(2),
laResultSet.getLocalDate(3));
}
private Item toItem(LAResultSet laResultSet) {
return new Item(laResultSet.getLong(1),
laResultSet.getString(2));
}
private DDLCommand createSequence(String sequenceName, String initialValue) {
String id_sequence = format(CREATE_SEQUENCE_DDL, sequenceName, initialValue);
return (conn) -> conn.statement(id_sequence).execute();
}
private DDLCommand createProductsTable() {
return (conn) -> conn.statement(CREATE_PRODUCTS_TABLE).execute();
}
private DDLCommand createItemsTable() {
return (conn) -> conn.statement(CREATE_ITEMS_TABLE).execute();
}
private DDLCommand dropAllObjects() {
return (conn) -> conn.statement(DROP_ALL_OBJECTS).execute();
}
}