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.protocol.bgp.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.io.InputStream;
16 import java.nio.file.WatchEvent;
17 import java.nio.file.WatchKey;
18 import java.nio.file.WatchService;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import javassist.ClassPool;
23 import javax.annotation.concurrent.GuardedBy;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.mdsal.binding.dom.adapter.BindingToNormalizedNodeCodec;
30 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
31 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
32 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
33 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
34 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
35 import org.opendaylight.protocol.bgp.config.loader.spi.ConfigFileProcessor;
36 import org.opendaylight.protocol.bgp.config.loader.spi.ConfigLoader;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40 public abstract class AbstractConfigLoader {
42 private final List<WatchEvent<?>> eventList = new ArrayList<>();
43 protected BindingToNormalizedNodeCodec mappingService;
44 protected ConfigLoader configLoader;
46 protected WatchService watchService;
48 private WatchKey watchKey;
50 private WatchEvent<?> watchEvent;
52 protected ConfigFileProcessor processor;
55 public void setUp() throws Exception {
56 MockitoAnnotations.initMocks(this);
57 this.mappingService = new BindingToNormalizedNodeCodec(
58 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
59 new BindingNormalizedNodeCodecRegistry(
60 StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()))));
61 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
62 registerModules(moduleInfoBackedContext);
63 this.mappingService.onGlobalContextUpdated(moduleInfoBackedContext.tryToCreateSchemaContext().get());
64 doAnswer(invocation -> true).when(this.watchKey).reset();
65 doReturn(this.eventList).when(this.watchKey).pollEvents();
66 doReturn(this.watchKey).when(this.watchService).take();
67 doReturn("watchKey").when(this.watchKey).toString();
68 doReturn("watchService").when(this.watchService).toString();
69 doReturn("watchEvent").when(this.watchEvent).toString();
70 doAnswer(invocation -> {
73 }).when(this.processor).loadConfiguration(any());
74 final SchemaContext schemaContext = YangParserTestUtils.parseYangStreams(
75 getFilesAsStreams(getYangModelsPaths()));
76 this.configLoader = new ConfigLoaderImpl(schemaContext,
77 this.mappingService, getResourceFolder(), this.watchService);
80 private synchronized void clearEvent() {
81 this.eventList.clear();
84 protected String getResourceFolder() {
85 return ClassLoader.getSystemClassLoader().getResource("initial").getPath();
88 protected abstract void registerModules(ModuleInfoBackedContext moduleInfoBackedContext) throws Exception;
90 protected abstract List<String> getYangModelsPaths();
92 private static List<InputStream> getFilesAsStreams(final List<String> paths) {
93 final List<InputStream> resources = new ArrayList<>();
94 final List<String> failedToFind = new ArrayList<>();
95 for (final String path : paths) {
96 final InputStream is = ConfigLoaderImplTest.class.getResourceAsStream(path);
98 failedToFind.add(path);
103 Assert.assertEquals("Some files were not found", Collections.emptyList(), failedToFind);
107 protected synchronized void triggerEvent(final String filename) {
108 doReturn(filename).when(this.watchEvent).context();
109 this.eventList.add(this.watchEvent);
113 public final void tearDown() throws Exception {
114 ((ConfigLoaderImpl) this.configLoader).close();