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.List;
19 import java.util.concurrent.CopyOnWriteArrayList;
20 import javax.annotation.concurrent.GuardedBy;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.bgpcep.config.loader.spi.ConfigFileProcessor;
26 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
27 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
28 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTestCustomizer;
29 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 public abstract class AbstractConfigLoader extends AbstractConcurrentDataBrokerTest {
34 private final List<WatchEvent<?>> eventList = new CopyOnWriteArrayList<>();
35 protected ConfigLoaderImpl configLoader;
37 private WatchService watchService;
39 ConfigFileProcessor processor;
41 private WatchKey watchKey;
43 private WatchEvent<?> watchEvent;
45 private FileWatcher fileWatcher;
46 protected BindingToNormalizedNodeCodec mappingService;
47 protected BindingCodecTreeFactory bindingCodecTreeFactory;
48 protected DOMSchemaService schemaService;
51 public void setUp() throws Exception {
52 MockitoAnnotations.initMocks(this);
53 doAnswer(invocation -> true).when(this.watchKey).reset();
54 doReturn(this.eventList).when(this.watchKey).pollEvents();
55 doReturn(this.watchKey).when(this.watchService).take();
56 doReturn("watchKey").when(this.watchKey).toString();
57 doReturn("watchService").when(this.watchService).toString();
58 doReturn("watchEvent").when(this.watchEvent).toString();
59 doReturn(getResourceFolder()).when(this.fileWatcher).getPathFile();
60 doReturn(this.watchService).when(this.fileWatcher).getWatchService();
61 doAnswer(invocation -> {
64 }).when(this.processor).loadConfiguration(any());
65 this.configLoader = new ConfigLoaderImpl(getSchemaContext(), this.mappingService, this.fileWatcher);
66 this.configLoader.init();
70 protected final AbstractDataBrokerTestCustomizer createDataBrokerTestCustomizer() {
71 final AbstractDataBrokerTestCustomizer customizer = super.createDataBrokerTestCustomizer();
72 this.mappingService = customizer.getBindingToNormalized();
73 this.bindingCodecTreeFactory = customizer.getBindingToNormalized();
74 this.schemaService = customizer.getSchemaService();
78 private synchronized void clearEvent() {
79 this.eventList.clear();
82 protected String getResourceFolder() {
83 return ClassLoader.getSystemClassLoader().getResource("initial").getPath();
86 protected synchronized void triggerEvent(final String filename) {
87 doReturn(filename).when(this.watchEvent).context();
88 this.eventList.add(this.watchEvent);
92 public void tearDown() throws Exception {
93 this.configLoader.close();