Bug 5386: Delete QoS and Queues entries from ovsdb on UNI deletion
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / command / EvcRemoveCommandTest.java
1 /*
2  * Copyright (c) 2016 CableLabs 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.unimgr.command;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.when;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.unimgr.impl.UnimgrMapper;
24 import org.opendaylight.unimgr.utils.EvcUtils;
25 import org.opendaylight.unimgr.utils.MdsalUtils;
26 import org.opendaylight.unimgr.utils.OvsdbUtils;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.EvcAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniDest;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.evc.UniSource;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.powermock.api.mockito.PowerMockito;
34 import org.powermock.core.classloader.annotations.PrepareForTest;
35 import org.powermock.modules.junit4.PowerMockRunner;
36
37 import com.google.common.base.Optional;
38
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest({EvcUtils.class, MdsalUtils.class, OvsdbUtils.class, UnimgrMapper.class})
41 public class EvcRemoveCommandTest {
42
43     private EvcRemoveCommand evcRemoveCommand;
44     private DataTreeModification<Link> evcLink;
45     private Link link;
46     private DataBroker dataBroker;
47
48     @SuppressWarnings("unchecked")
49     @Before
50     public void setUp(){
51         PowerMockito.mockStatic(MdsalUtils.class);
52         PowerMockito.mockStatic(EvcUtils.class);
53         PowerMockito.mockStatic(OvsdbUtils.class);
54         PowerMockito.mockStatic(UnimgrMapper.class);
55         dataBroker = mock(DataBroker.class);
56         link = mock(Link.class);
57         evcLink = DataTreeModificationHelper.getEvcLink(link);
58         evcRemoveCommand = new EvcRemoveCommand(dataBroker, evcLink);
59     }
60     /**
61      * Test method for {@link org.opendaylight.unimgr.command.EvcRemoveCommand#execute().
62      * @throws Exception
63      */
64     @SuppressWarnings({ "unchecked", "rawtypes" })
65     @Test
66     public void testEvcRemoveCommand() throws Exception {
67         final List<UniSource> unisSource = new ArrayList<UniSource>();
68         final UniSource uniSource = mock(UniSource.class);
69         final List<UniDest> unisDest = new ArrayList<UniDest>();
70         final UniDest uniDest = mock(UniDest.class);
71         final EvcAugmentation evcAugmentation = mock(EvcAugmentation.class);
72         final Optional<Node> optionalNode = mock(Optional.class);
73         final InstanceIdentifier instanceOfNode = mock(InstanceIdentifier.class);
74         unisSource.add(uniSource);
75         unisDest.add(uniDest);
76
77         when(link.getAugmentation(EvcAugmentation.class)).thenReturn(evcAugmentation);
78         when(evcAugmentation.getUniSource()).thenReturn(unisSource);
79         when(evcAugmentation.getUniDest()).thenReturn(unisDest);
80         when(uniSource.getUni()).thenReturn(instanceOfNode);
81         when(uniDest.getUni()).thenReturn(instanceOfNode);
82         when(MdsalUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class),
83                 any(InstanceIdentifier.class))).thenReturn(evcAugmentation);
84         when(MdsalUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class),
85                 any(InstanceIdentifier.class))).thenReturn(optionalNode);
86         PowerMockito.doNothing().when(EvcUtils.class, "deleteEvcData",
87                 any(DataBroker.class), any(Optional.class));
88         when(MdsalUtils.deleteNode(any(DataBroker.class), any(InstanceIdentifier.class),
89                 any(LogicalDatastoreType.class))).thenReturn(true);
90
91         evcRemoveCommand.execute();
92         PowerMockito.verifyStatic(times(2));
93         EvcUtils.deleteEvcData(any(DataBroker.class), any(Optional.class));
94         PowerMockito.verifyStatic(times(1));
95         MdsalUtils.deleteNode(any(DataBroker.class), any(InstanceIdentifier.class),
96                 any(LogicalDatastoreType.class));
97     }
98
99 }