2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
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;
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;
37 public class ActionDefinitionListenerTest {
39 private ActionDefinitionListener listener;
40 private DataObjectModification<ActionDefinition> rootNode;
41 private Set<DataTreeModification<ActionDefinition>> changes;
43 private DataBroker dataProvider;
45 private InstanceIdentifier<ActionDefinition> rootIdentifier;
47 @SuppressWarnings("unchecked")
51 dataProvider = mock(DataBroker.class);
53 listener = spy(new ActionDefinitionListener(dataProvider));
55 ActionDefinitionKey key = mock(ActionDefinitionKey.class);
57 rootNode = mock(DataObjectModification.class);
58 rootIdentifier = InstanceIdentifier.builder(SubjectFeatureDefinitions.class)
59 .child(ActionDefinition.class, key)
61 DataTreeIdentifier<ActionDefinition> rootPath =
62 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, rootIdentifier);
64 DataTreeModification<ActionDefinition> change = mock(DataTreeModification.class);
66 when(change.getRootNode()).thenReturn(rootNode);
67 when(change.getRootPath()).thenReturn(rootPath);
69 changes = ImmutableSet.of(change);
71 ActionDefinition def = new ActionDefinitionBuilder().setId(AllowActionDefinition.ID).build();
73 when(rootNode.getDataBefore()).thenReturn(def);
74 when(rootNode.getDataAfter()).thenReturn(def);
78 public void testOnDataTreeChanged_Write() {
79 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
81 WriteTransaction wt = resetTransaction();
83 listener.onDataTreeChanged(changes);
85 verify(wt).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
86 any(SupportedActionDefinition.class), eq(true));
90 public void testOnDataTreeChanged_SubtreeModified() {
91 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
93 WriteTransaction wt = resetTransaction();
95 listener.onDataTreeChanged(changes);
97 verify(wt).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class),
98 any(SupportedActionDefinition.class), eq(true));
102 public void testOnDataTreeChanged_Delete() {
103 when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
105 WriteTransaction wt = resetTransaction();
107 listener.onDataTreeChanged(changes);
109 verify(wt).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
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);