New tests for listeners
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / ActionDefinitionListenerTest.java
diff --git a/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ActionDefinitionListenerTest.java b/renderers/ofoverlay/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/ActionDefinitionListenerTest.java
new file mode 100755 (executable)
index 0000000..64c6d10
--- /dev/null
@@ -0,0 +1,113 @@
+package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;\r
+\r
+import static org.mockito.Matchers.any;\r
+import static org.mockito.Matchers.eq;\r
+import static org.mockito.Mockito.mock;\r
+import static org.mockito.Mockito.spy;\r
+import static org.mockito.Mockito.verify;\r
+import static org.mockito.Mockito.when;\r
+\r
+import java.util.Set;\r
+\r
+import com.google.common.collect.ImmutableSet;\r
+import com.google.common.util.concurrent.CheckedFuture;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;\r
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;\r
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;\r
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;\r
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;\r
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;\r
+import org.opendaylight.groupbasedpolicy.api.sf.AllowActionDefinition;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.SubjectFeatureDefinitions;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ActionDefinition;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ActionDefinitionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ActionDefinitionKey;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.SupportedActionDefinition;\r
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
+\r
+public class ActionDefinitionListenerTest {\r
+\r
+    private ActionDefinitionListener listener;\r
+    private DataObjectModification<ActionDefinition> rootNode;\r
+    private Set<DataTreeModification<ActionDefinition>> changes;\r
+\r
+    private DataBroker dataProvider;\r
+\r
+    private InstanceIdentifier<ActionDefinition> rootIdentifier;\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Before\r
+    public void init() {\r
+\r
+        dataProvider = mock(DataBroker.class);\r
+\r
+        listener = spy(new ActionDefinitionListener(dataProvider));\r
+\r
+        ActionDefinitionKey key = mock(ActionDefinitionKey.class);\r
+\r
+        rootNode = mock(DataObjectModification.class);\r
+        rootIdentifier = InstanceIdentifier.builder(SubjectFeatureDefinitions.class)\r
+                .child(ActionDefinition.class, key)\r
+                .build();\r
+        DataTreeIdentifier<ActionDefinition> rootPath =\r
+                new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, rootIdentifier);\r
+\r
+        DataTreeModification<ActionDefinition> change = mock(DataTreeModification.class);\r
+\r
+        when(change.getRootNode()).thenReturn(rootNode);\r
+        when(change.getRootPath()).thenReturn(rootPath);\r
+\r
+        changes = ImmutableSet.of(change);\r
+\r
+        ActionDefinition def = new ActionDefinitionBuilder().setId(AllowActionDefinition.ID).build();\r
+\r
+        when(rootNode.getDataBefore()).thenReturn(def);\r
+        when(rootNode.getDataAfter()).thenReturn(def);\r
+    }\r
+\r
+    @Test\r
+    public void testOnDataTreeChanged_Write() {\r
+        when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);\r
+\r
+        WriteTransaction wt = resetTransaction();\r
+\r
+        listener.onDataTreeChanged(changes);\r
+\r
+        verify(wt).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),\r
+                any(SupportedActionDefinition.class), eq(true));\r
+    }\r
+\r
+    @Test\r
+    public void testOnDataTreeChanged_SubtreeModified() {\r
+        when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);\r
+\r
+        WriteTransaction wt = resetTransaction();\r
+\r
+        listener.onDataTreeChanged(changes);\r
+\r
+        verify(wt).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),\r
+                any(SupportedActionDefinition.class), eq(true));\r
+    }\r
+\r
+    @Test\r
+    public void testOnDataTreeChanged_Delete() {\r
+        when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);\r
+\r
+        WriteTransaction wt = resetTransaction();\r
+\r
+        listener.onDataTreeChanged(changes);\r
+\r
+        verify(wt).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));\r
+    }\r
+\r
+    private WriteTransaction resetTransaction() {\r
+        WriteTransaction wt = mock(WriteTransaction.class);\r
+        CheckedFuture checkedFuture = mock(CheckedFuture.class);\r
+        when(wt.submit()).thenReturn(checkedFuture);\r
+        when(dataProvider.newWriteOnlyTransaction()).thenReturn(wt);\r
+        return wt;\r
+    }\r
+\r
+}\r