Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ dependencies {
implementation("com.github.spotbugs.snom:spotbugs-gradle-plugin:6.0.1")
implementation("org.owasp:dependency-check-gradle:10.0.3")

implementation("org.springframework.boot:spring-boot-gradle-plugin:3.5.7")
implementation("org.springframework.boot:spring-boot-gradle-plugin:4.0.1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("org.spockframework:spock-spring:2.4-M4-groovy-4.0")
testImplementation("org.spockframework:spock-spring:2.4-groovy-5.0")
}
5 changes: 3 additions & 2 deletions buildSrc/src/main/kotlin/unit-api.test.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ tasks.jacocoTestReport {

dependencies {
testCompileOnly("org.projectlombok:lombok:1.18.32")
testImplementation("org.apache.groovy:groovy:4.0.21")
testImplementation("org.spockframework:spock-core:2.4-M4-groovy-4.0")
testImplementation("org.apache.groovy:groovy:5.0.3")
testImplementation("org.spockframework:spock-core:2.4-groovy-5.0")
testRuntimeOnly("net.bytebuddy:byte-buddy:1.14.13")
testAnnotationProcessor("org.projectlombok:lombok:1.18.32")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=3.0.10-SNAPSHOT
version=4.0.0-SNAPSHOT
org.gradle.caching=true
1 change: 1 addition & 0 deletions spring-boot-jackson-starter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dependencies {
api project(":spring-boot-core-starter")

testImplementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-restclient'
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.raynigon.unit.api.jackson

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.web.client.RestClient
import tools.jackson.databind.ObjectMapper
import com.raynigon.unit.api.jackson.helpers.BasicApplicationConfig
import com.raynigon.unit.api.jackson.helpers.BasicRestController
import com.raynigon.unit.api.jackson.helpers.BasicService
import com.raynigon.unit.api.jackson.helpers.WeatherEntity
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.TestPropertySource
import spock.lang.Ignore
import spock.lang.Specification

import static com.raynigon.unit.api.core.units.si.SISystemUnitsConstants.Celsius
Expand All @@ -32,18 +31,29 @@ import static com.raynigon.unit.api.core.units.si.SISystemUnitsConstants.Percent
)
class JacksonStarterApplicationSpec extends Specification {

@LocalServerPort
int port

@Autowired
BasicService service

@Autowired
TestRestTemplate restTemplate
RestClient.Builder restClientBuilder

@Autowired
ObjectMapper objectMapper

RestClient restClient

def setup() {
restClient = restClientBuilder
.baseUrl("http://localhost:$port")
.build()
}

def 'context setup works'() {
expect:
objectMapper.getRegisteredModuleIds().contains(new UnitApiModule().getTypeId())
objectMapper.registeredModules().any { it instanceof UnitApiModule }
}

def 'entity creation works'() {
Expand All @@ -56,7 +66,12 @@ class JacksonStarterApplicationSpec extends Specification {
]

when:
def response = restTemplate.postForEntity("/api/basic-entity", data, Map.class)
def response = restClient
.post()
.uri("/api/basic-entity")
.body(data)
.retrieve()
.toEntity(Map.class)

then:
response.statusCode.'2xxSuccessful'
Expand Down Expand Up @@ -96,8 +111,8 @@ class JacksonStarterApplicationSpec extends Specification {

where:
entity | expected
new WeatherEntity(Celsius(30), Percent(10)) | '{"temperature":30.0,"humidity":"10.0%"}'
new WeatherEntity(Celsius(30), Percent(10.1)) | '{"temperature":30.0,"humidity":"10.1%"}'
new WeatherEntity(Celsius(30), Percent(-10)) | '{"temperature":30.0,"humidity":"-10.0%"}'
new WeatherEntity(Celsius(30), Percent(10)) | '{"humidity":"10.0%","temperature":30.0}'
new WeatherEntity(Celsius(30), Percent(10.1)) | '{"humidity":"10.1%","temperature":30.0}'
new WeatherEntity(Celsius(30), Percent(-10)) | '{"humidity":"-10.0%","temperature":30.0}'
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.raynigon.unit.api.jackson.helpers

import com.fasterxml.jackson.databind.ObjectMapper
import tools.jackson.databind.ObjectMapper
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import javax.measure.Quantity
import javax.measure.quantity.Length
import java.lang.annotation.Annotation

import static com.raynigon.unit.api.core.units.si.SISystemUnitsConstants.Metre
import static com.raynigon.unit.api.core.units.si.SISystemUnitsConstants.Metre as cMetre

class QuantityTypeSpec extends Specification {

Expand Down Expand Up @@ -158,17 +158,17 @@ class QuantityTypeSpec extends Specification {
where:
q0 | q1 | expected
null | null | true
null | Metre(0) | false
Metre(0) | null | false
Metre(0) | Metre(0) | true
null | cMetre(0) | false
cMetre(0) | null | false
cMetre(0) | cMetre(0) | true
}

def 'generate quantity hash code'() {
given:
QuantityType type = new QuantityType()

when:
def result = type.hashCode(Metre(0))
def result = type.hashCode(cMetre(0))

then:
result != 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.raynigon.unit.api.springdoc;

import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.databind.JavaType;
import com.raynigon.unit.api.core.annotation.QuantityShape;
import com.raynigon.unit.api.core.service.UnitsApiService;
import com.raynigon.unit.api.jackson.annotation.JsonUnit;
Expand All @@ -14,7 +14,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.*;

Check warning on line 17 in spring-boot-springdoc-starter/src/main/java/com/raynigon/unit/api/springdoc/UnitApiPropertyCustomizer.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck

Using the '.*' form of import should be avoided - java.util.*.
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.measure.Quantity;
Expand All @@ -24,7 +24,7 @@
import lombok.val;
import org.springdoc.core.customizers.PropertyCustomizer;

public class UnitApiPropertyCustomizer implements PropertyCustomizer {

Check warning on line 27 in spring-boot-springdoc-starter/src/main/java/com/raynigon/unit/api/springdoc/UnitApiPropertyCustomizer.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocTypeCheck

Missing a Javadoc comment.

@Override
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -72,7 +72,7 @@

@SuppressWarnings({"rawtypes", "unchecked"})
private Unit<?> resolveUnit(AnnotatedType type) {
SimpleType quantityType = (SimpleType) type.getType();
JavaType quantityType = (JavaType) type.getType();
Class<?> quantityBoundType = quantityType.getBindings().getBoundType(0).getRawClass();
Unit<?> unit = UnitsApiService.getInstance().getUnit((Class) quantityBoundType);
JsonUnit jsonUnit = resolveJsonUnit(type);
Expand All @@ -95,19 +95,19 @@

private QuantityShape resolveShape(AnnotatedType type) {
JsonUnit jsonUnit = resolveJsonUnit(type);
if (jsonUnit == null) return QuantityShape.NUMBER;

Check warning on line 98 in spring-boot-springdoc-starter/src/main/java/com/raynigon/unit/api/springdoc/UnitApiPropertyCustomizer.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck

'if' construct must use '{}'s.
return JsonUnitHelper.getShape(jsonUnit);
}

private Set<Annotation> resolveConstraints(AnnotatedType type) {
return Arrays.stream(type.getCtxAnnotations())
.filter(it -> UnitMin.class.isAssignableFrom(it.annotationType()) || UnitMax.class.isAssignableFrom(it.annotationType()))

Check warning on line 104 in spring-boot-springdoc-starter/src/main/java/com/raynigon/unit/api/springdoc/UnitApiPropertyCustomizer.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 137).
.collect(Collectors.toSet());
}

private boolean isApplicable(Type type) {
return (type instanceof SimpleType)
&& Quantity.class.isAssignableFrom(((SimpleType) type).getRawClass());
return (type instanceof JavaType)
&& Quantity.class.isAssignableFrom(((JavaType) type).getRawClass());
}

private String buildDescription(AnnotatedType type, Schema property, Unit<?> unit, Set<Annotation> constraints) {
Expand All @@ -132,7 +132,7 @@
UnitMax max = getAnnotation(constraints, UnitMax.class);
Unit<?> maxUnit = AbstractUnitValidator.createUnit(max.unit());

result += " and must be between " + min.value() + " " + minUnit.getSymbol() + " and " + max.value() + " " + maxUnit.getSymbol();

Check warning on line 135 in spring-boot-springdoc-starter/src/main/java/com/raynigon/unit/api/springdoc/UnitApiPropertyCustomizer.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck

Line is longer than 120 characters (found 144).
} else if (constraints.stream().anyMatch(it -> UnitMin.class.isAssignableFrom(it.annotationType()))) {
// Case 2: UnitMin is present in the constraints set
UnitMin min = getAnnotation(constraints, UnitMin.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.raynigon.unit.api.springdoc

import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.type.SimpleType
import com.fasterxml.jackson.databind.type.TypeBindings
import com.raynigon.unit.api.core.annotation.QuantityShape
import com.raynigon.unit.api.core.units.si.power.Watt
Expand Down Expand Up @@ -36,8 +35,6 @@ class UnitApiPropertyCustomizerSpec extends Specification {
Quantity.class,
[new SimpleType(Speed.class)] as JavaType[],
),
null,
null
)
annotatedType.ctxAnnotations([] as Annotation[])

Expand All @@ -63,8 +60,6 @@ class UnitApiPropertyCustomizerSpec extends Specification {
Quantity.class,
[new SimpleType(Speed.class)] as JavaType[],
),
null,
null
)
JsonUnit jsonUnit = Mock(JsonUnit)
annotatedType.ctxAnnotations([
Expand Down Expand Up @@ -99,8 +94,6 @@ class UnitApiPropertyCustomizerSpec extends Specification {
Quantity.class,
[new SimpleType(Speed.class)] as JavaType[],
),
null,
null
)
JsonUnit jsonUnit = Mock(JsonUnit)
annotatedType.ctxAnnotations([
Expand Down Expand Up @@ -138,8 +131,6 @@ class UnitApiPropertyCustomizerSpec extends Specification {
Quantity.class,
[new SimpleType(Speed.class)] as JavaType[],
),
null,
null
)
JsonUnit jsonUnit = Mock(JsonUnit)
annotatedType.ctxAnnotations([
Expand Down Expand Up @@ -182,9 +173,8 @@ class UnitApiPropertyCustomizerSpec extends Specification {
Quantity.class,
[new SimpleType(Speed.class)] as JavaType[],
),
null,
null
)

and:
JsonUnit jsonUnit = Mock(JsonUnit)
jsonUnit.annotationType() >> JsonUnit.class
Expand Down Expand Up @@ -231,8 +221,6 @@ class UnitApiPropertyCustomizerSpec extends Specification {
Quantity.class,
[new SimpleType(Speed.class)] as JavaType[],
),
null,
null
)
and:
JsonUnit jsonUnit = Mock(JsonUnit)
Expand Down Expand Up @@ -279,9 +267,7 @@ class UnitApiPropertyCustomizerSpec extends Specification {
TypeBindings.create(
Quantity.class,
[new SimpleType(Speed.class)] as JavaType[],
),
null,
null
)
)
and:
JsonUnit jsonUnit = Mock(JsonUnit)
Expand Down Expand Up @@ -319,4 +305,124 @@ class UnitApiPropertyCustomizerSpec extends Specification {
result.type == "string"
result.description == "speed is given in Kilometre per Hour (km/h) and must be between 0.0 W and 10.0 W"
}

class SimpleType extends JavaType {

private TypeBindings bindings

protected SimpleType(Class<?> raw) {
super(raw, 0, null, null, false)
this.bindings = null
}

protected SimpleType(Class<?> raw, TypeBindings bindings) {
super(raw, 0, null, null, false)
this.bindings = bindings
}

@Override
JavaType withContentType(JavaType contentType) {
return null
}

@Override
JavaType withStaticTyping() {
return null
}

@Override
JavaType withTypeHandler(Object h) {
return null
}

@Override
JavaType withContentTypeHandler(Object h) {
return null
}

@Override
JavaType withValueHandler(Object h) {
return null
}

@Override
JavaType withContentValueHandler(Object h) {
return null
}

@Override
JavaType refine(Class<?> rawType, TypeBindings bindings, JavaType superClass, JavaType[] superInterfaces) {
return null
}

@Override
boolean isContainerType() {
return false
}

@Override
int containedTypeCount() {
return 0
}

@Override
JavaType containedType(int index) {
return null
}

@Override
String containedTypeName(int index) {
return null
}

@Override
String toCanonical() {
return null
}

@Override
TypeBindings getBindings() {
return bindings
}

@Override
JavaType findSuperType(Class<?> erasedTarget) {
return null
}

@Override
JavaType getSuperClass() {
return null
}

@Override
List<JavaType> getInterfaces() {
return null
}

@Override
JavaType[] findTypeParameters(Class<?> expType) {
return null
}

@Override
StringBuilder getGenericSignature(StringBuilder sb) {
return null
}

@Override
StringBuilder getErasedSignature(StringBuilder sb) {
return null
}

@Override
String toString() {
return null
}

@Override
boolean equals(Object o) {
return false
}
}
}
4 changes: 2 additions & 2 deletions unit-api-jackson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ plugins {
}

dependencies {
compileOnly 'com.fasterxml.jackson.core:jackson-databind:2.20.1'
compileOnly 'tools.jackson.core:jackson-databind:3.0.3'
api project(":unit-api-core")

testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.20.1'
testImplementation 'tools.jackson.core:jackson-databind:3.0.3'
}
Loading
Loading