/*
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.openflowplugin.applications.notification.supplier.impl.item.stat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestChangeEventBuildHelper;
import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestData;
import org.opendaylight.openflowplugin.applications.notification.supplier.impl.helper.TestSupplierVerifyHelper;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
public class FlowTableStatNotificationSupplierImplTest {
private static final String FLOW_NODE_ID = "openflow:111";
private static final Short FLOW_TABLE_ID = 111;
private static final String FLOW_ID = "test-flow-111";
private FlowTableStatNotificationSupplierImpl notifSupplierImpl;
private NotificationProviderService notifProviderService;
private DataBroker dataBroker;
@Before
public void initalization() {
notifProviderService = mock(NotificationProviderService.class);
dataBroker = mock(DataBroker.class);
notifSupplierImpl = new FlowTableStatNotificationSupplierImpl(notifProviderService, dataBroker);
TestSupplierVerifyHelper.verifyDataTreeChangeListenerRegistration(dataBroker);
}
@Test(expected = NullPointerException.class)
public void testNullChangeEvent() {
notifSupplierImpl.onDataTreeChanged(null);
}
@Test(expected = NullPointerException.class)
public void testNullableChangeEvent() {
notifSupplierImpl.onDataTreeChanged( TestChangeEventBuildHelper.createNullTestDataTreeEvent());
}
@Test
public void testEmptyChangeEvent() {
notifSupplierImpl.onDataTreeChanged( TestChangeEventBuildHelper.createEmptyTestDataTreeEvent());
}
@Test
public void testCreate() {
final FlowTableStatisticsUpdate notification = notifSupplierImpl.createNotification(createTestFlowTableStat(),
createTestFlowTableStatPath());
assertNotNull(notification);
assertEquals(FLOW_NODE_ID, notification.getId().getValue());
}
@Test
public void testCreateChangeEvent() {
final TestData testData = new TestData(createTestFlowTableStatPath(),null,createTestFlowTableStat(),
DataObjectModification.ModificationType.WRITE);
Collection<DataTreeModification<FlowTableStatistics>> collection = new ArrayList<>();
collection.add(testData);
notifSupplierImpl.onDataTreeChanged(collection);
verify(notifProviderService, times(1)).publish(Matchers.any(FlowTableStatisticsUpdate.class));
}
@Test(expected = IllegalArgumentException.class)
public void testCreateFromNullNodeConnector() {
notifSupplierImpl.createNotification(null, createTestFlowTableStatPath());
}
@Test(expected = IllegalArgumentException.class)
public void testCreateFromNullPath() {
notifSupplierImpl.createNotification(createTestFlowTableStat(), null);
}
@Test(expected = IllegalArgumentException.class)
public void testUpdateFromNullNodeConnector() {
notifSupplierImpl.createNotification(null, createTestFlowTableStatPath());
}
@Test(expected = IllegalArgumentException.class)
public void testUpdateFromNullPath() {
notifSupplierImpl.createNotification(createTestFlowTableStat(), null);
}
private static InstanceIdentifier<FlowTableStatistics> createTestFlowTableStatPath() {
return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(FLOW_TABLE_ID))
.augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
}
private static FlowTableStatistics createTestFlowTableStat() {
final FlowTableStatisticsBuilder builder = new FlowTableStatisticsBuilder();
return builder.build();
}
}