Skip to content

Commit 2f80595

Browse files
authored
test(crd-generator): ensure type mappings Java to OpenAPIV3Schema
Signed-off-by: Marc Nuri <[email protected]>
1 parent 4dec479 commit 2f80595

File tree

5 files changed

+220
-1
lines changed

5 files changed

+220
-1
lines changed

crd-generator/pom.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
<modules>
3434
<module>apt</module>
3535
<module>api</module>
36-
<module>test</module>
3736
</modules>
37+
<!-- TODO: Remove after a fix for #5877 is implemented -->
38+
<profiles>
39+
<profile>
40+
<id>tests</id>
41+
<activation>
42+
<jdk>(,17]</jdk>
43+
</activation>
44+
<modules>
45+
<module>test</module>
46+
</modules>
47+
</profile>
48+
</profiles>
3849
</project>

crd-generator/test/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@
5151
<scope>test</scope>
5252
</dependency>
5353

54+
<dependency>
55+
<groupId>org.junit.jupiter</groupId>
56+
<artifactId>junit-jupiter-params</artifactId>
57+
<scope>test</scope>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>org.assertj</groupId>
62+
<artifactId>assertj-core</artifactId>
63+
<scope>test</scope>
64+
</dependency>
65+
5466
<dependency>
5567
<groupId>javax.validation</groupId>
5668
<artifactId>validation-api</artifactId>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crd.generator.types;
17+
18+
import io.fabric8.kubernetes.client.CustomResource;
19+
import io.fabric8.kubernetes.model.annotation.Group;
20+
import io.fabric8.kubernetes.model.annotation.Version;
21+
22+
@Group("type-mappings.fabric8.io")
23+
@Version("v1")
24+
public class TypeMappings extends CustomResource<TypeMappingsSpec, Void> {
25+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crd.generator.types;
17+
18+
import java.math.BigDecimal;
19+
import java.math.BigInteger;
20+
import java.sql.Timestamp;
21+
import java.time.Duration;
22+
import java.time.Instant;
23+
import java.time.LocalDate;
24+
import java.time.LocalDateTime;
25+
import java.time.MonthDay;
26+
import java.time.OffsetDateTime;
27+
import java.time.OffsetTime;
28+
import java.time.Period;
29+
import java.time.YearMonth;
30+
import java.time.ZonedDateTime;
31+
import java.util.Date;
32+
import java.util.UUID;
33+
34+
public class TypeMappingsSpec {
35+
36+
// Date/Time related types
37+
public Date date;
38+
public LocalDate localDate;
39+
public LocalDateTime localDateTime;
40+
public ZonedDateTime zonedDateTime;
41+
public OffsetDateTime offsetDateTime;
42+
public OffsetTime offsetTime;
43+
public YearMonth yearMonth;
44+
public MonthDay monthDay;
45+
public Instant instant;
46+
public Duration duration;
47+
public Period period;
48+
public Timestamp timestamp;
49+
50+
// Number related types
51+
public short aShort;
52+
public Short aShortObj;
53+
public int aInt;
54+
public Integer aIntegerObj;
55+
public long aLong;
56+
public Long aLongObj;
57+
public double aDouble;
58+
public Double aDoubleObj;
59+
public float aFloat;
60+
public Float aFloatObj;
61+
public Number aNumber;
62+
public BigInteger aBigInteger;
63+
public BigDecimal aBigDecimal;
64+
65+
// Boolean related types
66+
public boolean aBoolean;
67+
public Boolean aBooleanObj;
68+
69+
// String related types
70+
public char aChar;
71+
public Character aCharacterObj;
72+
public char[] aCharArray;
73+
public CharSequence aCharSequence;
74+
public String aString;
75+
public String[] aStringArray;
76+
77+
public byte aByte;
78+
public Byte aByteObj;
79+
public byte[] aByteArray;
80+
81+
public UUID uuid;
82+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.crd.generator.types;
17+
18+
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
19+
import io.fabric8.kubernetes.client.utils.Serialization;
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.Arguments;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
25+
import java.util.stream.Stream;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
class TypeMappingsTest {
30+
31+
private CustomResourceDefinition crd;
32+
33+
@BeforeEach
34+
void setUp() {
35+
crd = Serialization.unmarshal(
36+
TypeMappingsTest.class.getResourceAsStream("/META-INF/fabric8/typemappings.type-mappings.fabric8.io-v1.yml"),
37+
CustomResourceDefinition.class);
38+
}
39+
40+
@ParameterizedTest(name = "{0} type maps to {1}")
41+
@MethodSource("targetTypeCases")
42+
void targetType(String propertyName, String expectedType) {
43+
assertThat(crd.getSpec().getVersions().iterator().next().getSchema().getOpenAPIV3Schema().getProperties())
44+
.extracting(schema -> schema.get("spec").getProperties())
45+
.returns(expectedType, specProps -> specProps.get(propertyName).getType());
46+
}
47+
48+
private static Stream<Arguments> targetTypeCases() {
49+
return Stream.of(
50+
Arguments.of("date", "string"),
51+
Arguments.of("localDate", "object"), // to review
52+
Arguments.of("localDateTime", "object"), // to review
53+
Arguments.of("zonedDateTime", "object"), // to review
54+
Arguments.of("offsetDateTime", "object"), // to review
55+
Arguments.of("offsetTime", "object"), // to review
56+
Arguments.of("yearMonth", "object"), // to review
57+
Arguments.of("monthDay", "object"), // to review
58+
Arguments.of("instant", "object"), // to review
59+
Arguments.of("duration", "object"), // to review
60+
Arguments.of("period", "object"), // to review
61+
Arguments.of("timestamp", "object"), // to review
62+
// Arguments.of("aShort", "integer"), // TODO: Not even present in the CRD
63+
Arguments.of("aShortObj", "object"), // to review
64+
Arguments.of("aInt", "integer"),
65+
Arguments.of("aIntegerObj", "integer"),
66+
Arguments.of("aLong", "integer"),
67+
Arguments.of("aLongObj", "integer"),
68+
Arguments.of("aDouble", "number"),
69+
Arguments.of("aDoubleObj", "number"),
70+
Arguments.of("aFloat", "number"),
71+
Arguments.of("aFloatObj", "number"),
72+
Arguments.of("aNumber", "object"), // to review
73+
Arguments.of("aBigInteger", "object"), // to review
74+
Arguments.of("aBigDecimal", "object"), // to review
75+
Arguments.of("aBoolean", "boolean"),
76+
Arguments.of("aBooleanObj", "boolean"),
77+
// Arguments.of("aChar", "string"), // TODO: Not even present in the CRD
78+
Arguments.of("aCharacterObj", "object"), // to review
79+
Arguments.of("aCharArray", "array"),
80+
Arguments.of("aCharSequence", "object"), // to review
81+
Arguments.of("aString", "string"),
82+
Arguments.of("aStringArray", "array"),
83+
// Arguments.of("aByte", "?"), // TODO: Not even present in the CRD
84+
Arguments.of("aByteObj", "object"), // to review
85+
Arguments.of("aByteArray", "array"), // to review, should be string (base64)
86+
Arguments.of("uuid", "object")); // to review, should be string
87+
}
88+
89+
}

0 commit comments

Comments
 (0)