OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / applications / of-switch-config-pusher / src / test / java / org / opendaylight / openflowplugin / openflow / ofswitch / config / DefaultConfigPusherTest.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.openflow.ofswitch.config;
10
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.when;
14
15 import java.util.Collections;
16 import org.junit.After;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.Captor;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
28 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
29 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
30 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.openflowplugin.api.OFConstants;
33 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.NodeConfigService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.module.config.rev141015.SetConfigInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44
45 /**
46  * Test for {@link DefaultConfigPusher}.
47  */
48 @RunWith(MockitoJUnitRunner.class)
49 public class DefaultConfigPusherTest {
50     private DefaultConfigPusher defaultConfigPusher;
51     private static final InstanceIdentifier<Node> NODE_IID = InstanceIdentifier.create(Nodes.class)
52             .child(Node.class, new NodeKey(new NodeId("testnode:1")));
53     @Mock
54     private NodeConfigService nodeConfigService;
55     @Mock
56     private DataTreeModification<FlowCapableNode> dataTreeModification;
57     @Mock
58     private DeviceOwnershipService deviceOwnershipService;
59     @Captor
60     private ArgumentCaptor<SetConfigInput> setConfigInputCaptor;
61
62     @Before
63     public void setUp() throws Exception {
64         doReturn(RpcResultBuilder.success().buildFuture()).when(nodeConfigService).setConfig(any());
65         defaultConfigPusher = new DefaultConfigPusher(nodeConfigService, Mockito.mock(DataBroker.class),
66                 deviceOwnershipService);
67         final DataTreeIdentifier<FlowCapableNode> identifier =
68                 new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, NODE_IID);
69         Mockito.when(dataTreeModification.getRootPath()).thenReturn(identifier);
70         Mockito.when(dataTreeModification.getRootNode()).thenReturn(Mockito.mock(DataObjectModification.class));
71         Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.WRITE);
72         when(deviceOwnershipService.isEntityOwned(any())).thenReturn(true);
73     }
74
75     @Test
76     public void testOnDataTreeChanged() {
77         defaultConfigPusher.onDataTreeChanged(Collections.singleton(dataTreeModification));
78         Mockito.verify(nodeConfigService).setConfig(setConfigInputCaptor.capture());
79         final SetConfigInput captured = setConfigInputCaptor.getValue();
80         Assert.assertEquals(SwitchConfigFlag.FRAGNORMAL.toString(), captured.getFlag());
81         Assert.assertEquals(OFConstants.OFPCML_NO_BUFFER, captured.getMissSearchLength());
82         Assert.assertEquals(NODE_IID, captured.getNode().getValue());
83     }
84
85     @After
86     public void tearDown() {
87         defaultConfigPusher.close();
88     }
89
90 }