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 com.google.common.base.Optional;
21 import java.util.HashMap;
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.ovsdb.lib.notation.Column;
28 import org.opendaylight.ovsdb.lib.notation.Condition;
29 import org.opendaylight.ovsdb.lib.operations.Operation;
30 import org.opendaylight.ovsdb.lib.operations.Operations;
31 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
32 import org.opendaylight.ovsdb.lib.operations.Update;
33 import org.opendaylight.ovsdb.lib.operations.Where;
34 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
35 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
36 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
37 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
38 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
39 import org.opendaylight.ovsdb.schema.openvswitch.Port;
40 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
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.OvsdbTerminationPointAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.api.support.membermodification.MemberMatcher;
48 import org.powermock.api.support.membermodification.MemberModifier;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
52 @RunWith(PowerMockRunner.class)
53 @PrepareForTest({ TerminationPointUpdateCommand.class, TransactUtils.class, TyperUtils.class, VlanMode.class,
54 TerminationPointCreateCommand.class, InstanceIdentifier.class, Operations.class })
55 public class TerminationPointUpdateCommandTest {
57 private static final String TERMINATION_POINT_NAME = "termination point name";
58 private TerminationPointUpdateCommand terminationPointUpdateCommand;
62 terminationPointUpdateCommand = mock(TerminationPointUpdateCommand.class, Mockito.CALLS_REAL_METHODS);
65 @SuppressWarnings("unchecked")
67 public void testExecute() {
68 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> created
70 created.put(mock(InstanceIdentifier.class), mock(OvsdbTerminationPointAugmentation.class));
71 PowerMockito.mockStatic(TransactUtils.class);
72 PowerMockito.when(TransactUtils.extractCreated(any(DataChangeEvent.class),
73 eq(OvsdbTerminationPointAugmentation.class))).thenReturn(created);
74 MemberModifier.suppress(MemberMatcher.method(TerminationPointUpdateCommand.class, "updateTerminationPoint",
75 TransactionBuilder.class, BridgeOperationalState.class,
76 InstanceIdentifier.class, OvsdbTerminationPointAugmentation.class, InstanceIdentifierCodec.class));
77 doNothing().when(terminationPointUpdateCommand).updateTerminationPoint(any(TransactionBuilder.class),
78 any(BridgeOperationalState.class), any(InstanceIdentifier.class),
79 any(OvsdbTerminationPointAugmentation.class), any(InstanceIdentifierCodec.class));
81 Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> updated
83 updated.put(mock(InstanceIdentifier.class), mock(OvsdbTerminationPointAugmentation.class));
84 PowerMockito.when(TransactUtils.extractUpdated(any(DataChangeEvent.class),
85 eq(OvsdbTerminationPointAugmentation.class))).thenReturn(updated);
87 TransactionBuilder transactionBuilder = mock(TransactionBuilder.class);
88 terminationPointUpdateCommand.execute(transactionBuilder, mock(BridgeOperationalState.class),
89 mock(DataChangeEvent.class), mock(InstanceIdentifierCodec.class));
90 // TODO Verify something useful
93 @SuppressWarnings({ "unchecked", "rawtypes" })
95 public void testUpdateTerminationPoint() throws Exception {
96 TransactionBuilder transaction = mock(TransactionBuilder.class);
97 BridgeOperationalState state = mock(BridgeOperationalState.class);
98 OvsdbTerminationPointAugmentation terminationPoint = mock(OvsdbTerminationPointAugmentation.class);
99 when(terminationPoint.getName()).thenReturn(TERMINATION_POINT_NAME);
100 Node node = mock(Node.class);
101 when(node.augmentation(OvsdbBridgeAugmentation.class)).thenReturn(mock(OvsdbBridgeAugmentation.class));
102 Optional<Node> optNode = Optional.of(node);
103 when(state.getBridgeNode(any(InstanceIdentifier.class))).thenReturn(optNode);
105 // Test updateInterface()
106 Interface ovsInterface = mock(Interface.class);
107 when(transaction.getDatabaseSchema()).thenReturn(mock(DatabaseSchema.class));
108 PowerMockito.mockStatic(TyperUtils.class);
109 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(ovsInterface);
111 Interface extraInterface = mock(Interface.class);
112 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(extraInterface);
113 doNothing().when(extraInterface).setName(anyString());
115 Operations op = OvsdbNodeUpdateCommandTest.setOpField();
116 Update update = mock(Update.class);
117 when(op.update(any(Interface.class))).thenReturn(update);
119 Column<GenericTableSchema, String> column = mock(Column.class);
120 when(extraInterface.getNameColumn()).thenReturn(column);
121 ColumnSchema<GenericTableSchema, String> columnSchema = mock(ColumnSchema.class);
122 when(column.getSchema()).thenReturn(columnSchema);
123 when(columnSchema.opEqual(anyString())).thenReturn(mock(Condition.class));
124 Where where = mock(Where.class);
125 when(update.where(any(Condition.class))).thenReturn(where);
126 when(where.build()).thenReturn(mock(Operation.class));
127 when(transaction.add(any(Operation.class))).thenReturn(transaction);
128 PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(TerminationPointCreateCommand.class));
129 PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(InstanceIdentifier.class));
132 Port port = mock(Port.class);
133 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(port);
134 Port extraPort = mock(Port.class);
135 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(extraPort);
136 doNothing().when(extraPort).setName(anyString());
137 when(op.update(any(Port.class))).thenReturn(update);
138 when(extraPort.getNameColumn()).thenReturn(column);
140 InstanceIdentifier<OvsdbTerminationPointAugmentation> iid = mock(InstanceIdentifier.class);
141 terminationPointUpdateCommand.updateTerminationPoint(transaction, state, iid, terminationPoint,
142 mock(InstanceIdentifierCodec.class));
143 verify(transaction, times(1)).add(any(Operation.class));