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.verify;
17 import static org.mockito.Mockito.when;
19 import java.lang.reflect.Field;
20 import java.util.HashMap;
21 import java.util.HashSet;
23 import org.junit.Before;
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mockito;
28 import org.opendaylight.ovsdb.lib.notation.Column;
29 import org.opendaylight.ovsdb.lib.notation.Condition;
30 import org.opendaylight.ovsdb.lib.notation.UUID;
31 import org.opendaylight.ovsdb.lib.operations.Insert;
32 import org.opendaylight.ovsdb.lib.operations.Mutate;
33 import org.opendaylight.ovsdb.lib.operations.Operation;
34 import org.opendaylight.ovsdb.lib.operations.Operations;
35 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
36 import org.opendaylight.ovsdb.lib.operations.Where;
37 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
38 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
39 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
40 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
41 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
42 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
43 import org.opendaylight.ovsdb.schema.openvswitch.Port;
44 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.api.support.membermodification.MemberMatcher;
50 import org.powermock.api.support.membermodification.MemberModifier;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
54 @PrepareForTest({TyperUtils.class, TransactUtils.class, TerminationPointCreateCommand.class})
55 @RunWith(PowerMockRunner.class)
56 public class TerminationPointCreateCommandTest {
58 private static final String INTERFACE_NAME = "eth0";
59 private static final String TERMINATION_POINT_NAME = "termination point name";
60 private TerminationPointCreateCommand terminationPointCreateCommand;
64 terminationPointCreateCommand = mock(TerminationPointCreateCommand.class, Mockito.CALLS_REAL_METHODS);
67 @SuppressWarnings({ "unchecked", "rawtypes"})
69 @Ignore("This needs to be rewritten")
70 public void testExecute() throws Exception {
71 MemberModifier.suppress(MemberMatcher.method(TerminationPointCreateCommand.class, "getChanges"));
72 DataChangeEvent asynEvent = mock(DataChangeEvent.class);
73 Map<InstanceIdentifier<?>, DataObject> map = new HashMap<>();
74 OvsdbTerminationPointAugmentation terminationPoint = mock(OvsdbTerminationPointAugmentation.class);
75 InstanceIdentifier terminationPointIid = mock(InstanceIdentifier.class);
76 map.put(terminationPointIid, terminationPoint);
77 when(asynEvent.getCreatedData()).thenReturn(map);
78 when(terminationPoint.getName()).thenReturn(TERMINATION_POINT_NAME);
80 Interface ovsInterface = mock(Interface.class);
81 PowerMockito.mockStatic(TyperUtils.class);
82 TransactionBuilder transaction = mock(TransactionBuilder.class);
83 when(transaction.getDatabaseSchema()).thenReturn(mock(DatabaseSchema.class));
84 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(ovsInterface);
86 Operations op = (Operations) setField("op");
87 Insert<GenericTableSchema> insert = mock(Insert.class);
88 when(op.insert(any(Interface.class))).thenReturn(insert);
89 when(insert.withId(anyString())).thenReturn(insert);
90 MemberModifier.suppress(MemberMatcher.method(TerminationPointCreateCommand.class,
91 "stampInstanceIdentifier", TransactionBuilder.class, InstanceIdentifier.class, String.class));
92 when(ovsInterface.getName()).thenReturn(INTERFACE_NAME);
94 Port port = mock(Port.class);
95 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(port);
96 when(op.insert(any(Port.class))).thenReturn(insert);
98 Bridge bridge = mock(Bridge.class);
99 when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Bridge.class))).thenReturn(bridge);
100 doNothing().when(bridge).setName(anyString());
101 PowerMockito.whenNew(UUID.class).withAnyArguments().thenReturn(mock(UUID.class));
102 doNothing().when(bridge).setPorts(any(HashSet.class));
104 BridgeOperationalState bridgeOpState = mock(BridgeOperationalState.class);
106 terminationPointCreateCommand.execute(transaction, bridgeOpState, asynEvent,
107 mock(InstanceIdentifierCodec.class));
109 // TODO Actually verify something
112 @SuppressWarnings("unchecked")
114 public void testStampInstanceIdentifier() {
115 TransactionBuilder transaction = mock(TransactionBuilder.class);
117 when(transaction.getDatabaseSchema()).thenReturn(mock(DatabaseSchema.class));
118 PowerMockito.mockStatic(TyperUtils.class);
119 Port port = mock(Port.class);
120 PowerMockito.when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(port);
121 doNothing().when(port).setName(anyString());
122 doNothing().when(port).setExternalIds(any(HashMap.class));
123 when(port.getSchema()).thenReturn(mock(GenericTableSchema.class));
124 Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
125 when(port.getExternalIdsColumn()).thenReturn(column);
126 when(column.getSchema()).thenReturn(mock(ColumnSchema.class));
128 Mutate mutate = mock(Mutate.class);
129 PowerMockito.mockStatic(TransactUtils.class);
130 when(TransactUtils.stampInstanceIdentifierMutation(any(TransactionBuilder.class), any(InstanceIdentifier.class),
131 any(GenericTableSchema.class), any(ColumnSchema.class), any(InstanceIdentifierCodec.class))).thenReturn(
134 Column<GenericTableSchema, String> nameColumn = mock(Column.class);
135 when(port.getNameColumn()).thenReturn(nameColumn);
136 ColumnSchema<GenericTableSchema, String> nameColumnSchema = mock(ColumnSchema.class);
137 when(nameColumn.getSchema()).thenReturn(nameColumnSchema);
138 when(nameColumnSchema.opEqual(anyString())).thenReturn(mock(Condition.class));
139 Where where = mock(Where.class);
140 when(mutate.where(any(Condition.class))).thenReturn(where);
141 when(where.build()).thenReturn(mock(Operation.class));
142 when(transaction.add(any(Operation.class))).thenReturn(transaction);
144 String interfaceName = INTERFACE_NAME;
145 InstanceIdentifier<OvsdbTerminationPointAugmentation> iid = mock(InstanceIdentifier.class);
146 TerminationPointCreateCommand.stampInstanceIdentifier(transaction, iid, interfaceName,
147 mock(InstanceIdentifierCodec.class));
148 verify(port).setName(anyString());
149 verify(port).getExternalIdsColumn();
150 verify(transaction).add(any(Operation.class));
153 private Object setField(String fieldName) throws Exception {
154 Field field = Operations.class.getDeclaredField(fieldName);
155 field.setAccessible(true);
156 field.set(field.get(Operations.class), mock(Operations.class));
157 return field.get(Operations.class);