d1464baa6e0ffb48d2aa7ebf84ac83b484846527
[controller.git] / opendaylight / config / threadpool-config-impl / src / test / java / org / opendaylight / controller / config / threadpool / scheduled / TestingScheduledThreadPoolModule.java
1 package org.opendaylight.controller.config.threadpool.scheduled;\r
2 \r
3 import com.google.common.util.concurrent.ListenableFutureTask;\r
4 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;\r
5 import org.opendaylight.controller.config.api.ModuleIdentifier;\r
6 import org.opendaylight.controller.config.manager.impl.AbstractMockedModule;\r
7 import org.opendaylight.controller.config.spi.Module;\r
8 import org.opendaylight.controller.config.threadpool.util.ScheduledThreadPoolWrapper;\r
9 import org.opendaylight.controller.config.yang.threadpool.ScheduledThreadPoolServiceInterface;\r
10 import org.opendaylight.controller.config.yang.threadpool.impl.scheduled.ScheduledThreadPoolModuleMXBean;\r
11 \r
12 import javax.management.ObjectName;\r
13 import java.util.concurrent.ScheduledExecutorService;\r
14 import java.util.concurrent.ScheduledFuture;\r
15 import java.util.concurrent.TimeUnit;\r
16 \r
17 import static org.mockito.Matchers.any;\r
18 import static org.mockito.Matchers.anyBoolean;\r
19 import static org.mockito.Matchers.anyLong;\r
20 import static org.mockito.Mockito.doNothing;\r
21 import static org.mockito.Mockito.doReturn;\r
22 import static org.mockito.Mockito.mock;\r
23 \r
24 public class TestingScheduledThreadPoolModule extends AbstractMockedModule implements\r
25         ScheduledThreadPoolServiceInterface, Module, ScheduledThreadPoolModuleMXBean {\r
26 \r
27     public TestingScheduledThreadPoolModule(DynamicMBeanWithInstance old, ModuleIdentifier id) {\r
28         super(old, id);\r
29     }\r
30 \r
31     @Override\r
32     protected AutoCloseable prepareMockedInstance() throws Exception {\r
33         ScheduledThreadPoolWrapper instance = mock(ScheduledThreadPoolWrapper.class);\r
34         ScheduledExecutorService ses = mock(ScheduledExecutorService.class);\r
35         {// mockFuture\r
36             ScheduledFuture<?> future = mock(ScheduledFuture.class);\r
37             doReturn(false).when(future).cancel(anyBoolean());\r
38             try {\r
39                 doReturn(mock(Object.class)).when(future).get();\r
40             } catch (Exception e) {\r
41                 throw new RuntimeException(e);\r
42             }\r
43             doReturn(future).when(ses).schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class));\r
44             doReturn(future).when(ses).scheduleWithFixedDelay(any(Runnable.class), anyLong(), anyLong(),\r
45                     any(TimeUnit.class));\r
46 \r
47         }\r
48         doNothing().when(ses).execute(any(Runnable.class));\r
49         doNothing().when(ses).execute(any(ListenableFutureTask.class));\r
50         doReturn(ses).when(instance).getExecutor();\r
51         doNothing().when(instance).close();\r
52 \r
53         doReturn(1).when(instance).getMaxThreadCount();\r
54         return instance;\r
55     }\r
56 \r
57     @Override\r
58     public ObjectName getThreadFactory() {\r
59         return any(ObjectName.class);\r
60     }\r
61 \r
62     @Override\r
63     public void setThreadFactory(ObjectName threadFactory) {\r
64     }\r
65 \r
66     @Override\r
67     public Integer getMaxThreadCount() {\r
68         return 1;\r
69     }\r
70 \r
71     @Override\r
72     public void setMaxThreadCount(Integer maxThreadCount) {\r
73     }\r
74 \r
75 }\r