5b11c4656420168571d4cbf957a220331d7260f7
[bgpcep.git] / data-change-counter / src / test / java / org / opendaylight / controller / config / yang / bgpcep / data / change / counter / DataChangeCounterImplModuleTest.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.config.yang.bgpcep.data.change.counter;
10
11 import java.util.Collections;
12 import java.util.Set;
13 import javax.management.ObjectName;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.config.api.DependencyResolver;
20 import org.opendaylight.controller.config.api.DependencyResolverFactory;
21 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
22 import org.opendaylight.controller.config.api.ModuleIdentifier;
23 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
24 import org.opendaylight.controller.config.api.jmx.CommitStatus;
25 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
26 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
27 import org.opendaylight.controller.config.spi.Module;
28 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
29 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
30 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
33 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
34 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev160315.DataChangeCounter;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.osgi.framework.BundleContext;
40
41 public class DataChangeCounterImplModuleTest extends AbstractConfigTest {
42
43     private static final String FACTORY_NAME = DataChangeCounterImplModuleFactory.NAME;
44     private static final String INSTANCE_NAME = "data-change-counter";
45     private static final String DATA_BROKER_INSTANCE_NAME = "data-broker-instance";
46
47     private static final String COUNTER_ID = "counter";
48     private static final String NEW_COUNTER_ID = "new-counter";
49     private static final String TOPOLOGY_NAME = "test";
50     private static final String NEW_TOPOLOGY_NAME = "new-test";
51
52     @Mock
53     private CloseableDataBroker dataBroker;
54     @Mock
55     private BindingTransactionChain chain;
56     @Mock
57     private WriteTransaction wTx;
58     @Mock
59     private ListenerRegistration<DataTreeChangeListener<?>> registration;
60
61     @Before
62     public void setUp() throws Exception {
63         Mockito.doNothing().when(this.registration).close();
64         Mockito.doReturn(null).when(this.wTx).submit();
65         Mockito.doNothing().when(this.wTx).put(Mockito.any(LogicalDatastoreType.class), Mockito.any(), Mockito.any(DataChangeCounter.class));
66         Mockito.doReturn(this.registration).when(this.dataBroker).registerDataTreeChangeListener(Mockito.any(DataTreeIdentifier.class), Mockito.any(ClusteredDataTreeChangeListener.class));
67         Mockito.doNothing().when(this.wTx).delete(Mockito.any(LogicalDatastoreType.class), Mockito.any());
68         Mockito.doReturn(this.chain).when(this.dataBroker).createTransactionChain(Mockito.any(TransactionChainListener.class));
69         Mockito.doReturn(this.wTx).when(this.chain).newWriteOnlyTransaction();
70         Mockito.doReturn(this.wTx).when(this.dataBroker).newWriteOnlyTransaction();
71         Mockito.doNothing().when(this.chain).close();
72         Mockito.doNothing().when(this.dataBroker).close();
73         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext, new DataChangeCounterImplModuleFactory(), new MockDataBrokerModuleFct()));
74     }
75
76     @Test
77     public void testCreateBean() throws Exception {
78         final CommitStatus status = createInstance(COUNTER_ID, TOPOLOGY_NAME);
79         assertBeanCount(1, FACTORY_NAME);
80         assertStatus(status, 2, 0, 0);
81     }
82
83     @Test
84     public void testReusingOldInstance() throws Exception {
85         createInstance(COUNTER_ID, TOPOLOGY_NAME);
86         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
87         assertBeanCount(1, FACTORY_NAME);
88         final CommitStatus status = transaction.commit();
89         assertBeanCount(1, FACTORY_NAME);
90         assertStatus(status, 0, 0, 2);
91     }
92
93     @Test
94     public void testReconfigureBean() throws Exception {
95         createInstance(COUNTER_ID, TOPOLOGY_NAME);
96         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
97         final DataChangeCounterImplModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
98                 DataChangeCounterImplModuleMXBean.class);
99         mxBean.setCounterId(NEW_COUNTER_ID);
100         mxBean.setTopologyName(NEW_TOPOLOGY_NAME);
101         final CommitStatus status = transaction.commit();
102         assertBeanCount(1, FACTORY_NAME);
103         assertStatus(status, 0, 1, 1);
104
105         final ConfigTransactionJMXClient transaction2 = this.configRegistryClient.createTransaction();
106         final DataChangeCounterImplModuleMXBean mxBean2 = transaction2.newMXBeanProxy(transaction2.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
107                 DataChangeCounterImplModuleMXBean.class);
108         Assert.assertEquals(NEW_COUNTER_ID, mxBean2.getCounterId());
109         Assert.assertEquals(NEW_TOPOLOGY_NAME, mxBean2.getTopologyName());
110     }
111
112     private CommitStatus createInstance(final String counterId, final String topologyName) throws Exception {
113         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
114         final ObjectName on = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
115         final ObjectName dbOn = transaction.createModule(MockDataBrokerModuleFct.INSTANCE_NAME, DATA_BROKER_INSTANCE_NAME);
116         final DataChangeCounterImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, DataChangeCounterImplModuleMXBean.class);
117         mxBean.setCounterId(counterId);
118         mxBean.setTopologyName(topologyName);
119         mxBean.setDataProvider(dbOn);
120         return transaction.commit();
121     }
122
123     private final class MockDataBrokerModuleFct implements org.opendaylight.controller.config.spi.ModuleFactory {
124
125         private static final String INSTANCE_NAME = "data-broker-fct";
126
127         @Override
128         public String getImplementationName() {
129             return INSTANCE_NAME;
130         }
131
132         @Override
133         public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
134                 final BundleContext bundleContext) {
135             return new MockDataBrokerModule();
136         }
137
138         @Override
139         public Module createModule(final String instanceName, final DependencyResolver dependencyResolver,
140                 final DynamicMBeanWithInstance old, final BundleContext bundleContext) throws Exception {
141             return new MockDataBrokerModule();
142         }
143
144         @Override
145         public boolean isModuleImplementingServiceInterface(final Class<? extends AbstractServiceInterface> serviceInterface) {
146             return true;
147         }
148
149         @Override
150         public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
151             final java.util.Set<Class<? extends org.opendaylight.controller.config.api.annotations.AbstractServiceInterface>> serviceIfcs2 = new java.util.HashSet<Class<? extends org.opendaylight.controller.config.api.annotations.AbstractServiceInterface>>();
152             return java.util.Collections.unmodifiableSet(serviceIfcs2);
153         }
154
155         @Override
156         public Set<? extends Module> getDefaultModules(final DependencyResolverFactory dependencyResolverFactory,
157                 final BundleContext bundleContext) {
158             return Collections.emptySet();
159         }
160
161     }
162
163     private final class MockDataBrokerModule implements org.opendaylight.controller.config.spi.Module,org.opendaylight.controller.config.yang.md.sal.binding.impl.BindingAsyncDataBrokerImplModuleMXBean,org.opendaylight.controller.config.yang.md.sal.binding.DataBrokerServiceInterface {
164
165         @Override
166         public ModuleIdentifier getIdentifier() {
167             return new ModuleIdentifier(MockDataBrokerModuleFct.INSTANCE_NAME, DATA_BROKER_INSTANCE_NAME);
168         }
169
170         @Override
171         public ObjectName getBindingMappingService() {
172             return null;
173         }
174
175         @Override
176         public void setBindingMappingService(final ObjectName bindingMappingService) {
177             return;
178         }
179
180         @Override
181         public ObjectName getDomAsyncBroker() {
182             return null;
183         }
184
185         @Override
186         public void setDomAsyncBroker(final ObjectName domAsyncBroker) {
187             return;
188         }
189
190         @Override
191         public void validate() {
192             return;
193         }
194
195         @Override
196         public AutoCloseable getInstance() {
197             return DataChangeCounterImplModuleTest.this.dataBroker;
198         }
199
200         @Override
201         public ObjectName getSchemaService() {
202             return null;
203         }
204
205         @Override
206         public void setSchemaService(final ObjectName schemaService) {
207             return;
208         }
209
210         @Override
211         public boolean canReuse(final Module arg0) {
212             return true;
213         }
214
215     }
216
217     private interface CloseableDataBroker extends DataBroker, AutoCloseable {
218     }
219
220 }