| 1 |
|
package guru.mikelue.foxglove.examples; |
| 2 |
|
|
| 3 |
|
import java.io.IOException; |
| 4 |
|
|
| 5 |
|
import org.springframework.beans.factory.annotation.Autowired; |
| 6 |
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
| 7 |
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; |
| 8 |
|
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; |
| 9 |
|
import org.springframework.context.ApplicationContext; |
| 10 |
|
import org.springframework.context.annotation.Bean; |
| 11 |
|
import org.springframework.context.annotation.Configuration; |
| 12 |
|
import org.springframework.context.annotation.Scope; |
| 13 |
|
import org.springframework.jdbc.core.JdbcTemplate; |
| 14 |
|
import org.springframework.test.context.ContextConfiguration; |
| 15 |
|
import org.springframework.test.jdbc.JdbcTestUtils; |
| 16 |
|
import org.springframework.transaction.annotation.Propagation; |
| 17 |
|
import org.springframework.transaction.annotation.Transactional; |
| 18 |
|
import org.springframework.util.StreamUtils; |
| 19 |
|
|
| 20 |
|
import org.instancio.Instancio; |
| 21 |
|
import org.junit.jupiter.api.AfterEach; |
| 22 |
|
import org.junit.jupiter.api.BeforeAll; |
| 23 |
|
import org.junit.jupiter.api.BeforeEach; |
| 24 |
|
import org.junit.jupiter.api.Test; |
| 25 |
|
|
| 26 |
|
import guru.mikelue.foxglove.TableFacet; |
| 27 |
|
import guru.mikelue.foxglove.annotation.GenData; |
| 28 |
|
import guru.mikelue.foxglove.annotation.TableFacetsSource; |
| 29 |
|
import guru.mikelue.foxglove.functional.TableFacetProvider; |
| 30 |
|
import guru.mikelue.foxglove.jdbc.JdbcTableFacet; |
| 31 |
|
import guru.mikelue.foxglove.springframework.EnableFoxglove; |
| 32 |
|
|
| 33 |
|
import static guru.mikelue.foxglove.test.SampleSchema.*; |
| 34 |
|
import static java.nio.charset.StandardCharsets.UTF_8; |
| 35 |
|
import static org.assertj.core.api.Assertions.assertThat; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
@JdbcTest |
| 39 |
|
@AutoConfigureTestDatabase(replace = Replace.NONE) |
| 40 |
|
@ContextConfiguration(classes = SampleTestConfig.class) |
| 41 |
|
@EnableFoxglove |
| |
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 11 |
Complexity Density: 0.73 |
|
| 42 |
|
public class SpringFrameworkTest { |
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 43 |
4 |
public SpringFrameworkTest() {}... |
| 44 |
|
|
| 45 |
|
@TableFacetsSource |
| 46 |
|
TableFacet redCars = JdbcTableFacet.builder("ap_car") |
| 47 |
|
.numberOfRows(RANDOM_ROWS) |
| 48 |
|
.column("cr_color").fixed("red") |
| 49 |
|
.build(); |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 54 |
1 |
@Test... |
| 55 |
|
@GenData( |
| 56 |
|
facetsNames = { "redCars" } |
| 57 |
|
) |
| 58 |
|
void someTest( |
| 59 |
|
@Autowired |
| 60 |
|
JdbcTemplate jdbcTemplate |
| 61 |
|
) { |
| 62 |
1 |
assertThat( |
| 63 |
|
JdbcTestUtils.countRowsInTableWhere( |
| 64 |
|
jdbcTemplate, |
| 65 |
|
"ap_car", "cr_color = 'red'" |
| 66 |
|
) |
| 67 |
|
) |
| 68 |
|
.isEqualTo(RANDOM_ROWS); |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
final static int RANDOM_ROWS = Instancio.gen() |
| 73 |
|
.ints().range(5, 10).get(); |
| 74 |
|
|
| 75 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 76 |
1 |
@Test... |
| 77 |
|
@GenData( |
| 78 |
|
|
| 79 |
|
facetsNames = { "blueCars", "purpleCars", "yellowCars" } |
| 80 |
|
) |
| 81 |
|
void blueCarsTest( |
| 82 |
|
@Autowired |
| 83 |
|
JdbcTemplate jdbcTemplate |
| 84 |
|
) { |
| 85 |
1 |
assertThat( |
| 86 |
|
JdbcTestUtils.countRowsInTableWhere( |
| 87 |
|
jdbcTemplate, |
| 88 |
|
"ap_car", "cr_color IN ('blue', 'purple', 'yellow')" |
| 89 |
|
) |
| 90 |
|
) |
| 91 |
|
.isEqualTo(RANDOM_ROWS * 3); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 96 |
4 |
@AfterEach... |
| 97 |
|
void tearDownForYellowCars( |
| 98 |
|
@Autowired |
| 99 |
|
JdbcTemplate jdbcTemplate |
| 100 |
|
) { |
| 101 |
|
|
| 102 |
4 |
JdbcTestUtils.deleteFromTableWhere( |
| 103 |
|
jdbcTemplate, |
| 104 |
|
"ap_car", "cr_color = 'yellow'" |
| 105 |
|
); |
| 106 |
|
} |
| 107 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 108 |
1 |
@Test... |
| 109 |
|
@GenData( |
| 110 |
|
facetsNames = { "yellowCars" } |
| 111 |
|
) |
| 112 |
|
@Transactional(propagation = Propagation.NOT_SUPPORTED) |
| 113 |
|
void noRollback( |
| 114 |
|
@Autowired |
| 115 |
|
JdbcTemplate jdbcTemplate |
| 116 |
|
) { |
| 117 |
1 |
assertThat( |
| 118 |
|
JdbcTestUtils.countRowsInTableWhere( |
| 119 |
|
jdbcTemplate, |
| 120 |
|
"ap_car", "cr_color = 'yellow'" |
| 121 |
|
) |
| 122 |
|
) |
| 123 |
|
.isEqualTo(RANDOM_ROWS); |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 128 |
1 |
@Test... |
| 129 |
|
@GenData(ToyotaCarsProvider.class) |
| 130 |
|
void byProvider( |
| 131 |
|
@Autowired |
| 132 |
|
JdbcTemplate jdbcTemplate |
| 133 |
|
) { |
| 134 |
1 |
assertThat( |
| 135 |
|
JdbcTestUtils.countRowsInTableWhere( |
| 136 |
|
jdbcTemplate, |
| 137 |
|
"ap_car", "cr_brand = 'Toyota'" |
| 138 |
|
) |
| 139 |
|
) |
| 140 |
|
.isEqualTo(RANDOM_ROWS); |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 144 |
4 |
@BeforeEach... |
| 145 |
|
void setup() {} |
| 146 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 147 |
4 |
@AfterEach... |
| 148 |
|
void tearDown() {} |
| 149 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 150 |
1 |
@BeforeAll... |
| 151 |
|
static void globalSetup(ApplicationContext context) throws IOException |
| 152 |
|
{ |
| 153 |
1 |
var jdbcTemplate = context.getBean(JdbcTemplate.class); |
| 154 |
|
|
| 155 |
1 |
dropTables( |
| 156 |
|
jdbcTemplate, |
| 157 |
|
TABLE_DATA_TYPES, TABLE_RENT, TABLE_CAR_FEATURE, TABLE_CAR, TABLE_MEMBER, |
| 158 |
|
TABLE_CAR_ARCHIVED |
| 159 |
|
); |
| 160 |
|
|
| 161 |
1 |
build("classpath:data-types.sql", context); |
| 162 |
1 |
build("classpath:car-renting.sql", context); |
| 163 |
|
} |
| 164 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 165 |
1 |
private static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames)... |
| 166 |
|
{ |
| 167 |
1 |
for (var tableName: tableNames) { |
| 168 |
6 |
jdbcTemplate.execute( |
| 169 |
|
String.format("DROP TABLE IF EXISTS %s", tableName) |
| 170 |
|
); |
| 171 |
|
} |
| 172 |
|
} |
| 173 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 174 |
2 |
private static void build(String resourcePath, ApplicationContext appContext) throws IOException... |
| 175 |
|
{ |
| 176 |
2 |
var ddl = StreamUtils.copyToString( |
| 177 |
|
appContext.getResource(resourcePath) |
| 178 |
|
.getInputStream(), |
| 179 |
|
UTF_8 |
| 180 |
|
); |
| 181 |
|
|
| 182 |
2 |
var jdbcTemplate = appContext.getBean(JdbcTemplate.class); |
| 183 |
|
|
| 184 |
2 |
for (var stmt: ddl.split(";")) { |
| 185 |
11 |
jdbcTemplate.execute(stmt); |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 191 |
|
class ToyotaCarsProvider implements TableFacetProvider<TableFacet> { |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 192 |
1 |
@Override... |
| 193 |
|
public TableFacet getOne() |
| 194 |
|
{ |
| 195 |
1 |
return JdbcTableFacet.builder("ap_car") |
| 196 |
|
.numberOfRows(SpringFrameworkTest.RANDOM_ROWS) |
| 197 |
|
.column("cr_brand").fixed("Toyota") |
| 198 |
|
.build(); |
| 199 |
|
} |
| 200 |
|
} |
| 201 |
|
|
| 202 |
|
|
| 203 |
|
|
| 204 |
|
@Configuration |
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
| 205 |
|
class SampleTestConfig { |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 206 |
1 |
@Bean... |
| 207 |
|
ToyotaCarsProvider toyotaCars() |
| 208 |
|
{ |
| 209 |
1 |
return new ToyotaCarsProvider(); |
| 210 |
|
} |
| 211 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 212 |
1 |
@Bean @Scope("prototype")... |
| 213 |
|
TableFacet blueCars() |
| 214 |
|
{ |
| 215 |
1 |
return JdbcTableFacet.builder("ap_car") |
| 216 |
|
.numberOfRows(SpringFrameworkTest.RANDOM_ROWS) |
| 217 |
|
.column("cr_color").fixed("blue") |
| 218 |
|
.build(); |
| 219 |
|
} |
| 220 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 221 |
1 |
@Bean... |
| 222 |
|
TableFacetProvider<TableFacet> yellowCars() |
| 223 |
|
{ |
| 224 |
1 |
return () -> JdbcTableFacet.builder("ap_car") |
| 225 |
|
.numberOfRows(SpringFrameworkTest.RANDOM_ROWS) |
| 226 |
|
.column("cr_color").fixed("yellow") |
| 227 |
|
.build(); |
| 228 |
|
} |
| 229 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 230 |
1 |
@Bean @Scope("prototype")... |
| 231 |
|
TableFacet[] purpleCars() |
| 232 |
|
{ |
| 233 |
1 |
return new TableFacet[] { |
| 234 |
|
JdbcTableFacet.builder("ap_car") |
| 235 |
|
.numberOfRows(SpringFrameworkTest.RANDOM_ROWS) |
| 236 |
|
.column("cr_color").fixed("purple") |
| 237 |
|
.build() |
| 238 |
|
}; |
| 239 |
|
} |
| 240 |
|
} |
| 241 |
|
|