Bump MRI upstreams
[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 package org.opendaylight.openflowplugin.applications.topology.lldp;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.verify;
14
15 import java.util.Optional;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
24 import org.opendaylight.mdsal.eos.binding.api.Entity;
25 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
26 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
27 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.NonZeroUint32Type;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfig;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.lldp.discovery.config.rev160511.TopologyLldpDiscoveryConfigBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40
41 /**
42  * Test for {@link LLDPLinkAger}.
43  */
44 @RunWith(MockitoJUnitRunner.class)
45 public class LLDPLinkAgerTest {
46     private static final Uint32 LLDP_INTERVAL = Uint32.valueOf(5);
47     private static final Uint32 LINK_EXPIRATION_TIME = Uint32.TEN;
48
49     /**
50      * We need to wait while other tasks are finished before we can check anything in LLDPAgingTask.
51      */
52     private static final int SLEEP = 100;
53
54     private LLDPLinkAger lldpLinkAger;
55
56     @Mock
57     private LinkDiscovered link;
58     @Mock
59     private NotificationPublishService notificationService;
60     @Mock
61     private EntityOwnershipService eos;
62     @Mock
63     private DataBroker dataBroker;
64     @Mock
65     private LinkRemoved linkRemoved;
66
67
68     @Before
69     public void setUp() {
70         lldpLinkAger = new LLDPLinkAger(getConfig(), notificationService, getConfigurationService(), eos, dataBroker);
71         Mockito.when(link.getDestination()).thenReturn(new NodeConnectorRef(
72                 InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")))));
73         Mockito.when(eos.getOwnershipState(any(Entity.class))).thenReturn(
74                 Optional.of(EntityOwnershipState.IS_OWNER));
75     }
76
77     @Test
78     public void testPut() {
79         assertTrue(lldpLinkAger.isLinkToDateEmpty());
80         lldpLinkAger.put(link);
81         assertFalse(lldpLinkAger.isLinkToDateEmpty());
82     }
83
84     @Test
85     public void testClose() throws Exception {
86         lldpLinkAger.close();
87         assertTrue(lldpLinkAger.isLinkToDateEmpty());
88     }
89
90     /**
91      * Inner class LLDPAgingTask removes all expired records from linkToDate if any (in constructor of LLDPLinkAger).
92      */
93     @Test
94     public void testLLDPAgingTask() throws InterruptedException {
95         lldpLinkAger.put(link);
96         Thread.sleep(SLEEP);
97         verify(notificationService).putNotification(any(LinkRemoved.class));
98     }
99
100     private static TopologyLldpDiscoveryConfig getConfig() {
101         TopologyLldpDiscoveryConfigBuilder cfgBuilder = new TopologyLldpDiscoveryConfigBuilder();
102         cfgBuilder.setTopologyLldpInterval(new NonZeroUint32Type(LLDP_INTERVAL));
103         cfgBuilder.setTopologyLldpExpirationInterval(new NonZeroUint32Type(LINK_EXPIRATION_TIME));
104         return cfgBuilder.build();
105     }
106
107     private static ConfigurationService getConfigurationService() {
108         final ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
109         final TopologyLldpDiscoveryConfig config = getConfig();
110
111         Mockito.when(configurationService.registerListener(any())).thenReturn(() -> {
112         });
113
114         Mockito.lenient().when(configurationService.getProperty(Mockito.eq("topology-lldp-interval"), any()))
115                 .thenReturn(config.getTopologyLldpInterval());
116
117         Mockito.lenient().when(configurationService.getProperty(Mockito.eq("topology-lldp-expiration-interval"), any()))
118                 .thenReturn(config.getTopologyLldpExpirationInterval());
119
120         return configurationService;
121     }
122 }