2 * Copyright (c) 2015 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.controller.config.yang.bmp.impl;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyString;
12 import static org.mockito.Matchers.contains;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.util.Timer;
19 import io.netty.util.concurrent.EventExecutor;
20 import javax.management.InstanceAlreadyExistsException;
21 import javax.management.ObjectName;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.opendaylight.controller.config.api.jmx.CommitStatus;
27 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
28 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
29 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
30 import org.opendaylight.controller.config.yang.bmp.spi.SimpleBmpExtensionProviderContextModuleFactory;
31 import org.opendaylight.controller.config.yang.bmp.spi.SimpleBmpExtensionProviderContextModuleMXBean;
32 import org.opendaylight.controller.config.yang.netty.eventexecutor.GlobalEventExecutorModuleFactory;
33 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleFactory;
34 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleMXBean;
35 import org.osgi.framework.Filter;
36 import org.osgi.framework.ServiceListener;
37 import org.osgi.framework.ServiceReference;
39 public class BmpDispatcherImplModuleTest extends AbstractConfigTest {
40 private static final String INSTANCE_NAME = "bmp-message-fct";
41 private static final String FACTORY_NAME = BmpDispatcherImplModuleFactory.NAME;
43 private static final String BMP_EXTENSION_INSTANCE_NAME = "bmp-extension-impl";
44 private static final String BOSS_TG_INSTANCE_NAME = "boss-threadgroup-impl";
45 private static final String WORKER_TG_INSTANCE_NAME = "worker-threadgroup-impl";
48 public void setUp() throws Exception {
49 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext,
50 new BmpDispatcherImplModuleFactory(),
51 new NettyThreadgroupModuleFactory(),
52 new GlobalEventExecutorModuleFactory(),
53 new SimpleBmpExtensionProviderContextModuleFactory()));
55 doAnswer(new Answer<Filter>() {
57 public Filter answer(InvocationOnMock invocation) {
58 String str = invocation.getArgumentAt(0, String.class);
59 Filter mockFilter = mock(Filter.class);
60 doReturn(str).when(mockFilter).toString();
63 }).when(mockedContext).createFilter(anyString());
64 doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
66 setupMockService(EventLoopGroup.class);
67 setupMockService(Timer.class);
68 setupMockService(EventExecutor.class);
71 private void setupMockService(Class<?> serviceInterface) throws Exception {
72 ServiceReference<?> mockServiceRef = mock(ServiceReference.class);
73 doReturn(new ServiceReference[]{mockServiceRef}).when(mockedContext).
74 getServiceReferences(anyString(), contains(serviceInterface.getName()));
75 doReturn(mock(serviceInterface)).when(mockedContext).getService(mockServiceRef);
79 public void testCreateBean() throws Exception {
80 final CommitStatus status = createInstance();
81 assertBeanCount(1, FACTORY_NAME);
82 assertStatus(status, 4, 0, 0);
86 public void testReusingOldInstance() throws Exception {
88 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
89 assertBeanCount(1, FACTORY_NAME);
90 final CommitStatus status = transaction.commit();
91 assertBeanCount(1, FACTORY_NAME);
92 assertStatus(status, 0, 0, 4);
95 private CommitStatus createInstance() throws Exception {
96 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
97 createInstance(transaction);
98 return transaction.commit();
101 public static ObjectName createInstance(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
102 final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
103 final BmpDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BmpDispatcherImplModuleMXBean.class);
104 mxBean.setBossGroup(createThreadgroupInstance(transaction, BOSS_TG_INSTANCE_NAME, 10));
105 mxBean.setWorkerGroup(createThreadgroupInstance(transaction, WORKER_TG_INSTANCE_NAME, 10));
106 mxBean.setBmpExtensions(createBmpExtensionsInstance(transaction));
110 private static ObjectName createThreadgroupInstance(final ConfigTransactionJMXClient transaction, final String instanceName,
111 final Integer threadCount) throws InstanceAlreadyExistsException {
112 final ObjectName nameCreated = transaction.createModule(NettyThreadgroupModuleFactory.NAME, instanceName);
113 final NettyThreadgroupModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NettyThreadgroupModuleMXBean.class);
114 mxBean.setThreadCount(threadCount);
118 private static ObjectName createBmpExtensionsInstance(final ConfigTransactionJMXClient transaction)
119 throws InstanceAlreadyExistsException {
120 final ObjectName nameCreated = transaction.createModule(SimpleBmpExtensionProviderContextModuleFactory.NAME, BMP_EXTENSION_INSTANCE_NAME);
121 transaction.newMXBeanProxy(nameCreated, SimpleBmpExtensionProviderContextModuleMXBean.class);