[bs-35] Add validation for EndpointsProperties

[Fixes #48245695]
pull/1/merge
Dave Syer 12 years ago
parent 30087cf6b9
commit 89748028b4

@ -57,13 +57,7 @@
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<artifactId>slf4j-jdk14</artifactId>
<optional>true</optional>
<scope>test</scope>
</dependency>

@ -15,7 +15,9 @@
*/
package org.springframework.bootstrap.service.properties;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import org.springframework.bootstrap.context.annotation.ConfigurationProperties;
@ -28,14 +30,19 @@ import org.springframework.bootstrap.context.annotation.ConfigurationProperties;
@ConfigurationProperties(name = "endpoints", ignoreUnknownFields = false)
public class EndpointsProperties {
@Valid
private Endpoint varz = new Endpoint("/varz");
@Valid
private Endpoint healthz = new Endpoint("/healthz");
@Valid
private Endpoint error = new Endpoint("/error");
@Valid
private Endpoint shutdown = new Endpoint("/shutdown");
@Valid
private Endpoint trace = new Endpoint("/trace");
public Endpoint getVarz() {
@ -61,6 +68,7 @@ public class EndpointsProperties {
public static class Endpoint {
@NotNull
@Pattern(regexp = "/[^/]*", message = "Path must start with /")
private String path;
public Endpoint() {

@ -0,0 +1,80 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* 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.
*/
package org.springframework.bootstrap.service.properties;
import org.junit.Test;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Externalized configuration for endpoints (e.g. paths)
*
* @author Dave Syer
*
*/
public class EndpointsPropertiesTests {
private EndpointsProperties properties = new EndpointsProperties();
@Test
public void testDefaultPathValid() throws Exception {
assertEquals("/error", this.properties.getError().getPath());
Errors errors = validate(this.properties);
assertFalse(errors.hasErrors());
}
@Test
public void testQueryPathValid() throws Exception {
Errors errors = validate(new EndpointsProperties.Endpoint("/foo?bar"));
assertFalse(errors.hasErrors());
}
@Test
public void testEmptyPathInvalid() throws Exception {
Errors errors = validate(new EndpointsProperties.Endpoint(""));
assertTrue(errors.hasErrors());
}
@Test
public void testDoubleSlashInvalid() throws Exception {
Errors errors = validate(new EndpointsProperties.Endpoint("//foo"));
assertTrue(errors.hasErrors());
}
@Test
public void testEmptyPathInProperties() throws Exception {
this.properties.getError().setPath("");
Errors errors = validate(this.properties);
assertTrue(errors.hasErrors());
}
/**
* @return
*/
private Errors validate(Object target) {
BindException errors = new BindException(target, "properties");
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
validator.validate(target, errors);
return errors;
}
}
Loading…
Cancel
Save