2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.bgpcep.config.loader.impl;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.doReturn;
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;
36 public abstract class AbstractConfigLoader extends AbstractConcurrentDataBrokerTest {
38 private final List<WatchEvent<?>> eventList = new ArrayList<>();
39 protected ConfigLoaderImpl configLoader;
41 protected WatchService watchService;
43 private WatchKey watchKey;
45 private WatchEvent<?> watchEvent;
47 protected ConfigFileProcessor processor;
49 private FileWatcher fileWatcher;
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 -> {
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();
79 private synchronized void clearEvent() {
80 this.eventList.clear();
83 protected String getResourceFolder() {
84 return ClassLoader.getSystemClassLoader().getResource("initial").getPath();
87 protected abstract void registerModules(ModuleInfoBackedContext moduleInfoBackedContext) throws Exception;
89 protected synchronized void triggerEvent(final String filename) {
90 doReturn(filename).when(this.watchEvent).context();
91 this.eventList.add(this.watchEvent);
95 public final void tearDown() throws Exception {
96 ((ConfigLoaderImpl) this.configLoader).close();