From 196559a9eeb59b9974364b5753c41382f3beb9a7 Mon Sep 17 00:00:00 2001 From: Koushik R Date: Tue, 3 Dec 2024 13:11:45 +0530 Subject: [PATCH] a) Removed unnecessary imports. b) Moved for loops to forEach in validationUtils c) Overly used constants to static String declarations --- CHANGELOG.md | 5 ++- README.md | 2 +- leia-common/pom.xml | 4 +- .../leia/common/builder/SchemaBuilder.java | 28 ++++-------- .../leia/common/utils/QualifierUtils.java | 6 +-- .../common/utils/SchemaValidationUtils.java | 6 +-- .../validation/SchemaPayloadValidator.java | 44 +++++-------------- .../grookage/leia/common/LeiaTestUtils.java | 14 +----- .../common/builder/SchemaBuilderTest.java | 15 +------ .../common/stubs/TestRawCollectionStub.java | 7 +-- .../utils/SchemaValidationUtilsTest.java | 12 +---- .../SchemaPayloadValidatorTest.java | 12 +---- 12 files changed, 35 insertions(+), 120 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26d29ec..e4e40c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,15 +1,16 @@ # Changelog All notable changes to this project will be documented in this file. + ## [0.0.1-RC7] -- Introduced a `leia-common` module to host all the common utils classes +- Introduced a `leia-common` module to host all the common utils classes - Added annotation classes for the Qualifiers( PII, Encrypted, ShortLived) that can be added on the members of the Schema class - Replaced the `SchemaValidatable` annotation and moved it to a generic `SchemaDefinition` - Introduced `SchemaBuilder`: For building the schema request against a class annotated with SchemaDefinition - Introduced `SchemaPayloadValidator`: For validating a schema json payload against a specified SchemaKey -- Addressed issues in handling plain `Object` and Primitive Class types in `SchemaValidationUtils` +- Addressed issues in handling plain `Object` and Primitive Class types in `SchemaValidationUtils` ## [0.0.1-RC6] diff --git a/README.md b/README.md index 661a4d8..a334e1a 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ A sample schema looks like the following the `LeiaMessageProduceClient`, will multiplex the testSchema to both the versions, the transformationTargets ought to be jsonPathRules. -Please refer to the `SchemaBuilder` class which can be used to generate schema against a class. +Please refer to the `SchemaBuilder` class which can be used to generate schema against a class. #### Using the LeiaClientBundle diff --git a/leia-common/pom.xml b/leia-common/pom.xml index e88605f..4548e50 100644 --- a/leia-common/pom.xml +++ b/leia-common/pom.xml @@ -16,8 +16,8 @@ --> - 4.0.0 diff --git a/leia-common/src/main/java/com/grookage/leia/common/builder/SchemaBuilder.java b/leia-common/src/main/java/com/grookage/leia/common/builder/SchemaBuilder.java index 5a87de2..f4659e6 100644 --- a/leia-common/src/main/java/com/grookage/leia/common/builder/SchemaBuilder.java +++ b/leia-common/src/main/java/com/grookage/leia/common/builder/SchemaBuilder.java @@ -19,17 +19,7 @@ import com.grookage.leia.common.utils.FieldUtils; import com.grookage.leia.common.utils.QualifierUtils; import com.grookage.leia.models.annotations.SchemaDefinition; -import com.grookage.leia.models.attributes.ArrayAttribute; -import com.grookage.leia.models.attributes.BooleanAttribute; -import com.grookage.leia.models.attributes.DoubleAttribute; -import com.grookage.leia.models.attributes.EnumAttribute; -import com.grookage.leia.models.attributes.FloatAttribute; -import com.grookage.leia.models.attributes.IntegerAttribute; -import com.grookage.leia.models.attributes.LongAttribute; -import com.grookage.leia.models.attributes.MapAttribute; -import com.grookage.leia.models.attributes.ObjectAttribute; -import com.grookage.leia.models.attributes.SchemaAttribute; -import com.grookage.leia.models.attributes.StringAttribute; +import com.grookage.leia.models.attributes.*; import com.grookage.leia.models.qualifiers.QualifierInfo; import com.grookage.leia.models.schema.ingestion.CreateSchemaRequest; import lombok.experimental.UtilityClass; @@ -39,16 +29,14 @@ import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.Arrays; -import java.util.Collection; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; @UtilityClass public class SchemaBuilder { + + private static final String ELEMENT = "element"; + public Optional buildSchemaRequest(final Class klass) { if (Objects.isNull(klass) || !klass.isAnnotationPresent(SchemaDefinition.class)) { return Optional.empty(); @@ -143,7 +131,7 @@ private SchemaAttribute handleCollection(final ParameterizedType parameterizedTy name, optional, qualifiers, - schemaAttribute(elementType, "element", QualifierUtils.getQualifiers(elementType), + schemaAttribute(elementType, ELEMENT, QualifierUtils.getQualifiers(elementType), isOptional(elementType)) ); } @@ -157,7 +145,7 @@ private SchemaAttribute schemaAttribute(final GenericArrayType genericArrayType, name, optional, qualifiers, - schemaAttribute(componentType, "element", QualifierUtils.getQualifiers(componentType), + schemaAttribute(componentType, ELEMENT, QualifierUtils.getQualifiers(componentType), isOptional(componentType)) ); } @@ -187,7 +175,7 @@ private SchemaAttribute schemaAttribute(final Class klass, name, optional, qualifiers, - schemaAttribute(componentType, "element", QualifierUtils.getQualifiers(componentType), + schemaAttribute(componentType, ELEMENT, QualifierUtils.getQualifiers(componentType), isOptional(componentType)) ); } diff --git a/leia-common/src/main/java/com/grookage/leia/common/utils/QualifierUtils.java b/leia-common/src/main/java/com/grookage/leia/common/utils/QualifierUtils.java index 6e8e3ee..5447442 100644 --- a/leia-common/src/main/java/com/grookage/leia/common/utils/QualifierUtils.java +++ b/leia-common/src/main/java/com/grookage/leia/common/utils/QualifierUtils.java @@ -19,11 +19,7 @@ import com.grookage.leia.models.annotations.attribute.qualifiers.Encrypted; import com.grookage.leia.models.annotations.attribute.qualifiers.PII; import com.grookage.leia.models.annotations.attribute.qualifiers.ShortLived; -import com.grookage.leia.models.qualifiers.EncryptedQualifier; -import com.grookage.leia.models.qualifiers.PIIQualifier; -import com.grookage.leia.models.qualifiers.QualifierInfo; -import com.grookage.leia.models.qualifiers.QualifierType; -import com.grookage.leia.models.qualifiers.ShortLivedQualifier; +import com.grookage.leia.models.qualifiers.*; import lombok.experimental.UtilityClass; import java.lang.reflect.Field; diff --git a/leia-common/src/main/java/com/grookage/leia/common/utils/SchemaValidationUtils.java b/leia-common/src/main/java/com/grookage/leia/common/utils/SchemaValidationUtils.java index 0afba08..daa8a8e 100644 --- a/leia-common/src/main/java/com/grookage/leia/common/utils/SchemaValidationUtils.java +++ b/leia-common/src/main/java/com/grookage/leia/common/utils/SchemaValidationUtils.java @@ -19,11 +19,7 @@ import com.google.common.collect.Sets; import com.grookage.leia.common.exception.SchemaValidationException; import com.grookage.leia.common.exception.ValidationErrorCode; -import com.grookage.leia.models.attributes.ArrayAttribute; -import com.grookage.leia.models.attributes.MapAttribute; -import com.grookage.leia.models.attributes.ObjectAttribute; -import com.grookage.leia.models.attributes.SchemaAttribute; -import com.grookage.leia.models.attributes.SchemaAttributeHandler; +import com.grookage.leia.models.attributes.*; import com.grookage.leia.models.schema.SchemaDetails; import com.grookage.leia.models.schema.SchemaValidationType; import com.grookage.leia.models.schema.SchemaValidationVisitor; diff --git a/leia-common/src/main/java/com/grookage/leia/common/validation/SchemaPayloadValidator.java b/leia-common/src/main/java/com/grookage/leia/common/validation/SchemaPayloadValidator.java index f12cf98..c416892 100644 --- a/leia-common/src/main/java/com/grookage/leia/common/validation/SchemaPayloadValidator.java +++ b/leia-common/src/main/java/com/grookage/leia/common/validation/SchemaPayloadValidator.java @@ -17,41 +17,25 @@ package com.grookage.leia.common.validation; import com.fasterxml.jackson.databind.JsonNode; -import com.grookage.leia.models.attributes.ArrayAttribute; -import com.grookage.leia.models.attributes.BooleanAttribute; -import com.grookage.leia.models.attributes.ByteAttribute; -import com.grookage.leia.models.attributes.DoubleAttribute; -import com.grookage.leia.models.attributes.EnumAttribute; -import com.grookage.leia.models.attributes.FloatAttribute; -import com.grookage.leia.models.attributes.IntegerAttribute; -import com.grookage.leia.models.attributes.LongAttribute; -import com.grookage.leia.models.attributes.MapAttribute; -import com.grookage.leia.models.attributes.ObjectAttribute; -import com.grookage.leia.models.attributes.SchemaAttribute; -import com.grookage.leia.models.attributes.SchemaAttributeAcceptor; -import com.grookage.leia.models.attributes.StringAttribute; +import com.grookage.leia.models.attributes.*; import com.grookage.leia.models.schema.SchemaValidationType; import com.grookage.leia.models.utils.MapperUtils; import lombok.experimental.UtilityClass; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.stream.Collectors; @UtilityClass public class SchemaPayloadValidator { public static List validate(final JsonNode jsonNode, final SchemaValidationType validationType, final Set schemaAttributes) { - List validationErrors = new ArrayList<>(); - - Map schemaMap = new HashMap<>(); - for (SchemaAttribute attribute : schemaAttributes) { - schemaMap.put(attribute.getName(), attribute); - } + final List validationErrors = new ArrayList<>(); + final var schemaMap = schemaAttributes.stream() + .collect(Collectors.toMap(SchemaAttribute::getName, attribute -> attribute, (a, b) -> b)); // Validate extra fields in case of Strict Validation if (validationType == SchemaValidationType.STRICT) { @@ -63,23 +47,22 @@ public static List validate(final JsonNode jsonNode, } // Validate missing and type mismatched fields - for (SchemaAttribute attribute : schemaAttributes) { + // Check the attribute only if the jsonNode is an object + schemaAttributes.forEach(attribute -> { final var fieldName = attribute.getName(); - // Check the attribute only if the jsonNode is an object if (jsonNode.isObject() && !jsonNode.has(fieldName)) { if (!attribute.isOptional()) { validationErrors.add("Missing required field: " + fieldName); } - continue; + return; } - if (jsonNode.isValueNode()) { validateField(jsonNode, attribute, validationType, validationErrors); - continue; + return; } final var fieldNode = jsonNode.get(fieldName); validateField(fieldNode, attribute, validationType, validationErrors); - } + }); return validationErrors; } @@ -101,12 +84,9 @@ private void validateField(final JsonNode fieldNode, if (objectAttribute.getNestedAttributes() != null) { validationErrors.addAll(validate(fieldNode, validationType, objectAttribute.getNestedAttributes())); } - return; - } - if (attribute instanceof ArrayAttribute arrayAttribute) { + } else if (attribute instanceof ArrayAttribute arrayAttribute) { validateCollectionAttribute(fieldNode, arrayAttribute, validationType, validationErrors); - } - if (attribute instanceof MapAttribute mapAttribute) { + } else if (attribute instanceof MapAttribute mapAttribute) { validateMapAttribute(fieldNode, mapAttribute, validationType, validationErrors); } } diff --git a/leia-common/src/test/java/com/grookage/leia/common/LeiaTestUtils.java b/leia-common/src/test/java/com/grookage/leia/common/LeiaTestUtils.java index 331e35b..5879932 100644 --- a/leia-common/src/test/java/com/grookage/leia/common/LeiaTestUtils.java +++ b/leia-common/src/test/java/com/grookage/leia/common/LeiaTestUtils.java @@ -1,19 +1,7 @@ package com.grookage.leia.common; import com.grookage.leia.common.utils.QualifierUtils; -import com.grookage.leia.models.attributes.ArrayAttribute; -import com.grookage.leia.models.attributes.BooleanAttribute; -import com.grookage.leia.models.attributes.ByteAttribute; -import com.grookage.leia.models.attributes.DoubleAttribute; -import com.grookage.leia.models.attributes.EnumAttribute; -import com.grookage.leia.models.attributes.FloatAttribute; -import com.grookage.leia.models.attributes.IntegerAttribute; -import com.grookage.leia.models.attributes.LongAttribute; -import com.grookage.leia.models.attributes.MapAttribute; -import com.grookage.leia.models.attributes.ObjectAttribute; -import com.grookage.leia.models.attributes.SchemaAttribute; -import com.grookage.leia.models.attributes.SchemaAttributeAcceptor; -import com.grookage.leia.models.attributes.StringAttribute; +import com.grookage.leia.models.attributes.*; import com.grookage.leia.models.qualifiers.QualifierInfo; import com.grookage.leia.models.qualifiers.QualifierType; import com.grookage.leia.models.qualifiers.ShortLivedQualifier; diff --git a/leia-common/src/test/java/com/grookage/leia/common/builder/SchemaBuilderTest.java b/leia-common/src/test/java/com/grookage/leia/common/builder/SchemaBuilderTest.java index 3fb5d8f..50e83ef 100644 --- a/leia-common/src/test/java/com/grookage/leia/common/builder/SchemaBuilderTest.java +++ b/leia-common/src/test/java/com/grookage/leia/common/builder/SchemaBuilderTest.java @@ -1,23 +1,12 @@ package com.grookage.leia.common.builder; import com.grookage.leia.common.LeiaTestUtils; -import com.grookage.leia.common.stubs.NestedStub; -import com.grookage.leia.common.stubs.RecordStub; -import com.grookage.leia.common.stubs.TestEnum; -import com.grookage.leia.common.stubs.TestObjectStub; -import com.grookage.leia.common.stubs.TestParameterizedStub; -import com.grookage.leia.common.stubs.TestRawCollectionStub; +import com.grookage.leia.common.stubs.*; import com.grookage.leia.models.annotations.SchemaDefinition; import com.grookage.leia.models.annotations.attribute.Optional; import com.grookage.leia.models.annotations.attribute.qualifiers.Encrypted; import com.grookage.leia.models.annotations.attribute.qualifiers.PII; -import com.grookage.leia.models.attributes.ArrayAttribute; -import com.grookage.leia.models.attributes.EnumAttribute; -import com.grookage.leia.models.attributes.IntegerAttribute; -import com.grookage.leia.models.attributes.MapAttribute; -import com.grookage.leia.models.attributes.ObjectAttribute; -import com.grookage.leia.models.attributes.SchemaAttribute; -import com.grookage.leia.models.attributes.StringAttribute; +import com.grookage.leia.models.attributes.*; import com.grookage.leia.models.qualifiers.EncryptedQualifier; import com.grookage.leia.models.qualifiers.PIIQualifier; import com.grookage.leia.models.schema.SchemaType; diff --git a/leia-common/src/test/java/com/grookage/leia/common/stubs/TestRawCollectionStub.java b/leia-common/src/test/java/com/grookage/leia/common/stubs/TestRawCollectionStub.java index b5c5123..1f5d06d 100644 --- a/leia-common/src/test/java/com/grookage/leia/common/stubs/TestRawCollectionStub.java +++ b/leia-common/src/test/java/com/grookage/leia/common/stubs/TestRawCollectionStub.java @@ -4,12 +4,7 @@ import lombok.Data; import lombok.extern.jackson.Jacksonized; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.SortedMap; +import java.util.*; @Data @Builder diff --git a/leia-common/src/test/java/com/grookage/leia/common/utils/SchemaValidationUtilsTest.java b/leia-common/src/test/java/com/grookage/leia/common/utils/SchemaValidationUtilsTest.java index 1944db9..36daa3f 100644 --- a/leia-common/src/test/java/com/grookage/leia/common/utils/SchemaValidationUtilsTest.java +++ b/leia-common/src/test/java/com/grookage/leia/common/utils/SchemaValidationUtilsTest.java @@ -2,17 +2,7 @@ import com.grookage.leia.common.exception.ValidationErrorCode; import com.grookage.leia.models.ResourceHelper; -import com.grookage.leia.models.attributes.ArrayAttribute; -import com.grookage.leia.models.attributes.BooleanAttribute; -import com.grookage.leia.models.attributes.ByteAttribute; -import com.grookage.leia.models.attributes.DoubleAttribute; -import com.grookage.leia.models.attributes.EnumAttribute; -import com.grookage.leia.models.attributes.FloatAttribute; -import com.grookage.leia.models.attributes.IntegerAttribute; -import com.grookage.leia.models.attributes.LongAttribute; -import com.grookage.leia.models.attributes.MapAttribute; -import com.grookage.leia.models.attributes.ObjectAttribute; -import com.grookage.leia.models.attributes.StringAttribute; +import com.grookage.leia.models.attributes.*; import com.grookage.leia.models.schema.SchemaDetails; import com.grookage.leia.models.schema.SchemaValidationType; import lombok.SneakyThrows; diff --git a/leia-common/src/test/java/com/grookage/leia/common/validation/SchemaPayloadValidatorTest.java b/leia-common/src/test/java/com/grookage/leia/common/validation/SchemaPayloadValidatorTest.java index 67cfd92..41e3393 100644 --- a/leia-common/src/test/java/com/grookage/leia/common/validation/SchemaPayloadValidatorTest.java +++ b/leia-common/src/test/java/com/grookage/leia/common/validation/SchemaPayloadValidatorTest.java @@ -6,13 +6,7 @@ import com.grookage.leia.common.stubs.TestParameterizedStub; import com.grookage.leia.common.stubs.TestRawCollectionStub; import com.grookage.leia.models.ResourceHelper; -import com.grookage.leia.models.attributes.ArrayAttribute; -import com.grookage.leia.models.attributes.BooleanAttribute; -import com.grookage.leia.models.attributes.IntegerAttribute; -import com.grookage.leia.models.attributes.MapAttribute; -import com.grookage.leia.models.attributes.ObjectAttribute; -import com.grookage.leia.models.attributes.SchemaAttribute; -import com.grookage.leia.models.attributes.StringAttribute; +import com.grookage.leia.models.attributes.*; import com.grookage.leia.models.schema.SchemaValidationType; import lombok.SneakyThrows; import org.junit.jupiter.api.Assertions; @@ -20,9 +14,7 @@ import java.util.Set; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; class SchemaPayloadValidatorTest { @Test