Upgrade ietf-{inet,yang}-types to 2013-07-15
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbAutoAttachRemovedCommandTest.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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.ovsdb.southbound.transactions.md;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.base.Optional;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.ovsdb.lib.notation.UUID;
30 import org.opendaylight.ovsdb.schema.openvswitch.AutoAttach;
31 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
32 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
33 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
34 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Autoattach;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.AutoattachKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.api.support.membermodification.MemberModifier;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51
52 @PrepareForTest({OvsdbAutoAttachRemovedCommand.class, SouthboundMapper.class, SouthboundUtil.class})
53 @RunWith(PowerMockRunner.class)
54 public class OvsdbAutoAttachRemovedCommandTest {
55
56     private Map<UUID, AutoAttach> removedAutoAttachRows = new HashMap<>();
57     private OvsdbAutoAttachRemovedCommand ovsdbAutoAttachRemovedCommand;
58     private ReadWriteTransaction transaction;
59     private InstanceIdentifier<Autoattach> aaIid;
60
61     private static final UUID AUTOATTACH_UUID = new UUID("798f35d8-f40a-449a-94d3-c860f5547f9a");
62     private static final String CONNECTED_NODE_ID = "10.0.0.1";
63
64     @SuppressWarnings("unchecked")
65     @Before
66     public void setUp() throws Exception {
67         ovsdbAutoAttachRemovedCommand = mock(OvsdbAutoAttachRemovedCommand.class, Mockito.CALLS_REAL_METHODS);
68
69         AutoAttach autoAttach = mock(AutoAttach.class);
70         removedAutoAttachRows.put(AUTOATTACH_UUID, autoAttach);
71         MemberModifier.field(OvsdbAutoAttachRemovedCommand.class, "removedAutoAttachRows")
72                 .set(ovsdbAutoAttachRemovedCommand, removedAutoAttachRows);
73
74         OvsdbConnectionInstance ovsdbConnectionInstance = mock(OvsdbConnectionInstance.class);
75         when(ovsdbAutoAttachRemovedCommand.getOvsdbConnectionInstance()).thenReturn(ovsdbConnectionInstance);
76         AutoattachKey aaKey = new AutoattachKey(
77                 new Uri(SouthboundConstants.AUTOATTACH_URI_PREFIX + "://" + AUTOATTACH_UUID.toString()));
78         aaIid = InstanceIdentifier.create(NetworkTopology.class)
79                 .child(Topology.class, new TopologyKey(SouthboundConstants.OVSDB_TOPOLOGY_ID))
80                 .child(Node.class, new NodeKey(new NodeId(CONNECTED_NODE_ID)))
81                 .augmentation(OvsdbNodeAugmentation.class)
82                 .child(Autoattach.class, aaKey);
83         InstanceIdentifier<Node> nodeIid = aaIid.firstIdentifierOf(Node.class);
84         when(ovsdbConnectionInstance.getInstanceIdentifier()).thenReturn(nodeIid);
85
86         PowerMockito.mockStatic(SouthboundUtil.class);
87         Optional<Node> ovsdbNode = mock(Optional.class);
88         transaction = mock(ReadWriteTransaction.class);
89         PowerMockito.when(SouthboundUtil.readNode(transaction, nodeIid)).thenReturn(ovsdbNode);
90         when(ovsdbNode.isPresent()).thenReturn(true);
91         Node node = mock(Node.class);
92         when(ovsdbNode.get()).thenReturn(node);
93         NodeId nodeId = mock(NodeId.class);
94         when(node.getNodeId()).thenReturn(nodeId);
95
96         PowerMockito.mockStatic(SouthboundMapper.class);
97         when(ovsdbConnectionInstance.getNodeId()).thenReturn(nodeId);
98         when(SouthboundMapper.createInstanceIdentifier(nodeId)).thenReturn(nodeIid);
99
100         OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class);
101         when(node.getAugmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation);
102         List<Autoattach> autoAttachList = new ArrayList<>();
103         Autoattach aaEntry = mock(Autoattach.class);
104         autoAttachList.add(aaEntry);
105         when(aaEntry.getAutoattachUuid()).thenReturn(new Uuid(AUTOATTACH_UUID.toString()));
106         when(ovsdbNodeAugmentation.getAutoattach()).thenReturn(autoAttachList);
107         when(aaEntry.getKey()).thenReturn(aaKey);
108
109         doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
110     }
111
112     @Test
113     public void testExecute() {
114         ovsdbAutoAttachRemovedCommand.execute(transaction);
115         verify(transaction).delete(eq(LogicalDatastoreType.OPERATIONAL), eq(aaIid));
116     }
117 }