Skip to content

Commit

Permalink
Merge pull request #6 from ingokuba/validations
Browse files Browse the repository at this point in the history
Add validations to property and replace regex and required.
  • Loading branch information
ingokuba authored Sep 2, 2020
2 parents ef0bd92 + 666979a commit e1edd86
Show file tree
Hide file tree
Showing 16 changed files with 570 additions and 67 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/maven-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ on:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Build with Maven
run: mvn -B package --file pom.xml
run: mvn --batch-mode --update-snapshots clean package -DskipTests
- name: Unit tests
run: mvn --batch-mode --update-snapshots verify -DskipITs
20 changes: 20 additions & 0 deletions .github/workflows/maven_vulnerability.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

on:
push:
branches: [ master ]

jobs:
vulnerability:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Vulnerability check
run: mvn org.owasp:dependency-check-maven:5.3.2:check -DfailBuildOnAnyVulnerability=true -DsuppressionFiles=suppression.xml
40 changes: 38 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

<groupId>de.intension</groupId>
<artifactId>halo-builder</artifactId>
<version>1.0.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>

<name>HALO Builder</name>
<description>Java builder for the HATEOAS standard HALO</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Versions for JUnit 5 -->
<org.junit.jupiter-version>5.4.2</org.junit.jupiter-version>
<org.junit.platform-version>1.4.2</org.junit.platform-version>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -44,9 +48,41 @@
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${org.junit.jupiter-version}</version>
<scope>test</scope>
</dependency>
<!-- engine for surefire and failsafe -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${org.junit.jupiter-version}</version>
<scope>test</scope>
</dependency>
<!-- Needed for platform launching -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${org.junit.platform-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>${org.junit.platform-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
66 changes: 66 additions & 0 deletions src/main/java/de/intension/halo/AnnotationTransformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package de.intension.halo;

import java.lang.annotation.Annotation;
import java.util.Map;

import de.intension.halo.entity.Property;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

/**
* Transforms a property based on the given annotation.
* <br/>
* <br/>
* Implement this class with a simple constructor that calls sets the annotation type.
*
* <pre>
* public class ExampleTransformer
* extends AnnotationTransformer&lt;ExampleAnnotation&gt;
* {
* public ExampleTransformer()
* {
* super(ExampleAnnotation.class);
* }
* ...
* }
* </pre>
*/
@RequiredArgsConstructor
public abstract class AnnotationTransformer<T extends Annotation>
{

@NonNull
private Class<T> annotationType;

/**
* Check whether annotation class matches <b>&lt;T&gt;</b>.
*
* @param type Class of the annotation that is checked.
* @return <b>true</b> if annotation matches.
*/
public final boolean matchesAnnotation(Class<? extends Annotation> type)
{
return annotationType.equals(type);
}

/**
* Transform the property based on the information of an annotation.
*
* @param annotation Annotation to take information from.
* @param property Property to transform.
*/
public abstract void transformProperty(T annotation, Property property);

/**
* Transform the property based on the information of an annotation and use localization.
* Override this method if localization is needed for the transformation.
*
* @param annotation Annotation to take information from.
* @param property Property to transform.
* @param locales Localization provider.
*/
public void transformProperty(T annotation, Property property, Map<String, Map<String, String>> locales)
{
transformProperty(annotation, property);
}
}
36 changes: 19 additions & 17 deletions src/main/java/de/intension/halo/entity/Property.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.intension.halo.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -43,25 +44,12 @@ public class Property
*/
private Map<String, String> title;
/**
* Regular expression that restricts the {@link #value} of this property.
* <br/>
* <br/>
* In case of {@link #type} {@link DataType#DATE} this should contain the date format.
* List of validations to restrict the property value.
*
* @param regex Set restriction to property value.
* @return Restriction in form of a regular expression.
*/
private String regex;
/**
* Defines weither this property is required on the object.
* <br/>
* <br/>
* A value of <b>null</b> should be interpreted as <b>false</b>.
*
* @param required Set weither this property is required.
* @return <b>true</b> | <b>false</b> | <b>null</b>.
* @param validations Set restrictions for this property.
* @return Restrictions of the property value.
*/
private Boolean required;
private List<Validation> validations;
/**
* Defines weither this property is write protected.
* <br/>
Expand Down Expand Up @@ -134,4 +122,18 @@ public Property setTitle(String locale, String value)
title.put(locale, value);
return this;
}

/**
* Add validation to restrict the property value.
*
* @param validation Validation object.
*/
public Property addValidation(Validation validation)
{
if (validations == null) {
validations = new ArrayList<>();
}
validations.add(validation);
return this;
}
}
35 changes: 35 additions & 0 deletions src/main/java/de/intension/halo/entity/Validation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package de.intension.halo.entity;

import java.util.Map;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
@NoArgsConstructor
@RequiredArgsConstructor
public class Validation
{

/**
*
*/
@NonNull
private String name;
/**
* Validation object.
*/
@NonNull
private Object value;
/**
* Multi-language message of the validation.
*
* @param message Set mappings of language code to translation.
* @return Mappings of language code to translation.
*/
private Map<String, String> message;
}
Loading

0 comments on commit e1edd86

Please sign in to comment.