Last active
July 5, 2016 06:23
-
-
Save takaki/0e190fc2c04c01d4c9a8d5ef96b17afd to your computer and use it in GitHub Desktop.
JAX-RSのBean Validationの例 ref: http://qiita.com/takaki@github/items/fdde635d5a505f931fc1
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
import javax.validation.constraints.DecimalMax; | |
import javax.validation.constraints.DecimalMin; | |
import javax.validation.constraints.NotNull; | |
import javax.validation.constraints.Pattern; | |
// ... | |
// ...... | |
// ... | |
@Path("/") | |
public class HelloValidateSampleResource { | |
@GET | |
@Path("minmax") | |
public String minmax( | |
@NotNull @DecimalMax(value = "10") @DecimalMin(value = "3") @QueryParam("number") int number) { | |
return Integer.toString(number * 10); | |
} | |
@GET | |
@Path("pattern") | |
public String pattern( | |
@NotNull @Pattern(regexp = "a+b*c") @QueryParam("pattern") String pattern) { | |
return "pattern = " + pattern; | |
} | |
} |
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
class HelloValidateSampleResourceTest extends Specification { | |
@Shared | |
def jerseyTest = new JerseyTest() { | |
@Override | |
protected Application configure() { | |
return new ResourceConfig(HelloValidateSampleResource.class). | |
property(ServerProperties.BV_DISABLE_VALIDATE_ON_EXECUTABLE_OVERRIDE_CHECK, true) | |
} | |
} | |
def setupSpec() { | |
jerseyTest.setUp() | |
} | |
def cleanupSpec() { | |
jerseyTest.tearDown() | |
} | |
def "test minmax"() { | |
when: | |
def response = jerseyTest.target("/minmax").queryParam("number", "5").request().get() | |
then: | |
response.getStatus() == 200 | |
response.readEntity(String.class) == "50" | |
expect: | |
jerseyTest.target("/minmax").request().get().getStatus() != 200 | |
jerseyTest.target("/minmax").queryParam("number", "20").request().get().getStatus() != 200 | |
jerseyTest.target("/minmax").queryParam("number", "1").request().get().getStatus() != 200 | |
} | |
def "test pattern" () { | |
expect: | |
jerseyTest.target("/pattern").queryParam("pattern", "abc").request().get().getStatus() == 200 | |
jerseyTest.target("/pattern").queryParam("pattern", "aac").request().get().getStatus() == 200 | |
jerseyTest.target("/pattern").queryParam("pattern", "xxx").request().get().getStatus() != 200 | |
jerseyTest.target("/pattern").queryParam("pattern", "xabc").request().get().getStatus() != 200 | |
} | |
} |
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
import org.hibernate.validator.constraints.Email; | |
import org.hibernate.validator.constraints.Length; | |
// ... | |
// ...... | |
// ... | |
@GET | |
@Path("length") | |
public String length(@NotNull @Length(min = 3, max = 10) @QueryParam("str") String str) { | |
return str; | |
} | |
@GET | |
@Path("email") | |
public String email(@NotNull @Email @QueryParam("email") String email) { | |
return email; | |
} | |
} | |
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
def "test length" () { | |
expect: | |
jerseyTest.target("/length").queryParam("str", "1234567").request().get().getStatus() == 200 | |
jerseyTest.target("/length").queryParam("str", "123").request().get().getStatus() == 200 | |
jerseyTest.target("/length").queryParam("str", "abcdefghijk").request().get().getStatus() != 200 | |
jerseyTest.target("/length").queryParam("str", "aa").request().get().getStatus() != 200 | |
} | |
def "test email" () { | |
expect: | |
jerseyTest.target("/email").queryParam("email", "test@example.org").request().get().getStatus() == 200 | |
jerseyTest.target("/email").queryParam("email", "test@localhost").request().get().getStatus() == 200 | |
jerseyTest.target("/email").queryParam("email", "test").request().get().getStatus() != 200 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment