Upgrade powermock to 2.0.0
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbPortRemoveCommandTest.java
1 /*
2  * Copyright (c) 2015 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.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.ovsdb.lib.message.TableUpdates;
31 import org.opendaylight.ovsdb.lib.notation.Column;
32 import org.opendaylight.ovsdb.lib.notation.UUID;
33 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
34 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
35 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
36 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
37 import org.opendaylight.ovsdb.schema.openvswitch.Port;
38 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
39 import org.opendaylight.ovsdb.southbound.OvsdbConnectionInstance;
40 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.powermock.api.mockito.PowerMockito;
44 import org.powermock.api.support.membermodification.MemberModifier;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.powermock.reflect.Whitebox;
48
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({TyperUtils.class, SouthboundMapper.class, InstanceIdentifier.class})
51 public class OvsdbPortRemoveCommandTest {
52
53     private static final String PORT_NAME = "port0";
54     private OvsdbPortRemoveCommand ovsdbPortRemoveCommand;
55
56     @Before
57     public void setUp() throws Exception {
58         ovsdbPortRemoveCommand = PowerMockito.mock(OvsdbPortRemoveCommand.class, Mockito.CALLS_REAL_METHODS);
59     }
60
61     @Test
62     public void testOvsdbPortRemoveCommandTest() {
63         OvsdbConnectionInstance key = mock(OvsdbConnectionInstance.class);
64         TableUpdates updates = mock(TableUpdates.class);
65         DatabaseSchema dbSchema = mock(DatabaseSchema.class);
66         OvsdbPortRemoveCommand ovsdbPortRemoveCommand1 =
67                 new OvsdbPortRemoveCommand(mock(InstanceIdentifierCodec.class), key, updates, dbSchema);
68         assertEquals(key, Whitebox.getInternalState(ovsdbPortRemoveCommand1, "key"));
69         assertEquals(updates, Whitebox.getInternalState(ovsdbPortRemoveCommand1, "updates"));
70         assertEquals(dbSchema, Whitebox.getInternalState(ovsdbPortRemoveCommand1, "dbSchema"));
71     }
72
73     @Test
74     @SuppressWarnings("unchecked")
75     public void testExecute() throws Exception {
76         when(ovsdbPortRemoveCommand.getUpdates()).thenReturn(mock(TableUpdates.class));
77         when(ovsdbPortRemoveCommand.getDbSchema()).thenReturn(mock(DatabaseSchema.class));
78         UUID uuid = mock(UUID.class);
79         Map<UUID, Port> portRemovedRows = new HashMap<>();
80         Port port = mock(Port.class);
81         portRemovedRows.put(uuid, port);
82
83         PowerMockito.mockStatic(TyperUtils.class);
84         PowerMockito.doReturn(portRemovedRows).when(TyperUtils.class);
85         TyperUtils.extractRowsRemoved(eq(Port.class), any(TableUpdates.class), any(DatabaseSchema.class));
86
87         Map<UUID, Bridge> bridgeUpdatedRows = new HashMap<>();
88         Bridge updatedBridgeData = mock(Bridge.class);
89         bridgeUpdatedRows.put(uuid, updatedBridgeData);
90         PowerMockito.doReturn(bridgeUpdatedRows).when(TyperUtils.class);
91         TyperUtils.extractRowsUpdated(eq(Bridge.class), any(TableUpdates.class), any(DatabaseSchema.class));
92
93         Map<UUID, Bridge> bridgeUpdatedOldRows = new HashMap<>();
94         Bridge oldBridgeData = mock(Bridge.class);
95         bridgeUpdatedOldRows.put(uuid, oldBridgeData);
96         PowerMockito.doReturn(bridgeUpdatedOldRows).when(TyperUtils.class);
97         TyperUtils.extractRowsOld(eq(Bridge.class), any(TableUpdates.class), any(DatabaseSchema.class));
98
99         Column<GenericTableSchema, Set<UUID>> column = mock(Column.class);
100         when(oldBridgeData.getPortsColumn()).thenReturn(column);
101         Set<UUID> uuids = new HashSet<>();
102         uuids.add(uuid);
103         when(column.getData()).thenReturn(uuids);
104
105         Column<GenericTableSchema, UUID> uuidColumn = mock(Column.class);
106         when(port.getUuidColumn()).thenReturn(uuidColumn);
107         when(uuidColumn.getData()).thenReturn(uuid);
108
109         when(port.getName()).thenReturn(PORT_NAME);
110         InstanceIdentifier<Node> nodeIID = mock(InstanceIdentifier.class);
111         doReturn(mock(OvsdbConnectionInstance.class)).when(ovsdbPortRemoveCommand).getOvsdbConnectionInstance();
112
113         PowerMockito.mockStatic(SouthboundMapper.class);
114         PowerMockito.doReturn(nodeIID).when(SouthboundMapper.class);
115         SouthboundMapper.createInstanceIdentifier(eq(null),
116             any(OvsdbConnectionInstance.class), any(Bridge.class));
117
118         MemberModifier.suppress(MemberModifier.methodsDeclaredIn(InstanceIdentifier.class));
119         ReadWriteTransaction transaction = mock(ReadWriteTransaction.class);
120         doNothing().when(transaction).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
121
122         ovsdbPortRemoveCommand.execute(transaction);
123         verify(transaction).delete(any(LogicalDatastoreType.class), eq(null));
124     }
125 }