894b80e56fd237dc699492714947daf2914604e9
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMDataTreeChangeServiceAdapterTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.controller.md.sal.binding.impl;
9
10 import static org.mockito.AdditionalMatchers.not;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.isA;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.reset;
15 import static org.mockito.Mockito.verify;
16 import java.util.Collection;
17 import org.hamcrest.Description;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.ArgumentMatcher;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeService;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
31 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
32 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
33 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
35 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
36 import org.opendaylight.yangtools.concepts.ListenerRegistration;
37 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40
41 /**
42  * Unit tests for BindingDOMDataTreeChangeServiceAdapter.
43  *
44  * @author Thomas Pantelis
45  */
46 public class BindingDOMDataTreeChangeServiceAdapterTest {
47     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
48
49     @Mock
50     private DOMDataTreeChangeService mockDOMService;
51
52     @Mock
53     private GeneratedClassLoadingStrategy classLoadingStrategy;
54
55     @Mock
56     private BindingNormalizedNodeCodecRegistry codecRegistry;
57
58     @Mock
59     private YangInstanceIdentifier mockYangID;
60
61     @SuppressWarnings("rawtypes")
62     @Mock
63     private ListenerRegistration mockDOMReg;
64
65     @Before
66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         doReturn(mockYangID).when(codecRegistry).toYangInstanceIdentifier(TOP_PATH);
69     }
70
71     @Test
72     public void testRegisterDataTreeChangeListener() {
73         BindingToNormalizedNodeCodec codec = new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry);
74
75         DataTreeChangeService service = BindingDOMDataTreeChangeServiceAdapter.create(codec, mockDOMService);
76
77         doReturn(mockDOMReg).when(mockDOMService).registerDataTreeChangeListener(domDataTreeIdentifier(mockYangID),
78                 any(DOMDataTreeChangeListener.class));
79         DataTreeIdentifier<Top> treeId = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, TOP_PATH);
80         TestClusteredDataTreeChangeListener mockClusteredListener = new TestClusteredDataTreeChangeListener();
81         service.registerDataTreeChangeListener(treeId , mockClusteredListener);
82
83         verify(mockDOMService).registerDataTreeChangeListener(domDataTreeIdentifier(mockYangID),
84                 isA(ClusteredDOMDataTreeChangeListener.class));
85
86         reset(mockDOMService);
87         doReturn(mockDOMReg).when(mockDOMService).registerDataTreeChangeListener(domDataTreeIdentifier(mockYangID),
88                 any(DOMDataTreeChangeListener.class));
89         TestDataTreeChangeListener mockNonClusteredListener = new TestDataTreeChangeListener();
90         service.registerDataTreeChangeListener(treeId , mockNonClusteredListener);
91
92         verify(mockDOMService).registerDataTreeChangeListener(domDataTreeIdentifier(mockYangID),
93                 not(isA(ClusteredDOMDataTreeChangeListener.class)));
94     }
95
96     static DOMDataTreeIdentifier domDataTreeIdentifier(final YangInstanceIdentifier yangID) {
97         return Matchers.argThat(new ArgumentMatcher<DOMDataTreeIdentifier>() {
98             @Override
99             public boolean matches(Object argument) {
100                 DOMDataTreeIdentifier treeId = (DOMDataTreeIdentifier) argument;
101                 return treeId.getDatastoreType() == LogicalDatastoreType.CONFIGURATION &&
102                         yangID.equals(treeId.getRootIdentifier());
103             }
104
105             @Override
106             public void describeTo(Description description) {
107                 description.appendValue(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, yangID));
108             }
109         });
110     }
111
112     private static class TestClusteredDataTreeChangeListener implements ClusteredDataTreeChangeListener<Top> {
113         @Override
114         public void onDataTreeChanged(Collection<DataTreeModification<Top>> changes) {
115         }
116     }
117
118     private static class TestDataTreeChangeListener implements DataTreeChangeListener<Top> {
119         @Override
120         public void onDataTreeChanged(Collection<DataTreeModification<Top>> changes) {
121         }
122     }
123 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.