Switch ConfigLoader to mdsal-binding-runtime
[bgpcep.git] / config-loader / config-loader-impl / src / test / java / org / opendaylight / bgpcep / config / loader / impl / OSGiConfigLoaderTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, 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 package org.opendaylight.bgpcep.config.loader.impl;
9
10 import static org.mockito.Mockito.doAnswer;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.doThrow;
13 import static org.mockito.Mockito.timeout;
14 import static org.mockito.Mockito.verify;
15
16 import java.nio.file.WatchEvent;
17 import java.nio.file.WatchKey;
18 import java.nio.file.WatchService;
19 import java.util.List;
20 import org.junit.After;
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.mdsal.binding.runtime.api.BindingRuntimeContext;
27
28 @RunWith(MockitoJUnitRunner.StrictStubs.class)
29 public class OSGiConfigLoaderTest {
30     @Mock
31     private FileWatcher watcher;
32     @Mock
33     private WatchService watchService;
34     @Mock
35     private WatchKey watchKey;
36     @Mock
37     private WatchEvent<?> watchEvent;
38     @Mock
39     private BindingRuntimeContext bindingContext;
40
41     private OSGiConfigLoader loader;
42
43     @Before
44     public void before() throws InterruptedException {
45         doReturn(watchService).when(watcher).getWatchService();
46         doReturn("foo").when(watcher).getPathFile();
47         doReturn(watchKey).when(watchService).take();
48         doAnswer(inv -> {
49             doThrow(new RuntimeException("enough!")).when(watchKey).pollEvents();
50             return List.of(watchEvent);
51         }).when(watchKey).pollEvents();
52         doReturn("watchEvent").when(watchEvent).context();
53         doReturn(true).when(watchKey).reset();
54
55         loader = new OSGiConfigLoader();
56         loader.watcher = watcher;
57         loader.runtimeContext = bindingContext;
58     }
59
60     @After
61     public void after() {
62         loader.deactivate();
63     }
64
65     @Test
66     public void testSimpleConfigLoader() {
67         loader.activate();
68         verify(watchKey, timeout(10000)).reset();
69     }
70 }