Skip to content

Commit

Permalink
adds leia-common module and moved all validation utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Gunda Abhishek committed Nov 29, 2024
1 parent ae6eeff commit 5da6e95
Show file tree
Hide file tree
Showing 26 changed files with 454 additions and 233 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
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 Down
5 changes: 5 additions & 0 deletions leia-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions leia-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@
<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>
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
@@ -1,10 +1,8 @@
package com.grookage.leia.client.utils;
package com.grookage.leia.common.builder;

import com.grookage.leia.common.utils.QualifierUtils;
import com.grookage.leia.common.utils.Utils;
import com.grookage.leia.models.annotations.Optional;
import com.grookage.leia.models.annotations.qualifiers.Encrypted;
import com.grookage.leia.models.annotations.qualifiers.PII;
import com.grookage.leia.models.annotations.qualifiers.ShortLived;
import com.grookage.leia.models.annotations.qualifiers.Standard;
import com.grookage.leia.models.attributes.ArrayAttribute;
import com.grookage.leia.models.attributes.BooleanAttribute;
import com.grookage.leia.models.attributes.DoubleAttribute;
Expand All @@ -16,40 +14,32 @@
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.qualifiers.EncryptedQualifier;
import com.grookage.leia.models.qualifiers.PIIQualifier;
import com.grookage.leia.models.qualifiers.QualifierInfo;
import com.grookage.leia.models.qualifiers.ShortLivedQualifier;
import com.grookage.leia.models.qualifiers.StandardQualifier;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.ClassUtils;

import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@UtilityClass
public class SchemaUtil {
public class SchemaBuilder {
public Set<SchemaAttribute> buildSchemaAttributes(final Class<?> klass) {
return getAllFields(klass)
.stream().map(SchemaUtil::schemaAttribute)
return Utils.getAllFields(klass)
.stream().map(SchemaBuilder::schemaAttribute)
.collect(Collectors.toSet());
}

private SchemaAttribute schemaAttribute(final Field field) {
return schemaAttribute(
field.getGenericType(),
field.getName(),
getQualifierInfo(field),
QualifierUtils.getQualifierInfo(field),
isOptional(field)
);
}
Expand Down Expand Up @@ -103,8 +93,8 @@ private SchemaAttribute handleMap(ParameterizedType parameterizedType,
name,
optional,
qualifiers,
schemaAttribute(keyType, "key", getQualifierInfo(keyType), isOptional(keyType)),
schemaAttribute(valueType, "value", getQualifierInfo(valueType), isOptional(valueType))
schemaAttribute(keyType, "key", QualifierUtils.getQualifierInfo(keyType), isOptional(keyType)),
schemaAttribute(valueType, "value", QualifierUtils.getQualifierInfo(valueType), isOptional(valueType))
);
}

Expand All @@ -116,7 +106,8 @@ private SchemaAttribute handleCollection(ParameterizedType parameterizedType,
name,
optional,
qualifiers,
schemaAttribute(elementType, "element", getQualifierInfo(elementType), isOptional(elementType))
schemaAttribute(elementType, "element", QualifierUtils.getQualifierInfo(elementType),
isOptional(elementType))
);
}

Expand All @@ -129,7 +120,8 @@ private SchemaAttribute schemaAttribute(final GenericArrayType genericArrayType,
name,
optional,
qualifiers,
schemaAttribute(componentType, "element", getQualifierInfo(componentType), isOptional(componentType))
schemaAttribute(componentType, "element", QualifierUtils.getQualifierInfo(componentType),
isOptional(componentType))
);
}

Expand All @@ -143,7 +135,7 @@ private SchemaAttribute schemaAttribute(final Class<?> klass,
}

if (klass.isEnum()) {
return new EnumAttribute(name, optional, qualifiers, getEnumValues(klass));
return new EnumAttribute(name, optional, qualifiers, Utils.getEnumValues(klass));
}

if (klass.isPrimitive()) {
Expand All @@ -157,7 +149,8 @@ private SchemaAttribute schemaAttribute(final Class<?> klass,
name,
optional,
qualifiers,
schemaAttribute(componentType, "element", getQualifierInfo(componentType), isOptional(componentType))
schemaAttribute(componentType, "element", QualifierUtils.getQualifierInfo(componentType),
isOptional(componentType))
);
}

Expand Down Expand Up @@ -190,63 +183,6 @@ private SchemaAttribute handlePrimitive(final Class<?> klass,

}

private List<Field> getAllFields(Class<?> type) {
List<Field> fields = new ArrayList<>();
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
fields.addAll(Arrays.asList(c.getDeclaredFields()));
}
return fields;
}

private Set<String> getEnumValues(Class<?> klass) {
return Arrays.stream(klass.getEnumConstants())
.map(enumConstant -> ((Enum<?>) enumConstant).name())
.collect(Collectors.toSet());
}

private Set<QualifierInfo> getQualifierInfo(Field field) {
Set<QualifierInfo> qualifierInfos = new HashSet<>();
if (field.isAnnotationPresent(Encrypted.class)) {
qualifierInfos.add(new EncryptedQualifier());
}
if (field.isAnnotationPresent(Standard.class)) {
qualifierInfos.add(new StandardQualifier());
}
if (field.isAnnotationPresent(PII.class)) {
qualifierInfos.add(new PIIQualifier());
}
if (field.isAnnotationPresent(ShortLived.class)) {
final var shortLived = field.getAnnotation(ShortLived.class);
qualifierInfos.add(new ShortLivedQualifier(shortLived.ttlSeconds()));
}
return qualifierInfos;
}

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

private Set<QualifierInfo> getQualifierInfo(Class<?> klass) {
Set<QualifierInfo> qualifierInfos = new HashSet<>();
if (klass.isAnnotationPresent(Encrypted.class)) {
qualifierInfos.add(new EncryptedQualifier());
}
if (klass.isAnnotationPresent(Standard.class)) {
qualifierInfos.add(new StandardQualifier());
}
if (klass.isAnnotationPresent(PII.class)) {
qualifierInfos.add(new PIIQualifier());
}
if (klass.isAnnotationPresent(ShortLived.class)) {
final var shortLived = klass.getAnnotation(ShortLived.class);
qualifierInfos.add(new ShortLivedQualifier(shortLived.ttlSeconds()));
}
return qualifierInfos;
}

private boolean isOptional(Type type) {
if (type instanceof Class<?> klass) {
return isOptional(klass);
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.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,55 @@
package com.grookage.leia.common.utils;

import com.grookage.leia.models.annotations.qualifiers.Encrypted;
import com.grookage.leia.models.annotations.qualifiers.PII;
import com.grookage.leia.models.annotations.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.ShortLivedQualifier;
import lombok.experimental.UtilityClass;

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

@UtilityClass
public class QualifierUtils {
public Set<QualifierInfo> getQualifierInfo(Type type) {
if (type instanceof Class<?> klass) {
return getQualifierInfo(klass);
}
return new HashSet<>();
}

public Set<QualifierInfo> getQualifierInfo(Field field) {
Set<QualifierInfo> qualifierInfos = new HashSet<>();
if (field.isAnnotationPresent(Encrypted.class)) {
qualifierInfos.add(new EncryptedQualifier());
}
if (field.isAnnotationPresent(PII.class)) {
qualifierInfos.add(new PIIQualifier());
}
if (field.isAnnotationPresent(ShortLived.class)) {
final var shortLived = field.getAnnotation(ShortLived.class);
qualifierInfos.add(new ShortLivedQualifier(shortLived.ttlSeconds()));
}
return qualifierInfos;
}

public Set<QualifierInfo> getQualifierInfo(Class<?> klass) {
Set<QualifierInfo> qualifierInfos = new HashSet<>();
if (klass.isAnnotationPresent(Encrypted.class)) {
qualifierInfos.add(new EncryptedQualifier());
}
if (klass.isAnnotationPresent(PII.class)) {
qualifierInfos.add(new PIIQualifier());
}
if (klass.isAnnotationPresent(ShortLived.class)) {
final var shortLived = klass.getAnnotation(ShortLived.class);
qualifierInfos.add(new ShortLivedQualifier(shortLived.ttlSeconds()));
}
return qualifierInfos;
}
}
Loading

0 comments on commit 5da6e95

Please sign in to comment.