Make methods static
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointCreateCommandTest.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.ovsdb.transact;
10
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyString;
13 import static org.mockito.ArgumentMatchers.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;
18
19 import java.lang.reflect.Field;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
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.GenericTableSchema;
39 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
40 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
41 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
42 import org.opendaylight.ovsdb.schema.openvswitch.Port;
43 import org.opendaylight.ovsdb.southbound.InstanceIdentifierCodec;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
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;
52
53 @PrepareForTest({TransactUtils.class, TerminationPointCreateCommand.class})
54 @RunWith(PowerMockRunner.class)
55 public class TerminationPointCreateCommandTest {
56
57     private static final String INTERFACE_NAME = "eth0";
58     private static final String TERMINATION_POINT_NAME = "termination point name";
59     private TerminationPointCreateCommand terminationPointCreateCommand;
60
61     @Before
62     public void setUp() {
63         terminationPointCreateCommand = mock(TerminationPointCreateCommand.class, Mockito.CALLS_REAL_METHODS);
64     }
65
66     @SuppressWarnings({ "unchecked", "rawtypes"})
67     @Test
68     @Ignore("This needs to be rewritten")
69     public void testExecute() throws Exception {
70         MemberModifier.suppress(MemberMatcher.method(TerminationPointCreateCommand.class, "getChanges"));
71         DataChangeEvent asynEvent = mock(DataChangeEvent.class);
72         Map<InstanceIdentifier<?>, DataObject> map = new HashMap<>();
73         OvsdbTerminationPointAugmentation terminationPoint = mock(OvsdbTerminationPointAugmentation.class);
74         InstanceIdentifier terminationPointIid = mock(InstanceIdentifier.class);
75         map.put(terminationPointIid, terminationPoint);
76         when(asynEvent.getCreatedData()).thenReturn(map);
77         when(terminationPoint.getName()).thenReturn(TERMINATION_POINT_NAME);
78
79         Interface ovsInterface = mock(Interface.class);
80         PowerMockito.mockStatic(TyperUtils.class);
81         TransactionBuilder transaction = mock(TransactionBuilder.class);
82         when(transaction.getTypedRowWrapper(eq(Interface.class))).thenReturn(ovsInterface);
83         //createInterface()
84         Operations op = (Operations) setField("op");
85         Insert<GenericTableSchema> insert = mock(Insert.class);
86         when(op.insert(any(Interface.class))).thenReturn(insert);
87         when(insert.withId(anyString())).thenReturn(insert);
88         MemberModifier.suppress(MemberMatcher.method(TerminationPointCreateCommand.class,
89                 "stampInstanceIdentifier", TransactionBuilder.class, InstanceIdentifier.class, String.class));
90         when(ovsInterface.getName()).thenReturn(INTERFACE_NAME);
91
92         Port port = mock(Port.class);
93         when(transaction.getTypedRowWrapper(eq(Port.class))).thenReturn(port);
94         when(op.insert(any(Port.class))).thenReturn(insert);
95
96         Bridge bridge = mock(Bridge.class);
97         when(transaction.getTypedRowWrapper(eq(Bridge.class))).thenReturn(bridge);
98         doNothing().when(bridge).setName(anyString());
99         PowerMockito.whenNew(UUID.class).withAnyArguments().thenReturn(mock(UUID.class));
100         doNothing().when(bridge).setPorts(any(HashSet.class));
101
102         BridgeOperationalState bridgeOpState = mock(BridgeOperationalState.class);
103
104         terminationPointCreateCommand.execute(transaction, bridgeOpState, asynEvent,
105                 mock(InstanceIdentifierCodec.class));
106
107         // TODO Actually verify something
108     }
109
110     @SuppressWarnings("unchecked")
111     @Test
112     public void testStampInstanceIdentifier() {
113         TransactionBuilder transaction = mock(TransactionBuilder.class);
114
115         Port port = mock(Port.class);
116         when(transaction.getTypedRowWrapper(eq(Port.class))).thenReturn(port);
117         doNothing().when(port).setName(anyString());
118         doNothing().when(port).setExternalIds(any(HashMap.class));
119         when(port.getSchema()).thenReturn(mock(GenericTableSchema.class));
120         Column<GenericTableSchema, Map<String, String>> column = mock(Column.class);
121         when(port.getExternalIdsColumn()).thenReturn(column);
122         when(column.getSchema()).thenReturn(mock(ColumnSchema.class));
123
124         Mutate mutate = mock(Mutate.class);
125         PowerMockito.mockStatic(TransactUtils.class);
126         when(TransactUtils.stampInstanceIdentifierMutation(any(TransactionBuilder.class), any(InstanceIdentifier.class),
127                 any(GenericTableSchema.class), any(ColumnSchema.class), any(InstanceIdentifierCodec.class))).thenReturn(
128                 mutate);
129
130         Column<GenericTableSchema, String> nameColumn = mock(Column.class);
131         when(port.getNameColumn()).thenReturn(nameColumn);
132         ColumnSchema<GenericTableSchema, String> nameColumnSchema = mock(ColumnSchema.class);
133         when(nameColumn.getSchema()).thenReturn(nameColumnSchema);
134         when(nameColumnSchema.opEqual(anyString())).thenReturn(mock(Condition.class));
135         Where where = mock(Where.class);
136         when(mutate.where(any(Condition.class))).thenReturn(where);
137         when(where.build()).thenReturn(mock(Operation.class));
138         when(transaction.add(any(Operation.class))).thenReturn(transaction);
139
140         String interfaceName = INTERFACE_NAME;
141         InstanceIdentifier<OvsdbTerminationPointAugmentation> iid = mock(InstanceIdentifier.class);
142         TerminationPointCreateCommand.stampInstanceIdentifier(transaction, iid, interfaceName,
143                 mock(InstanceIdentifierCodec.class));
144         verify(port).setName(anyString());
145         verify(port).getExternalIdsColumn();
146         verify(transaction).add(any(Operation.class));
147     }
148
149     private static Object setField(final String fieldName) throws Exception {
150         Field field = Operations.class.getDeclaredField(fieldName);
151         field.setAccessible(true);
152         field.set(field.get(Operations.class), mock(Operations.class));
153         return field.get(Operations.class);
154     }
155 }