Fix checkstyle warnings for impl/karaf, lifecycle, common, mastership
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / configuration / ConfigurationServiceFactoryImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.configuration;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.collect.ImmutableMap;
19 import java.util.Dictionary;
20 import java.util.Hashtable;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowplugin.api.OFConstants;
27 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
28 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
29 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint16Type;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.ServiceReference;
35 import org.osgi.service.cm.Configuration;
36 import org.osgi.service.cm.ConfigurationAdmin;
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class ConfigurationServiceFactoryImplTest {
40     private static final int CONFIG_PROP_COUNT = 16;
41     private static final boolean IS_STATISTICS_POLLING_ON = true;
42     private static final int BARRIER_COUNT_LIMIT = 2000;
43     private static final long BARRIER_INTERVAL_TIMEOUT_LIMIT = 3000;
44     private static final long ECHO_REPLY_TIMEOUT = 4000;
45     private static final boolean ENABLE_FLOW_REMOVED_NOTIFICATION = true;
46     private static final boolean SKIP_TABLE_FEATURES = true;
47     private static final long BASIC_TIMER_DELAY = 2690;
48     private static final long MAXIMUM_TIMER_DELAY = 3679;
49     private static final boolean SWITCH_FEATURES_MANDATORY = false;
50     private static final boolean IS_STATISTICS_RPC_ENABLED = false;
51     private static final boolean USE_SINGLE_LAYER_SERIALIZATION = true;
52     private static final int RPC_REQUESTS_QUOTA = 2500;
53     private static final long GLOBAL_NOTIFICATION_QUOTA = 9000;
54     private static final int THREAD_POOL_MIN_THREADS = 3;
55     private static final int THREAD_POOL_MIN_THREADS_UPDATE = 4;
56     private static final int THREAD_POOL_MAX_THREADS = 1000;
57     private static final long THREAD_POOL_TIMEOUT = 60;
58
59     @Mock
60     private OpenflowProviderConfig config;
61
62     @Mock
63     private BundleContext bundleContext;
64
65     @Mock
66     private ConfigurationListener configurationListener;
67
68     @Mock
69     private ConfigurationAdmin configurationAdmin;
70
71     @Mock
72     private ServiceReference<ConfigurationAdmin> serviceReference;
73
74     @Mock
75     private Configuration configuration;
76
77     private ConfigurationService configurationService;
78
79     @Before
80     public void setUp() throws Exception {
81         when(config.isIsStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
82         when(config.getBarrierCountLimit()).thenReturn(new NonZeroUint16Type(BARRIER_COUNT_LIMIT));
83         when(config.getBarrierIntervalTimeoutLimit()).thenReturn(new NonZeroUint32Type(BARRIER_INTERVAL_TIMEOUT_LIMIT));
84         when(config.getEchoReplyTimeout()).thenReturn(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT));
85         when(config.isEnableFlowRemovedNotification()).thenReturn(ENABLE_FLOW_REMOVED_NOTIFICATION);
86         when(config.isSkipTableFeatures()).thenReturn(SKIP_TABLE_FEATURES);
87         when(config.getBasicTimerDelay()).thenReturn(new NonZeroUint32Type(BASIC_TIMER_DELAY));
88         when(config.getMaximumTimerDelay()).thenReturn(new NonZeroUint32Type(MAXIMUM_TIMER_DELAY));
89         when(config.isSwitchFeaturesMandatory()).thenReturn(SWITCH_FEATURES_MANDATORY);
90         when(config.isIsStatisticsRpcEnabled()).thenReturn(IS_STATISTICS_RPC_ENABLED);
91         when(config.isUseSingleLayerSerialization()).thenReturn(USE_SINGLE_LAYER_SERIALIZATION);
92         when(config.getRpcRequestsQuota()).thenReturn(new NonZeroUint16Type(RPC_REQUESTS_QUOTA));
93         when(config.getGlobalNotificationQuota()).thenReturn(GLOBAL_NOTIFICATION_QUOTA);
94         when(config.getThreadPoolMinThreads()).thenReturn(THREAD_POOL_MIN_THREADS);
95         when(config.getThreadPoolMaxThreads()).thenReturn(new NonZeroUint16Type(THREAD_POOL_MAX_THREADS));
96         when(config.getThreadPoolTimeout()).thenReturn(THREAD_POOL_TIMEOUT);
97
98         final Dictionary<String, Object> properties = new Hashtable<>();
99         properties.put(ConfigurationProperty.IS_STATISTICS_POLLING_ON.toString(), IS_STATISTICS_POLLING_ON);
100
101         when(configuration.getProperties()).thenReturn(properties);
102         when(configurationAdmin.getConfiguration(OFConstants.CONFIG_FILE_ID)).thenReturn(configuration);
103         when(bundleContext.getService(serviceReference)).thenReturn(configurationAdmin);
104         when(bundleContext.getServiceReference(ConfigurationAdmin.class)).thenReturn(serviceReference);
105
106         configurationService = new ConfigurationServiceFactoryImpl()
107                 .newInstance(config, bundleContext);
108     }
109
110     @Test
111     public void update() throws Exception {
112         final int tpMinThreads = configurationService
113                 .getProperty(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), Integer::valueOf);
114
115         configurationService.update(ImmutableMap
116                 .<String, String>builder()
117                 .put(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), String
118                         .valueOf(THREAD_POOL_MIN_THREADS_UPDATE))
119                 .build());
120
121         final int tpMinThreadsNew = configurationService
122                 .getProperty(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), Integer::valueOf);
123
124         assertNotEquals(tpMinThreadsNew, tpMinThreads);
125         assertEquals(tpMinThreadsNew, THREAD_POOL_MIN_THREADS_UPDATE);
126     }
127
128     @Test
129     public void getProperty() throws Exception {
130         final int tpMaxThreads = configurationService
131                 .getProperty(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(), Integer::valueOf);
132
133         assertEquals(THREAD_POOL_MAX_THREADS, tpMaxThreads);
134     }
135
136     @Test
137     public void registerListener() throws Exception {
138         configurationService.registerListener(configurationListener);
139         verify(configurationListener, times(CONFIG_PROP_COUNT)).onPropertyChanged(any(), any());
140     }
141
142     @Test(expected = NumberFormatException.class)
143     public void close() throws Exception {
144         configurationService.close();
145         configurationService.getProperty(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(), Integer::valueOf);
146     }
147
148 }