-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProposalRepositoryTest.java
162 lines (129 loc) · 5.21 KB
/
ProposalRepositoryTest.java
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
/*
* Copyright (c) 2023 the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package org.viablespark.persistence;
import org.viablespark.persistence.dsl.SqlQuery;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
@TestInstance(Lifecycle.PER_CLASS)
public class ProposalRepositoryTest {
final Logger logger = LoggerFactory.getLogger(ProposalRepositoryTest.class);
private EmbeddedDatabase db;
private ProposalRepository repository;
@BeforeEach
public void setUp() {
// creates an HSQL in-memory database populated from default scripts
// classpath:schema.sql and classpath:data.sql
db = new EmbeddedDatabaseBuilder()
.addDefaultScripts()
.setName("ProposalTest")
.build();
repository = new ProposalRepository(new JdbcTemplate(db));
}
@AfterEach
public void tearDown() {
db.shutdown();
}
@Test
public void testSave() throws Exception {
Proposal proposal = new Proposal();
proposal.setDistance(123);
proposal.setPropDate(new Date());
proposal.setPropName("Name1");
proposal.setPropId("ID2");
proposal.setContractor(new Contractor("sc_key",1L)); //foreign-key
Optional<Key> optionalKey = repository.save(proposal);
logger.info("Entity Saved, let's check valid keys");
assertTrue(optionalKey.isPresent());
var key = optionalKey.get();
assertEquals(1, key.count());
assertNotNull(key.primaryKey());
assertEquals("pr_key",key.primaryKey().getKey());
logger.info("Let's update");
var foundOption = repository.get(key,Proposal.class);
assertTrue(foundOption.isPresent());
var found = foundOption.get();
found.setDistance(567);
found.setPropName("Saved again");
var keyOption = repository.save(found);
keyOption.ifPresent( k -> assertEquals(key,k) );
repository.get(key,Proposal.class)
.ifPresent( f -> assertEquals("Saved again",f.getPropName()));
}
@Test
public void testDelete() {
Proposal e = new Proposal();
e.setRefs(Key.of("pr_key", 1L));
repository.delete(e);
logger.info("Entity Deleted");
}
@Test
public void testGet() {
Optional<Proposal> result = repository.get(Key.of("pr_key", 1L), Proposal.class);
assertTrue(result.isPresent());
assertEquals(1L, result.get().getRefs().primaryKey().getValue());
}
@Test
public void testQuery() {
List<Proposal> results = repository.queryEntity(SqlQuery
.where(Pair.of("dist >= ?", 10))
.primaryKey("pr_key"),
Proposal.class);
assertEquals(3, results.size());
results.forEach(e -> assertTrue(e.getId() > 0));
}
@Test
public void testRowQuery(){
var mapper = new ProposalMapper();
var results = repository.query(
SqlQuery.asRawSql("select * from est_proposal p " +
"INNER JOIN contractor c on (c.sc_key = p.sc_key) "+
"where dist > 0"),
mapper);
results.forEach(e -> assertTrue(e.getId() > 0));
assertEquals(2, mapper.getContractors().size());
}
@Test
public void testRowSetQuery() {
List<Proposal> results = repository.query(SqlQuery
.asRawSql("select * from est_proposal"),
(rowSet,row) -> {
Proposal e = new Proposal();
e.setPr_key(rowSet.getLong("pr_key"));
e.setDistance(rowSet.getInt("dist"));
e.setPropId(rowSet.getString("prop_id"));
e.setPropName(rowSet.getString("proposal_name"));
e.setContractor(
new Contractor("sc_key",rowSet.getLong("sc_key")));
return e;
});
assertEquals(3, results.size());
results.forEach(e -> assertTrue(e.getId() > 0));
}
public static class ProposalRepository extends BaseRepository<Proposal> {
public ProposalRepository(JdbcTemplate db) {
super(db);
}
}
}