2 * Copyright (c) 2015 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
20 import java.lang.reflect.Field;
21 import java.util.HashMap;
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.common.api.data.AsyncDataChangeEvent;
29 import org.opendaylight.ovsdb.lib.notation.Column;
30 import org.opendaylight.ovsdb.lib.notation.Condition;
31 import org.opendaylight.ovsdb.lib.operations.Operation;
32 import org.opendaylight.ovsdb.lib.operations.Operations;
33 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
34 import org.opendaylight.ovsdb.lib.operations.Update;
35 import org.opendaylight.ovsdb.lib.operations.Where;
36 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
37 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
38 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
39 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
40 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
41 import org.opendaylight.ovsdb.schema.openvswitch.Port;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.api.support.membermodification.MemberMatcher;
49 import org.powermock.api.support.membermodification.MemberModifier;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
53 import com.google.common.base.Optional;
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({TerminationPointUpdateCommand.class, TransactUtils.class, TyperUtils.class, VlanMode.class, TerminationPointCreateCommand.class, InstanceIdentifier.class})
57 public class TerminationPointUpdateCommandTest {
59 private static final String TERMINATION_POINT_NAME = "termination point name";
60 private TerminationPointUpdateCommand terminationPointUpdateCommand;
64 terminationPointUpdateCommand = mock(TerminationPointUpdateCommand.class, Mockito.CALLS_REAL_METHODS);
67 @SuppressWarnings("unchecked")
69 public void testExecute() {
70 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> created = new HashMap<>();
71 created.put(mock(InstanceIdentifier.class), mock(OvsdbTerminationPointAugmentation.class));
72 PowerMockito.mockStatic(TransactUtils.class);
73 PowerMockito.when(TransactUtils.extractCreated(any(AsyncDataChangeEvent.class), eq(OvsdbTerminationPointAugmentation.class))).thenReturn(created);
74 MemberModifier.suppress(MemberMatcher.method(TerminationPointUpdateCommand.class, "updateTerminationPoint",
75 TransactionBuilder.class, BridgeOperationalState.class,
76 InstanceIdentifier.class, OvsdbTerminationPointAugmentation.class));
77 doNothing().when(terminationPointUpdateCommand)
78 .updateTerminationPoint(any(TransactionBuilder.class), any(BridgeOperationalState.class), any(InstanceIdentifier.class), any(OvsdbTerminationPointAugmentation.class));
80 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> updated = new HashMap<>();
81 updated.put(mock(InstanceIdentifier.class), mock(OvsdbTerminationPointAugmentation.class));
82 PowerMockito.when(TransactUtils.extractUpdated(any(AsyncDataChangeEvent.class), eq(OvsdbTerminationPointAugmentation.class))).thenReturn(updated);
84 TransactionBuilder transactionBuilder = mock(TransactionBuilder.class);
85 terminationPointUpdateCommand.execute(transactionBuilder, mock(BridgeOperationalState.class), mock(AsyncDataChangeEvent.class));
86 // TODO Verify something useful
89 @SuppressWarnings({ "unchecked", "rawtypes" })
91 public void testUpdateTerminationPoint() throws Exception {
92 TransactionBuilder transaction = mock(TransactionBuilder.class);
93 BridgeOperationalState state = mock(BridgeOperationalState.class);
94 InstanceIdentifier<OvsdbTerminationPointAugmentation> iid = mock(InstanceIdentifier.class);
95 OvsdbTerminationPointAugmentation terminationPoint = mock(OvsdbTerminationPointAugmentation.class);
96 when(terminationPoint.getName()).thenReturn(TERMINATION_POINT_NAME);
97 Optional<Node> optNode = (Optional<Node>)mock(Optional.class);
98 when(state.getBridgeNode(any(InstanceIdentifier.class))).thenReturn(optNode);
99 when(state.getBridgeNode(any(InstanceIdentifier.class)).get()).thenReturn(mock(Node.class));
100 when(state.getBridgeNode(any(InstanceIdentifier.class)).get().getAugmentation(OvsdbBridgeAugmentation.class)).thenReturn(mock(OvsdbBridgeAugmentation.class));
102 // Test updateInterface()
103 Interface ovsInterface = mock(Interface.class);
104 when(transaction.getDatabaseSchema()).thenReturn(mock(DatabaseSchema.class));
105 PowerMockito.mockStatic(TyperUtils.class);
106 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(ovsInterface);
108 Interface extraInterface = mock(Interface.class);
109 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(extraInterface);
110 doNothing().when(extraInterface).setName(anyString());
112 Operations op = (Operations) setField("op");
113 Update update = mock(Update.class);
114 when(op.update(any(Interface.class))).thenReturn(update);
116 Column<GenericTableSchema, String> column = mock(Column.class);
117 when(extraInterface.getNameColumn()).thenReturn(column);
118 ColumnSchema<GenericTableSchema, String> columnSchema = mock(ColumnSchema.class);
119 when(column.getSchema()).thenReturn(columnSchema);
120 when(columnSchema.opEqual(anyString())).thenReturn(mock(Condition.class));
121 Where where = mock(Where.class);
122 when(update.where(any(Condition.class))).thenReturn(where);
123 when(where.build()).thenReturn(mock(Operation.class));
124 when(transaction.add(any(Operation.class))).thenReturn(transaction);
125 PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(TerminationPointCreateCommand.class));
126 PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(InstanceIdentifier.class));
129 Port port = mock(Port.class);
130 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(port);
131 Port extraPort = mock(Port.class);
132 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(extraPort);
133 doNothing().when(extraPort).setName(anyString());
134 when(op.update(any(Port.class))).thenReturn(update);
135 when(extraPort.getNameColumn()).thenReturn(column);
137 terminationPointUpdateCommand.updateTerminationPoint(transaction, state, iid, terminationPoint);
138 verify(transaction, times(2)).add(any(Operation.class));
141 private Object setField(String fieldName) throws Exception {
142 Field field = Operations.class.getDeclaredField(fieldName);
143 field.setAccessible(true);
144 field.set(field.get(Operations.class), mock(Operations.class));
145 return field.get(Operations.class);