Skip to content

Commit 0729a5b

Browse files
author
pith
committed
ExceptionMapper on Seed's ValidationException
1 parent 6122b5b commit 0729a5b

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@
7676
<optional>true</optional>
7777
</dependency>
7878

79+
<dependency>
80+
<groupId>javax.ws.rs</groupId>
81+
<artifactId>javax.ws.rs-api</artifactId>
82+
<version>2.0.1</version>
83+
<scope>provided</scope>
84+
</dependency>
85+
86+
<!-- TESTS-->
7987
<dependency>
8088
<groupId>org.seedstack.seed</groupId>
8189
<artifactId>seed-testing</artifactId>
@@ -94,6 +102,12 @@
94102
<version>${javax-el.version}</version>
95103
<scope>test</scope>
96104
</dependency>
105+
<dependency>
106+
<groupId>org.glassfish.jersey.containers</groupId>
107+
<artifactId>jersey-container-servlet</artifactId>
108+
<version>2.22.1</version>
109+
<scope>test</scope>
110+
</dependency>
97111
<dependency>
98112
<groupId>ch.qos.logback</groupId>
99113
<artifactId>logback-classic</artifactId>

src/main/java/org/seedstack/validation/ValidationException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
public class ValidationException extends SeedException {
2020
private static final long serialVersionUID = 1L;
2121

22-
protected ValidationException(ErrorCode errorCode) {
22+
public ValidationException(ErrorCode errorCode) {
2323
super(errorCode);
2424
}
2525
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.validation.internal;
9+
10+
import org.seedstack.validation.ValidationException;
11+
12+
import javax.ws.rs.core.Response;
13+
import javax.ws.rs.ext.ExceptionMapper;
14+
import javax.ws.rs.ext.Provider;
15+
16+
@Provider
17+
public class ValidationExceptionMapper implements ExceptionMapper<ValidationException> {
18+
19+
@Override
20+
public Response toResponse(ValidationException exception) {
21+
return Response.status(Response.Status.BAD_REQUEST).build();
22+
}
23+
}

src/main/java/org/seedstack/validation/internal/ValidationPlugin.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ public String name() {
3030

3131
@Override
3232
public Object nativeUnitModule() {
33-
3433
if (validationModule == null) {
3534
factory = Validation.buildDefaultValidatorFactory();
36-
3735
validationModule = new ValidationModule(factory, validationService);
3836
}
39-
4037
return validationModule;
4138
}
4239

@@ -51,10 +48,8 @@ public ValidationService getValidationService() {
5148

5249
@Override
5350
public void stop() {
54-
5551
if (factory != null) {
5652
factory.close();
5753
}
58-
5954
}
6055
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) 2013-2015, The SeedStack authors <http://seedstack.org>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
package org.seedstack.validation.internal;
9+
10+
import org.assertj.core.api.Assertions;
11+
import org.junit.Test;
12+
import org.seedstack.validation.ValidationException;
13+
14+
import javax.ws.rs.core.Response;
15+
import javax.ws.rs.ext.Provider;
16+
17+
public class ValidationExceptionMapperTest {
18+
19+
@Test
20+
public void testIsProvider() {
21+
ValidationExceptionMapper.class.isAnnotationPresent(Provider.class);
22+
}
23+
24+
@Test
25+
public void testReturnError400() {
26+
Response response = new ValidationExceptionMapper()
27+
.toResponse(new ValidationException(ValidationErrorCode.VALIDATION_ISSUE));
28+
Assertions.assertThat(response.getStatus()).isEqualTo(400);
29+
}
30+
}

0 commit comments

Comments
 (0)