|
| 1 | +package io.ipdata.client; |
| 2 | + |
| 3 | +import io.ipdata.client.error.IpdataException; |
| 4 | +import io.ipdata.client.error.RemoteIpdataException; |
| 5 | +import io.ipdata.client.model.IpdataModel; |
| 6 | +import io.ipdata.client.service.IpdataService; |
| 7 | +import lombok.SneakyThrows; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import static io.ipdata.client.service.IpdataField.ASN; |
| 12 | +import static io.ipdata.client.service.IpdataField.CURRENCY; |
| 13 | + |
| 14 | +public class EdgeCaseTest { |
| 15 | + |
| 16 | + private static final TestContext TEST_CONTEXT = new TestContext(MockIpdataServer.API_KEY, MockIpdataServer.getInstance().getUrl()); |
| 17 | + |
| 18 | + @Test(expected = IllegalArgumentException.class) |
| 19 | + @SneakyThrows |
| 20 | + public void testGetFieldsWithNoFieldsThrows() { |
| 21 | + IpdataService service = TEST_CONTEXT.ipdataService(); |
| 22 | + service.getFields("8.8.8.8"); |
| 23 | + } |
| 24 | + |
| 25 | + @Test(expected = IllegalArgumentException.class) |
| 26 | + @SneakyThrows |
| 27 | + public void testGetFieldsWithNoFieldsThrowsCaching() { |
| 28 | + IpdataService service = TEST_CONTEXT.cachingIpdataService(); |
| 29 | + service.getFields("8.8.8.8"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @SneakyThrows |
| 34 | + public void testInvalidKeyReturnsRemoteException() { |
| 35 | + IpdataService service = Ipdata.builder() |
| 36 | + .url(TEST_CONTEXT.url()) |
| 37 | + .key("INVALID_KEY") |
| 38 | + .noCache() |
| 39 | + .get(); |
| 40 | + try { |
| 41 | + service.ipdata("8.8.8.8"); |
| 42 | + Assert.fail("Expected RemoteIpdataException"); |
| 43 | + } catch (RemoteIpdataException e) { |
| 44 | + Assert.assertEquals(401, e.getStatus()); |
| 45 | + Assert.assertNotNull(e.getMessage()); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + @SneakyThrows |
| 51 | + public void testCachedInvalidKeyUnwrapsException() { |
| 52 | + IpdataService service = Ipdata.builder() |
| 53 | + .url(TEST_CONTEXT.url()) |
| 54 | + .key("INVALID_KEY") |
| 55 | + .withDefaultCache() |
| 56 | + .get(); |
| 57 | + try { |
| 58 | + service.ipdata("8.8.8.8"); |
| 59 | + Assert.fail("Expected RemoteIpdataException"); |
| 60 | + } catch (RemoteIpdataException e) { |
| 61 | + Assert.assertEquals(401, e.getStatus()); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + @SneakyThrows |
| 67 | + public void testGetFieldsReturnsSelectedFields() { |
| 68 | + IpdataService service = TEST_CONTEXT.ipdataService(); |
| 69 | + IpdataModel model = service.getFields("8.8.8.8", ASN, CURRENCY); |
| 70 | + Assert.assertNotNull(model.asn()); |
| 71 | + Assert.assertNotNull(model.currency()); |
| 72 | + } |
| 73 | +} |
0 commit comments