Merge "Drop Felix Gogo" into stable/boron
[openflowplugin.git] / applications / table-miss-enforcer / src / test / java / org / opendaylight / openflowplugin / applications / tableMissEnforcer / LLDPDataChangeListenerTest.java
index 4a05f25e3d9b812296c3b7aa9084021e08c27b9f..08df369ef45a87282ba19683893a94b2af8f5e0d 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/**
  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -10,38 +10,82 @@ package org.opendaylight.openflowplugin.applications.tableMissEnforcer;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+
+import java.util.Collections;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
 import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+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.DataObjectModification.ModificationType;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
+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.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
+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;
 
 /**
- * Created by Martin Bobak mbobak@cisco.com on 8/29/14.
+ * Test for {@link LLDPPacketPuntEnforcer}.
  */
+@RunWith(MockitoJUnitRunner.class)
 public class LLDPDataChangeListenerTest {
-
+    private LLDPPacketPuntEnforcer lldpPacketPuntEnforcer;
+    private final static InstanceIdentifier<Node> nodeIID = InstanceIdentifier.create(Nodes.class)
+            .child(Node.class, new NodeKey(new NodeId("testnode:1")));
     @Mock
-    private static SalFlowService flowService;
+    private SalFlowService flowService;
+    @Mock
+    private DataTreeModification<FlowCapableNode> dataTreeModification;
+    @Captor
+    private ArgumentCaptor<AddFlowInput> addFlowInputCaptor;
+
+    @Before
+    public void setUp() {
+        lldpPacketPuntEnforcer = new LLDPPacketPuntEnforcer(flowService, Mockito.mock(DataBroker.class));
+        final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, nodeIID);
+        Mockito.when(dataTreeModification.getRootPath()).thenReturn(identifier);
+        Mockito.when(dataTreeModification.getRootNode()).thenReturn(Mockito.mock(DataObjectModification.class));
+        Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.WRITE);
+    }
 
-    /**
-     * Test method for {@link org.opendaylight.openflowplugin.applications.tableMissEnforcer.LLDPPacketPuntEnforcer#createFlow()}
-     * which ensures that LLDPDataChangeListener creates proper flow for
-     */
     @Test
-    public void testCreateFlow() {
-        LLDPPacketPuntEnforcer lldpDataChangeListener = new LLDPPacketPuntEnforcer(flowService);
-        evaluateFlow(lldpDataChangeListener.createFlow());
+    public void testOnDataTreeChanged() {
+        lldpPacketPuntEnforcer.onDataTreeChanged(Collections.singleton(dataTreeModification));
+        Mockito.verify(flowService).addFlow(addFlowInputCaptor.capture());
+        AddFlowInput captured = addFlowInputCaptor.getValue();
+        Assert.assertEquals(nodeIID, captured.getNode().getValue());
     }
 
-    private static void evaluateFlow(final Flow flow) {
+    @Test
+    public void testCreateFlow() {
+        final Flow flow = lldpPacketPuntEnforcer.createFlow();
         evaluateInstructions(flow.getInstructions());
     }
 
+    @After
+    public void tearDown() {
+        lldpPacketPuntEnforcer.close();
+    }
+
     private static void evaluateInstructions(final Instructions instructions) {
         assertNotNull(instructions.getInstruction());
         assertEquals(1, instructions.getInstruction().size());