BGPCEP-580: Implement PCEP stats DS rendering
[bgpcep.git] / config-loader / config-loader-impl / src / test / java / org / opendaylight / protocol / bgp / 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.protocol.bgp.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.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;
39
40 public abstract class AbstractConfigLoader {
41     @GuardedBy("this")
42     private final List<WatchEvent<?>> eventList = new ArrayList<>();
43     protected BindingToNormalizedNodeCodec mappingService;
44     protected ConfigLoader configLoader;
45     @Mock
46     protected WatchService watchService;
47     @Mock
48     private WatchKey watchKey;
49     @Mock
50     private WatchEvent<?> watchEvent;
51     @Mock
52     protected ConfigFileProcessor processor;
53
54     @Before
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 -> {
71             clearEvent();
72             return null;
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);
78     }
79
80     private synchronized void clearEvent() {
81         this.eventList.clear();
82     }
83
84     protected String getResourceFolder() {
85         return ClassLoader.getSystemClassLoader().getResource("initial").getPath();
86     }
87
88     protected abstract void registerModules(ModuleInfoBackedContext moduleInfoBackedContext) throws Exception;
89
90     protected abstract List<String> getYangModelsPaths();
91
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);
97             if (is == null) {
98                 failedToFind.add(path);
99             } else {
100                 resources.add(is);
101             }
102         }
103         Assert.assertEquals("Some files were not found", Collections.emptyList(), failedToFind);
104         return resources;
105     }
106
107     protected synchronized void triggerEvent(final String filename) {
108         doReturn(filename).when(this.watchEvent).context();
109         this.eventList.add(this.watchEvent);
110     }
111
112     @After
113     public final void tearDown() throws Exception {
114         ((ConfigLoaderImpl) this.configLoader).close();
115     }
116 }