Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Gunda Abhishek committed Dec 2, 2024
1 parent 0a5fd98 commit 5ba56cb
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ 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.

#### Using the LeiaClientBundle

On the client side, please use the `LeiaClientBundle`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
* limitations under the License.
*/

package com.grookage.leia.common.validation;
package com.grookage.leia.common.utils;

import com.google.common.collect.Sets;
import com.grookage.leia.common.exception.SchemaValidationException;
import com.grookage.leia.common.exception.ValidationErrorCode;
import com.grookage.leia.common.utils.Utils;
import com.grookage.leia.models.attributes.ArrayAttribute;
import com.grookage.leia.models.attributes.MapAttribute;
import com.grookage.leia.models.attributes.ObjectAttribute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.grookage.leia.common.utils;
package com.grookage.leia.common.validation;

import com.fasterxml.jackson.databind.JsonNode;
import com.grookage.leia.models.attributes.ArrayAttribute;
Expand Down Expand Up @@ -42,7 +42,7 @@
import java.util.Set;

@UtilityClass
public class ValidationUtils {
public class SchemaPayloadValidator {
public static List<String> validate(final JsonNode jsonNode,
final SchemaValidationType validationType,
final Set<SchemaAttribute> schemaAttributes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.grookage.leia.common.validation;
package com.grookage.leia.common.utils;

import com.grookage.leia.common.exception.ValidationErrorCode;
import com.grookage.leia.models.ResourceHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.grookage.leia.common.utils;
package com.grookage.leia.common.validation;

import com.grookage.leia.common.stubs.NestedStub;
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.utils.SchemaAttributeUtils;
import com.grookage.leia.models.ResourceHelper;
import com.grookage.leia.models.attributes.ArrayAttribute;
import com.grookage.leia.models.attributes.BooleanAttribute;
Expand All @@ -23,7 +24,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ValidationUtilsTest {
class SchemaPayloadValidatorTest {
@Test
void testValidJsonAgainstSchema() throws Exception {
final var jsonNode = ResourceHelper.getObjectMapper().readTree("""
Expand All @@ -40,7 +41,7 @@ void testValidJsonAgainstSchema() throws Exception {
new BooleanAttribute("isActive", false, null)
);

final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);

assertTrue(errors.isEmpty());
}
Expand All @@ -62,7 +63,7 @@ void testUnexpectedFieldInJson() throws Exception {
new BooleanAttribute("isActive", false, null)
);

final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);

assertFalse(errors.isEmpty());
assertEquals(1, errors.size());
Expand All @@ -82,7 +83,7 @@ void testMissingRequiredField() throws Exception {
new IntegerAttribute("age", false, null)
);

final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);

assertFalse(errors.isEmpty());
assertEquals(1, errors.size());
Expand All @@ -103,7 +104,7 @@ void testTypeMismatch() throws Exception {
new IntegerAttribute("age", false, null)
);

final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);

assertFalse(errors.isEmpty());
assertEquals(1, errors.size());
Expand All @@ -130,7 +131,7 @@ void testNestedObjectValidation() throws Exception {
new ObjectAttribute("user", false, null, nestedAttributes)
);

final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);

assertTrue(errors.isEmpty());
}
Expand All @@ -147,7 +148,7 @@ void testArrayValidation() throws Exception {
new ArrayAttribute("numbers", false, null, new IntegerAttribute("element", false, null))
);

final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);

assertTrue(errors.isEmpty());
}
Expand All @@ -173,7 +174,7 @@ void testMapValidation() throws Exception {
)
);

final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);

assertTrue(errors.isEmpty());
}
Expand All @@ -198,7 +199,7 @@ void testInvalidMapValueType() throws Exception {
)
);

final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);

assertFalse(errors.isEmpty());
assertEquals(1, errors.size());
Expand All @@ -210,7 +211,7 @@ void testValidateNested() {
final var jsonNode = ResourceHelper.getObjectMapper().valueToTree(ResourceHelper.getResource("stubs/validNestedStub.json",
NestedStub.class));
final var schemaAttributes = SchemaAttributeUtils.getSchemaAttributes(NestedStub.class);
final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
assertTrue(errors.isEmpty());
}

Expand All @@ -220,7 +221,7 @@ void testValidateParameterizedStub() {
final var jsonNode = ResourceHelper.getObjectMapper().valueToTree(ResourceHelper.getResource("stubs/validParameterizedStub.json",
TestParameterizedStub.class));
final var schemaAttributes = SchemaAttributeUtils.getSchemaAttributes(TestParameterizedStub.class);
final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
assertTrue(errors.isEmpty());
}

Expand All @@ -230,7 +231,7 @@ void testObjectValidation() {
final var jsonNode = ResourceHelper.getObjectMapper().valueToTree(ResourceHelper.getResource("stubs/validObjectStub.json",
TestObjectStub.class));
final var schemaAttributes = SchemaAttributeUtils.getSchemaAttributes(TestObjectStub.class);
final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
Assertions.assertTrue(errors.isEmpty());
}

Expand All @@ -240,7 +241,7 @@ void testRawCollectionSchemaValidation() {
final var jsonNode = ResourceHelper.getObjectMapper().valueToTree(ResourceHelper.getResource("stubs/validRawCollectionStub.json",
TestRawCollectionStub.class));
final var schemaAttributes = SchemaAttributeUtils.getSchemaAttributes(TestRawCollectionStub.class);
final var errors = ValidationUtils.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
final var errors = SchemaPayloadValidator.validate(jsonNode, SchemaValidationType.STRICT, schemaAttributes);
Assertions.assertTrue(errors.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.codahale.metrics.annotation.ExceptionMetered;
import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.databind.JsonNode;
import com.grookage.leia.common.utils.ValidationUtils;
import com.grookage.leia.common.validation.SchemaPayloadValidator;
import com.grookage.leia.core.exception.LeiaErrorCode;
import com.grookage.leia.core.exception.LeiaException;
import com.grookage.leia.core.retrieval.SchemaRetriever;
Expand Down Expand Up @@ -90,7 +90,8 @@ public GenericResponse<List<String>> validateSchema(@Valid SchemaKey schemaKey,
@Valid JsonNode jsonNode) {
final var schemaDetails = schemaRetriever.getSchemaDetails(schemaKey)
.orElseThrow(() -> LeiaException.error(LeiaErrorCode.NO_SCHEMA_FOUND));
final var validationErrors = ValidationUtils.validate(jsonNode, schemaDetails.getValidationType(), schemaDetails.getAttributes());
final var validationErrors = SchemaPayloadValidator.validate(jsonNode, schemaDetails.getValidationType(),
schemaDetails.getAttributes());
if (validationErrors.isEmpty()) {
return GenericResponse.<List<String>>builder()
.success(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.grookage.leia.common.exception.SchemaValidationException;
import com.grookage.leia.common.exception.ValidationErrorCode;
import com.grookage.leia.common.validation.SchemaValidationUtils;
import com.grookage.leia.common.utils.SchemaValidationUtils;
import com.grookage.leia.models.annotations.SchemaDefinition;
import com.grookage.leia.models.schema.SchemaDetails;
import com.grookage.leia.models.schema.SchemaKey;
Expand Down

0 comments on commit 5ba56cb

Please sign in to comment.