Skip to content

Commit

Permalink
reformat. fabric8io#4350
Browse files Browse the repository at this point in the history
  • Loading branch information
xRodney committed Aug 26, 2022
1 parent ed54646 commit 192c841
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 37 deletions.
Expand Up @@ -17,7 +17,6 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

import io.fabric8.crd.generator.annotation.SchemaSwap;
import io.fabric8.crd.generator.utils.Types;
import io.fabric8.kubernetes.api.model.Duration;
Expand Down Expand Up @@ -198,7 +197,7 @@ private void extractSchemaSwaps(ClassRef definitionType, AnnotationRef annotatio
case ANNOTATION_SCHEMA_SWAPS:
Map<String, Object> params = annotation.getParameters();
Object[] values = (Object[]) params.get("value");
for (Object value : values) {
for (Object value : values) {
extractSchemaSwap(definitionType, value, schemaSwaps);
}
break;
Expand All @@ -209,17 +208,17 @@ private void extractSchemaSwap(ClassRef definitionType, Object annotation, Inter
if (annotation instanceof SchemaSwap) {
SchemaSwap schemaSwap = (SchemaSwap) annotation;
schemaSwaps.registerSwap(definitionType,
extractClassRef(schemaSwap.originalType()),
schemaSwap.fieldName(),
extractClassRef(schemaSwap.targetType()));
extractClassRef(schemaSwap.originalType()),
schemaSwap.fieldName(),
extractClassRef(schemaSwap.targetType()));

} else if (annotation instanceof AnnotationRef
&& ((AnnotationRef) annotation).getClassRef().getFullyQualifiedName().equals(ANNOTATION_SCHEMA_SWAP)) {
Map<String, Object> params = ((AnnotationRef) annotation).getParameters();
schemaSwaps.registerSwap(definitionType,
extractClassRef(params.get("originalType")),
(String) params.get("fieldName"),
extractClassRef(params.get("targetType")));
extractClassRef(params.get("originalType")),
(String) params.get("fieldName"),
extractClassRef(params.get("targetType")));

} else {
throw new IllegalArgumentException("Unmanaged annotation type passed to the SchemaSwaps: " + annotation);
Expand All @@ -228,8 +227,8 @@ private void extractSchemaSwap(ClassRef definitionType, Object annotation, Inter

private void validateRemainingSchemaSwaps(InternalSchemaSwaps schemaSwaps) {
String unmatchedSchemaSwaps = schemaSwaps.getUnusedSwaps()
.map(Object::toString)
.collect(Collectors.joining(","));
.map(Object::toString)
.collect(Collectors.joining(","));
if (!unmatchedSchemaSwaps.isEmpty()) {
throw new IllegalArgumentException("Unmatched SchemaSwaps: " + unmatchedSchemaSwaps);
}
Expand Down
Expand Up @@ -15,15 +15,15 @@
*/
package io.fabric8.crd.generator;

import io.sundr.model.ClassRef;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.stream.Stream;

import io.sundr.model.ClassRef;

public class InternalSchemaSwaps {
private final Map<Key, Value> swaps = new HashMap<>();

Expand All @@ -50,7 +50,6 @@ private static class Key {
private final ClassRef originalType;
private final String fieldName;


public Key(ClassRef originalType, String fieldName) {
this.originalType = originalType;
this.fieldName = fieldName;
Expand Down Expand Up @@ -84,9 +83,9 @@ public int hashCode() {
@Override
public String toString() {
return new StringJoiner(", ", Key.class.getSimpleName() + "[", "]")
.add("originalType=" + originalType)
.add("fieldName='" + fieldName + "'")
.toString();
.add("originalType=" + originalType)
.add("fieldName='" + fieldName + "'")
.toString();
}
}

Expand Down Expand Up @@ -127,7 +126,8 @@ public boolean isUsed() {

@Override
public String toString() {
return "@SchemaSwap(originalType=" + originalType + ", fieldName=\"" + fieldName + "\", targetType="+targetType + ") on " + definitionType;
return "@SchemaSwap(originalType=" + originalType + ", fieldName=\"" + fieldName + "\", targetType=" + targetType
+ ") on " + definitionType;
}
}
}
Expand Up @@ -20,6 +20,6 @@

@SchemaSwap(originalType = SchemaSwapSpec.SomeObject.class, fieldName = "shouldBeString", targetType = String.class)
@SchemaSwap(originalType = SchemaSwapSpec.AnotherObject.class, fieldName = "shouldBeInt", targetType = Integer.class)
public class MultipleSchemaSwaps extends CustomResource<SchemaSwapSpec, Void> {
public class MultipleSchemaSwaps extends CustomResource<SchemaSwapSpec, Void> {

}
Expand Up @@ -18,11 +18,11 @@
import com.fasterxml.jackson.databind.JsonNode;
import io.fabric8.crd.example.annotated.Annotated;
import io.fabric8.crd.example.basic.Basic;
import io.fabric8.crd.example.extraction.Extraction;
import io.fabric8.crd.example.extraction.IncorrectExtraction;
import io.fabric8.crd.example.extraction.IncorrectExtraction2;
import io.fabric8.crd.example.extraction.MultipleSchemaSwaps;
import io.fabric8.crd.example.json.ContainingJson;
import io.fabric8.crd.example.extraction.Extraction;
import io.fabric8.crd.example.person.Person;
import io.fabric8.crd.generator.utils.Types;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
Expand All @@ -45,16 +45,16 @@ void shouldCreateJsonSchemaFromClass() {
Map<String, JSONSchemaProps> properties = schema.getProperties();
assertEquals(7, properties.size());
final List<String> personTypes = properties.get("type").getEnum().stream().map(JsonNode::asText)
.collect(Collectors.toList());
.collect(Collectors.toList());
assertEquals(2, personTypes.size());
assertTrue(personTypes.contains("crazy"));
assertTrue(personTypes.contains("crazier"));
final Map<String, JSONSchemaProps> addressProperties = properties.get("addresses").getItems()
.getSchema().getProperties();
.getSchema().getProperties();
assertEquals(5, addressProperties.size());
final List<String> addressTypes = addressProperties.get("type").getEnum().stream()
.map(JsonNode::asText)
.collect(Collectors.toList());
.map(JsonNode::asText)
.collect(Collectors.toList());
assertEquals(2, addressTypes.size());
assertTrue(addressTypes.contains("home"));
assertTrue(addressTypes.contains("work"));
Expand Down Expand Up @@ -179,7 +179,6 @@ void shouldExtractPropertiesSchemaFromExtractValueAnnotation() {
assertNull(barProps.get("baz"));
}


@Test
void shouldExtractPropertiesSchemaFromSchemaSwapAnnotations() {
TypeDef extraction = Types.typeDefFrom(MultipleSchemaSwaps.class);
Expand Down Expand Up @@ -216,16 +215,22 @@ void shouldExtractPropertiesSchemaFromSchemaSwapAnnotations() {
@Test
void shouldThrowIfSchemaSwapHasUnmatchedField() {
TypeDef incorrectExtraction = Types.typeDefFrom(IncorrectExtraction.class);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> JsonSchema.from(incorrectExtraction));
assertEquals("Unmatched SchemaSwaps: @SchemaSwap(originalType=io.fabric8.crd.example.extraction.ExtractionSpec, fieldName=\"FOO\", targetType=io"
+ ".fabric8.crd.example.extraction.FooExtractor) on io.fabric8.crd.example.extraction.IncorrectExtraction", exception.getMessage());
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> JsonSchema.from(incorrectExtraction));
assertEquals(
"Unmatched SchemaSwaps: @SchemaSwap(originalType=io.fabric8.crd.example.extraction.ExtractionSpec, fieldName=\"FOO\", targetType=io"
+ ".fabric8.crd.example.extraction.FooExtractor) on io.fabric8.crd.example.extraction.IncorrectExtraction",
exception.getMessage());
}

@Test
void shouldThrowIfSchemaSwapHasUnmatchedClass() {
TypeDef incorrectExtraction2 = Types.typeDefFrom(IncorrectExtraction2.class);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> JsonSchema.from(incorrectExtraction2));
assertEquals("Unmatched SchemaSwaps: @SchemaSwap(originalType=io.fabric8.crd.example.basic.BasicSpec, fieldName=\"bar\", targetType=io.fabric8.crd"
+ ".example.extraction.FooExtractor) on io.fabric8.crd.example.extraction.IncorrectExtraction2", exception.getMessage());
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> JsonSchema.from(incorrectExtraction2));
assertEquals(
"Unmatched SchemaSwaps: @SchemaSwap(originalType=io.fabric8.crd.example.basic.BasicSpec, fieldName=\"bar\", targetType=io.fabric8.crd"
+ ".example.extraction.FooExtractor) on io.fabric8.crd.example.extraction.IncorrectExtraction2",
exception.getMessage());
}
}
Expand Up @@ -24,6 +24,6 @@
@Group("acme.com")
@SchemaSwap(originalType = SchemaSwapSpec.SomeObject.class, fieldName = "shouldBeString", targetType = String.class)
@SchemaSwap(originalType = SchemaSwapSpec.AnotherObject.class, fieldName = "shouldBeInt", targetType = Integer.class)
public class MultipleSchemaSwaps extends CustomResource<SchemaSwapSpec, Void> {
public class MultipleSchemaSwaps extends CustomResource<SchemaSwapSpec, Void> {

}
Expand Up @@ -15,16 +15,15 @@
*/
package io.fabric8.crd.generator.schemaswaps;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down

0 comments on commit 192c841

Please sign in to comment.