|
1 | 1 | package com.datastax.driver.core.policies; |
2 | 2 |
|
| 3 | +import org.testng.annotations.Test; |
| 4 | + |
| 5 | +import javax.naming.NamingException; |
| 6 | +import javax.naming.directory.BasicAttributes; |
| 7 | +import javax.naming.directory.InitialDirContext; |
3 | 8 | import java.net.InetAddress; |
4 | 9 | import java.net.InetSocketAddress; |
5 | 10 |
|
6 | | -import org.testng.annotations.Test; |
7 | | - |
8 | 11 | import static org.assertj.core.api.Assertions.assertThat; |
| 12 | +import static org.mockito.Mockito.*; |
9 | 13 |
|
10 | 14 | public class EC2MultiRegionAddressTranslaterTest { |
| 15 | + |
| 16 | + @Test(groups = "unit") |
| 17 | + public void should_return_same_address_when_no_entry_found() throws Exception { |
| 18 | + InitialDirContext mock = mock(InitialDirContext.class); |
| 19 | + when(mock.getAttributes(anyString(), any(String[].class))) |
| 20 | + .thenReturn(new BasicAttributes()); |
| 21 | + EC2MultiRegionAddressTranslater translater = new EC2MultiRegionAddressTranslater(mock); |
| 22 | + |
| 23 | + InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); |
| 24 | + assertThat(translater.translate(address)).isEqualTo(address); |
| 25 | + } |
| 26 | + |
| 27 | + @Test(groups = "unit") |
| 28 | + public void should_return_same_address_when_exception_encountered() throws Exception { |
| 29 | + InitialDirContext mock = mock(InitialDirContext.class); |
| 30 | + when(mock.getAttributes(anyString(), any(String[].class))) |
| 31 | + .thenThrow(new NamingException("Problem resolving address (not really).")); |
| 32 | + EC2MultiRegionAddressTranslater translater = new EC2MultiRegionAddressTranslater(mock); |
| 33 | + |
| 34 | + InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); |
| 35 | + assertThat(translater.translate(address)).isEqualTo(address); |
| 36 | + } |
| 37 | + |
| 38 | + @Test(groups = "unit") |
| 39 | + public void should_return_new_address_when_match_found() throws Exception { |
| 40 | + InetSocketAddress expectedAddress = new InetSocketAddress("54.32.55.66", 9042); |
| 41 | + |
| 42 | + InitialDirContext mock = mock(InitialDirContext.class); |
| 43 | + when(mock.getAttributes("5.2.0.192.in-addr.arpa", new String[] { "PTR" })) |
| 44 | + .thenReturn(new BasicAttributes("PTR", expectedAddress.getHostName())); |
| 45 | + EC2MultiRegionAddressTranslater translater = new EC2MultiRegionAddressTranslater(mock); |
| 46 | + |
| 47 | + InetSocketAddress address = new InetSocketAddress("192.0.2.5", 9042); |
| 48 | + assertThat(translater.translate(address)).isEqualTo(expectedAddress); |
| 49 | + } |
| 50 | + |
| 51 | + @Test(groups = "unit") |
| 52 | + public void should_close_context_when_closed() throws Exception { |
| 53 | + InitialDirContext mock = mock(InitialDirContext.class); |
| 54 | + EC2MultiRegionAddressTranslater translater = new EC2MultiRegionAddressTranslater(mock); |
| 55 | + |
| 56 | + // ensure close has not been called to this point. |
| 57 | + verify(mock, times(0)).close(); |
| 58 | + translater.close(); |
| 59 | + // ensure close is closed. |
| 60 | + verify(mock).close(); |
| 61 | + } |
| 62 | + |
11 | 63 | @Test(groups = "unit") |
12 | 64 | public void should_build_reversed_domain_name_for_ip_v4() throws Exception { |
13 | 65 | InetAddress address = InetAddress.getByName("192.0.2.5"); |
|
0 commit comments