Skip to content

Commit

Permalink
GH-1 add new sub module. implement string validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffnelson committed Nov 21, 2019
1 parent 57b021e commit 48b2045
Show file tree
Hide file tree
Showing 7 changed files with 278 additions and 48 deletions.
94 changes: 48 additions & 46 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
buildscript { repositories { mavenCentral() } }
buildscript { repositories { mavenCentral()
} }

repositories { mavenCentral() }
allprojects {

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'jacoco'
repositories { mavenCentral() }

group = 'com.github.jeffnelson'
version = currentVersion
archivesBaseName = project.name
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'
apply plugin: 'jacoco'

sourceCompatibility = 1.8
group = 'com.github.jeffnelson'
version = currentVersion
archivesBaseName = project.name

dependencies {
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.11'
sourceCompatibility = 1.8

compileOnly 'org.projectlombok:lombok:1.16.22'
dependencies {
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.11'

testCompileOnly 'org.projectlombok:lombok:1.16.22'
testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.4.RELEASE'
testCompile 'org.springframework.boot:spring-boot-starter-web:1.5.4.RELEASE'
testCompile 'com.google.guava:guava:28.0-jre'
testCompile 'commons-io:commons-io:2.6'
}

jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
includes = [
'com.github.jeffnelson.jackson.patch.deser.PatchFieldDeserializer'
]
excludes = []
compileOnly 'org.projectlombok:lombok:1.16.22'

limit { minimum = 1.0 }
}
testCompileOnly 'org.projectlombok:lombok:1.16.22'
testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.4.RELEASE'
testCompile 'org.springframework.boot:spring-boot-starter-web:1.5.4.RELEASE'
testCompile 'com.google.guava:guava:28.0-jre'
testCompile 'commons-io:commons-io:2.6'
}
}

// always run jacocoTestReport and coverageVerification
test.finalizedBy(jacocoTestReport)
jacocoTestReport.finalizedBy(jacocoTestCoverageVerification)
jacocoTestCoverageVerification {
violationRules {
rule {
element = 'CLASS'
includes = [
'com.github.jeffnelson.jackson.patch.deser.PatchFieldDeserializer'
]
excludes = []

jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
limit { minimum = 1.0 }
}
}
}
}

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
// always run jacocoTestReport and coverageVerification
test.finalizedBy(jacocoTestReport)
jacocoTestReport.finalizedBy(jacocoTestCoverageVerification)

artifacts { archives sourcesJar }
jacocoTestReport {
reports {
xml.enabled false
csv.enabled false
html.destination "${buildDir}/jacocoHtml"
}
}

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

artifacts { archives sourcesJar }
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
currentVersion=1.0.0-SNAPSHOT
currentVersion=1.0.1-SNAPSHOT
6 changes: 6 additions & 0 deletions jackson-merge-patch-validations/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies {
compile rootProject

compile 'org.apache.commons:commons-lang3:3.5'
compile 'javax.validation:validation-api:1.1.0.Final'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

package com.github.jeffnelson.jackson.patch.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.github.jeffnelson.jackson.patch.PatchField;
import com.github.jeffnelson.jackson.patch.validator.constraints.PatchStringValid;

public class PatchStringValidator implements ConstraintValidator<PatchStringValid,PatchField<String>> {

boolean allowNull, allowEmpty, allowBlank;

@Override
public void initialize(PatchStringValid anno) {
this.allowNull = anno.allowNull();
this.allowEmpty = anno.allowEmpty();
this.allowBlank = anno.allowBlank();
}

@Override
public boolean isValid(PatchField<String> value, ConstraintValidatorContext context) {
if (value.shouldPatch()) {
String val = value.getValue();
if (!allowBlank) {
// this involves the most checks. isNotBlank requires not blank, not empty, and not null
return StringUtils.isNotBlank(val);
}
if (!allowEmpty) {
// isNotEmpty requires not empty and not null
return StringUtils.isNotEmpty(val);
}
if (!allowNull) {
return val != null;
}
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

package com.github.jeffnelson.jackson.patch.validator.constraints;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

import com.github.jeffnelson.jackson.patch.PatchField;
import com.github.jeffnelson.jackson.patch.validator.PatchStringValidator;

/**
* Annotation to apply validation to {@literal PatchField<String>}
* <p>
* Default behavior. If {@link PatchField#shouldPatch()} returns true,
* <ul>
* <li>do not allow null
* <li>do not allow empty
* <li>do not allow blank
* </ul>
*
* @author jeff.nelson
*
*/
@Constraint(validatedBy = PatchStringValidator.class)
@Retention(RUNTIME)
@Target(FIELD)
public @interface PatchStringValid {

String message() default "Required";

boolean allowNull() default false;

boolean allowEmpty() default false;

boolean allowBlank() default false;

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@

package com.github.jeffnelson.jackson.patch.validator;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import com.github.jeffnelson.jackson.patch.PatchField;

public class PatchStringValidatorTest {

private PatchStringValidator validator;

@Before
public void setup() {
validator = new PatchStringValidator();
}

@Test
public void testNoPatch() {
PatchField<String> value = PatchField.<String> builder().shouldPatch(false).build();

validator.allowBlank = false;
validator.allowEmpty = false;
validator.allowNull = false;

assertTrue(validator.isValid(value, null));

validator.allowBlank = true;

assertTrue(validator.isValid(value, null));

validator.allowEmpty = true;

assertTrue(validator.isValid(value, null));

validator.allowNull = true;

assertTrue(validator.isValid(value, null));
}

@Test
public void testPatch_helloWorld() {
PatchField<String> value = PatchField.<String> builder().shouldPatch(true).value("hello, world").build();

validator.allowBlank = false;
validator.allowEmpty = false;
validator.allowNull = false;

assertTrue(validator.isValid(value, null));

validator.allowBlank = true;

assertTrue(validator.isValid(value, null));

validator.allowEmpty = true;

assertTrue(validator.isValid(value, null));

validator.allowNull = true;

assertTrue(validator.isValid(value, null));
}

@Test
public void testPatch_null() {
PatchField<String> value = PatchField.<String> builder().shouldPatch(true).value(null).build();

validator.allowBlank = false;
validator.allowEmpty = false;
validator.allowNull = false;

assertFalse(validator.isValid(value, null));

validator.allowBlank = true;

assertFalse(validator.isValid(value, null));

validator.allowEmpty = true;

assertFalse(validator.isValid(value, null));

validator.allowNull = true;

assertTrue(validator.isValid(value, null));
}

@Test
public void testPatch_empty() {
PatchField<String> value = PatchField.<String> builder().shouldPatch(true).value("").build();

validator.allowBlank = false;
validator.allowEmpty = false;
validator.allowNull = false;

assertFalse(validator.isValid(value, null));

validator.allowBlank = true;

assertFalse(validator.isValid(value, null));

validator.allowEmpty = true;

assertTrue(validator.isValid(value, null));

validator.allowNull = true;

assertTrue(validator.isValid(value, null));
}

@Test
public void testPatch_blank() {
PatchField<String> value = PatchField.<String> builder().shouldPatch(true).value(" ").build();

validator.allowBlank = false;
validator.allowEmpty = false;
validator.allowNull = false;

assertFalse(validator.isValid(value, null));

validator.allowBlank = true;

assertTrue(validator.isValid(value, null));

validator.allowEmpty = true;

assertTrue(validator.isValid(value, null));

validator.allowNull = true;

assertTrue(validator.isValid(value, null));
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@

include 'jackson-merge-patch-validations'

0 comments on commit 48b2045

Please sign in to comment.