d6379dda926fd22ae2da4914877e3101f32d9219
[ovsdb.git] / southbound / southbound-impl / src / test / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TerminationPointUpdateCommandTest.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.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;
19
20 import java.lang.reflect.Field;
21 import java.util.HashMap;
22 import java.util.Map;
23
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.OvsdbTerminationPointAugmentation;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.powermock.api.mockito.PowerMockito;
46 import org.powermock.api.support.membermodification.MemberMatcher;
47 import org.powermock.api.support.membermodification.MemberModifier;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({TerminationPointUpdateCommand.class, TransactUtils.class, TyperUtils.class, VlanMode.class, TerminationPointCreateCommand.class, InstanceIdentifier.class})
53 public class TerminationPointUpdateCommandTest {
54
55     private static final String TERMINATION_POINT_NAME = "termination point name";
56     private TerminationPointUpdateCommand terminationPointUpdateCommand;
57
58     @Before
59     public void setUp() {
60         terminationPointUpdateCommand = mock(TerminationPointUpdateCommand.class, Mockito.CALLS_REAL_METHODS);
61     }
62
63     @SuppressWarnings("unchecked")
64     @Test
65     public void testExecute() {
66         Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> created = new HashMap<>();
67         created.put(mock(InstanceIdentifier.class), mock(OvsdbTerminationPointAugmentation.class));
68         PowerMockito.mockStatic(TransactUtils.class);
69         PowerMockito.when(TransactUtils.extractCreated(any(AsyncDataChangeEvent.class), eq(OvsdbTerminationPointAugmentation.class))).thenReturn(created);
70         MemberModifier.suppress(MemberMatcher.method(TerminationPointUpdateCommand.class, "updateTerminationPoint",
71                 TransactionBuilder.class, InstanceIdentifier.class, OvsdbTerminationPointAugmentation.class));
72         doNothing().when(terminationPointUpdateCommand)
73                 .updateTerminationPoint(any(TransactionBuilder.class), any(InstanceIdentifier.class), any(OvsdbTerminationPointAugmentation.class));
74
75         Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> updated = new HashMap<>();
76         updated.put(mock(InstanceIdentifier.class), mock(OvsdbTerminationPointAugmentation.class));
77         PowerMockito.when(TransactUtils.extractUpdated(any(AsyncDataChangeEvent.class), eq(OvsdbTerminationPointAugmentation.class))).thenReturn(updated);
78
79         TransactionBuilder transactionBuilder = mock(TransactionBuilder.class);
80         terminationPointUpdateCommand.execute(transactionBuilder, mock(BridgeOperationalState.class), mock(AsyncDataChangeEvent.class));
81         // TODO Verify something useful
82     }
83
84     @SuppressWarnings({ "unchecked", "rawtypes" })
85     @Test
86     public void testUpdateTerminationPoint() throws Exception {
87         TransactionBuilder transaction = mock(TransactionBuilder.class);
88         InstanceIdentifier<OvsdbTerminationPointAugmentation> iid = mock(InstanceIdentifier.class);
89         OvsdbTerminationPointAugmentation terminationPoint = mock(OvsdbTerminationPointAugmentation.class);
90         when(terminationPoint.getName()).thenReturn(TERMINATION_POINT_NAME);
91
92         // Test updateInterface()
93         Interface ovsInterface = mock(Interface.class);
94         when(transaction.getDatabaseSchema()).thenReturn(mock(DatabaseSchema.class));
95         PowerMockito.mockStatic(TyperUtils.class);
96         when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(ovsInterface);
97
98         Interface extraInterface = mock(Interface.class);
99         when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(extraInterface);
100         doNothing().when(extraInterface).setName(anyString());
101
102         Operations op = (Operations) setField("op");
103         Update update = mock(Update.class);
104         when(op.update(any(Interface.class))).thenReturn(update);
105
106         Column<GenericTableSchema, String> column = mock(Column.class);
107         when(extraInterface.getNameColumn()).thenReturn(column);
108         ColumnSchema<GenericTableSchema, String> columnSchema = mock(ColumnSchema.class);
109         when(column.getSchema()).thenReturn(columnSchema);
110         when(columnSchema.opEqual(anyString())).thenReturn(mock(Condition.class));
111         Where where = mock(Where.class);
112         when(update.where(any(Condition.class))).thenReturn(where);
113         when(where.build()).thenReturn(mock(Operation.class));
114         when(transaction.add(any(Operation.class))).thenReturn(transaction);
115         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(TerminationPointCreateCommand.class));
116         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(InstanceIdentifier.class));
117
118         // Test updatePort()
119         Port port = mock(Port.class);
120         when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(port);
121         Port extraPort = mock(Port.class);
122         when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(extraPort);
123         doNothing().when(extraPort).setName(anyString());
124         when(op.update(any(Port.class))).thenReturn(update);
125         when(extraPort.getNameColumn()).thenReturn(column);
126
127         terminationPointUpdateCommand.updateTerminationPoint(transaction, iid, terminationPoint);
128         verify(transaction, times(2)).add(any(Operation.class));
129     }
130
131     private Object setField(String fieldName) throws Exception {
132         Field field = Operations.class.getDeclaredField(fieldName);
133         field.setAccessible(true);
134         field.set(field.get(Operations.class), mock(Operations.class));
135         return field.get(Operations.class);
136     }
137 }