Skip to content

Commit 7692704

Browse files
committed
Added a functional test and coverage
1 parent 7e334f5 commit 7692704

8 files changed

Lines changed: 316 additions & 121 deletions

File tree

pom.groovy

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,74 @@ project {
77
'project.build.sourceEncoding' 'UTF-8'
88
'maven.compiler.source' '6'
99
'maven.compiler.target' '6'
10+
'version.client.feign' '9.7.0'
11+
'version.build.jacoco' '0.8.4'
12+
'version.build.surefire' '3.0.0-M3'
1013
}
11-
dependencies{
12-
dependency ('io.github.openfeign:feign-core:9.7.0')
13-
dependency ('io.github.openfeign:feign-jackson:9.7.0')
14-
dependency ('io.github.openfeign:feign-httpclient:9.7.0')
15-
dependency ('com.google.guava:guava:20.0')
16-
dependency ('org.slf4j:slf4j-api:1.7.30')
17-
dependency ('org.slf4j:slf4j-log4j12:1.7.30')
18-
dependency ('org.projectlombok:lombok:1.18.10')
14+
dependencies {
15+
dependency('io.github.openfeign:feign-core:${version.client.feign}')
16+
dependency('io.github.openfeign:feign-jackson:${version.client.feign}')
17+
dependency('io.github.openfeign:feign-httpclient:${version.client.feign}')
18+
dependency('com.google.guava:guava:20.0')
19+
dependency('org.slf4j:slf4j-api:1.7.30')
20+
dependency('org.slf4j:slf4j-log4j12:1.7.30')
21+
dependency('org.projectlombok:lombok:1.18.10')
1922
/* testing */
2023
dependency('org.hamcrest:hamcrest:2.2:test')
21-
dependency('junit:junit:4.13')
24+
dependency('junit:junit:4.13:test')
25+
dependency('org.skyscreamer:jsonassert:1.5.0:test')
2226
}
23-
build{
27+
build {
2428
plugins {
25-
plugin('org.apache.maven.plugins:maven-resources-plugin:2.6'){
26-
configuration{
29+
plugin('org.apache.maven.plugins:maven-resources-plugin:2.6') {
30+
configuration {
2731
encoding '${project.build.sourceEncoding}'
2832
}
2933
}
34+
plugin {
35+
groupId 'org.jacoco'
36+
artifactId 'jacoco-maven-plugin'
37+
version '${version.build.jacoco}'
38+
executions {
39+
execution {
40+
id 'prepare-agent'
41+
phase 'test-compile'
42+
goals 'prepare-agent'
43+
configuration {
44+
propertyName 'surefireArgLine'
45+
destFile '${project.build.directory}/coverage-reports/jacoco-ut.exec'
46+
}
47+
}
48+
execution {
49+
id 'post-test-reports'
50+
phase 'post-integration-test'
51+
goals 'report'
52+
configuration {
53+
dataFile '${project.build.directory}/coverage-reports/jacoco-ut.exec'
54+
outputDirectory '${project.reporting.outputDirectory}/code-coverage'
55+
}
56+
}
57+
}
58+
}
59+
plugin {
60+
artifactId 'maven-surefire-plugin'
61+
version '${version.build.surefire}'
62+
configuration {
63+
useFile 'false'
64+
includes {}
65+
additionalClasspathElements {
66+
additionalClasspathElement '${project.basedir}/src/test/resources'
67+
additionalClasspathElement '${project.build.testOutputDirectory}'
68+
}
69+
argLine '${surefireArgLine}'
70+
}
71+
}
3072
}
31-
resources{
32-
resource{
73+
resources {
74+
resource {
3375
directory 'src/main/resources'
3476
filtering true
35-
includes{
36-
'VERSION'
37-
}
77+
includes('VERSION')
3878
}
3979
}
4080
}

src/main/java/io/ipdata/client/model/IpdataModel.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import lombok.experimental.Accessors;
1010

1111
@Setter(AccessLevel.PACKAGE)
12-
@ToString @Getter @Accessors(fluent = true)
12+
@ToString
13+
@Getter
14+
@Accessors(fluent = true)
1315
public class IpdataModel {
1416
private String ip;
1517
@JsonProperty("is_eu")
16-
private boolean isEu;
18+
private boolean eu;
1719
private String city;
1820
private String organization;
1921
private String region;
@@ -36,4 +38,8 @@ public class IpdataModel {
3638
private Threat threat;
3739
//meta
3840
private int count;
41+
42+
public boolean isEu() {
43+
return eu;
44+
}
3945
}
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package io.ipdata.client.model;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import lombok.Getter;
45
import lombok.ToString;
5-
import lombok.experimental.Accessors;
66

77
@Getter @ToString
8-
@Accessors(fluent = true)
98
public class Threat {
10-
private boolean isTor;
11-
private boolean isProxy;
12-
private boolean isAnonymous;
13-
private boolean isKnownAttacker;
14-
private boolean isKnownAbuser;
15-
private boolean isThreat;
16-
private boolean isBogon;
9+
@JsonProperty("is_tor")
10+
private boolean tor;
11+
@JsonProperty("is_proxy")
12+
private boolean proxy;
13+
@JsonProperty("is_anonymous")
14+
private boolean anonymous;
15+
@JsonProperty("is_known_attacker")
16+
private boolean knownAttacker;
17+
@JsonProperty("is_known_abuser")
18+
private boolean knownAbuser;
19+
@JsonProperty("is_threat")
20+
private boolean threat;
21+
@JsonProperty("is_bogon")
22+
private boolean bogon;
1723
}

src/main/java/io/ipdata/client/service/FieldDecoder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.google.common.io.CharStreams;
5-
import feign.FeignException;
65
import feign.Response;
7-
import feign.codec.DecodeException;
86
import feign.codec.Decoder;
97
import java.io.IOException;
108
import java.lang.reflect.Type;

src/main/java/io/ipdata/client/service/IpdataField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static class IpdataFieldComparator implements java.util.Comparator<Ipdata
5050
@Override
5151
public int compare(IpdataField o1, IpdataField o2) {
5252
if (o1 == null && o2 == null) {
53-
return Integer.MAX_VALUE;
53+
return 0;
5454
}
5555
if (o1 == null) {
5656
return o2.name == null ? 0 : Integer.MIN_VALUE;
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package io.ipdata.client;
2+
3+
import static com.fasterxml.jackson.databind.PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES;
4+
import static io.ipdata.client.TestUtils.KEY;
5+
import static org.junit.runners.Parameterized.Parameters;
6+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
7+
8+
import com.fasterxml.jackson.annotation.JsonAutoDetect;
9+
import com.fasterxml.jackson.annotation.JsonInclude;
10+
import com.fasterxml.jackson.annotation.PropertyAccessor;
11+
import com.fasterxml.jackson.databind.DeserializationFeature;
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import com.google.common.collect.ImmutableMap;
14+
import feign.httpclient.ApacheHttpClient;
15+
import io.ipdata.client.error.RateLimitException;
16+
import io.ipdata.client.model.Asn;
17+
import io.ipdata.client.model.Currency;
18+
import io.ipdata.client.model.IpdataModel;
19+
import io.ipdata.client.model.Threat;
20+
import io.ipdata.client.service.IpdataField;
21+
import io.ipdata.client.service.IpdataService;
22+
import java.net.URL;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
import lombok.SneakyThrows;
26+
import org.apache.http.client.HttpClient;
27+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
28+
import org.apache.http.impl.client.HttpClientBuilder;
29+
import org.junit.Assert;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.Parameterized;
33+
34+
@RunWith(Parameterized.class)
35+
public class IpdataFunctionalTest {
36+
37+
private static IpdataService IPDATA_SERVICE;
38+
private static IpdataService CACHING_IPDATA_SERVICE;
39+
private static ObjectMapper MAPPER;
40+
private static HttpClient HTTP_CLIENT;
41+
42+
@Parameterized.Parameter
43+
public IpdataService ipdataService;
44+
45+
@Test
46+
@SneakyThrows
47+
public void testFullresponse() {
48+
IpdataModel ipdataModel = ipdataService.ipdata("8.8.8.8");
49+
String serialized = MAPPER.writeValueAsString(ipdataModel);
50+
String expected = TestUtils.get(HTTP_CLIENT, "/8.8.8.8", null);
51+
expected = MAPPER.writeValueAsString(MAPPER.readValue(expected, IpdataModel.class));
52+
assertEquals(serialized, expected, false);
53+
if (ipdataService == CACHING_IPDATA_SERVICE) {
54+
//value will be returned from cache now
55+
ipdataModel = ipdataService.ipdata("8.8.8.8");
56+
serialized = MAPPER.writeValueAsString(ipdataModel);
57+
assertEquals(serialized, expected, false);
58+
}
59+
}
60+
61+
@Test
62+
@SneakyThrows
63+
public void testASN() {
64+
Asn asn = ipdataService.asn("8.8.8.8");
65+
String serialized = MAPPER.writeValueAsString(asn);
66+
String expected = TestUtils.get(HTTP_CLIENT, "/8.8.8.8/asn", null);
67+
expected = MAPPER.writeValueAsString(MAPPER.readValue(expected, Asn.class));
68+
assertEquals(serialized, expected, false);
69+
if (ipdataService == CACHING_IPDATA_SERVICE) {
70+
//value will be returned from cache now
71+
asn = ipdataService.asn("8.8.8.8");
72+
serialized = MAPPER.writeValueAsString(asn);
73+
assertEquals(serialized, expected, false);
74+
}
75+
}
76+
77+
@Test
78+
@SneakyThrows
79+
public void testThreat() {
80+
Threat threat = ipdataService.threat("8.8.8.8");
81+
String serialized = MAPPER.writeValueAsString(threat);
82+
String expected = TestUtils.get(HTTP_CLIENT, "/8.8.8.8/threat", null);
83+
expected = MAPPER.writeValueAsString(MAPPER.readValue(expected, Threat.class));
84+
assertEquals(serialized, expected, false);
85+
if (ipdataService == CACHING_IPDATA_SERVICE) {
86+
//value will be returned from cache now
87+
threat = ipdataService.threat("8.8.8.8");
88+
serialized = MAPPER.writeValueAsString(threat);
89+
assertEquals(serialized, expected, false);
90+
}
91+
}
92+
93+
@Test
94+
@SneakyThrows
95+
public void testCurrency() {
96+
Currency currency = ipdataService.currency("8.8.8.8");
97+
String serialized = MAPPER.writeValueAsString(currency);
98+
String expected = TestUtils.get(HTTP_CLIENT, "/8.8.8.8/currency", null);
99+
expected = MAPPER.writeValueAsString(MAPPER.readValue(expected, Currency.class));
100+
assertEquals(serialized, expected, false);
101+
if (ipdataService == CACHING_IPDATA_SERVICE) {
102+
//value will be returned from cache now
103+
currency = ipdataService.currency("8.8.8.8");
104+
serialized = MAPPER.writeValueAsString(currency);
105+
assertEquals(serialized, expected, false);
106+
}
107+
}
108+
109+
@Test
110+
@SneakyThrows
111+
public void testFieldSelection() {
112+
IpdataModel ipdataModel = ipdataService.getFields("8.8.8.8", IpdataField.ASN, IpdataField.CURRENCY);
113+
String serialized = MAPPER.writeValueAsString(ipdataModel);
114+
String expected = TestUtils.get(HTTP_CLIENT, "/8.8.8.8", ImmutableMap.of("fields", "asn,currency"));
115+
expected = MAPPER.writeValueAsString(MAPPER.readValue(expected, IpdataModel.class));
116+
assertEquals(serialized, expected, false);
117+
Assert.assertNull(ipdataModel.threat());
118+
if (ipdataService == CACHING_IPDATA_SERVICE) {
119+
//value will be returned from cache now
120+
ipdataModel = ipdataService.getFields("8.8.8.8", IpdataField.ASN, IpdataField.CURRENCY);
121+
serialized = MAPPER.writeValueAsString(ipdataModel);
122+
assertEquals(serialized, expected, false);
123+
}
124+
}
125+
126+
@SneakyThrows
127+
@Test
128+
public void testSingleFields() {
129+
String field = ipdataService.getCountryName("8.8.8.8");
130+
String expected = TestUtils.get(HTTP_CLIENT, "/8.8.8.8/country_name", null);
131+
Assert.assertEquals(field, expected);
132+
}
133+
134+
@SneakyThrows
135+
@Test
136+
public void testBulkResponse() {
137+
List<IpdataModel> ipdataModels = ipdataService.bulkIpdata(Arrays.asList("8.8.8.8", "1.1.1.1"));
138+
String serialized = MAPPER.writeValueAsString(ipdataModels);
139+
String expected = TestUtils.post(HTTP_CLIENT, "/bulk", "[\"8.8.8.8\",\"1.1.1.1\"]", null);
140+
expected = MAPPER.writeValueAsString(MAPPER.readValue(expected, IpdataModel[].class));
141+
assertEquals(serialized, expected, false);
142+
}
143+
144+
@SneakyThrows
145+
@Test(expected = RateLimitException.class)
146+
public void testError() {
147+
URL url = new URL("https://api.ipdata.co");
148+
IpdataService serviceWithInvalidKey = Ipdata.builder().url(url)
149+
.key("THIS_IS_AN_INVALID_KEY")
150+
.feignClient(new ApacheHttpClient(HttpClientBuilder.create()
151+
.setSSLHostnameVerifier(new NoopHostnameVerifier())
152+
.build())).get();
153+
serviceWithInvalidKey.ipdata("8.8.8.8");
154+
}
155+
156+
@Parameters
157+
public static Iterable<IpdataService> data() {
158+
init();
159+
return Arrays.asList(IPDATA_SERVICE, CACHING_IPDATA_SERVICE);
160+
}
161+
162+
@SneakyThrows
163+
public static void init() {
164+
URL url = new URL("https://api.ipdata.co");
165+
MAPPER = new ObjectMapper();
166+
MAPPER.setPropertyNamingStrategy(CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
167+
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
168+
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
169+
MAPPER.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
170+
HTTP_CLIENT = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier())
171+
.build();
172+
IPDATA_SERVICE = Ipdata.builder().url(url).key(KEY)
173+
.feignClient(new ApacheHttpClient(HttpClientBuilder.create()
174+
.setSSLHostnameVerifier(new NoopHostnameVerifier())
175+
.build())).get();
176+
CACHING_IPDATA_SERVICE = Ipdata.builder().url(url)
177+
.feignClient(new ApacheHttpClient(HttpClientBuilder.create()
178+
.setSSLHostnameVerifier(new NoopHostnameVerifier())
179+
.build()))
180+
.withDefaultCache().key(KEY).get();
181+
}
182+
183+
}

0 commit comments

Comments
 (0)