032e94a56a5b260dc1ff31cbe60ff5941f9ca81b
[bgpcep.git] / config-loader / config-loader-impl / src / test / java / org / opendaylight / bgpcep / config / loader / impl / AbstractConfigLoader.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.bgpcep.config.loader.impl;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doReturn;
14
15 import java.nio.file.WatchEvent;
16 import java.nio.file.WatchKey;
17 import java.nio.file.WatchService;
18 import java.util.ArrayList;
19 import java.util.List;
20 import javassist.ClassPool;
21 import javax.annotation.concurrent.GuardedBy;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.bgpcep.config.loader.spi.ConfigFileProcessor;
27 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
28 import org.opendaylight.mdsal.binding.dom.adapter.BindingToNormalizedNodeCodec;
29 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
30 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
31 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
32 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
33 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35
36 public abstract class AbstractConfigLoader extends AbstractConcurrentDataBrokerTest {
37     @GuardedBy("this")
38     private final List<WatchEvent<?>> eventList = new ArrayList<>();
39     protected ConfigLoaderImpl configLoader;
40     @Mock
41     protected WatchService watchService;
42     @Mock
43     private WatchKey watchKey;
44     @Mock
45     private WatchEvent<?> watchEvent;
46     @Mock
47     protected ConfigFileProcessor processor;
48     @Mock
49     private FileWatcher fileWatcher;
50
51     @Before
52     public void setUp() throws Exception {
53         MockitoAnnotations.initMocks(this);
54         final BindingToNormalizedNodeCodec mappingService = new BindingToNormalizedNodeCodec(
55                 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
56                 new BindingNormalizedNodeCodecRegistry(
57                         StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()))));
58         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
59         registerModules(moduleInfoBackedContext);
60         mappingService.onGlobalContextUpdated(moduleInfoBackedContext.tryToCreateSchemaContext().get());
61         doAnswer(invocation -> true).when(this.watchKey).reset();
62         doReturn(this.eventList).when(this.watchKey).pollEvents();
63         doReturn(this.watchKey).when(this.watchService).take();
64         doReturn("watchKey").when(this.watchKey).toString();
65         doReturn("watchService").when(this.watchService).toString();
66         doReturn("watchEvent").when(this.watchEvent).toString();
67         doReturn(getResourceFolder()).when(this.fileWatcher).getPathFile();
68         doReturn(this.watchService).when(this.fileWatcher).getWatchService();
69         doAnswer(invocation -> {
70             clearEvent();
71             return null;
72         }).when(this.processor).loadConfiguration(any());
73         final SchemaContext schemaContext = getSchemaContext();
74         this.configLoader = new ConfigLoaderImpl(schemaContext,
75                 mappingService, this.fileWatcher);
76         this.configLoader.init();
77     }
78
79     private synchronized void clearEvent() {
80         this.eventList.clear();
81     }
82
83     protected String getResourceFolder() {
84         return ClassLoader.getSystemClassLoader().getResource("initial").getPath();
85     }
86
87     protected abstract void registerModules(ModuleInfoBackedContext moduleInfoBackedContext) throws Exception;
88
89     protected synchronized void triggerEvent(final String filename) {
90         doReturn(filename).when(this.watchEvent).context();
91         this.eventList.add(this.watchEvent);
92     }
93
94
95     @After
96     public final void tearDown() throws Exception {
97         ((ConfigLoaderImpl) this.configLoader).close();
98     }
99 }