-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1 add new sub module. implement string validation
- Loading branch information
1 parent
57b021e
commit 48b2045
Showing
7 changed files
with
278 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
currentVersion=1.0.0-SNAPSHOT | ||
currentVersion=1.0.1-SNAPSHOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
42 changes: 42 additions & 0 deletions
42
...ons/src/main/java/com/github/jeffnelson/jackson/patch/validator/PatchStringValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...main/java/com/github/jeffnelson/jackson/patch/validator/constraints/PatchStringValid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; | ||
} |
135 changes: 135 additions & 0 deletions
135
...src/test/java/com/github/jeffnelson/jackson/patch/validator/PatchStringValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
|
||
include 'jackson-merge-patch-validations' |