Migrate MockitoJUnitRunner
[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.ArgumentMatchers.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.Hashtable;
20 import java.util.Map;
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.junit.MockitoJUnitRunner;
26 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationListener;
27 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationProperty;
28 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint16Type;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
32 import org.opendaylight.yangtools.yang.common.Uint16;
33 import org.opendaylight.yangtools.yang.common.Uint32;
34
35 @RunWith(MockitoJUnitRunner.class)
36 public class ConfigurationServiceFactoryImplTest {
37     private static final int CONFIG_PROP_COUNT = 24;
38     private static final boolean IS_STATISTICS_POLLING_ON = true;
39     private static final int BARRIER_COUNT_LIMIT = 2000;
40     private static final long BARRIER_INTERVAL_TIMEOUT_LIMIT = 3000;
41     private static final long ECHO_REPLY_TIMEOUT = 4000;
42     private static final boolean ENABLE_FLOW_REMOVED_NOTIFICATION = true;
43     private static final boolean SKIP_TABLE_FEATURES = true;
44     private static final long BASIC_TIMER_DELAY = 2690;
45     private static final long MAXIMUM_TIMER_DELAY = 3679;
46     private static final boolean SWITCH_FEATURES_MANDATORY = false;
47     private static final boolean IS_STATISTICS_RPC_ENABLED = false;
48     private static final boolean USE_SINGLE_LAYER_SERIALIZATION = true;
49     private static final int RPC_REQUESTS_QUOTA = 2500;
50     private static final Uint32 GLOBAL_NOTIFICATION_QUOTA = Uint32.valueOf(9000);
51     private static final Uint16 THREAD_POOL_MIN_THREADS = Uint16.valueOf(3);
52     private static final int THREAD_POOL_MIN_THREADS_UPDATE = 4;
53     private static final int THREAD_POOL_MAX_THREADS = 1000;
54     private static final Uint32 THREAD_POOL_TIMEOUT = Uint32.valueOf(60);
55     private static final Uint16 DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = Uint16.ZERO;
56     private static final Uint16 DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS = Uint16.valueOf(60);
57
58     @Mock
59     private OpenflowProviderConfig config;
60
61     @Mock
62     private ConfigurationListener configurationListener;
63
64     private ConfigurationService configurationService;
65
66     @Before
67     public void setUp() {
68         when(config.isIsStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
69         when(config.isIsFlowStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
70         when(config.isIsTableStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
71         when(config.isIsFlowStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
72         when(config.isIsGroupStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
73         when(config.isIsMeterStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
74         when(config.isIsQueueStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
75         when(config.isIsPortStatisticsPollingOn()).thenReturn(IS_STATISTICS_POLLING_ON);
76         when(config.getBarrierCountLimit()).thenReturn(new NonZeroUint16Type(BARRIER_COUNT_LIMIT));
77         when(config.getBarrierIntervalTimeoutLimit()).thenReturn(new NonZeroUint32Type(BARRIER_INTERVAL_TIMEOUT_LIMIT));
78         when(config.getEchoReplyTimeout()).thenReturn(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT));
79         when(config.isEnableFlowRemovedNotification()).thenReturn(ENABLE_FLOW_REMOVED_NOTIFICATION);
80         when(config.isSkipTableFeatures()).thenReturn(SKIP_TABLE_FEATURES);
81         when(config.getBasicTimerDelay()).thenReturn(new NonZeroUint32Type(BASIC_TIMER_DELAY));
82         when(config.getMaximumTimerDelay()).thenReturn(new NonZeroUint32Type(MAXIMUM_TIMER_DELAY));
83         when(config.isSwitchFeaturesMandatory()).thenReturn(SWITCH_FEATURES_MANDATORY);
84         when(config.isIsStatisticsRpcEnabled()).thenReturn(IS_STATISTICS_RPC_ENABLED);
85         when(config.isUseSingleLayerSerialization()).thenReturn(USE_SINGLE_LAYER_SERIALIZATION);
86         when(config.getRpcRequestsQuota()).thenReturn(new NonZeroUint16Type(RPC_REQUESTS_QUOTA));
87         when(config.getGlobalNotificationQuota()).thenReturn(GLOBAL_NOTIFICATION_QUOTA);
88         when(config.getThreadPoolMinThreads()).thenReturn(THREAD_POOL_MIN_THREADS);
89         when(config.getThreadPoolMaxThreads()).thenReturn(new NonZeroUint16Type(THREAD_POOL_MAX_THREADS));
90         when(config.getThreadPoolTimeout()).thenReturn(THREAD_POOL_TIMEOUT);
91         when(config.getDeviceConnectionRateLimitPerMin()).thenReturn(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN);
92         when(config.getDeviceConnectionHoldTimeInSeconds()).thenReturn(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS);
93
94         final Map<String, String> properties = new Hashtable<>();
95         properties.put(ConfigurationProperty.IS_STATISTICS_POLLING_ON.toString(),
96                 Boolean.toString(IS_STATISTICS_POLLING_ON));
97
98         configurationService = new ConfigurationServiceFactoryImpl().newInstance(config);
99         configurationService.update(properties);
100     }
101
102     @Test
103     public void update() {
104         final int tpMinThreads = configurationService
105                 .getProperty(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), Integer::valueOf);
106
107         configurationService.update(ImmutableMap
108                 .<String, String>builder()
109                 .put(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), String
110                         .valueOf(THREAD_POOL_MIN_THREADS_UPDATE))
111                 .build());
112
113         final int tpMinThreadsNew = configurationService
114                 .getProperty(ConfigurationProperty.THREAD_POOL_MIN_THREADS.toString(), Integer::valueOf);
115
116         assertNotEquals(tpMinThreadsNew, tpMinThreads);
117         assertEquals(tpMinThreadsNew, THREAD_POOL_MIN_THREADS_UPDATE);
118     }
119
120     @Test
121     public void getProperty() {
122         final int tpMaxThreads = configurationService
123                 .getProperty(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(), Integer::valueOf);
124
125         assertEquals(THREAD_POOL_MAX_THREADS, tpMaxThreads);
126     }
127
128     @Test
129     public void registerListener() {
130         configurationService.registerListener(configurationListener);
131         verify(configurationListener, times(CONFIG_PROP_COUNT)).onPropertyChanged(any(), any());
132     }
133
134     @Test(expected = NumberFormatException.class)
135     public void close() throws Exception {
136         configurationService.close();
137         configurationService.getProperty(ConfigurationProperty.THREAD_POOL_MAX_THREADS.toString(), Integer::valueOf);
138     }
139 }