Skip to content

Commit

Permalink
add testing and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
SirCotare committed Sep 9, 2024
1 parent 215b271 commit f50e118
Show file tree
Hide file tree
Showing 67 changed files with 1,986 additions and 90 deletions.
50 changes: 49 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<!-- used to scan the classpath -->
<!-- https://mvnrepository.com/artifact/io.github.classgraph/classgraph -->
<dependency>
Expand Down Expand Up @@ -88,6 +88,54 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>

<!-- Database -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>

<!-- Test-Containers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.20.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.20.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.20.1</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package it.aboutbits.springboot.toolbox.boot.type;

import it.aboutbits.springboot.toolbox.reflection.util.ClassScannerUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import it.aboutbits.springboot.toolbox.type.jackson.CustomTypeDeserializer;
import it.aboutbits.springboot.toolbox.type.jackson.CustomTypeSerializer;
import it.aboutbits.springboot.toolbox.type.mvc.CustomTypePropertyEditor;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
Expand All @@ -14,65 +12,39 @@
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.InitBinder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Configuration
public class CustomTypeConfiguration {
public static final String LIBRARY_BASE_PACKAGE_NAME = "it.aboutbits.springboot.toolbox";

private String[] packageNamesToScan;
private ClassScannerUtil.ClassScanner classScanner;

public void setAdditionalTypePackages(String[] additionalTypePackages) {
var tmp = new ArrayList<String>();
tmp.add(LIBRARY_BASE_PACKAGE_NAME);
tmp.addAll(Arrays.asList(additionalTypePackages));

this.packageNamesToScan = tmp.toArray(new String[0]);

classScanner = ClassScannerUtil.getScannerForPackages(packageNamesToScan);
}
@ControllerAdvice
public static class CustomTypePropertyBinder {
@SuppressWarnings("rawtypes")
private final Set<Class<? extends CustomType>> types;

@PostConstruct
public void init() {
log.info("CustomTypeConfiguration enabled. Scanning: {}", Arrays.toString(packageNamesToScan));
}
public CustomTypePropertyBinder(CustomTypeScanner configuration) {
this.types = configuration.findAllCustomTypeRecords();
}

@ControllerAdvice
public class CustomTypePropertyBinder {
@InitBinder
public void initBinder(WebDataBinder binder) {
var types = findAllCustomTypeRecords();
for (var clazz : types) {
binder.registerCustomEditor(clazz, new CustomTypePropertyEditor<>(clazz));
}
}
}

@Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
var types = findAllCustomTypeRecords();
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer(CustomTypeScanner configuration) {
var types = configuration.findAllCustomTypeRecords();

var deserializers = types.stream()
.map(CustomTypeDeserializer::new)
.toList()
.toArray(new CustomTypeDeserializer[types.size()]);

classScanner.close();

return builder -> builder
.serializers(new CustomTypeSerializer())
.deserializers(deserializers);
}

@SuppressWarnings("rawtypes")
private Set<Class<? extends CustomType>> findAllCustomTypeRecords() {
return classScanner.getSubTypesOf(CustomType.class).stream()
.filter(Record.class::isAssignableFrom)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionR
);
var value = attributes.getStringArray("additionalTypePackages");

var builder = BeanDefinitionBuilder.genericBeanDefinition(CustomTypeConfiguration.class);
var builder = BeanDefinitionBuilder.genericBeanDefinition(CustomTypeScanner.class);
builder.addPropertyValue("additionalTypePackages", value);
registry.registerBeanDefinition("CustomTypeConfiguration", builder.getBeanDefinition());
registry.registerBeanDefinition("CustomTypeScanner", builder.getBeanDefinition());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package it.aboutbits.springboot.toolbox.boot.type;

import it.aboutbits.springboot.toolbox.reflection.util.ClassScannerUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class CustomTypeScanner {
public static final String LIBRARY_BASE_PACKAGE_NAME = "it.aboutbits.springboot.toolbox";

private String[] packageNamesToScan;
private ClassScannerUtil.ClassScanner classScanner;

public void setAdditionalTypePackages(String[] additionalTypePackages) {
var tmp = new ArrayList<String>();
tmp.add(LIBRARY_BASE_PACKAGE_NAME);
tmp.addAll(Arrays.stream(additionalTypePackages)
.filter(item -> !item.isBlank())
.collect(Collectors.toSet()));

this.packageNamesToScan = tmp.toArray(new String[0]);

classScanner = ClassScannerUtil.getScannerForPackages(packageNamesToScan);
}

@PostConstruct
public void init() {
log.info("CustomTypeConfiguration enabled. Scanning: {}", Arrays.toString(packageNamesToScan));
}

@SuppressWarnings("rawtypes")
public Set<Class<? extends CustomType>> findAllCustomTypeRecords() {
return classScanner.getSubTypesOf(CustomType.class).stream()
.filter(Record.class::isAssignableFrom)
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import(CustomTypeConfigurationRegistrar.class)
@Import({CustomTypeConfigurationRegistrar.class, CustomTypeConfiguration.class})
public @interface RegisterCustomTypes {
String[] additionalTypePackages() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.aboutbits.springboot.toolbox.persistence.identity.EntityId;
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
Expand Down Expand Up @@ -38,6 +39,9 @@ public Schema<?> resolve(
if (Long.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Long.TYPE));
}
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Long.TYPE));
}
if (Float.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Float.TYPE));
}
Expand All @@ -47,6 +51,9 @@ public Schema<?> resolve(
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Double.TYPE));
}
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Double.TYPE));
}
if (String.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(String.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import it.aboutbits.springboot.toolbox.persistence.identity.EntityId;
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.CustomType;
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.customizers.PropertyCustomizer;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.BigInteger;

@Slf4j
@Component
Expand Down Expand Up @@ -53,6 +55,14 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
property.set$ref(null);
return property;
}
if (BigInteger.class.isAssignableFrom(wrappedType)) {
property.type("integer");
property.format("int64");
property.setDescription(displayName);
property.setProperties(null);
property.set$ref(null);
return property;
}
if (Float.class.isAssignableFrom(wrappedType)) {
property.type("number");
property.format("float");
Expand All @@ -77,6 +87,15 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
property.set$ref(null);
return property;
}

if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
property.type("number");
property.format("");
property.setDescription(displayName);
property.setProperties(null);
property.set$ref(null);
return property;
}
if (String.class.isAssignableFrom(wrappedType)) {
property.type("string");
property.format(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import io.swagger.v3.oas.models.media.Schema;
import it.aboutbits.springboot.toolbox.persistence.identity.EntityId;
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Iterator;

@Component
Expand Down Expand Up @@ -37,6 +39,9 @@ public Schema<?> resolve(
if (Long.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Long.TYPE));
}
if (BigInteger.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Long.TYPE));
}
if (Float.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Float.TYPE));
}
Expand All @@ -46,6 +51,9 @@ public Schema<?> resolve(
if (BigDecimal.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Double.TYPE));
}
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(Double.TYPE));
}
if (String.class.isAssignableFrom(wrappedType)) {
return context.resolve(new AnnotatedType(String.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import io.swagger.v3.oas.models.media.Schema;
import it.aboutbits.springboot.toolbox.persistence.identity.EntityId;
import it.aboutbits.springboot.toolbox.reflection.util.RecordReflectionUtil;
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.customizers.PropertyCustomizer;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.BigInteger;

@Slf4j
@Component
Expand Down Expand Up @@ -59,6 +61,14 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
property.set$ref(null);
return property;
}
if (BigInteger.class.isAssignableFrom(wrappedType)) {
property.type("integer");
property.format("int64");
property.setDescription(displayName);
property.setProperties(null);
property.set$ref(null);
return property;
}
if (Float.class.isAssignableFrom(wrappedType)) {
property.type("number");
property.format("float");
Expand All @@ -83,6 +93,14 @@ public Schema<?> customize(Schema property, AnnotatedType annotatedType) {
property.set$ref(null);
return property;
}
if (ScaledBigDecimal.class.isAssignableFrom(wrappedType)) {
property.type("number");
property.format("");
property.setDescription(displayName);
property.setProperties(null);
property.set$ref(null);
return property;
}
if (String.class.isAssignableFrom(wrappedType)) {
property.type("string");
property.format(null);
Expand Down
Loading

0 comments on commit f50e118

Please sign in to comment.