Refactor StrictBGPPeerRegistryTest 90/24790/2
authorDana Kutenicsova <dkutenic@cisco.com>
Wed, 5 Aug 2015 08:29:46 +0000 (10:29 +0200)
committerDana Kutenicsova <dkutenic@cisco.com>
Wed, 5 Aug 2015 08:45:15 +0000 (10:45 +0200)
Renamed some tests and moved some setup variables
to global (static/final).

Change-Id: I0bcd130cf149ab6286fab0f5fb1d4da28cac9f7f
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
bgp/rib-impl/src/test/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistryTest.java

index 32db6165bc79212339140910b55dfdbc674a9b14..478edb6da2c2b883a66089154c4a8ec82312069e 100644 (file)
@@ -36,9 +36,16 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mess
 
 public class StrictBGPPeerRegistryTest {
 
+    private static final AsNumber LOCAL_AS = new AsNumber(1234L);
+    private static final AsNumber REMOTE_AS = new AsNumber(1235L);
+    private static final Ipv4Address FROM = new Ipv4Address("0.0.0.1");
+    private static final IpAddress REMOTE_IP = new IpAddress(FROM);
+    private static final Ipv4Address TO = new Ipv4Address("255.255.255.255");
+
+    private final ReusableBGPPeer peer1 = getMockSession();
+    private final Open classicOpen = createOpen(TO, LOCAL_AS);
     private StrictBGPPeerRegistry peerRegistry;
     private BGPSessionPreferences mockPreferences;
-    private static final AsNumber AS1 = new AsNumber(1234L);
 
     private Open createOpen(final Ipv4Address bgpId, final AsNumber as) {
         final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder()
@@ -51,7 +58,13 @@ public class StrictBGPPeerRegistryTest {
     @Before
     public void setUp() throws Exception {
         this.peerRegistry = new StrictBGPPeerRegistry();
-        this.mockPreferences = getMockPreferences(AS1);
+        this.mockPreferences =  new BGPSessionPreferences(LOCAL_AS, 1, new Ipv4Address("0.0.0.1"), LOCAL_AS, Collections.<BgpParameters> emptyList());
+    }
+
+    private static ReusableBGPPeer getMockSession() {
+        final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
+        Mockito.doNothing().when(mock).releaseConnection();
+        return mock;
     }
 
     @Test
@@ -62,33 +75,22 @@ public class StrictBGPPeerRegistryTest {
     }
 
     @Test
-    public void testDuplicate() throws Exception {
-        final Ipv4Address from = new Ipv4Address("0.0.0.1");
-        final IpAddress remoteIp = new IpAddress(from);
-        final Ipv4Address to = new Ipv4Address("255.255.255.255");
-
-        final ReusableBGPPeer session1 = getMockSession();
-        this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
-
-        this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
+    public void testDuplicatePeerConnection() throws Exception {
+        this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
+        this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
         try {
-            this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
+            this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
         }
-
         fail("Same peer cannot be connected twice");
     }
 
     @Test
-    public void testNotAllowed() throws Exception {
-        final Ipv4Address from = new Ipv4Address("0.0.0.1");
-        final IpAddress remoteIp = new IpAddress(from);
-        final Ipv4Address to = new Ipv4Address("255.255.255.255");
-
+    public void testPeerNotConfigured() throws Exception {
         try {
-            this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
+            this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
         } catch (final IllegalStateException e) {
             return;
         }
@@ -96,145 +98,104 @@ public class StrictBGPPeerRegistryTest {
     }
 
     @Test
-    public void testOk() throws Exception {
-        final Ipv4Address from = new Ipv4Address("0.0.0.1");
-
-        final Ipv4Address to = new Ipv4Address("255.255.255.255");
-        final IpAddress remoteIp = new IpAddress(to);
+    public void testPeerConnectionSuccessfull() throws Exception {
         final Ipv4Address to2 = new Ipv4Address("255.255.255.254");
         final IpAddress remoteIp2 = new IpAddress(to2);
 
-        final ReusableBGPPeer session1 = getMockSession();
-        this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+        this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
         final ReusableBGPPeer session2 = getMockSession();
         this.peerRegistry.addPeer(remoteIp2, session2, this.mockPreferences);
 
-        final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
-        assertSame(session1, returnedSession1);
-        final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, from, to2, createOpen(to, AS1));
+        final BGPSessionListener returnedSession1 = this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
+        assertSame(this.peer1, returnedSession1);
+        final BGPSessionListener returnedSession2 = this.peerRegistry.getPeer(remoteIp2, FROM, to2, this.classicOpen);
         assertSame(session2, returnedSession2);
 
-        Mockito.verifyZeroInteractions(session1);
+        Mockito.verifyZeroInteractions(this.peer1);
         Mockito.verifyZeroInteractions(session2);
     }
 
     @Test
-    public void testDropSecond() throws Exception {
+    public void testDropSecondPeer() throws Exception {
         final Ipv4Address higher = new Ipv4Address("192.168.200.200");
         final Ipv4Address lower = new Ipv4Address("10.10.10.10");
         final IpAddress remoteIp = new IpAddress(lower);
 
-        final ReusableBGPPeer session1 = getMockSession();
-        this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+        this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
 
-        this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, AS1));
+        this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
         try {
-            this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, AS1));
+            this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
         }
-
         fail("Same peer cannot be connected twice");
     }
 
     @Test
-    public void testDropFirst() throws Exception {
+    public void testDropFirstPeer() throws Exception {
         final Ipv4Address higher = new Ipv4Address("123.123.123.123");
         final Ipv4Address lower = new Ipv4Address("123.123.123.122");
         final IpAddress remoteIp = new IpAddress(lower);
 
-        final ReusableBGPPeer session1 = getMockSession();
-        this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+        this.peerRegistry.addPeer(remoteIp, this.peer1, this.mockPreferences);
 
-        this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, AS1));
-        this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, AS1));
-        Mockito.verify(session1).releaseConnection();
+        this.peerRegistry.getPeer(remoteIp, lower, higher, createOpen(higher, LOCAL_AS));
+        this.peerRegistry.getPeer(remoteIp, higher, lower, createOpen(lower, LOCAL_AS));
+        Mockito.verify(this.peer1).releaseConnection();
     }
 
     @Test
-    public void testDuplicateDifferentIds() throws Exception {
-        final Ipv4Address from = new Ipv4Address("0.0.0.1");
-        final IpAddress remoteIp = new IpAddress(from);
-        final Ipv4Address to = new Ipv4Address("255.255.255.255");
-
-        final ReusableBGPPeer session1 = getMockSession();
-        this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+    public void testDuplicatePeersWDifferentIds() throws Exception {
+        this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
 
-        this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
+        this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
         try {
-            this.peerRegistry.getPeer(remoteIp, to, to, createOpen(to, AS1));
+            this.peerRegistry.getPeer(REMOTE_IP, TO, TO, this.classicOpen);
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
         }
-
         fail("Same peer cannot be connected twice");
     }
 
     @Test
-    public void testDuplicateHigerAs() throws Exception {
-        final Ipv4Address from = new Ipv4Address("0.0.0.1");
-        final IpAddress remoteIp = new IpAddress(from);
-        final Ipv4Address to = new Ipv4Address("255.255.255.255");
-        final AsNumber as2 = new AsNumber(1235L);
-
-        final ReusableBGPPeer session1 = getMockSession();
-        this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
-
-        this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
-        this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
-        Mockito.verify(session1).releaseConnection();
+    public void testDuplicatePeersHigherAs() throws Exception {
+        this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
+
+        this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
+        this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, REMOTE_AS));
+        Mockito.verify(this.peer1).releaseConnection();
     }
 
     @Test
-    public void testDuplicateLowerAs() throws Exception {
-        final Ipv4Address from = new Ipv4Address("0.0.0.1");
-        final IpAddress remoteIp = new IpAddress(from);
-        final Ipv4Address to = new Ipv4Address("255.255.255.255");
+    public void testDuplicatePeersLowerAs() throws Exception {
         final AsNumber as2 = new AsNumber(3L);
 
-        final ReusableBGPPeer session1 = getMockSession();
-        this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
+        this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
 
-        this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, AS1));
+        this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, this.classicOpen);
         try {
-            this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
+            this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.CEASE, e.getError());
             return;
         }
-
         fail("Same peer cannot be connected twice");
     }
 
     @Test
     public void testAsMismatch() throws Exception {
-        final Ipv4Address from = new Ipv4Address("0.0.0.1");
-        final IpAddress remoteIp = new IpAddress(from);
-        final Ipv4Address to = new Ipv4Address("255.255.255.255");
         final AsNumber as2 = new AsNumber(3L);
 
-        final ReusableBGPPeer session1 = getMockSession();
-        this.peerRegistry.addPeer(remoteIp, session1, this.mockPreferences);
-
+        this.peerRegistry.addPeer(REMOTE_IP, this.peer1, this.mockPreferences);
         try {
-            this.peerRegistry.getPeer(remoteIp, from, to, createOpen(to, as2));
+            this.peerRegistry.getPeer(REMOTE_IP, FROM, TO, createOpen(TO, as2));
         } catch (final BGPDocumentedException e) {
             assertEquals(BGPError.BAD_PEER_AS, e.getError());
             return;
         }
-
         fail("Peer AS number mismatch");
     }
-
-    private static ReusableBGPPeer getMockSession() {
-        final ReusableBGPPeer mock = Mockito.mock(ReusableBGPPeer.class);
-        Mockito.doNothing().when(mock).releaseConnection();
-        return mock;
-    }
-
-    public BGPSessionPreferences getMockPreferences(final AsNumber remoteAs) {
-        return new BGPSessionPreferences(AS1, 1, new Ipv4Address("0.0.0.1"), remoteAs, Collections.<BgpParameters> emptyList());
-    }
 }