NIFI-3485: This closes #1514. Updated test to mock out use of InetAdress when looking up domain. There was an issue where unresolvable domains got routed to an ISP page. This should fix that issue.

This commit is contained in:
JJ 2017-02-15 18:05:28 -06:00 committed by joewitt
parent 8b90343715
commit 598563277e

View File

@ -30,9 +30,13 @@ import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners; import org.apache.nifi.util.TestRunners;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -41,11 +45,14 @@ import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PrepareForTest({GeoEnrichIP.class})
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public class TestGeoEnrichIP { public class TestGeoEnrichIP {
DatabaseReader databaseReader; DatabaseReader databaseReader;
@ -54,6 +61,7 @@ public class TestGeoEnrichIP {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mockStatic(InetAddress.class);
databaseReader = mock(DatabaseReader.class); databaseReader = mock(DatabaseReader.class);
geoEnrichIP = new TestableGeoEnrichIP(); geoEnrichIP = new TestableGeoEnrichIP();
testRunner = TestRunners.newTestRunner(geoEnrichIP); testRunner = TestRunners.newTestRunner(geoEnrichIP);
@ -260,6 +268,8 @@ public class TestGeoEnrichIP {
final Map<String, String> attributes = new HashMap<>(); final Map<String, String> attributes = new HashMap<>();
attributes.put("ip", "somenonexistentdomain.comm"); attributes.put("ip", "somenonexistentdomain.comm");
when(InetAddress.getByName("somenonexistentdomain.comm")).thenThrow(UnknownHostException.class);
testRunner.enqueue(new byte[0], attributes); testRunner.enqueue(new byte[0], attributes);
testRunner.run(); testRunner.run();