| 1 |
|
package guru.mikelue.foxglove.springframework; |
| 2 |
|
|
| 3 |
|
import java.util.List; |
| 4 |
|
import java.util.stream.Stream; |
| 5 |
|
|
| 6 |
|
import javax.sql.DataSource; |
| 7 |
|
|
| 8 |
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
| 9 |
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; |
| 10 |
|
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; |
| 11 |
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 12 |
|
import org.springframework.context.annotation.Bean; |
| 13 |
|
import org.springframework.context.annotation.Configuration; |
| 14 |
|
import org.springframework.context.annotation.Primary; |
| 15 |
|
|
| 16 |
|
import org.apache.commons.lang3.reflect.MethodUtils; |
| 17 |
|
import org.junit.jupiter.api.AfterEach; |
| 18 |
|
import org.junit.jupiter.api.BeforeEach; |
| 19 |
|
import org.junit.jupiter.api.Test; |
| 20 |
|
import org.junit.jupiter.params.ParameterizedTest; |
| 21 |
|
import org.junit.jupiter.params.provider.CsvSource; |
| 22 |
|
import org.junit.jupiter.params.provider.ValueSource; |
| 23 |
|
|
| 24 |
|
import guru.mikelue.misc.testlib.AbstractTestBase; |
| 25 |
|
|
| 26 |
|
import guru.mikelue.foxglove.DataGenerator; |
| 27 |
|
import guru.mikelue.foxglove.TableFacet; |
| 28 |
|
import guru.mikelue.foxglove.annotation.DataGenContext; |
| 29 |
|
import guru.mikelue.foxglove.annotation.DataGeneratorSource; |
| 30 |
|
import guru.mikelue.foxglove.annotation.GenData; |
| 31 |
|
import guru.mikelue.foxglove.annotation.TableFacetsSource; |
| 32 |
|
import guru.mikelue.foxglove.functional.DataGeneratorProvider; |
| 33 |
|
import guru.mikelue.foxglove.functional.TableFacetProvider; |
| 34 |
|
import guru.mikelue.foxglove.functional.TableFacetsProvider; |
| 35 |
|
import guru.mikelue.foxglove.jdbc.JdbcTableFacet; |
| 36 |
|
import guru.mikelue.foxglove.setting.DataSettingInfo; |
| 37 |
|
import mockit.Mocked; |
| 38 |
|
import mockit.Verifications; |
| 39 |
|
|
| 40 |
|
import static org.assertj.core.api.Assertions.assertThat; |
| 41 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (55) |
Complexity: 14 |
Complexity Density: 0.33 |
|
| 42 |
|
public class SpringContextObjectFactoryTest extends AbstractTestBase { |
| 43 |
|
@Mocked |
| 44 |
|
private DataGenContext<TableFacet> mockDataGenContext; |
| 45 |
|
|
| 46 |
|
private static DataGenContext<TableFacet> dataGenContext = |
| 47 |
|
DataGenContext.loadFrom(SpringContextObjectFactoryTest.class); |
| 48 |
|
|
| 49 |
|
@DataGeneratorSource |
| 50 |
|
private DataGenerator<TableFacet> defaultDataGenerator = new FakeDataGenerator(); |
| 51 |
|
|
| 52 |
|
@TableFacetsSource |
| 53 |
|
private TableFacet defaultFacet = JdbcTableFacet.builder("TABLE_DEFAULT") |
| 54 |
|
.column("col_default").fixed("default") |
| 55 |
|
.numberOfRows(3) |
| 56 |
|
.build(); |
| 57 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 58 |
11 |
public SpringContextObjectFactoryTest() {}... |
| 59 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 60 |
11 |
@BeforeEach... |
| 61 |
|
void setup() {} |
| 62 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 63 |
11 |
@AfterEach... |
| 64 |
|
void tearDown() {} |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@link |
| 68 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
| 69 |
1 |
@Test... |
| 70 |
|
void fallbackToDefaultDataGenerator() throws Exception |
| 71 |
|
{ |
| 72 |
1 |
var appContext = new AnnotationConfigApplicationContext(EmptyConfig.class); |
| 73 |
|
|
| 74 |
1 |
var testedFactory = new SpringContextObjectFactory( |
| 75 |
|
appContext, this, getByMethod("defaultMethod"), |
| 76 |
|
dataGenContext, "defaultMethod" |
| 77 |
|
); |
| 78 |
|
|
| 79 |
1 |
var testedGenerator = (FakeDataGenerator)testedFactory.getDataGenerator(); |
| 80 |
1 |
assertThat(testedGenerator.id) |
| 81 |
|
.isEqualTo(-1); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
@link |
| 86 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
4-
|
|
| 87 |
4 |
@ParameterizedTest... |
| 88 |
|
@ValueSource(strings = { |
| 89 |
|
"defaultMethod", |
| 90 |
|
"nameSourceByProvider", |
| 91 |
|
"namedSourcesByObject", |
| 92 |
|
"typedSources" |
| 93 |
|
}) |
| 94 |
|
void getDataGeneratorBySpring( |
| 95 |
|
String methodName |
| 96 |
|
) throws Exception { |
| 97 |
4 |
var appContext = new AnnotationConfigApplicationContext(SpringProvidingConfig.class); |
| 98 |
4 |
var sampleTestingObject = new Object(); |
| 99 |
|
|
| 100 |
4 |
var testedFactory = new SpringContextObjectFactory( |
| 101 |
|
appContext, sampleTestingObject, getByMethod(methodName), |
| 102 |
|
mockDataGenContext, methodName |
| 103 |
|
); |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
4 |
var testedGenerator = (FakeDataGenerator)testedFactory.getDataGenerator(); |
| 109 |
4 |
assertThat(testedGenerator.id) |
| 110 |
|
.isEqualTo(1); |
| 111 |
|
|
| 112 |
|
|
| 113 |
4 |
verifyNoneOfDataGenContextCalled(sampleTestingObject); |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
|
| 117 |
|
@link@link |
| 118 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0.38 |
4-
|
|
| 119 |
1 |
@Test... |
| 120 |
|
void autoDataGeneratorByDataSource() throws Exception |
| 121 |
|
{ |
| 122 |
1 |
var sampleTestingObject = new Object(); |
| 123 |
|
|
| 124 |
1 |
DataGenerator<TableFacet> testedGenerator; |
| 125 |
|
|
| 126 |
1 |
try (var appContext = new AnnotationConfigApplicationContext(DataSourceSpringContext.class)) { |
| 127 |
1 |
var testedFactory = new SpringContextObjectFactory( |
| 128 |
|
appContext, sampleTestingObject, getByMethod("defaultMethod"), |
| 129 |
|
mockDataGenContext, "defaultMethod" |
| 130 |
|
); |
| 131 |
|
|
| 132 |
1 |
testedGenerator = testedFactory.getDataGenerator(); |
| 133 |
|
} |
| 134 |
|
|
| 135 |
1 |
assertThat(testedGenerator) |
| 136 |
|
.isNotInstanceOf(FakeDataGenerator.class); |
| 137 |
|
|
| 138 |
1 |
verifyNoneOfDataGenContextCalled(sampleTestingObject); |
| 139 |
|
} |
| 140 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 141 |
5 |
@SuppressWarnings({ "unchecked" })... |
| 142 |
|
private void verifyNoneOfDataGenContextCalled(Object sampleTestingObject) |
| 143 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 144 |
5 |
new Verifications() {{... |
| 145 |
5 |
mockDataGenContext.getDefaultDataGenerator(sampleTestingObject); |
| 146 |
5 |
times = 0; |
| 147 |
|
|
| 148 |
5 |
mockDataGenContext.getTypedDataGenerator( |
| 149 |
|
(Class<DataGeneratorProvider<TableFacet>>)any, sampleTestingObject |
| 150 |
|
); |
| 151 |
5 |
times = 0; |
| 152 |
|
|
| 153 |
5 |
mockDataGenContext.getNamedDataGenerator( |
| 154 |
|
anyString, sampleTestingObject |
| 155 |
|
); |
| 156 |
5 |
times = 0; |
| 157 |
|
}}; |
| 158 |
|
} |
| 159 |
|
|
| 160 |
|
|
| 161 |
|
@link |
| 162 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
| 163 |
1 |
@Test... |
| 164 |
|
void fallbackToDefaultTableFacet() throws Exception |
| 165 |
|
{ |
| 166 |
1 |
var appContext = new AnnotationConfigApplicationContext(EmptyConfig.class); |
| 167 |
|
|
| 168 |
1 |
var testedFactory = new SpringContextObjectFactory( |
| 169 |
|
appContext, this, getByMethod("defaultMethod"), |
| 170 |
|
dataGenContext, "defaultMethod" |
| 171 |
|
); |
| 172 |
|
|
| 173 |
1 |
var testedGenerator = testedFactory.getTableFacets(); |
| 174 |
1 |
assertThat(testedGenerator) |
| 175 |
|
.element(0) |
| 176 |
|
.extracting(t -> t.getNumberOfRows()) |
| 177 |
|
.isEqualTo(3); |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
| 181 |
|
@link |
| 182 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4-
|
|
| 183 |
4 |
@SuppressWarnings("unchecked")... |
| 184 |
|
@ParameterizedTest |
| 185 |
|
@CsvSource({ |
| 186 |
|
"defaultMethod,1", |
| 187 |
|
"nameSourceByProvider,4", |
| 188 |
|
"namedSourcesByObject,4", |
| 189 |
|
"typedSources,1", |
| 190 |
|
}) |
| 191 |
|
void getTableFacetsBySpring( |
| 192 |
|
String methodName, int expectedSize |
| 193 |
|
) throws Exception { |
| 194 |
4 |
var appContext = new AnnotationConfigApplicationContext(SpringProvidingConfig.class); |
| 195 |
4 |
var sampleTestingObject = new Object(); |
| 196 |
|
|
| 197 |
4 |
var testedFactory = new SpringContextObjectFactory( |
| 198 |
|
appContext, sampleTestingObject, getByMethod(methodName), |
| 199 |
|
mockDataGenContext, methodName |
| 200 |
|
); |
| 201 |
|
|
| 202 |
4 |
var testedFacets = testedFactory.getTableFacets(); |
| 203 |
4 |
assertThat(testedFacets) |
| 204 |
|
.hasSize(expectedSize) |
| 205 |
|
.allSatisfy(t -> { |
| 206 |
10 |
assertThat(t.getNumberOfRows()) |
| 207 |
|
.isEqualTo(5); |
| 208 |
|
}); |
| 209 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 210 |
4 |
new Verifications() {{... |
| 211 |
4 |
mockDataGenContext.getDefaultTableFacets(sampleTestingObject); |
| 212 |
4 |
times = 0; |
| 213 |
|
|
| 214 |
4 |
mockDataGenContext.getTypedTableFacets( |
| 215 |
|
(Class<? extends TableFacetsProvider<TableFacet>>)any, methodName |
| 216 |
|
); |
| 217 |
4 |
times = 0; |
| 218 |
|
|
| 219 |
4 |
mockDataGenContext.getNamedTableFacets(anyString, sampleTestingObject); |
| 220 |
4 |
times = 0; |
| 221 |
|
}}; |
| 222 |
|
} |
| 223 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 224 |
11 |
private static GenData getByMethod(String methodName)... |
| 225 |
|
{ |
| 226 |
11 |
return MethodUtils.getMatchingMethod( |
| 227 |
|
GenDataCases.class, methodName |
| 228 |
|
) |
| 229 |
|
.getAnnotation(GenData.class); |
| 230 |
|
} |
| 231 |
|
} |
| 232 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 4 |
Complexity Density: - |
|
| 233 |
|
class GenDataCases { |
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 234 |
0 |
@GenData... |
| 235 |
|
void defaultMethod() {} |
| 236 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 237 |
0 |
@GenData(... |
| 238 |
|
generatorName = "redDataGenerator", |
| 239 |
|
facetsNames = { "redTableFacet", "tableFacetOfList", "tableFacetOfArray", "tableFacetOfStream" } |
| 240 |
|
) |
| 241 |
|
void nameSourceByProvider() {} |
| 242 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 243 |
0 |
@GenData(... |
| 244 |
|
generatorName = "blueDataGenerator", |
| 245 |
|
facetsNames = { "blueTableFacet", "tableFacetOfList", "tableFacetOfArray", "tableFacetOfStream" } |
| 246 |
|
) |
| 247 |
|
void namedSourcesByObject() {} |
| 248 |
|
|
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 249 |
0 |
@GenData(... |
| 250 |
|
generator = SpringProvidingConfig.RedDataGeneratorProvider.class, |
| 251 |
|
facets = { SpringProvidingConfig.RedTableFacetProvider.class } |
| 252 |
|
) |
| 253 |
|
void typedSources() {} |
| 254 |
|
} |
| 255 |
|
|
| 256 |
|
@Configuration |
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 257 |
|
class EmptyConfig {} |
| 258 |
|
|
| 259 |
|
@JdbcTest |
| 260 |
|
@AutoConfigureTestDatabase(replace = Replace.NONE) |
| 261 |
|
@Configuration |
| |
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 262 |
|
class DataSourceSpringContext {} |
| 263 |
|
|
| 264 |
|
@Configuration |
| |
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 9 |
Complexity Density: 1 |
|
| 265 |
|
class SpringProvidingConfig { |
| 266 |
|
static TableFacet sampleFacet = JdbcTableFacet.builder("TABLE_SAMPLE") |
| 267 |
|
.column("col_sample").fixed("sample") |
| 268 |
|
.numberOfRows(5) |
| 269 |
|
.build(); |
| 270 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 271 |
|
static class RedDataGeneratorProvider implements DataGeneratorProvider<TableFacet> { |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 272 |
2 |
@Override... |
| 273 |
|
public DataGenerator<TableFacet> get() |
| 274 |
|
{ |
| 275 |
2 |
return new FakeDataGenerator(1); |
| 276 |
|
} |
| 277 |
|
} |
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 278 |
|
static class RedTableFacetProvider implements TableFacetProvider<TableFacet> { |
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 279 |
2 |
@Override... |
| 280 |
|
public TableFacet getOne() |
| 281 |
|
{ |
| 282 |
2 |
return sampleFacet; |
| 283 |
|
} |
| 284 |
|
} |
| 285 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 286 |
8 |
@Primary @Bean... |
| 287 |
|
TableFacetProvider<TableFacet> defaultTableFacets() |
| 288 |
|
{ |
| 289 |
8 |
return () -> sampleFacet; |
| 290 |
|
} |
| 291 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 292 |
8 |
@Primary @Bean... |
| 293 |
|
DataGeneratorProvider<?> dataGeneratorProvider() |
| 294 |
|
{ |
| 295 |
8 |
return () -> new FakeDataGenerator(1); |
| 296 |
|
} |
| 297 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 298 |
8 |
@Bean("redDataGenerator")... |
| 299 |
|
RedDataGeneratorProvider redDataGeneratorProvider() |
| 300 |
|
{ |
| 301 |
8 |
return new RedDataGeneratorProvider(); |
| 302 |
|
} |
| 303 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 304 |
8 |
@Bean("blueDataGenerator")... |
| 305 |
|
DataGenerator<?> blueDataGenerator() |
| 306 |
|
{ |
| 307 |
8 |
return new FakeDataGenerator(1); |
| 308 |
|
} |
| 309 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 310 |
8 |
@Bean("redTableFacet")... |
| 311 |
|
RedTableFacetProvider redTableFacet() |
| 312 |
|
{ |
| 313 |
8 |
return new RedTableFacetProvider(); |
| 314 |
|
} |
| 315 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 316 |
8 |
@Bean("blueTableFacet")... |
| 317 |
|
TableFacet blueTableFacet() |
| 318 |
|
{ |
| 319 |
8 |
return sampleFacet; |
| 320 |
|
} |
| 321 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 322 |
8 |
@Bean("tableFacetOfArray")... |
| 323 |
|
TableFacet[] tableFacetOfArray() |
| 324 |
|
{ |
| 325 |
8 |
return new TableFacet[] { sampleFacet }; |
| 326 |
|
} |
| 327 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 328 |
8 |
@Bean("tableFacetOfList")... |
| 329 |
|
List<TableFacet> tableFacetOfList() |
| 330 |
|
{ |
| 331 |
8 |
return List.of(sampleFacet); |
| 332 |
|
} |
| 333 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 334 |
8 |
@Bean("tableFacetOfStream")... |
| 335 |
|
Stream<TableFacet> tableFacetOfStream() |
| 336 |
|
{ |
| 337 |
8 |
return Stream.of(sampleFacet); |
| 338 |
|
} |
| 339 |
|
} |
| 340 |
|
|
| |
|
| 50% |
Uncovered Elements: 4 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
| 341 |
|
class FakeDataGenerator implements DataGenerator<TableFacet> { |
| 342 |
|
final int id; |
| 343 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 344 |
11 |
FakeDataGenerator()... |
| 345 |
|
{ |
| 346 |
11 |
this(-1); |
| 347 |
|
} |
| 348 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 349 |
22 |
FakeDataGenerator(int id)... |
| 350 |
|
{ |
| 351 |
22 |
this.id = id; |
| 352 |
|
} |
| 353 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 354 |
0 |
@Override... |
| 355 |
|
public FakeDataGenerator withSetting(DataSettingInfo setting) |
| 356 |
|
{ |
| 357 |
0 |
throw new UnsupportedOperationException("Unimplemented method 'withSetting'"); |
| 358 |
|
} |
| 359 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 360 |
0 |
@Override... |
| 361 |
|
public int generate(List<TableFacet> tables) |
| 362 |
|
{ |
| 363 |
0 |
throw new UnsupportedOperationException("Unimplemented method 'generate'"); |
| 364 |
|
} |
| 365 |
|
} |