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