2 * Copyright (c) 2016 Cisco Systems, Inc. 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.openflowplugin.applications.frsync.impl.strategy;
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.junit.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;
45 * Test for {@link TableForwarder}.
47 @RunWith(MockitoJUnitRunner.class)
48 public class TableForwarderTest {
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")
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);
69 private ArgumentCaptor<UpdateTableInput> updateTableInputCpt;
71 private SalTableService salTableService;
73 private TransactionId txId;
75 private TableForwarder tableForwarder;
80 tableForwarder = new TableForwarder(salTableService);
81 txId = new TransactionId(BigInteger.ONE);
85 public void testUpdate() throws Exception {
86 Mockito.when(salTableService.updateTable(updateTableInputCpt.capture())).thenReturn(
87 RpcResultBuilder.success(
88 new UpdateTableOutputBuilder()
89 .setTransactionId(txId)
94 final TableFeatures tableFeaturesUpdate = new TableFeaturesBuilder(tableFeatures)
95 .setName("another-table")
97 final Future<RpcResult<UpdateTableOutput>> updateResult = tableForwarder.update(
98 tableFeaturesPath, tableFeatures, tableFeaturesUpdate, flowCapableNodePath);
100 Mockito.verify(salTableService).updateTable(ArgumentMatchers.any());
102 Assert.assertTrue(updateResult.isDone());
103 final RpcResult<UpdateTableOutput> updateTableResult = updateResult.get(2, TimeUnit.SECONDS);
104 Assert.assertTrue(updateTableResult.isSuccessful());
106 Assert.assertEquals(1, updateTableResult.getResult().getTransactionId().getValue().intValue());
108 final UpdateTableInput updateTableInput = updateTableInputCpt.getValue();
109 Assert.assertEquals(tablePath, updateTableInput.getTableRef().getValue());
110 Assert.assertEquals(nodePath, updateTableInput.getNode().getValue());
112 Assert.assertEquals(1, updateTableInput.getOriginalTable().getTableFeatures().size());
113 Assert.assertEquals("test-table", updateTableInput.getOriginalTable().nonnullTableFeatures().values()
114 .iterator().next().getName());
115 Assert.assertEquals(1, updateTableInput.getUpdatedTable().getTableFeatures().size());
116 Assert.assertEquals("another-table", updateTableInput.getUpdatedTable().nonnullTableFeatures().values()
117 .iterator().next().getName());