UT for ovsdb.southbound transact package
[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.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 import java.lang.reflect.Field;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mockito;
29 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
30 import org.opendaylight.ovsdb.lib.notation.Column;
31 import org.opendaylight.ovsdb.lib.notation.Condition;
32 import org.opendaylight.ovsdb.lib.operations.Operation;
33 import org.opendaylight.ovsdb.lib.operations.Operations;
34 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
35 import org.opendaylight.ovsdb.lib.operations.Update;
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.Interface;
42 import org.opendaylight.ovsdb.schema.openvswitch.Port;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbPortInterfaceAttributes.VlanMode;
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 import org.powermock.reflect.Whitebox;
53
54 @RunWith(PowerMockRunner.class)
55 @PrepareForTest({TerminationPointUpdateCommand.class, TransactUtils.class, TyperUtils.class, VlanMode.class, TerminationPointCreateCommand.class, InstanceIdentifier.class})
56 public class TerminationPointUpdateCommandTest {
57
58     private static final String TERMINATION_POINT_NAME = "termination point name";
59     private TerminationPointUpdateCommand terminationPointUpdateCommand;
60
61     @Before
62     public void setUp() {
63         terminationPointUpdateCommand = mock(TerminationPointUpdateCommand.class, Mockito.CALLS_REAL_METHODS);
64     }
65
66     @SuppressWarnings("unchecked")
67     @Test
68     public void testTerminationPointUpdateCommand() {
69         BridgeOperationalState state = mock(BridgeOperationalState.class);
70         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes = mock(AsyncDataChangeEvent.class);
71         TerminationPointUpdateCommand terminationPointUpdateCommand1 = new TerminationPointUpdateCommand(state, changes);
72         assertEquals(state, Whitebox.getInternalState(terminationPointUpdateCommand1, "operationalState"));
73         assertEquals(changes, Whitebox.getInternalState(terminationPointUpdateCommand1, "changes"));
74     }
75
76     @SuppressWarnings("unchecked")
77     @Test
78     public void testExecute() {
79         Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> created = new HashMap<>();
80         created.put(mock(InstanceIdentifier.class), mock(OvsdbTerminationPointAugmentation.class));
81         PowerMockito.mockStatic(TransactUtils.class);
82         MemberModifier.suppress(MemberMatcher.method(TerminationPointUpdateCommand.class, "getChanges"));
83         when(terminationPointUpdateCommand.getChanges()).thenReturn(mock(AsyncDataChangeEvent.class));
84         PowerMockito.when(TransactUtils.extractCreated(any(AsyncDataChangeEvent.class), eq(OvsdbTerminationPointAugmentation.class))).thenReturn(created);
85         MemberModifier.suppress(MemberMatcher.method(TerminationPointUpdateCommand.class, "updateTerminationPoint",
86                 TransactionBuilder.class, InstanceIdentifier.class, OvsdbTerminationPointAugmentation.class));
87         doNothing().when(terminationPointUpdateCommand)
88                 .updateTerminationPoint(any(TransactionBuilder.class), any(InstanceIdentifier.class), any(OvsdbTerminationPointAugmentation.class));
89
90         Map<InstanceIdentifier<OvsdbTerminationPointAugmentation>, OvsdbTerminationPointAugmentation> updated = new HashMap<>();
91         updated.put(mock(InstanceIdentifier.class), mock(OvsdbTerminationPointAugmentation.class));
92         PowerMockito.when(TransactUtils.extractUpdated(any(AsyncDataChangeEvent.class), eq(OvsdbTerminationPointAugmentation.class))).thenReturn(updated);
93
94         TransactionBuilder transactionBuilder = mock(TransactionBuilder.class);
95         terminationPointUpdateCommand.execute(transactionBuilder);
96         verify(terminationPointUpdateCommand, times(2)).
97                 updateTerminationPoint(any(TransactionBuilder.class), any(InstanceIdentifier.class), any(OvsdbTerminationPointAugmentation.class));
98     }
99
100     @SuppressWarnings({ "unchecked", "rawtypes" })
101     @Test
102     public void testUpdateTerminationPoint() throws Exception {
103         TransactionBuilder transaction = mock(TransactionBuilder.class);
104         InstanceIdentifier<OvsdbTerminationPointAugmentation> iid = mock(InstanceIdentifier.class);
105         OvsdbTerminationPointAugmentation terminationPoint = mock(OvsdbTerminationPointAugmentation.class);
106         when(terminationPoint.getName()).thenReturn(TERMINATION_POINT_NAME);
107
108         // Test updateInterface()
109         Interface ovsInterface = mock(Interface.class);
110         when(transaction.getDatabaseSchema()).thenReturn(mock(DatabaseSchema.class));
111         PowerMockito.mockStatic(TyperUtils.class);
112         when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(ovsInterface);
113
114         Interface extraInterface = mock(Interface.class);
115         when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Interface.class))).thenReturn(extraInterface);
116         doNothing().when(extraInterface).setName(anyString());
117
118         Operations op = (Operations) setField("op");
119         Update update = mock(Update.class);
120         when(op.update(any(Interface.class))).thenReturn(update);
121
122         Column<GenericTableSchema, String> column = mock(Column.class);
123         when(extraInterface.getNameColumn()).thenReturn(column);
124         ColumnSchema<GenericTableSchema, String> columnSchema = mock(ColumnSchema.class);
125         when(column.getSchema()).thenReturn(columnSchema);
126         when(columnSchema.opEqual(anyString())).thenReturn(mock(Condition.class));
127         Where where = mock(Where.class);
128         when(update.where(any(Condition.class))).thenReturn(where);
129         when(where.build()).thenReturn(mock(Operation.class));
130         when(transaction.add(any(Operation.class))).thenReturn(transaction);
131         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(TerminationPointCreateCommand.class));
132         PowerMockito.suppress(MemberMatcher.methodsDeclaredIn(InstanceIdentifier.class));
133
134         // Test updatePort()
135         Port port = mock(Port.class);
136         when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(port);
137         Port extraPort = mock(Port.class);
138         when(TyperUtils.getTypedRowWrapper(any(DatabaseSchema.class), eq(Port.class))).thenReturn(extraPort);
139         doNothing().when(extraPort).setName(anyString());
140         when(op.update(any(Port.class))).thenReturn(update);
141         when(extraPort.getNameColumn()).thenReturn(column);
142
143         terminationPointUpdateCommand.updateTerminationPoint(transaction, iid, terminationPoint);
144         verify(transaction, times(2)).add(any(Operation.class));
145     }
146
147     private Object setField(String fieldName) throws Exception {
148         Field field = Operations.class.getDeclaredField(fieldName);
149         field.setAccessible(true);
150         field.set(field.get(Operations.class), mock(Operations.class));
151         return field.get(Operations.class);
152     }
153 }