OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / strategy / TableForwarderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.openflowplugin.applications.frsync.impl.strategy;
10
11 import java.math.BigInteger;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.ArgumentMatchers;
20 import org.mockito.Captor;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
43
44 /**
45  * Test for {@link TableForwarder}.
46  */
47 @RunWith(MockitoJUnitRunner.class)
48 public class TableForwarderTest {
49
50     private final NodeKey s1Key = new NodeKey(new NodeId("S1"));
51     private final Short tableId = (short) 42;
52     private final TableKey tableKey = new TableKey(tableId);
53     private final TableFeaturesKey tableFeaturesKey = new TableFeaturesKey(tableId);
54     private final TableFeatures tableFeatures = new TableFeaturesBuilder()
55             .setName("test-table")
56             .setTableId(tableId)
57             .build();
58
59     private final KeyedInstanceIdentifier<Node, NodeKey> nodePath = InstanceIdentifier.create(Nodes.class)
60             .child(Node.class, s1Key);
61     private final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = nodePath
62             .augmentation(FlowCapableNode.class);
63     private final KeyedInstanceIdentifier<Table, TableKey> tablePath = flowCapableNodePath
64             .child(Table.class, tableKey);
65     private final InstanceIdentifier<TableFeatures> tableFeaturesPath = flowCapableNodePath
66             .child(TableFeatures.class, tableFeaturesKey);
67
68     @Captor
69     private ArgumentCaptor<UpdateTableInput> updateTableInputCpt;
70     @Mock
71     private SalTableService salTableService;
72
73     private TransactionId txId;
74
75     private TableForwarder tableForwarder;
76
77
78     @Before
79     public void setUp() throws Exception {
80         tableForwarder = new TableForwarder(salTableService);
81         txId = new TransactionId(BigInteger.ONE);
82     }
83
84     @Test
85     public void testUpdate() throws Exception {
86         Mockito.when(salTableService.updateTable(updateTableInputCpt.capture())).thenReturn(
87                 RpcResultBuilder.success(
88                         new UpdateTableOutputBuilder()
89                                 .setTransactionId(txId)
90                                 .build()
91                 ).buildFuture()
92         );
93
94         final TableFeatures tableFeaturesUpdate = new TableFeaturesBuilder(tableFeatures)
95                 .setName("another-table")
96                 .build();
97         final Future<RpcResult<UpdateTableOutput>> updateResult = tableForwarder.update(
98                 tableFeaturesPath, tableFeatures, tableFeaturesUpdate, flowCapableNodePath);
99
100         Mockito.verify(salTableService).updateTable(ArgumentMatchers.<UpdateTableInput>any());
101
102         Assert.assertTrue(updateResult.isDone());
103         final RpcResult<UpdateTableOutput> updateTableResult = updateResult.get(2, TimeUnit.SECONDS);
104         Assert.assertTrue(updateTableResult.isSuccessful());
105
106         Assert.assertEquals(1, updateTableResult.getResult().getTransactionId().getValue().intValue());
107
108         final UpdateTableInput updateTableInput = updateTableInputCpt.getValue();
109         Assert.assertEquals(tablePath, updateTableInput.getTableRef().getValue());
110         Assert.assertEquals(nodePath, updateTableInput.getNode().getValue());
111
112         Assert.assertEquals(1, updateTableInput.getOriginalTable().getTableFeatures().size());
113         Assert.assertEquals("test-table", updateTableInput.getOriginalTable().getTableFeatures().get(0).getName());
114         Assert.assertEquals(1, updateTableInput.getUpdatedTable().getTableFeatures().size());
115         Assert.assertEquals("another-table", updateTableInput.getUpdatedTable().getTableFeatures().get(0).getName());
116     }
117 }