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