Replaced reference from deprecated junit.framework.Assert to org.junit.Assert
[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.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
31 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.data.change.counter.rev140815.DataChangeCounter;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.osgi.framework.BundleContext;
37
38 public class DataChangeCounterImplModuleTest extends AbstractConfigTest {
39
40     private static final String FACTORY_NAME = DataChangeCounterImplModuleFactory.NAME;
41     private static final String INSTANCE_NAME = DataChangeCounterImplModuleFactory.SINGLETON_NAME;
42     private static final String DATA_BROKER_INSTANCE_NAME = "data-broker-instance";
43
44     private static final String TOPOLOGY_NAME = "test";
45     private static final String NEW_TOPOLOGY_NAME = "new-test";
46
47     @Mock
48     private CloseableDataBroker dataBorker;
49     @Mock
50     private WriteTransaction wTx;
51     @Mock
52     private ListenerRegistration<DataChangeListener> registration;
53
54     @Before
55     public void setUp() throws Exception {
56         Mockito.doNothing().when(this.registration).close();
57         Mockito.doReturn(null).when(this.wTx).submit();
58         Mockito.doNothing().when(this.wTx).put(Mockito.any(LogicalDatastoreType.class), Mockito.<InstanceIdentifier<DataChangeCounter>>any(), Mockito.any(DataChangeCounter.class));
59         Mockito.doReturn(registration).when(this.dataBorker).registerDataChangeListener(Mockito.any(LogicalDatastoreType.class), Mockito.<InstanceIdentifier<?>>any(), Mockito.any(DataChangeListener.class), Mockito.any(DataBroker.DataChangeScope.class));
60         Mockito.doNothing().when(this.wTx).delete(Mockito.any(LogicalDatastoreType.class), Mockito.<InstanceIdentifier<?>>any());
61         Mockito.doReturn(this.wTx).when(this.dataBorker).newWriteOnlyTransaction();
62         Mockito.doNothing().when(this.dataBorker).close();
63         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, new DataChangeCounterImplModuleFactory(), new MockDataBrokerModuleFct()));
64     }
65
66     @Test
67     public void testCreateBean() throws Exception {
68         final CommitStatus status = createInstance(TOPOLOGY_NAME);
69         assertBeanCount(1, FACTORY_NAME);
70         assertStatus(status, 2, 0, 0);
71     }
72
73     @Test
74     public void testReusingOldInstance() throws Exception {
75         createInstance(TOPOLOGY_NAME);
76         final ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
77         assertBeanCount(1, FACTORY_NAME);
78         final CommitStatus status = transaction.commit();
79         assertBeanCount(1, FACTORY_NAME);
80         assertStatus(status, 0, 0, 2);
81     }
82
83     @Test
84     public void testReconfigureBean() throws Exception {
85         createInstance(TOPOLOGY_NAME);
86         final ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
87         final DataChangeCounterImplModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
88                 DataChangeCounterImplModuleMXBean.class);
89         mxBean.setTopologyName(NEW_TOPOLOGY_NAME);
90         final CommitStatus status = transaction.commit();
91         assertBeanCount(1, FACTORY_NAME);
92         assertStatus(status, 0, 1, 1);
93
94         final ConfigTransactionJMXClient transaction2 = configRegistryClient.createTransaction();
95         final DataChangeCounterImplModuleMXBean mxBean2 = transaction2.newMXBeanProxy(transaction2.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
96                 DataChangeCounterImplModuleMXBean.class);
97         Assert.assertEquals(NEW_TOPOLOGY_NAME, mxBean2.getTopologyName());
98     }
99
100     private CommitStatus createInstance(final String topologyName) throws Exception {
101         final ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
102         final ObjectName on = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
103         final ObjectName dbOn = transaction.createModule(MockDataBrokerModuleFct.INSTANCE_NAME, DATA_BROKER_INSTANCE_NAME);
104         final DataChangeCounterImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, DataChangeCounterImplModuleMXBean.class);
105         mxBean.setTopologyName(topologyName);
106         mxBean.setDataProvider(dbOn);
107         return transaction.commit();
108     }
109
110     private final class MockDataBrokerModuleFct implements org.opendaylight.controller.config.spi.ModuleFactory {
111
112         private static final String INSTANCE_NAME = "data-broker-fct";
113
114         @Override
115         public String getImplementationName() {
116             return INSTANCE_NAME;
117         }
118
119         @Override
120         public Module createModule(String instanceName, DependencyResolver dependencyResolver,
121                 BundleContext bundleContext) {
122             return new MockDataBrokerModule();
123         }
124
125         @Override
126         public Module createModule(String instanceName, DependencyResolver dependencyResolver,
127                 DynamicMBeanWithInstance old, BundleContext bundleContext) throws Exception {
128             return new MockDataBrokerModule();
129         }
130
131         @Override
132         public boolean isModuleImplementingServiceInterface(Class<? extends AbstractServiceInterface> serviceInterface) {
133             return true;
134         }
135
136         @Override
137         public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
138             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>>();
139             return java.util.Collections.unmodifiableSet(serviceIfcs2);
140         }
141
142         @Override
143         public Set<? extends Module> getDefaultModules(DependencyResolverFactory dependencyResolverFactory,
144                 BundleContext bundleContext) {
145             return Collections.emptySet();
146         }
147
148     }
149
150     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 {
151
152         @Override
153         public ModuleIdentifier getIdentifier() {
154             return new ModuleIdentifier(MockDataBrokerModuleFct.INSTANCE_NAME, DATA_BROKER_INSTANCE_NAME);
155         }
156
157         @Override
158         public ObjectName getBindingMappingService() {
159             return null;
160         }
161
162         @Override
163         public void setBindingMappingService(ObjectName bindingMappingService) {
164             return;
165         }
166
167         @Override
168         public ObjectName getDomAsyncBroker() {
169             return null;
170         }
171
172         @Override
173         public void setDomAsyncBroker(ObjectName domAsyncBroker) {
174             return;
175         }
176
177         @Override
178         public void validate() {
179             return;
180         }
181
182         @Override
183         public AutoCloseable getInstance() {
184             return (AutoCloseable) DataChangeCounterImplModuleTest.this.dataBorker;
185         }
186
187     }
188
189     private interface CloseableDataBroker extends DataBroker, AutoCloseable {
190     }
191
192 }