From 95ddc58185dd3b744f3d425ba6eb2bae987c0178 Mon Sep 17 00:00:00 2001 From: David Sondermann Date: Mon, 25 Nov 2024 19:07:12 +0000 Subject: [PATCH] test: fix Sonar smells in CRD generator tests Signed-off-by: David Sondermann --- .../java/generator/CompilationTest.java | 9 +- .../fabric8/java/generator/GeneratorTest.java | 404 ++++++++---------- 2 files changed, 191 insertions(+), 222 deletions(-) diff --git a/java-generator/core/src/test/java/io/fabric8/java/generator/CompilationTest.java b/java-generator/core/src/test/java/io/fabric8/java/generator/CompilationTest.java index 031b7de554..8396259661 100644 --- a/java-generator/core/src/test/java/io/fabric8/java/generator/CompilationTest.java +++ b/java-generator/core/src/test/java/io/fabric8/java/generator/CompilationTest.java @@ -121,11 +121,10 @@ void testCalicoIPPoolCRDDoesNotCompileWhenDuplicatesAreNotDeprecated() throws Ex .build(); // Assert - assertThatThrownBy(() -> { - // Act - new FileJavaGenerator(config, crd).run(tempDir); - javac().compile(getSources(tempDir)); - }).as("The current CRD should not compile since it contains duplicate fields which are not marked as deprecated") + assertThatThrownBy( + // Act + () -> new FileJavaGenerator(config, crd).run(tempDir)) + .as("The current CRD should not compile since it contains duplicate fields which are not marked as deprecated") .isInstanceOf(JavaGeneratorException.class); } diff --git a/java-generator/core/src/test/java/io/fabric8/java/generator/GeneratorTest.java b/java-generator/core/src/test/java/io/fabric8/java/generator/GeneratorTest.java index 2adea0a392..87bb038dff 100644 --- a/java-generator/core/src/test/java/io/fabric8/java/generator/GeneratorTest.java +++ b/java-generator/core/src/test/java/io/fabric8/java/generator/GeneratorTest.java @@ -17,9 +17,14 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.TextNode; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.EnumDeclaration; +import com.github.javaparser.ast.body.FieldDeclaration; +import com.github.javaparser.ast.expr.NormalAnnotationExpr; import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr; import io.fabric8.java.generator.exceptions.JavaGeneratorException; import io.fabric8.java.generator.nodes.AbstractJSONSchema2Pojo; +import io.fabric8.java.generator.nodes.GeneratorResult.ClassResult; import io.fabric8.java.generator.nodes.JArray; import io.fabric8.java.generator.nodes.JCRObject; import io.fabric8.java.generator.nodes.JEnum; @@ -30,6 +35,7 @@ import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps; import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaPropsOrBool; import io.fabric8.kubernetes.client.utils.Serialization; +import org.assertj.core.api.OptionalAssert; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -82,8 +88,7 @@ void testCR() { // Assert assertThat(res.getTopLevelClasses()).singleElement().satisfies(classResult -> { assertThat(classResult.getName()).isEqualTo("Type"); - assertThat(classResult.getPackageDeclaration()).isPresent() - .hasValueSatisfying(packageDeclaration -> assertThat(packageDeclaration.getNameAsString()).isEqualTo("v1alpha1")); + assertPackageDeclaration(classResult, "v1alpha1"); }); } @@ -110,9 +115,11 @@ void testNamespacedCR() { final var res = cro.generateJava(); // Assert - assertThat(res.getTopLevelClasses().get(0).getClassByName("Type")).isPresent() - .hasValueSatisfying(type -> assertThat(type.getImplementedTypes().get(0).getNameWithScope()) - .isEqualTo("io.fabric8.kubernetes.api.model.Namespaced")); + assertThat(res.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "Type") + .hasValueSatisfying(declaration -> assertThat(declaration.getImplementedTypes()).singleElement() + .satisfies( + type -> assertThat(type.getNameWithScope()).isEqualTo("io.fabric8.kubernetes.api.model.Namespaced")))); } @Test @@ -138,8 +145,9 @@ void testClusterScopeCR() { final var res = cro.generateJava(); // Assert - assertThat(res.getTopLevelClasses().get(0).getClassByName("Type")).isPresent() - .hasValueSatisfying(type -> assertThat(type.getImplementedTypes()).isEmpty()); + assertThat(res.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "Type") + .hasValueSatisfying(type -> assertThat(type.getImplementedTypes()).isEmpty())); } @Test @@ -312,7 +320,7 @@ void testEmptyObject() { // Assert assertThat(obj.getType()).isEqualTo("v1alpha1.T"); assertThat(res.getTopLevelClasses()).singleElement() - .satisfies(classResult -> assertThat(classResult.getName()).isEqualTo("T")); + .satisfies(classResult -> assertClassByName(classResult, "T")); } @Test @@ -335,7 +343,7 @@ void testEmptyObjectWithoutNamespace() { // Assert assertThat(obj.getType()).isEqualTo("T"); assertThat(res.getTopLevelClasses()).singleElement() - .satisfies(classResult -> assertThat(classResult.getName()).isEqualTo("T")); + .satisfies(classResult -> assertClassByName(classResult, "T")); } @Test @@ -361,13 +369,12 @@ void testObjectOfPrimitives() { // Assert assertThat(obj.getType()).isEqualTo("v1alpha1.T"); - assertThat(res.getTopLevelClasses()).singleElement().satisfies(classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getClassByName("T")).isPresent().hasValueSatisfying(clz -> { - assertThat(clz.getFields()).hasSize(1); - assertThat(clz.getFieldByName("o1")).isPresent(); - }); - }); + assertThat(res.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying(declaration -> { + assertThat(declaration.getFields()).hasSize(1); + assertThat(declaration.getFieldByName("o1")).isPresent(); + })); } @Test @@ -394,9 +401,10 @@ void testObjectWithRequiredField() { final var res = obj.generateJava(); // Assert - assertThat(res.getTopLevelClasses().get(0).getClassByName("T")).isPresent() - .hasValueSatisfying(clz -> assertThat(clz.getFieldByName("o1")).isPresent() - .hasValueSatisfying(o1Field -> assertThat(o1Field.getAnnotationByName("Required")).isPresent())); + assertThat(res.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying(declaration -> assertThat(declaration.getFieldByName("o1")).isPresent() + .hasValueSatisfying(field -> assertThat(field.getAnnotationByName("Required")).isPresent()))); } @Test @@ -430,10 +438,14 @@ void testObjectWithAndWithoutGeneratedAnnotation() { final var res2 = obj2.generateJava(); // Assert - assertThat(res1.getTopLevelClasses().get(0).getClassByName("T")).isPresent() - .hasValueSatisfying(clz -> assertThat(clz.getAnnotationByName(generatedAnnotationName)).isPresent()); - assertThat(res2.getTopLevelClasses().get(0).getClassByName("T")).isPresent() - .hasValueSatisfying(clz -> assertThat(clz.getAnnotationByName(generatedAnnotationName)).isNotPresent()); + assertThat(res1.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying( + declaration -> assertThat(declaration.getAnnotationByName(generatedAnnotationName)).isPresent())); + assertThat(res2.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying( + declaration -> assertThat(declaration.getAnnotationByName(generatedAnnotationName)).isNotPresent())); } @Test @@ -459,14 +471,12 @@ void testDefaultEnum() { // Assert assertThat(enu.getType()).isEqualTo("T"); - assertThat(res.getInnerClasses()).singleElement().satisfies(classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getEnumByName("T")).isPresent() - .hasValueSatisfying(enumDeclaration -> assertThat(enumDeclaration.getEntries()).satisfiesExactly( - ecd -> assertThat(ecd.getName().asString()).isEqualTo("FOO"), - ecd -> assertThat(ecd.getName().asString()).isEqualTo("BAR"), - ecd -> assertThat(ecd.getName().asString()).isEqualTo("BAZ"))); - }); + assertThat(res.getInnerClasses()).singleElement() + .satisfies(classResult -> assertEnumByName(classResult, "T") + .hasValueSatisfying(enumDeclaration -> assertThat(enumDeclaration.getEntries()).satisfiesExactly( + ecd -> assertThat(ecd.getName()).hasToString("FOO"), + ecd -> assertThat(ecd.getName()).hasToString("BAR"), + ecd -> assertThat(ecd.getName()).hasToString("BAZ")))); } @Test @@ -492,23 +502,21 @@ void testLongEnum() { // Assert assertThat(enu.getType()).isEqualTo("T"); - assertThat(res.getInnerClasses()).singleElement().satisfies(classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getEnumByName("T")).isPresent() - .hasValueSatisfying(enumDeclaration -> assertThat(enumDeclaration.getEntries()).satisfiesExactly( - ecd -> { - assertThat(ecd.getName().asString()).isEqualTo("V__1"); - assertThat(ecd.getArgument(0).toString()).isEqualTo("1L"); - }, - ecd -> { - assertThat(ecd.getName().asString()).isEqualTo("V__2"); - assertThat(ecd.getArgument(0).toString()).isEqualTo("2L"); - }, - ecd -> { - assertThat(ecd.getName().asString()).isEqualTo("V__3"); - assertThat(ecd.getArgument(0).toString()).isEqualTo("3L"); - })); - }); + assertThat(res.getInnerClasses()).singleElement() + .satisfies(classResult -> assertEnumByName(classResult, "T") + .hasValueSatisfying(enumDeclaration -> assertThat(enumDeclaration.getEntries()).satisfiesExactly( + ecd -> { + assertThat(ecd.getName()).hasToString("V__1"); + assertThat(ecd.getArgument(0)).hasToString("1L"); + }, + ecd -> { + assertThat(ecd.getName()).hasToString("V__2"); + assertThat(ecd.getArgument(0)).hasToString("2L"); + }, + ecd -> { + assertThat(ecd.getName()).hasToString("V__3"); + assertThat(ecd.getArgument(0)).hasToString("3L"); + }))); } @Test @@ -535,23 +543,21 @@ void testIntEnum() { // Assert assertThat(enu.getType()).isEqualTo("T"); - assertThat(res.getInnerClasses()).singleElement().satisfies(classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getEnumByName("T")).isPresent() - .hasValueSatisfying(enumDeclaration -> assertThat(enumDeclaration.getEntries()).satisfiesExactly( - ecd -> { - assertThat(ecd.getName().asString()).isEqualTo("V__1"); - assertThat(ecd.getArgument(0).toString()).isEqualTo("1"); - }, - ecd -> { - assertThat(ecd.getName().asString()).isEqualTo("V__2"); - assertThat(ecd.getArgument(0).toString()).isEqualTo("2"); - }, - ecd -> { - assertThat(ecd.getName().asString()).isEqualTo("V__3"); - assertThat(ecd.getArgument(0).toString()).isEqualTo("3"); - })); - }); + assertThat(res.getInnerClasses()).singleElement() + .satisfies(classResult -> assertEnumByName(classResult, "T") + .hasValueSatisfying(enumDeclaration -> assertThat(enumDeclaration.getEntries()).satisfiesExactly( + ecd -> { + assertThat(ecd.getName()).hasToString("V__1"); + assertThat(ecd.getArgument(0)).hasToString("1"); + }, + ecd -> { + assertThat(ecd.getName()).hasToString("V__2"); + assertThat(ecd.getArgument(0)).hasToString("2"); + }, + ecd -> { + assertThat(ecd.getName()).hasToString("V__3"); + assertThat(ecd.getArgument(0)).hasToString("3"); + }))); } @Test @@ -577,14 +583,12 @@ void testNotUppercaseEnum() { // Assert assertThat(enu.getType()).isEqualTo("T"); - assertThat(res.getInnerClasses()).singleElement().satisfies(classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getEnumByName("T")).isPresent() - .hasValueSatisfying(enumDeclaration -> assertThat(enumDeclaration.getEntries()).satisfiesExactly( - ecd -> assertThat(ecd.getName().asString()).isEqualTo("foo"), - ecd -> assertThat(ecd.getName().asString()).isEqualTo("bar"), - ecd -> assertThat(ecd.getName().asString()).isEqualTo("baz"))); - }); + assertThat(res.getInnerClasses()).singleElement() + .satisfies(classResult -> assertEnumByName(classResult, "T") + .hasValueSatisfying(enumDeclaration -> assertThat(enumDeclaration.getEntries()).satisfiesExactly( + ecd -> assertThat(ecd.getName()).hasToString("foo"), + ecd -> assertThat(ecd.getName()).hasToString("bar"), + ecd -> assertThat(ecd.getName()).hasToString("baz")))); } @Test @@ -612,7 +616,7 @@ void testArrayOfObjects() { // Assert assertThat(array.getType()).isEqualTo("java.util.List"); assertThat(res.getTopLevelClasses()).singleElement() - .satisfies(classResult -> assertThat(classResult.getName()).isEqualTo("T")); + .satisfies(classResult -> assertClassByName(classResult, "T")); } @Test @@ -640,7 +644,7 @@ void testMapOfObjects() { // Assert assertThat(map.getType()).isEqualTo("java.util.Map"); assertThat(res.getTopLevelClasses()).singleElement() - .satisfies(classResult -> assertThat(classResult.getName()).isEqualTo("T")); + .satisfies(classResult -> assertClassByName(classResult, "T")); } @Test @@ -666,14 +670,9 @@ void testObjectOfObjects() { // Assert assertThat(res.getTopLevelClasses()).satisfiesExactly( - classResult -> { - assertThat(classResult.getName()).isEqualTo("O1"); - assertThat(classResult.getClassByName("O1")).isPresent(); - }, classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getClassByName("T")).isPresent() - .hasValueSatisfying(clz -> assertThat(clz.getFieldByName("o1")).isPresent()); - }); + classResult -> assertClassByName(classResult, "O1"), + classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying(declaration -> assertThat(declaration.getFieldByName("o1")).isPresent())); } @Test @@ -694,12 +693,10 @@ void testObjectWithPreservedFields() { final var res = obj.generateJava(); // Assert - assertThat(res.getTopLevelClasses()).singleElement().satisfies( - classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getClassByName("T")).isPresent().hasValueSatisfying( - coiDeclaration -> assertThat(coiDeclaration.getFieldByName("additionalProperties")).isPresent()); - }); + assertThat(res.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying( + coiDeclaration -> assertThat(coiDeclaration.getFieldByName("additionalProperties")).isPresent())); } @Test @@ -721,12 +718,10 @@ void testConfigToGeneratePreservedUnknownFields() { final var res = obj.generateJava(); // Assert - assertThat(res.getTopLevelClasses()).singleElement().satisfies( - classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getClassByName("T")).isPresent().hasValueSatisfying( - coiDeclaration -> assertThat(coiDeclaration.getFieldByName("additionalProperties")).isPresent()); - }); + assertThat(res.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying( + coiDeclaration -> assertThat(coiDeclaration.getFieldByName("additionalProperties")).isPresent())); } @Test @@ -752,12 +747,9 @@ void testObjectWithSpecialFieldNames() { final var res = obj.generateJava(); // Assert - assertThat(res.getTopLevelClasses()).singleElement().satisfies( - classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getClassByName("T")).isPresent().hasValueSatisfying( - coiDeclaration -> assertThat(coiDeclaration.getFieldByName("description")).isPresent()); - }); + assertThat(res.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying(coiDeclaration -> assertThat(coiDeclaration.getFieldByName("description")).isPresent())); } @Test @@ -781,52 +773,25 @@ void testObjectNullableFieldsManagement() { // Assert assertThat(res.getTopLevelClasses()).satisfiesExactly( - classResult -> { - assertThat(classResult.getName()).isEqualTo("O1"); - assertThat(classResult.getClassByName("O1")).isPresent(); - }, classResult -> { - assertThat(classResult.getName()).isEqualTo("O2"); - assertThat(classResult.getClassByName("O2")).isPresent(); - }, classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getClassByName("T")).isPresent() - .hasValueSatisfying(clz -> { - assertThat(clz.getFields()).hasSize(2); - assertThat(clz.getFieldByName("o1")).isPresent().hasValueSatisfying(o1Field -> { - assertThat(o1Field.getAnnotations()).hasSize(3); - assertThat(o1Field.getAnnotationByName("com.fasterxml.jackson.annotation.JsonSetter")).isPresent() - .hasValueSatisfying(annotationExpr -> { - assertThat(annotationExpr).isInstanceOf(SingleMemberAnnotationExpr.class); - SingleMemberAnnotationExpr annotation = (SingleMemberAnnotationExpr) annotationExpr; - assertThat(annotation.getMemberValue().toString()) - .isEqualTo("nulls = com.fasterxml.jackson.annotation.Nulls.SET"); - }); - assertThat(o1Field.getAnnotationByName("com.fasterxml.jackson.annotation.JsonProperty")).isPresent() - .hasValueSatisfying(annotationExpr -> { - assertThat(annotationExpr).isInstanceOf(SingleMemberAnnotationExpr.class); - SingleMemberAnnotationExpr annotation = (SingleMemberAnnotationExpr) annotationExpr; - assertThat(annotation.getMemberValue().toString()).isEqualTo("\"o1\""); - }); - assertThat(o1Field.getAnnotationByName("io.fabric8.generator.annotation.Nullable")).isPresent(); - }); - assertThat(clz.getFieldByName("o2")).isPresent().hasValueSatisfying(o2Field -> { - assertThat(o2Field.getAnnotations()).hasSize(2); - assertThat(o2Field.getAnnotationByName("com.fasterxml.jackson.annotation.JsonSetter")).isPresent() - .hasValueSatisfying(annotationExpr -> { - assertThat(annotationExpr).isInstanceOf(SingleMemberAnnotationExpr.class); - SingleMemberAnnotationExpr annotation = (SingleMemberAnnotationExpr) annotationExpr; - assertThat(annotation.getMemberValue().toString()) - .isEqualTo("nulls = com.fasterxml.jackson.annotation.Nulls.SKIP"); - }); - assertThat(o2Field.getAnnotationByName("com.fasterxml.jackson.annotation.JsonProperty")).isPresent() - .hasValueSatisfying(annotationExpr -> { - assertThat(annotationExpr).isInstanceOf(SingleMemberAnnotationExpr.class); - SingleMemberAnnotationExpr annotation = (SingleMemberAnnotationExpr) annotationExpr; - assertThat(annotation.getMemberValue().toString()).isEqualTo("\"o2\""); - }); - }); + classResult -> assertClassByName(classResult, "O1"), + classResult -> assertClassByName(classResult, "O2"), + classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying(declaration -> { + assertThat(declaration.getFields()).hasSize(2); + assertThat(declaration.getFieldByName("o1")).isPresent().hasValueSatisfying(field -> { + assertThat(field.getAnnotations()).hasSize(3); + assertAnnotationValue(field, "com.fasterxml.jackson.annotation.JsonSetter", + "nulls = com.fasterxml.jackson.annotation.Nulls.SET"); + assertAnnotationValue(field, "com.fasterxml.jackson.annotation.JsonProperty", "\"o1\""); + assertAnnotationValue(field, "io.fabric8.generator.annotation.Nullable", null); }); - }); + assertThat(declaration.getFieldByName("o2")).isPresent().hasValueSatisfying(field -> { + assertThat(field.getAnnotations()).hasSize(2); + assertAnnotationValue(field, "com.fasterxml.jackson.annotation.JsonSetter", + "nulls = com.fasterxml.jackson.annotation.Nulls.SKIP"); + assertAnnotationValue(field, "com.fasterxml.jackson.annotation.JsonProperty", "\"o2\""); + }); + })); } @Test @@ -844,16 +809,12 @@ void testObjectWithAdditionalPropertiesTrue() { final var res = jobj.generateJava(); // Assert - assertThat(res.getTopLevelClasses()).singleElement().satisfies( - classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getClassByName("T")).isPresent().hasValueSatisfying(clz -> { - assertThat(clz.getFields()).hasSize(1); - assertThat(clz.getFieldByName("o1")).isPresent() - .hasValueSatisfying(o1Field -> assertThat(o1Field.getElementType().asString()) - .isEqualTo("io.fabric8.kubernetes.api.model.AnyType")); - }); - }); + assertThat(res.getTopLevelClasses()).singleElement() + .satisfies(classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying(declaration -> { + assertThat(declaration.getFields()).hasSize(1); + assertFieldElementType(declaration, "o1", "io.fabric8.kubernetes.api.model.AnyType"); + })); } @Test @@ -879,54 +840,32 @@ void testClassNamesDisambiguationWithPackageNesting() { // Assert assertThat(res.getTopLevelClasses()).satisfiesExactly( classResult -> { - assertThat(classResult.getName()).isEqualTo("O1"); - assertThat(classResult.getPackageDeclaration()).isPresent() - .hasValueSatisfying( - packageDeclaration -> assertThat(packageDeclaration.getNameAsString()).isEqualTo("v1alpha1.t")); - assertThat(classResult.getClassByName("O1")).isPresent(); + assertPackageDeclaration(classResult, "v1alpha1.t"); + assertClassByName(classResult, "O1"); }, classResult -> { - assertThat(classResult.getName()).isEqualTo("O1"); - assertThat(classResult.getPackageDeclaration()).isPresent() - .hasValueSatisfying( - packageDeclaration -> assertThat(packageDeclaration.getNameAsString()).isEqualTo("v1alpha1.t.o2")); - assertThat(classResult.getClassByName("O1")).isPresent(); + assertPackageDeclaration(classResult, "v1alpha1.t.o2"); + assertClassByName(classResult, "O1"); }, classResult -> { - assertThat(classResult.getName()).isEqualTo("O2"); - assertThat(classResult.getPackageDeclaration()).isPresent() - .hasValueSatisfying( - packageDeclaration -> assertThat(packageDeclaration.getNameAsString()).isEqualTo("v1alpha1.t.o2")); - assertThat(classResult.getClassByName("O2")).isPresent(); + assertPackageDeclaration(classResult, "v1alpha1.t.o2"); + assertClassByName(classResult, "O2"); }, classResult -> { - assertThat(classResult.getName()).isEqualTo("O3"); - assertThat(classResult.getPackageDeclaration()).isPresent() - .hasValueSatisfying( - packageDeclaration -> assertThat(packageDeclaration.getNameAsString()).isEqualTo("v1alpha1.t.o2")); - assertThat(classResult.getClassByName("O3")).isPresent(); + assertPackageDeclaration(classResult, "v1alpha1.t.o2"); + assertClassByName(classResult, "O3"); }, classResult -> { - assertThat(classResult.getName()).isEqualTo("O2"); - assertThat(classResult.getPackageDeclaration()).isPresent() - .hasValueSatisfying( - packageDeclaration -> assertThat(packageDeclaration.getNameAsString()).isEqualTo("v1alpha1.t")); - assertThat(classResult.getClassByName("O2")).isPresent().hasValueSatisfying(clz -> { - assertThat(clz.getFields()).hasSize(3); - assertThat(clz.getFieldByName("o1")).isPresent() - .hasValueSatisfying(o1Field -> assertThat(o1Field.getElementType().asString()).isEqualTo("v1alpha1.t.o2.O1")); - assertThat(clz.getFieldByName("o2")).isPresent() - .hasValueSatisfying(o2Field -> assertThat(o2Field.getElementType().asString()).isEqualTo("v1alpha1.t.o2.O2")); - assertThat(clz.getFieldByName("o3")).isPresent() - .hasValueSatisfying(o3Field -> assertThat(o3Field.getElementType().asString()).isEqualTo("v1alpha1.t.o2.O3")); + assertPackageDeclaration(classResult, "v1alpha1.t"); + assertClassByName(classResult, "O2").hasValueSatisfying(declaration -> { + assertThat(declaration.getFields()).hasSize(3); + assertFieldElementType(declaration, "o1", "v1alpha1.t.o2.O1"); + assertFieldElementType(declaration, "o2", "v1alpha1.t.o2.O2"); + assertFieldElementType(declaration, "o3", "v1alpha1.t.o2.O3"); }); }, classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getPackageDeclaration()).isPresent() - .hasValueSatisfying(packageDeclaration -> assertThat(packageDeclaration.getNameAsString()).isEqualTo("v1alpha1")); - assertThat(classResult.getClassByName("T")).isPresent() - .hasValueSatisfying(clz -> { - assertThat(clz.getFields()).hasSize(2); - assertThat(clz.getFieldByName("o1")).isPresent() - .hasValueSatisfying(o1Field -> assertThat(o1Field.getElementType().asString()).isEqualTo("v1alpha1.t.O1")); - assertThat(clz.getFieldByName("o2")).isPresent() - .hasValueSatisfying(o2Field -> assertThat(o2Field.getElementType().asString()).isEqualTo("v1alpha1.t.O2")); + assertPackageDeclaration(classResult, "v1alpha1"); + assertClassByName(classResult, "T") + .hasValueSatisfying(declaration -> { + assertThat(declaration.getFields()).hasSize(2); + assertFieldElementType(declaration, "o1", "v1alpha1.t.O1"); + assertFieldElementType(declaration, "o2", "v1alpha1.t.O2"); }); }); } @@ -954,25 +893,18 @@ void testObjectDefaultFieldsManagement() { // Assert assertThat(res.getTopLevelClasses()).satisfiesExactly( - classResult -> { - assertThat(classResult.getName()).isEqualTo("O1"); - assertThat(classResult.getClassByName("O1")).isPresent(); - }, classResult -> { - assertThat(classResult.getName()).isEqualTo("O2"); - assertThat(classResult.getClassByName("O2")).isPresent(); - }, classResult -> { - assertThat(classResult.getName()).isEqualTo("T"); - assertThat(classResult.getClassByName("T")).isPresent() - .hasValueSatisfying(clz -> { - assertThat(clz.getFields()).hasSize(2); - assertThat(clz.getFieldByName("o1")).isPresent() - .hasValueSatisfying(o1Field -> assertThat(o1Field.getVariable(0)).isNotNull() - .satisfies(variableDeclarator -> assertThat(variableDeclarator.getInitializer()).isPresent())); - assertThat(clz.getFieldByName("o2")).isPresent() - .hasValueSatisfying(o2Field -> assertThat(o2Field.getVariable(0)).isNotNull() - .satisfies(variableDeclarator -> assertThat(variableDeclarator.getInitializer()).isNotPresent())); - }); - }); + classResult -> assertClassByName(classResult, "O1"), + classResult -> assertClassByName(classResult, "O2"), + classResult -> assertClassByName(classResult, "T") + .hasValueSatisfying(declaration -> { + assertThat(declaration.getFields()).hasSize(2); + assertThat(declaration.getFieldByName("o1")).isPresent() + .hasValueSatisfying(field -> assertThat(field.getVariables()).singleElement() + .satisfies(declarator -> assertThat(declarator.getInitializer()).isPresent())); + assertThat(declaration.getFieldByName("o2")).isPresent() + .hasValueSatisfying(field -> assertThat(field.getVariables()).singleElement() + .satisfies(declarator -> assertThat(declarator.getInitializer()).isNotPresent())); + })); } @Test @@ -1037,4 +969,42 @@ void testExistingJavaTypeObject() { assertThat(obj.getType()).isEqualTo("org.test.ExistingJavaType"); assertThat(res.getTopLevelClasses()).isEmpty(); } + + private static OptionalAssert assertClassByName(ClassResult classResult, + String expectedClassName) { + assertThat(classResult.getName()).isEqualTo(expectedClassName); + return assertThat(classResult.getClassByName(expectedClassName)).isPresent(); + } + + @SuppressWarnings("SameParameterValue") + private static OptionalAssert assertEnumByName(ClassResult classResult, + String expectedClassName) { + assertThat(classResult.getName()).isEqualTo(expectedClassName); + return assertThat(classResult.getEnumByName(expectedClassName)).isPresent(); + } + + private static void assertAnnotationValue(FieldDeclaration field, String annotationName, String expectedMemberValue) { + assertThat(field.getAnnotationByName(annotationName)).isPresent() + .hasValueSatisfying(annotationExpr -> { + if (expectedMemberValue != null) { + assertThat(annotationExpr).isInstanceOf(SingleMemberAnnotationExpr.class); + SingleMemberAnnotationExpr annotation = (SingleMemberAnnotationExpr) annotationExpr; + assertThat(annotation.getMemberValue()).hasToString(expectedMemberValue); + } else { + assertThat(annotationExpr).isInstanceOf(NormalAnnotationExpr.class); + } + }); + } + + private static void assertFieldElementType(ClassOrInterfaceDeclaration declaration, String fieldName, + String expectedElementType) { + assertThat(declaration.getFieldByName(fieldName)).isPresent() + .hasValueSatisfying(field -> assertThat(field.getElementType().asString()) + .isEqualTo(expectedElementType)); + } + + private static void assertPackageDeclaration(ClassResult classResult, String expectedPackageName) { + assertThat(classResult.getPackageDeclaration()).isPresent() + .hasValueSatisfying(declaration -> assertThat(declaration.getNameAsString()).isEqualTo(expectedPackageName)); + } }