Skip to content

Commit 4bdcf7e

Browse files
committed
implemented ipv4 and ipv6 support, changed signature of FormatValidator#validate() , added guava dependency
1 parent 1c83b62 commit 4bdcf7e

File tree

4 files changed

+114
-11
lines changed

4 files changed

+114
-11
lines changed

core/pom.xml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,16 @@
7575
</plugins>
7676
</build>
7777
<dependencies>
78-
<dependency>
79-
<groupId>org.json</groupId>
80-
<artifactId>json</artifactId>
81-
<version>20160212</version>
82-
</dependency>
78+
<dependency>
79+
<groupId>org.json</groupId>
80+
<artifactId>json</artifactId>
81+
<version>20160212</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>com.google.guava</groupId>
85+
<artifactId>guava</artifactId>
86+
<version>19.0</version>
87+
</dependency>
8388
<dependency>
8489
<groupId>junit</groupId>
8590
<artifactId>junit</artifactId>
@@ -90,6 +95,7 @@
9095
<groupId>org.mockito</groupId>
9196
<artifactId>mockito-core</artifactId>
9297
<version>1.10.19</version>
98+
<scope>test</scope>
9399
</dependency>
94100
</dependencies>
95101
</project>
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.everit.json.schema;
22

3+
import java.util.Optional;
4+
35
import org.everit.json.schema.internal.DefaultFormatValidator;
46

57
@FunctionalInterface
@@ -8,11 +10,10 @@ public interface FormatValidator {
810
/**
911
* No-operation implementation (never throws {@link ValidationException}).
1012
*/
11-
public static final FormatValidator NONE = (subject, format) -> {
12-
};
13+
public static final FormatValidator NONE = (subject, format) -> Optional.empty();
1314

1415
public static FormatValidator DEFAULT = new DefaultFormatValidator();
1516

16-
void validate(String subject, Format format);
17+
Optional<String> validate(String subject, Format format);
1718

1819
}
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
package org.everit.json.schema.internal;
22

3+
import java.net.InetAddress;
4+
import java.util.Objects;
5+
import java.util.Optional;
6+
37
import org.everit.json.schema.Format;
48
import org.everit.json.schema.FormatValidator;
59

10+
import com.google.common.net.InetAddresses;
11+
612
public class DefaultFormatValidator implements FormatValidator {
713

8-
@Override
9-
public void validate(String subject, Format format) {
10-
// TODO Auto-generated method stub
14+
private static final int IPV4_LENGTH = 4;
15+
16+
private static final int IPV6_LENGTH = 16;
1117

18+
private Optional<InetAddress> asInetAddress(final String subject) {
19+
if (InetAddresses.isInetAddress(subject)) {
20+
return Optional.of(InetAddresses.forString(subject));
21+
} else {
22+
return Optional.empty();
23+
}
1224
}
1325

26+
private Optional<String> checkIpAddress(final String subject, final int expectedLength,
27+
final String failureFormat) {
28+
return asInetAddress(subject)
29+
.filter(addr -> addr.getAddress().length == expectedLength)
30+
.map(addr -> Optional.<String> empty())
31+
.orElse(Optional.of(String.format(failureFormat, subject)));
32+
}
33+
34+
@Override
35+
public Optional<String> validate(final String subject, final Format format) {
36+
Objects.requireNonNull(format, "format cannot be null");
37+
switch (format) {
38+
case IPV4:
39+
return checkIpAddress(subject, IPV4_LENGTH, "[%s] is not a valid ipv4 address");
40+
case IPV6:
41+
return checkIpAddress(subject, IPV6_LENGTH, "[%s] is not a valid ipv6 address");
42+
default:
43+
throw new IllegalArgumentException("unsupported format: " + format);
44+
}
45+
}
1446
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.everit.json.schema.internal;
2+
3+
import java.util.Optional;
4+
5+
import org.everit.json.schema.Format;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
public class DefaultFormatValidatorTest {
10+
11+
private static final String THERE_IS_NO_PLACE_LIKE = "127.0.0.1";
12+
13+
private static final String IPV6_ADDR = "2001:db8:85a3:0:0:8a2e:370:7334";
14+
15+
private void assertFailure(final String subject, final Format format, final String expectedFailure) {
16+
Optional<String> opt = new DefaultFormatValidator().validate(subject, format);
17+
Assert.assertNotNull("the optional is not null", opt);
18+
Assert.assertTrue("failure exists", opt.isPresent());
19+
Assert.assertEquals(expectedFailure, opt.get());
20+
}
21+
22+
private void assertSuccess(final String subject, final Format format) {
23+
Optional<String> opt = new DefaultFormatValidator().validate(subject, format);
24+
Assert.assertNotNull("the optional is not null", opt);
25+
Assert.assertFalse("failure not exist", opt.isPresent());
26+
}
27+
28+
@Test
29+
public void ipv4Failure() {
30+
assertFailure("asd", Format.IPV4, "[asd] is not a valid ipv4 address");
31+
}
32+
33+
@Test
34+
public void ipv4LengthFailure() {
35+
assertFailure(IPV6_ADDR, Format.IPV4,
36+
"[2001:db8:85a3:0:0:8a2e:370:7334] is not a valid ipv4 address");
37+
}
38+
39+
@Test
40+
public void ipv4Success() {
41+
assertSuccess(THERE_IS_NO_PLACE_LIKE, Format.IPV4);
42+
}
43+
44+
@Test
45+
public void ipv6Failure() {
46+
assertFailure("asd", Format.IPV6, "[asd] is not a valid ipv6 address");
47+
}
48+
49+
@Test
50+
public void ipv6LengthFailure() {
51+
assertFailure(THERE_IS_NO_PLACE_LIKE, Format.IPV6, "[127.0.0.1] is not a valid ipv6 address");
52+
}
53+
54+
@Test
55+
public void ipv6Success() {
56+
assertSuccess(IPV6_ADDR, Format.IPV6);
57+
}
58+
59+
@Test(expected = NullPointerException.class)
60+
public void nullFormat() {
61+
new DefaultFormatValidator().validate("asd", null);
62+
}
63+
64+
}

0 commit comments

Comments
 (0)