Bug-5543 - Bo: Update JUnit tests part 2 52/40652/4
authormiroslav.macko <miroslav.macko@pantheon.tech>
Wed, 22 Jun 2016 06:55:25 +0000 (08:55 +0200)
committermiroslav.macko <miroslav.macko@pantheon.tech>
Mon, 27 Jun 2016 10:42:26 +0000 (12:42 +0200)
- Added topology-lldp-discovery tests

Change-Id: I4d98f6d71e9984a083874f3cde7fedb97e41404f
Signed-off-by: miroslav.macko <miroslav.macko@pantheon.tech>
applications/topology-lldp-discovery/pom.xml
applications/topology-lldp-discovery/src/main/java/org/opendaylight/openflowplugin/applications/topology/lldp/LLDPLinkAger.java
applications/topology-lldp-discovery/src/test/java/org/opendaylight/openflowplugin/applications/topology/lldp/LLDPLinkAgerTest.java [new file with mode: 0644]

index af6ec356198079a2a264ce6093e1288f8cb7a513..594e5db0eea723fca56cacd5aecea85310bcaa00 100644 (file)
       <groupId>org.opendaylight.controller.model</groupId>
       <artifactId>model-inventory</artifactId>
     </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <scope>test</scope>
+    </dependency>
+
 
   </dependencies>
 
index 9acd4b2de34251d1a1c4972ffba82cde103bf24d..b238bbbeee73c5e065dad0c37c792e06a013e5d2 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.openflowplugin.applications.topology.lldp;
 
+import com.google.common.annotations.VisibleForTesting;
 import java.util.Date;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -68,5 +69,11 @@ public class LLDPLinkAger implements AutoCloseable {
         }
 
     }
+
+    @VisibleForTesting
+    public boolean isLinkToDateEmpty() {
+        return linkToDate.isEmpty();
+    }
+
 }
 
diff --git a/applications/topology-lldp-discovery/src/test/java/org/opendaylight/openflowplugin/applications/topology/lldp/LLDPLinkAgerTest.java b/applications/topology-lldp-discovery/src/test/java/org/opendaylight/openflowplugin/applications/topology/lldp/LLDPLinkAgerTest.java
new file mode 100644 (file)
index 0000000..056fde6
--- /dev/null
@@ -0,0 +1,86 @@
+/**
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.applications.topology.lldp;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.verify;
+
+import java.util.Date;
+import java.util.Map;
+import java.util.Timer;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Test for {@link LLDPLinkAger}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class LLDPLinkAgerTest {
+
+    private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAgerTest.class);
+
+    private LLDPLinkAger lldpLinkAger;
+    private final long LLDP_INTERVAL = 5L;
+    private final long LINK_EXPIRATION_TIME = 10L;
+    /**
+     * We need to w8 while other tasks are finished before we can check anything
+     * in LLDPAgingTask
+     */
+    private final int SLEEP = 100;
+
+
+    @Mock
+    private LinkDiscovered link;
+    @Mock
+    private Map<LinkDiscovered, Date> linkToDate;
+    @Mock
+    private Timer timer;
+    @Mock
+    private NotificationProviderService notificationService;
+    @Mock
+    private LinkRemoved linkRemoved;
+
+    @Before
+    public void setUp() throws Exception {
+        lldpLinkAger = new LLDPLinkAger(LLDP_INTERVAL, LINK_EXPIRATION_TIME, notificationService);
+    }
+
+    @Test
+    public void testPut() {
+        assertTrue(lldpLinkAger.isLinkToDateEmpty());
+        lldpLinkAger.put(link);
+        assertFalse(lldpLinkAger.isLinkToDateEmpty());
+    }
+
+    @Test
+    public void testClose() {
+        lldpLinkAger.close();
+        assertTrue(lldpLinkAger.isLinkToDateEmpty());
+    }
+
+    /**
+     * Inner class LLDPAgingTask removes all expired records from linkToDate if any (in constructor of LLDPLinkAger)
+     */
+    @Test
+    public void testLLDPAgingTask() throws InterruptedException {
+        lldpLinkAger.put(link);
+        Thread.sleep(SLEEP);
+        verify(notificationService).publish(Matchers.any(LinkRemoved.class));
+    }
+}
\ No newline at end of file