use thread-safe list in ConfigLoaderImplTest
[bgpcep.git] / config-loader / config-loader-impl / src / test / java / org / opendaylight / bgpcep / 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.bgpcep.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.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;
31
32 public abstract class AbstractConfigLoader extends AbstractConcurrentDataBrokerTest {
33     @GuardedBy("this")
34     private final List<WatchEvent<?>> eventList = new CopyOnWriteArrayList<>();
35     protected ConfigLoaderImpl configLoader;
36     @Mock
37     private WatchService watchService;
38     @Mock
39     ConfigFileProcessor processor;
40     @Mock
41     private WatchKey watchKey;
42     @Mock
43     private WatchEvent<?> watchEvent;
44     @Mock
45     private FileWatcher fileWatcher;
46     protected BindingToNormalizedNodeCodec mappingService;
47     protected BindingCodecTreeFactory bindingCodecTreeFactory;
48     protected DOMSchemaService schemaService;
49
50     @Before
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 -> {
62             clearEvent();
63             return null;
64         }).when(this.processor).loadConfiguration(any());
65         this.configLoader = new ConfigLoaderImpl(getSchemaContext(), this.mappingService, this.fileWatcher);
66         this.configLoader.init();
67     }
68
69     @Override
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();
75         return customizer;
76     }
77
78     private synchronized void clearEvent() {
79         this.eventList.clear();
80     }
81
82     protected String getResourceFolder() {
83         return ClassLoader.getSystemClassLoader().getResource("initial").getPath();
84     }
85
86     protected synchronized void triggerEvent(final String filename) {
87         doReturn(filename).when(this.watchEvent).context();
88         this.eventList.add(this.watchEvent);
89     }
90
91     @After
92     public void tearDown() throws Exception {
93         this.configLoader.close();
94     }
95 }