From 2c16819b4d62dcdcca4d9543ff1f20993cb354c8 Mon Sep 17 00:00:00 2001 From: Lavanya Singh Date: Thu, 9 Feb 2023 15:06:38 -0500 Subject: [PATCH] refactoring test --- .../product/SafeExternalAliasExample.java | 50 -------- .../java/types/ExternalImportSafetyTests.java | 112 ++++++++++-------- .../java/types/SafetyEvaluatorTest.java | 1 - 3 files changed, 64 insertions(+), 99 deletions(-) delete mode 100644 conjure-java-core/src/integrationInput/java/com/palantir/product/SafeExternalAliasExample.java diff --git a/conjure-java-core/src/integrationInput/java/com/palantir/product/SafeExternalAliasExample.java b/conjure-java-core/src/integrationInput/java/com/palantir/product/SafeExternalAliasExample.java deleted file mode 100644 index 6f434d818..000000000 --- a/conjure-java-core/src/integrationInput/java/com/palantir/product/SafeExternalAliasExample.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.palantir.product; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonValue; -import com.palantir.logsafe.Preconditions; -import com.palantir.logsafe.Safe; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import javax.annotation.processing.Generated; - -@Safe -@Generated("com.palantir.conjure.java.types.AliasGenerator") -public final class SafeExternalAliasExample { - private final SafeExternalLongAlias value; - - private SafeExternalAliasExample(@Nonnull SafeExternalLongAlias value) { - this.value = Preconditions.checkNotNull(value, "value cannot be null"); - } - - @JsonValue - public SafeExternalLongAlias get() { - return value; - } - - @Override - public String toString() { - return value.toString(); - } - - @Override - public boolean equals(@Nullable Object other) { - return this == other - || (other instanceof SafeExternalAliasExample - && this.value.equals(((SafeExternalAliasExample) other).value)); - } - - @Override - public int hashCode() { - return value.hashCode(); - } - - public static SafeExternalAliasExample valueOf(String value) { - return of(SafeExternalLongAlias.valueOf(value)); - } - - @JsonCreator(mode = JsonCreator.Mode.DELEGATING) - public static SafeExternalAliasExample of(@Nonnull SafeExternalLongAlias value) { - return new SafeExternalAliasExample(value); - } -} diff --git a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/ExternalImportSafetyTests.java b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/ExternalImportSafetyTests.java index 55afc0b62..eb2d1102b 100644 --- a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/ExternalImportSafetyTests.java +++ b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/ExternalImportSafetyTests.java @@ -27,11 +27,11 @@ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedParameterizedType; import java.lang.reflect.AnnotatedType; +import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Parameter; import java.util.Arrays; -import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -90,7 +90,7 @@ public void testUnionAnnotations() { assertMethodParamWithTypeParameterHasAnnotation( visitorBuilder.get(), "safeLong", "safeLongVisitor", "Long", Safe.class); assertMethodParamWithTypeParameterHasAnnotation( - visitorBuilder.get(), "unknown", "unknownVisitor", "Long", Safe.class); + visitorBuilder.get(), "unknown", "unknownVisitor", "String", Safe.class); Optional> stageVisitorBuilder = Arrays.stream(ExternalLongUnionExample.class.getDeclaredClasses()) .filter(subclass -> subclass.getName().contains("SafeLongStageVisitorBuilder")) @@ -105,45 +105,28 @@ public void testUnionAnnotations() { .findAny(); assertThat(unknownStageVisitorBuilder).isPresent(); assertMethodParamWithTypeParameterHasAnnotation( - unknownStageVisitorBuilder.get(), "unknown", "unknownVisitor", "Long", Safe.class); - } - - private void assertFieldTypeParamHasAnnotation( - Class parentClass, String fieldName, String typeName, Class annotation) { - AnnotatedType type = Arrays.stream(parentClass.getDeclaredFields()) - .filter(field -> field.getName().contains(fieldName)) - .findAny() - .get() - .getAnnotatedType(); - assertThat(((AnnotatedParameterizedType) type).getAnnotatedActualTypeArguments()) - .filteredOn(t -> t.getType().getTypeName().contains(typeName)) - .hasSize(1) - .allMatch(t -> t.isAnnotationPresent(annotation)); + unknownStageVisitorBuilder.get(), "unknown", "unknownVisitor", "String", Safe.class); } private void assertMethodHasAnnotation( Class parentClass, String methodName, Class annotation) { - Method[] methods = parentClass.getMethods(); - assertThat(methods) - .filteredOn(method -> method.getName().equals(methodName)) - .hasSize(1) + Stream desiredMethods = getMatchingMethods(parentClass, methodName); + assertThat(desiredMethods) + .withFailMessage(String.format( + "Expected %s:%s to have annotation %s", + parentClass.getName(), methodName, annotation.getName())) .allMatch(method -> method.isAnnotationPresent(annotation)); } private void assertMethodParamHasAnnotation( Class parentClass, String methodName, String parameterName, Class annotation) { - Method[] methods = parentClass.getMethods(); - Optional desiredMethod = Arrays.stream(methods) - .filter(method -> method.getName().equals(methodName)) - .findAny(); - assertThat(desiredMethod).isPresent(); - assertThat(desiredMethod) - .map(method -> Arrays.stream(desiredMethod.get().getParameters()) - .filter(parameter -> parameter.getName().contains(parameterName)) - .findAny()) - .isPresent() - .get() - .satisfies(parameter -> parameter.get().isAnnotationPresent(annotation)); + Stream desiredMethods = getMatchingMethods(parentClass, methodName); + assertThat(desiredMethods) + .withFailMessage(String.format( + "Expected %s:%s parameter %s to have annotation %s", + parentClass.getName(), methodName, parameterName, annotation.getName())) + .map(method -> getMatchingParameter(method, parameterName)) + .allMatch(parameter -> parameter.isAnnotationPresent(annotation)); } private void assertMethodParamWithTypeParameterHasAnnotation( @@ -152,20 +135,53 @@ private void assertMethodParamWithTypeParameterHasAnnotation( String parameterName, String typeParameter, Class annotation) { - Method[] methods = parentClass.getMethods(); - List desiredMethods = Arrays.stream(methods) - .filter(method -> method.getName().equals(methodName)) - .collect(Collectors.toList()); - assertThat(desiredMethods).isNotEmpty(); - Stream annotatedTypes = desiredMethods.stream() - .map(method -> Arrays.stream(method.getParameters()) - .filter(parameter -> parameter.getName().contains(parameterName)) - .findAny() - .get() - .getAnnotatedType()); - assertThat(annotatedTypes).allMatch(annotatedType -> Arrays.stream( - ((AnnotatedParameterizedType) annotatedType).getAnnotatedActualTypeArguments()) - .filter(t -> t.getType().getTypeName().contains(typeParameter)) - .allMatch(t -> t.isAnnotationPresent(annotation))); + Stream desiredMethods = getMatchingMethods(parentClass, methodName); + Stream annotatedTypes = desiredMethods.map( + method -> getMatchingParameter(method, parameterName).getAnnotatedType()); + assertThat(annotatedTypes) + .withFailMessage(String.format( + "Expected %s:%s parameter %s of type %s to have annotation %s", + parentClass.getName(), methodName, parameterName, typeParameter, annotation.getName())) + .map(annotatedType -> getAnnotatedTypeParameter(annotatedType, typeParameter)) + .allMatch(t -> t.isAnnotationPresent(annotation)); + } + + private void assertFieldTypeParamHasAnnotation( + Class parentClass, String fieldName, String typeName, Class annotation) { + Stream desiredFields = Arrays.stream(parentClass.getDeclaredFields()) + .filter(field -> field.getName().contains(fieldName)); + Stream annotatedTypeParameters = + desiredFields.map(Field::getAnnotatedType).map(type -> getAnnotatedTypeParameter(type, typeName)); + assertThat(annotatedTypeParameters) + .withFailMessage(String.format( + "Expected %s:%s of type %s to have annotation %s", + parentClass.getName(), fieldName, typeName, annotation.getName())) + .allMatch(t -> t.isAnnotationPresent(annotation)); + } + + private Stream getMatchingMethods(Class parentClass, String methodName) { + return Arrays.stream(parentClass.getMethods()) + .filter(method -> method.getName().equals(methodName)); + } + + private Parameter getMatchingParameter(Method method, String parameterName) { + Optional parameterIfExists = Arrays.stream(method.getParameters()) + .filter(parameter -> parameter.getName().contains(parameterName)) + .findAny(); + assertThat(parameterIfExists) + .withFailMessage(String.format("Expected to find parameter %s on method %s", parameterName, method)) + .isPresent(); + return parameterIfExists.get(); + } + + private AnnotatedType getAnnotatedTypeParameter(AnnotatedType parameterizedType, String parameter) { + Optional typeParameter = Arrays.stream( + ((AnnotatedParameterizedType) parameterizedType).getAnnotatedActualTypeArguments()) + .filter(t -> t.getType().getTypeName().contains(parameter)) + .findAny(); + assertThat(typeParameter) + .withFailMessage("Expected type parameter %s on type %s", typeParameter, parameterizedType) + .isPresent(); + return typeParameter.get(); } } diff --git a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/SafetyEvaluatorTest.java b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/SafetyEvaluatorTest.java index a54ec43a0..095e38a91 100644 --- a/conjure-java-core/src/test/java/com/palantir/conjure/java/types/SafetyEvaluatorTest.java +++ b/conjure-java-core/src/test/java/com/palantir/conjure/java/types/SafetyEvaluatorTest.java @@ -137,7 +137,6 @@ void testMapSafeKey() { .types(object) .types(SAFE_ALIAS) .build(); - // there's an ABI break between 4.28.0 -> 4.36.0 ConjureDefinitionValidator.validateAll(conjureDef, SafetyDeclarationRequirements.ALLOWED); SafetyEvaluator evaluator = new SafetyEvaluator(conjureDef); assertThat(evaluator.evaluate(object)).isEmpty();