|
| 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