Skip to content

Commit f50e118

Browse files
committed
add testing and fix bugs
1 parent 215b271 commit f50e118

67 files changed

Lines changed: 1986 additions & 90 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pom.xml

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<artifactId>lombok</artifactId>
4343
<optional>true</optional>
4444
</dependency>
45-
45+
4646
<!-- used to scan the classpath -->
4747
<!-- https://mvnrepository.com/artifact/io.github.classgraph/classgraph -->
4848
<dependency>
@@ -88,6 +88,54 @@
8888
<dependency>
8989
<groupId>org.springframework.boot</groupId>
9090
<artifactId>spring-boot-starter-test</artifactId>
91+
<scope>test</scope>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.springframework.boot</groupId>
95+
<artifactId>spring-boot-starter-web</artifactId>
96+
<scope>test</scope>
97+
</dependency>
98+
99+
<!-- Database -->
100+
<dependency>
101+
<groupId>org.liquibase</groupId>
102+
<artifactId>liquibase-core</artifactId>
103+
<scope>test</scope>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.postgresql</groupId>
107+
<artifactId>postgresql</artifactId>
108+
<scope>test</scope>
109+
</dependency>
110+
111+
<!-- Test-Containers -->
112+
<dependency>
113+
<groupId>org.testcontainers</groupId>
114+
<artifactId>testcontainers</artifactId>
115+
<version>1.20.1</version>
116+
<scope>test</scope>
117+
<exclusions>
118+
<exclusion>
119+
<groupId>org.hamcrest</groupId>
120+
<artifactId>hamcrest-core</artifactId>
121+
</exclusion>
122+
<exclusion>
123+
<groupId>org.hamcrest</groupId>
124+
<artifactId>hamcrest-library</artifactId>
125+
</exclusion>
126+
</exclusions>
127+
</dependency>
128+
<dependency>
129+
<groupId>org.testcontainers</groupId>
130+
<artifactId>junit-jupiter</artifactId>
131+
<version>1.20.1</version>
132+
<scope>test</scope>
133+
</dependency>
134+
<dependency>
135+
<groupId>org.testcontainers</groupId>
136+
<artifactId>postgresql</artifactId>
137+
<version>1.20.1</version>
138+
<scope>test</scope>
91139
</dependency>
92140
</dependencies>
93141

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package it.aboutbits.springboot.toolbox.boot.type;
22

3-
import it.aboutbits.springboot.toolbox.reflection.util.ClassScannerUtil;
43
import it.aboutbits.springboot.toolbox.type.CustomType;
54
import it.aboutbits.springboot.toolbox.type.jackson.CustomTypeDeserializer;
65
import it.aboutbits.springboot.toolbox.type.jackson.CustomTypeSerializer;
76
import it.aboutbits.springboot.toolbox.type.mvc.CustomTypePropertyEditor;
8-
import jakarta.annotation.PostConstruct;
97
import lombok.extern.slf4j.Slf4j;
108
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
119
import org.springframework.context.annotation.Bean;
@@ -14,65 +12,39 @@
1412
import org.springframework.web.bind.annotation.ControllerAdvice;
1513
import org.springframework.web.bind.annotation.InitBinder;
1614

17-
import java.util.ArrayList;
18-
import java.util.Arrays;
1915
import java.util.Set;
20-
import java.util.stream.Collectors;
2116

2217
@Slf4j
2318
@Configuration
2419
public class CustomTypeConfiguration {
25-
public static final String LIBRARY_BASE_PACKAGE_NAME = "it.aboutbits.springboot.toolbox";
26-
27-
private String[] packageNamesToScan;
28-
private ClassScannerUtil.ClassScanner classScanner;
29-
30-
public void setAdditionalTypePackages(String[] additionalTypePackages) {
31-
var tmp = new ArrayList<String>();
32-
tmp.add(LIBRARY_BASE_PACKAGE_NAME);
33-
tmp.addAll(Arrays.asList(additionalTypePackages));
34-
35-
this.packageNamesToScan = tmp.toArray(new String[0]);
36-
37-
classScanner = ClassScannerUtil.getScannerForPackages(packageNamesToScan);
38-
}
20+
@ControllerAdvice
21+
public static class CustomTypePropertyBinder {
22+
@SuppressWarnings("rawtypes")
23+
private final Set<Class<? extends CustomType>> types;
3924

40-
@PostConstruct
41-
public void init() {
42-
log.info("CustomTypeConfiguration enabled. Scanning: {}", Arrays.toString(packageNamesToScan));
43-
}
25+
public CustomTypePropertyBinder(CustomTypeScanner configuration) {
26+
this.types = configuration.findAllCustomTypeRecords();
27+
}
4428

45-
@ControllerAdvice
46-
public class CustomTypePropertyBinder {
4729
@InitBinder
4830
public void initBinder(WebDataBinder binder) {
49-
var types = findAllCustomTypeRecords();
5031
for (var clazz : types) {
5132
binder.registerCustomEditor(clazz, new CustomTypePropertyEditor<>(clazz));
5233
}
5334
}
5435
}
5536

5637
@Bean
57-
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
58-
var types = findAllCustomTypeRecords();
38+
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer(CustomTypeScanner configuration) {
39+
var types = configuration.findAllCustomTypeRecords();
5940

6041
var deserializers = types.stream()
6142
.map(CustomTypeDeserializer::new)
6243
.toList()
6344
.toArray(new CustomTypeDeserializer[types.size()]);
6445

65-
classScanner.close();
66-
6746
return builder -> builder
6847
.serializers(new CustomTypeSerializer())
6948
.deserializers(deserializers);
7049
}
71-
72-
@SuppressWarnings("rawtypes")
73-
private Set<Class<? extends CustomType>> findAllCustomTypeRecords() {
74-
return classScanner.getSubTypesOf(CustomType.class).stream()
75-
.filter(Record.class::isAssignableFrom)
76-
.collect(Collectors.toSet());
77-
}
7850
}

src/main/java/it/aboutbits/springboot/toolbox/boot/type/CustomTypeConfigurationRegistrar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionR
2121
);
2222
var value = attributes.getStringArray("additionalTypePackages");
2323

24-
var builder = BeanDefinitionBuilder.genericBeanDefinition(CustomTypeConfiguration.class);
24+
var builder = BeanDefinitionBuilder.genericBeanDefinition(CustomTypeScanner.class);
2525
builder.addPropertyValue("additionalTypePackages", value);
26-
registry.registerBeanDefinition("CustomTypeConfiguration", builder.getBeanDefinition());
26+
registry.registerBeanDefinition("CustomTypeScanner", builder.getBeanDefinition());
2727
}
2828
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package it.aboutbits.springboot.toolbox.boot.type;
2+
3+
import it.aboutbits.springboot.toolbox.reflection.util.ClassScannerUtil;
4+
import it.aboutbits.springboot.toolbox.type.CustomType;
5+
import jakarta.annotation.PostConstruct;
6+
import lombok.extern.slf4j.Slf4j;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.Set;
11+
import java.util.stream.Collectors;
12+
13+
@Slf4j
14+
public class CustomTypeScanner {
15+
public static final String LIBRARY_BASE_PACKAGE_NAME = "it.aboutbits.springboot.toolbox";
16+
17+
private String[] packageNamesToScan;
18+
private ClassScannerUtil.ClassScanner classScanner;
19+
20+
public void setAdditionalTypePackages(String[] additionalTypePackages) {
21+
var tmp = new ArrayList<String>();
22+
tmp.add(LIBRARY_BASE_PACKAGE_NAME);
23+
tmp.addAll(Arrays.stream(additionalTypePackages)
24+
.filter(item -> !item.isBlank())
25+
.collect(Collectors.toSet()));
26+
27+
this.packageNamesToScan = tmp.toArray(new String[0]);
28+
29+
classScanner = ClassScannerUtil.getScannerForPackages(packageNamesToScan);
30+
}
31+
32+
@PostConstruct
33+
public void init() {
34+
log.info("CustomTypeConfiguration enabled. Scanning: {}", Arrays.toString(packageNamesToScan));
35+
}
36+
37+
@SuppressWarnings("rawtypes")
38+
public Set<Class<? extends CustomType>> findAllCustomTypeRecords() {
39+
return classScanner.getSubTypesOf(CustomType.class).stream()
40+
.filter(Record.class::isAssignableFrom)
41+
.collect(Collectors.toSet());
42+
}
43+
}

src/main/java/it/aboutbits/springboot/toolbox/boot/type/RegisterCustomTypes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
@Target({ElementType.TYPE})
1111
@Retention(RetentionPolicy.RUNTIME)
12-
@Import(CustomTypeConfigurationRegistrar.class)
12+
@Import({CustomTypeConfigurationRegistrar.class, CustomTypeConfiguration.class})
1313
public @interface RegisterCustomTypes {
1414
String[] additionalTypePackages() default "";
1515
}

src/main/java/it/aboutbits/springboot/toolbox/boot/type/swagger/CustomTypeModelConverter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import it.aboutbits.springboot.toolbox.persistence.identity.EntityId;
88
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
99
import it.aboutbits.springboot.toolbox.type.CustomType;
10+
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
1011
import org.springframework.stereotype.Component;
1112

1213
import java.math.BigDecimal;
@@ -38,6 +39,9 @@ public Schema<?> resolve(
3839
if (Long.class.isAssignableFrom(wrappedType)) {
3940
return context.resolve(new AnnotatedType(Long.TYPE));
4041
}
42+
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
43+
return context.resolve(new AnnotatedType(Long.TYPE));
44+
}
4145
if (Float.class.isAssignableFrom(wrappedType)) {
4246
return context.resolve(new AnnotatedType(Float.TYPE));
4347
}
@@ -47,6 +51,9 @@ public Schema<?> resolve(
4751
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
4852
return context.resolve(new AnnotatedType(Double.TYPE));
4953
}
54+
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
55+
return context.resolve(new AnnotatedType(Double.TYPE));
56+
}
5057
if (String.class.isAssignableFrom(wrappedType)) {
5158
return context.resolve(new AnnotatedType(String.class));
5259
}

src/main/java/it/aboutbits/springboot/toolbox/boot/type/swagger/CustomTypePropertyCustomizer.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
import it.aboutbits.springboot.toolbox.persistence.identity.EntityId;
77
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
88
import it.aboutbits.springboot.toolbox.type.CustomType;
9+
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
910
import lombok.extern.slf4j.Slf4j;
1011
import org.springdoc.core.customizers.PropertyCustomizer;
1112
import org.springframework.stereotype.Component;
1213

1314
import java.math.BigDecimal;
15+
import java.math.BigInteger;
1416

1517
@Slf4j
1618
@Component
@@ -53,6 +55,14 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
5355
property.set$ref(null);
5456
return property;
5557
}
58+
if (BigInteger.class.isAssignableFrom(wrappedType)) {
59+
property.type("integer");
60+
property.format("int64");
61+
property.setDescription(displayName);
62+
property.setProperties(null);
63+
property.set$ref(null);
64+
return property;
65+
}
5666
if (Float.class.isAssignableFrom(wrappedType)) {
5767
property.type("number");
5868
property.format("float");
@@ -77,6 +87,15 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
7787
property.set$ref(null);
7888
return property;
7989
}
90+
91+
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
92+
property.type("number");
93+
property.format("");
94+
property.setDescription(displayName);
95+
property.setProperties(null);
96+
property.set$ref(null);
97+
return property;
98+
}
8099
if (String.class.isAssignableFrom(wrappedType)) {
81100
property.type("string");
82101
property.format(null);

src/main/java/it/aboutbits/springboot/toolbox/boot/type/swagger/EntityIdModelConverter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import io.swagger.v3.oas.models.media.Schema;
77
import it.aboutbits.springboot.toolbox.persistence.identity.EntityId;
88
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
9+
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
910
import org.springframework.stereotype.Component;
1011

1112
import java.math.BigDecimal;
13+
import java.math.BigInteger;
1214
import java.util.Iterator;
1315

1416
@Component
@@ -37,6 +39,9 @@ public Schema<?> resolve(
3739
if (Long.class.isAssignableFrom(wrappedType)) {
3840
return context.resolve(new AnnotatedType(Long.TYPE));
3941
}
42+
if (BigInteger.class.isAssignableFrom(wrappedType)) {
43+
return context.resolve(new AnnotatedType(Long.TYPE));
44+
}
4045
if (Float.class.isAssignableFrom(wrappedType)) {
4146
return context.resolve(new AnnotatedType(Float.TYPE));
4247
}
@@ -46,6 +51,9 @@ public Schema<?> resolve(
4651
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
4752
return context.resolve(new AnnotatedType(Double.TYPE));
4853
}
54+
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
55+
return context.resolve(new AnnotatedType(Double.TYPE));
56+
}
4957
if (String.class.isAssignableFrom(wrappedType)) {
5058
return context.resolve(new AnnotatedType(String.class));
5159
}

src/main/java/it/aboutbits/springboot/toolbox/boot/type/swagger/EntityIdPropertyCustomizer.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import io.swagger.v3.oas.models.media.Schema;
66
import it.aboutbits.springboot.toolbox.persistence.identity.EntityId;
77
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
8+
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
89
import lombok.NonNull;
910
import lombok.extern.slf4j.Slf4j;
1011
import org.springdoc.core.customizers.PropertyCustomizer;
1112
import org.springframework.stereotype.Component;
1213

1314
import java.math.BigDecimal;
15+
import java.math.BigInteger;
1416

1517
@Slf4j
1618
@Component
@@ -59,6 +61,14 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
5961
property.set$ref(null);
6062
return property;
6163
}
64+
if (BigInteger.class.isAssignableFrom(wrappedType)) {
65+
property.type("integer");
66+
property.format("int64");
67+
property.setDescription(displayName);
68+
property.setProperties(null);
69+
property.set$ref(null);
70+
return property;
71+
}
6272
if (Float.class.isAssignableFrom(wrappedType)) {
6373
property.type("number");
6474
property.format("float");
@@ -83,6 +93,14 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
8393
property.set$ref(null);
8494
return property;
8595
}
96+
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
97+
property.type("number");
98+
property.format("");
99+
property.setDescription(displayName);
100+
property.setProperties(null);
101+
property.set$ref(null);
102+
return property;
103+
}
86104
if (String.class.isAssignableFrom(wrappedType)) {
87105
property.type("string");
88106
property.format(null);

0 commit comments

Comments
 (0)