Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util classes for building and validating schema + leia-common #11

Merged
merged 13 commits into from
Dec 3, 2024
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Changelog

All notable changes to this project will be documented in this file.
## [0.0.1-RC7]

- Introduced utils classes for building SchemaAttributes from Class and validating a json payload
against the SchemaDetails of a specified schema key
- Introduced a `leia-common` module to host all the common utils classes
- Removed the `SchemaValidatable` annotation and moved it to a generic `SchemaDefinition`
- Few Bug fixes in SchemaValidationUtils

## [0.0.1-RC6]

- LeiaClient - Introduced AuthHeaderSupplier in LeiaClientSupplier to support authentication on leia-server
Expand Down
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Leia is a governance and metadata framework aimed at meeting compliance requirem
<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-bom</artifactId>
<versio>0.0.1-RC6</version>
<versio>0.0.1-RC7</version>
</dependency>
```

Expand Down Expand Up @@ -92,9 +92,11 @@ A sample schema looks like the following
"type": "ARRAY",
"name": "testAttribute",
"optional": true,
"qualifierInfo": {
"type": "PII"
}
"qualifiers": [
{
"type": "PII"
}
]
},
{
"type": "ENUM",
Expand All @@ -103,9 +105,11 @@ A sample schema looks like the following
"values": [
"TEST_ENUM"
],
"qualifierInfo": {
"type": "PII"
}
"qualifiers": [
{
"type": "PII"
}
]
}
],
"transformationTargets": [
Expand All @@ -132,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
7 changes: 6 additions & 1 deletion leia-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia</artifactId>
<version>0.0.1-RC6</version>
<version>0.0.1-RC7</version>
</parent>

<artifactId>leia-bom</artifactId>
Expand All @@ -39,6 +39,11 @@
<artifactId>leia-models</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion leia-client-dropwizard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC6</version>
<version>0.0.1-RC7</version>
<relativePath>../leia-parent</relativePath>
</parent>

Expand Down
8 changes: 7 additions & 1 deletion leia-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
<parent>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC6</version>
<version>0.0.1-RC7</version>
<relativePath>../leia-parent</relativePath>
</parent>

<artifactId>leia-client</artifactId>

<properties>
<lang3.version>3.11</lang3.version>
<maven.deploy.skip>false</maven.deploy.skip>
</properties>

Expand Down Expand Up @@ -73,6 +74,11 @@
<artifactId>leia-models</artifactId>
</dependency>

<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-common</artifactId>
</dependency>

<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-schema-validator</artifactId>
Expand Down
92 changes: 92 additions & 0 deletions leia-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2024. Koushik R <[email protected]>.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->


<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-parent</artifactId>
<version>0.0.1-RC7</version>
<relativePath>../leia-parent</relativePath>
</parent>

<artifactId>leia-common</artifactId>

<properties>
<lang3.version>3.11</lang3.version>
<maven.deploy.skip>false</maven.deploy.skip>
</properties>

<dependencies>
<dependency>
<artifactId>lombok</artifactId>
<groupId>org.projectlombok</groupId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${lang3.version}</version>
</dependency>

<dependency>
<artifactId>junit-jupiter</artifactId>
<groupId>org.junit.jupiter</groupId>
</dependency>

<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-models</artifactId>
</dependency>

<dependency>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
<scope>test</scope>
</dependency>

<dependency>
<artifactId>mockito-junit-jupiter</artifactId>
<groupId>org.mockito</groupId>
<scope>test</scope>
</dependency>

<dependency>
<artifactId>mockito-inline</artifactId>
<groupId>org.mockito</groupId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.grookage.leia</groupId>
<artifactId>leia-models</artifactId>
<type>test-jar</type>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>


</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024. Koushik R <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.grookage.leia.common.builder;

import com.grookage.leia.common.utils.SchemaAttributeUtils;
import com.grookage.leia.models.annotations.SchemaDefinition;
import com.grookage.leia.models.schema.ingestion.CreateSchemaRequest;
import lombok.experimental.UtilityClass;

import java.util.Objects;
import java.util.Optional;

@UtilityClass
public class SchemaBuilder {
public Optional<CreateSchemaRequest> buildSchemaRequest(final Class<?> klass) {
if (Objects.isNull(klass) || !klass.isAnnotationPresent(SchemaDefinition.class)) {
return Optional.empty();
}
final var schemaDefinition = klass.getAnnotation(SchemaDefinition.class);
return Optional.of(CreateSchemaRequest.builder()
.schemaName(schemaDefinition.name())
.namespace(schemaDefinition.namespace())
.description(schemaDefinition.description())
.schemaType(schemaDefinition.type())
.validationType(schemaDefinition.validation())
.attributes(SchemaAttributeUtils.getSchemaAttributes(klass))
.build()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.grookage.leia.validator.exception;
package com.grookage.leia.common.exception;

import lombok.Builder;
import lombok.Getter;
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.validator.exception;
package com.grookage.leia.common.exception;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2024. Koushik R <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.grookage.leia.common.utils;

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 lombok.experimental.UtilityClass;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

@UtilityClass
public class QualifierUtils {
public Optional<QualifierInfo> filter(final Set<QualifierInfo> qualifiers,
final QualifierType type) {
if (Objects.isNull(qualifiers)) {
return Optional.empty();
}
return qualifiers.stream()
.filter(qualifierInfo -> qualifierInfo.getType().equals(type))
.findFirst();
}

public Set<QualifierInfo> getQualifierInfo(final Type type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, rename this to getQualifer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, changed it to getQualifiers

if (type instanceof Class<?> klass) {
return getQualifierInfo(klass);
}
return new HashSet<>();
}

public Set<QualifierInfo> getQualifierInfo(final Field field) {
koushikr marked this conversation as resolved.
Show resolved Hide resolved
Set<QualifierInfo> qualifiers = new HashSet<>();
if (field.isAnnotationPresent(Encrypted.class)) {
qualifiers.add(new EncryptedQualifier());
}
if (field.isAnnotationPresent(PII.class)) {
qualifiers.add(new PIIQualifier());
}
if (field.isAnnotationPresent(ShortLived.class)) {
final var shortLived = field.getAnnotation(ShortLived.class);
qualifiers.add(new ShortLivedQualifier(shortLived.ttlSeconds()));
}
return qualifiers;
}

public Set<QualifierInfo> getQualifierInfo(final Class<?> klass) {
koushikr marked this conversation as resolved.
Show resolved Hide resolved
Set<QualifierInfo> qualifiers = new HashSet<>();
if (klass.isAnnotationPresent(Encrypted.class)) {
qualifiers.add(new EncryptedQualifier());
}
if (klass.isAnnotationPresent(PII.class)) {
qualifiers.add(new PIIQualifier());
}
if (klass.isAnnotationPresent(ShortLived.class)) {
final var shortLived = klass.getAnnotation(ShortLived.class);
qualifiers.add(new ShortLivedQualifier(shortLived.ttlSeconds()));
}
return qualifiers;
}
}
Loading