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
8 package org.opendaylight.bgpcep.config.loader.impl;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doAnswer;
12 import static org.mockito.Mockito.doReturn;
14 import java.nio.file.WatchEvent;
15 import java.nio.file.WatchKey;
16 import java.nio.file.WatchService;
17 import java.util.List;
18 import java.util.concurrent.CopyOnWriteArrayList;
19 import org.checkerframework.checker.lock.qual.GuardedBy;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.bgpcep.config.loader.spi.ConfigFileProcessor;
25 import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
26 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
27 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTestCustomizer;
28 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
30 public abstract class AbstractConfigLoader extends AbstractConcurrentDataBrokerTest {
32 private final List<WatchEvent<?>> eventList = new CopyOnWriteArrayList<>();
33 protected ConfigLoaderImpl configLoader;
35 private WatchService watchService;
37 ConfigFileProcessor processor;
39 private WatchKey watchKey;
41 private WatchEvent<?> watchEvent;
43 private FileWatcher fileWatcher;
44 protected AdapterContext mappingService;
45 protected DOMSchemaService schemaService;
47 public AbstractConfigLoader() {
52 public void setUp() throws Exception {
53 MockitoAnnotations.initMocks(this);
54 doAnswer(invocation -> true).when(this.watchKey).reset();
55 doReturn(this.eventList).when(this.watchKey).pollEvents();
56 doReturn(this.watchKey).when(this.watchService).take();
57 doReturn("watchKey").when(this.watchKey).toString();
58 doReturn("watchService").when(this.watchService).toString();
59 doReturn("watchEvent").when(this.watchEvent).toString();
60 doReturn(getResourceFolder()).when(this.fileWatcher).getPathFile();
61 doReturn(this.watchService).when(this.fileWatcher).getWatchService();
62 doAnswer(invocation -> {
65 }).when(this.processor).loadConfiguration(any());
66 this.configLoader = new ConfigLoaderImpl(getSchemaContext(), this.mappingService.currentSerializer(),
68 this.configLoader.init();
72 protected final AbstractDataBrokerTestCustomizer createDataBrokerTestCustomizer() {
73 final AbstractDataBrokerTestCustomizer customizer = super.createDataBrokerTestCustomizer();
74 this.mappingService = customizer.getAdapterContext();
75 this.schemaService = customizer.getSchemaService();
79 private synchronized void clearEvent() {
80 this.eventList.clear();
83 protected String getResourceFolder() {
84 return ClassLoader.getSystemClassLoader().getResource("initial").getPath();
87 protected synchronized void triggerEvent(final String filename) {
88 doReturn(filename).when(this.watchEvent).context();
89 this.eventList.add(this.watchEvent);
93 public void tearDown() throws Exception {
94 this.configLoader.close();