Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / test / mock / TableFeaturesListenerTest.java
1 /**
2  * Copyright (c) 2014 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 package test.mock;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.List;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
26 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import test.mock.util.FRMTest;
39 import test.mock.util.RpcProviderRegistryMock;
40 import test.mock.util.SalTableServiceMock;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class TableFeaturesListenerTest extends FRMTest {
44     private ForwardingRulesManagerImpl forwardingRulesManager;
45     private final static NodeId NODE_ID = new NodeId("testnode:1");
46     private final static NodeKey s1Key = new NodeKey(NODE_ID);
47     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
48     @Mock
49     ClusterSingletonServiceProvider clusterSingletonService;
50     @Mock
51     DeviceMastershipManager deviceMastershipManager;
52     @Mock
53     private NotificationProviderService notificationService;
54
55     @Before
56     public void setUp() {
57         forwardingRulesManager = new ForwardingRulesManagerImpl(
58                 getDataBroker(),
59                 rpcProviderRegistryMock,
60                 getConfig(),
61                 clusterSingletonService,
62                 notificationService);
63         forwardingRulesManager.start();
64         // TODO consider tests rewrite (added because of complicated access)
65         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
66         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
67     }
68
69     @Test
70     public void updateFlowTest() {
71         TableKey tableKey = new TableKey((short) 2);
72         TableFeaturesKey tableFeaturesKey = new TableFeaturesKey(tableKey.getId());
73
74         addTable(tableKey, s1Key);
75
76         TableFeatures tableFeaturesData = new TableFeaturesBuilder().setKey(tableFeaturesKey).build();
77         InstanceIdentifier<TableFeatures> tableFeaturesII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
78                 .augmentation(FlowCapableNode.class).child(TableFeatures.class, tableFeaturesKey);
79         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
80         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableFeaturesII, tableFeaturesData);
81         assertCommit(writeTx.submit());
82
83         tableFeaturesData = new TableFeaturesBuilder().setKey(tableFeaturesKey).setName("dummy name").build();
84         writeTx = getDataBroker().newWriteOnlyTransaction();
85         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableFeaturesII, tableFeaturesData);
86         assertCommit(writeTx.submit());
87
88         SalTableServiceMock salTableServiceMock = (SalTableServiceMock) forwardingRulesManager.getSalTableService();
89         List<UpdateTableInput> updateTableInputs = salTableServiceMock.getUpdateTableInput();
90         assertEquals(1, updateTableInputs.size());
91         assertEquals("DOM-0", updateTableInputs.get(0).getTransactionUri().getValue());
92     }
93
94     @After
95     public void tearDown() throws Exception {
96         forwardingRulesManager.close();
97     }
98
99 }