26ce912ed54abc91eb708bd62ccc32d0515bafee
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / sf / ActionDefinitionListenerTest.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 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import java.util.Set;
18
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.groupbasedpolicy.api.sf.AllowActionDefinition;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.SubjectFeatureDefinitions;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ActionDefinition;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ActionDefinitionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.definitions.ActionDefinitionKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.capabilities.SupportedActionDefinition;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36
37 public class ActionDefinitionListenerTest {
38
39     private ActionDefinitionListener listener;
40     private DataObjectModification<ActionDefinition> rootNode;
41     private Set<DataTreeModification<ActionDefinition>> changes;
42
43     private DataBroker dataProvider;
44
45     private InstanceIdentifier<ActionDefinition> rootIdentifier;
46
47     @SuppressWarnings("unchecked")
48     @Before
49     public void init() {
50
51         dataProvider = mock(DataBroker.class);
52
53         listener = spy(new ActionDefinitionListener(dataProvider));
54
55         ActionDefinitionKey key = mock(ActionDefinitionKey.class);
56
57         rootNode = mock(DataObjectModification.class);
58         rootIdentifier = InstanceIdentifier.builder(SubjectFeatureDefinitions.class)
59                 .child(ActionDefinition.class, key)
60                 .build();
61         DataTreeIdentifier<ActionDefinition> rootPath =
62                 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, rootIdentifier);
63
64         DataTreeModification<ActionDefinition> change = mock(DataTreeModification.class);
65
66         when(change.getRootNode()).thenReturn(rootNode);
67         when(change.getRootPath()).thenReturn(rootPath);
68
69         changes = ImmutableSet.of(change);
70
71         ActionDefinition def = new ActionDefinitionBuilder().setId(AllowActionDefinition.ID).build();
72
73         when(rootNode.getDataBefore()).thenReturn(def);
74         when(rootNode.getDataAfter()).thenReturn(def);
75     }
76
77     @Test
78     public void testOnDataTreeChanged_Write() {
79         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
80
81         WriteTransaction wt = resetTransaction();
82
83         listener.onDataTreeChanged(changes);
84
85         verify(wt).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
86                 any(SupportedActionDefinition.class), eq(true));
87     }
88
89     @Test
90     public void testOnDataTreeChanged_SubtreeModified() {
91         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
92
93         WriteTransaction wt = resetTransaction();
94
95         listener.onDataTreeChanged(changes);
96
97         verify(wt).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
98                 any(SupportedActionDefinition.class), eq(true));
99     }
100
101     @Test
102     public void testOnDataTreeChanged_Delete() {
103         when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
104
105         WriteTransaction wt = resetTransaction();
106
107         listener.onDataTreeChanged(changes);
108
109         verify(wt).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
110     }
111
112     private WriteTransaction resetTransaction() {
113         WriteTransaction wt = mock(WriteTransaction.class);
114         CheckedFuture checkedFuture = mock(CheckedFuture.class);
115         when(wt.submit()).thenReturn(checkedFuture);
116         when(dataProvider.newWriteOnlyTransaction()).thenReturn(wt);
117         return wt;
118     }
119
120 }