Skip to content

Commit

Permalink
a) Removed unnecessary imports.
Browse files Browse the repository at this point in the history
b) Moved for loops to forEach in validationUtils
c) Overly used constants to static String declarations
  • Loading branch information
koushikr committed Dec 3, 2024
1 parent 5b7f8b9 commit 196559a
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 120 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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]

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions leia-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
-->


<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CreateSchemaRequest> buildSchemaRequest(final Class<?> klass) {
if (Objects.isNull(klass) || !klass.isAnnotationPresent(SchemaDefinition.class)) {
return Optional.empty();
Expand Down Expand Up @@ -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))
);
}
Expand All @@ -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))
);
}
Expand Down Expand Up @@ -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))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> validate(final JsonNode jsonNode,
final SchemaValidationType validationType,
final Set<SchemaAttribute> schemaAttributes) {
List<String> validationErrors = new ArrayList<>();

Map<String, SchemaAttribute> schemaMap = new HashMap<>();
for (SchemaAttribute attribute : schemaAttributes) {
schemaMap.put(attribute.getName(), attribute);
}
final List<String> 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) {
Expand All @@ -63,23 +47,22 @@ public static List<String> 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;
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@
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;
import org.junit.jupiter.api.Test;

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
Expand Down

0 comments on commit 196559a

Please sign in to comment.