Fix checkstyle violations in applications
[openflowplugin.git] / applications / topology-lldp-discovery / src / test / java / org / opendaylight / openflowplugin / applications / topology / lldp / LLDPLinkAgerTest.java
1 /**
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.applications.topology.lldp;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.verify;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Matchers;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.NonZeroUint32Type;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfigBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Test for {@link LLDPLinkAger}.
34  */
35 @RunWith(MockitoJUnitRunner.class)
36 public class LLDPLinkAgerTest {
37
38     private static final Logger LOG = LoggerFactory.getLogger(LLDPLinkAgerTest.class);
39
40     private static final long LLDP_INTERVAL = 5L;
41     private static final long LINK_EXPIRATION_TIME = 10L;
42
43     /**
44      * We need to wait while other tasks are finished before we can check anything in LLDPAgingTask.
45      */
46     private static final int SLEEP = 100;
47
48     private LLDPLinkAger lldpLinkAger;
49
50     @Mock
51     private LinkDiscovered link;
52     @Mock
53     private NotificationProviderService notificationService;
54
55     @Before
56     public void setUp() throws Exception {
57         lldpLinkAger = new LLDPLinkAger(getConfig(), notificationService, getConfigurationService());
58     }
59
60     @Test
61     public void testPut() {
62         assertTrue(lldpLinkAger.isLinkToDateEmpty());
63         lldpLinkAger.put(link);
64         assertFalse(lldpLinkAger.isLinkToDateEmpty());
65     }
66
67     @Test
68     public void testClose() throws Exception {
69         lldpLinkAger.close();
70         assertTrue(lldpLinkAger.isLinkToDateEmpty());
71     }
72
73     /**
74      * Inner class LLDPAgingTask removes all expired records from linkToDate if any (in constructor of LLDPLinkAger).
75      */
76     @Test
77     public void testLLDPAgingTask() throws InterruptedException {
78         lldpLinkAger.put(link);
79         Thread.sleep(SLEEP);
80         verify(notificationService).publish(Matchers.any(LinkRemoved.class));
81     }
82
83     private TopologyLldpDiscoveryConfig getConfig() {
84         TopologyLldpDiscoveryConfigBuilder cfgBuilder = new TopologyLldpDiscoveryConfigBuilder();
85         cfgBuilder.setTopologyLldpInterval(new NonZeroUint32Type(LLDP_INTERVAL));
86         cfgBuilder.setTopologyLldpExpirationInterval(new NonZeroUint32Type(LINK_EXPIRATION_TIME));
87         return cfgBuilder.build();
88     }
89
90     private ConfigurationService getConfigurationService() {
91         final ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
92         final TopologyLldpDiscoveryConfig config = getConfig();
93
94         Mockito.when(configurationService.registerListener(Mockito.any())).thenReturn(() -> {
95         });
96
97         Mockito.when(configurationService.getProperty(Mockito.eq("topology-lldp-interval"), Mockito.any()))
98                 .thenReturn(config.getTopologyLldpInterval());
99
100         Mockito.when(configurationService.getProperty(Mockito.eq("topology-lldp-expiration-interval"), Mockito.any()))
101                 .thenReturn(config.getTopologyLldpExpirationInterval());
102
103         return configurationService;
104     }
105 }