Merge "Fix for Bug 3"
[controller.git] / opendaylight / config / threadpool-config-impl / src / test / java / org / opendaylight / controller / config / threadpool / scheduled / TestingScheduledThreadPoolModule.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.config.threadpool.scheduled;\r
9 \r
10 import com.google.common.util.concurrent.ListenableFutureTask;\r
11 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;\r
12 import org.opendaylight.controller.config.api.ModuleIdentifier;\r
13 import org.opendaylight.controller.config.manager.impl.AbstractMockedModule;\r
14 import org.opendaylight.controller.config.spi.Module;\r
15 import org.opendaylight.controller.config.threadpool.util.ScheduledThreadPoolWrapper;\r
16 import org.opendaylight.controller.config.yang.threadpool.ScheduledThreadPoolServiceInterface;\r
17 import org.opendaylight.controller.config.yang.threadpool.impl.scheduled.ScheduledThreadPoolModuleMXBean;\r
18 \r
19 import javax.management.ObjectName;\r
20 import java.util.concurrent.ScheduledExecutorService;\r
21 import java.util.concurrent.ScheduledFuture;\r
22 import java.util.concurrent.TimeUnit;\r
23 \r
24 import static org.mockito.Matchers.any;\r
25 import static org.mockito.Matchers.anyBoolean;\r
26 import static org.mockito.Matchers.anyLong;\r
27 import static org.mockito.Mockito.doNothing;\r
28 import static org.mockito.Mockito.doReturn;\r
29 import static org.mockito.Mockito.mock;\r
30 \r
31 public class TestingScheduledThreadPoolModule extends AbstractMockedModule implements\r
32         ScheduledThreadPoolServiceInterface, Module, ScheduledThreadPoolModuleMXBean {\r
33 \r
34     public TestingScheduledThreadPoolModule(DynamicMBeanWithInstance old, ModuleIdentifier id) {\r
35         super(old, id);\r
36     }\r
37 \r
38     @Override\r
39     protected AutoCloseable prepareMockedInstance() throws Exception {\r
40         ScheduledThreadPoolWrapper instance = mock(ScheduledThreadPoolWrapper.class);\r
41         ScheduledExecutorService ses = mock(ScheduledExecutorService.class);\r
42         {// mockFuture\r
43             ScheduledFuture<?> future = mock(ScheduledFuture.class);\r
44             doReturn(false).when(future).cancel(anyBoolean());\r
45             try {\r
46                 doReturn(mock(Object.class)).when(future).get();\r
47             } catch (Exception e) {\r
48                 throw new RuntimeException(e);\r
49             }\r
50             doReturn(future).when(ses).schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class));\r
51             doReturn(future).when(ses).scheduleWithFixedDelay(any(Runnable.class), anyLong(), anyLong(),\r
52                     any(TimeUnit.class));\r
53 \r
54         }\r
55         doNothing().when(ses).execute(any(Runnable.class));\r
56         doNothing().when(ses).execute(any(ListenableFutureTask.class));\r
57         doReturn(ses).when(instance).getExecutor();\r
58         doNothing().when(instance).close();\r
59 \r
60         doReturn(1).when(instance).getMaxThreadCount();\r
61         return instance;\r
62     }\r
63 \r
64     @Override\r
65     public ObjectName getThreadFactory() {\r
66         return any(ObjectName.class);\r
67     }\r
68 \r
69     @Override\r
70     public void setThreadFactory(ObjectName threadFactory) {\r
71     }\r
72 \r
73     @Override\r
74     public Integer getMaxThreadCount() {\r
75         return 1;\r
76     }\r
77 \r
78     @Override\r
79     public void setMaxThreadCount(Integer maxThreadCount) {\r
80     }\r
81 \r
82 }\r