8a7b95ff3ec47c6cbe2c9f7c2199932fee682624
[controller.git] / opendaylight / config / yang-store-impl / src / test / java / org / opendaylight / controller / config / yang / store / impl / ExtenderYangTrackerCustomizerTest.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.controller.config.yang.store.impl;
9
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Lists;
12 import com.google.common.collect.Maps;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mock;
16 import org.mockito.MockitoAnnotations;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
18 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.BundleListener;
23
24 import java.io.InputStream;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.regex.Pattern;
32
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Matchers.anyCollectionOf;
35 import static org.mockito.Mockito.doNothing;
36 import static org.mockito.Mockito.doReturn;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.verifyNoMoreInteractions;
41
42 public class ExtenderYangTrackerCustomizerTest {
43
44
45     private ExtenderYangTracker tested;
46     @Mock
47     private MbeParser parser;
48     @Mock
49     private YangStoreSnapshotImpl yangStoreSnapshot;
50     @Mock
51     private BundleContext bundleContext;
52
53     private Map<String, Map.Entry<Module, String>> moduleMap = Maps.newHashMap();
54
55     @Before
56     public void setUp() throws YangStoreException {
57
58         moduleMap.put("1", new Map.Entry<Module, String>() {
59             @Override
60             public Module getKey() {
61                 return mock(Module.class);
62             }
63
64             @Override
65             public String getValue() {
66                 return "v";
67             }
68
69             @Override
70             public String setValue(String value) {
71                 return "v";
72             }
73         });
74
75         MockitoAnnotations.initMocks(this);
76         doNothing().when(bundleContext).addBundleListener(any(BundleListener.class));
77         doReturn(new Bundle[0]).when(bundleContext).getBundles();
78         tested = new ExtenderYangTracker(parser, Optional.<Pattern>absent(), bundleContext);
79         doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
80                 anyCollectionOf(InputStream.class));
81         doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
82         doReturn("mock yang store").when(yangStoreSnapshot).toString();
83         doNothing().when(yangStoreSnapshot).close();
84         doReturn(Collections.emptyMap()).when(yangStoreSnapshot).getModuleMXBeanEntryMap();
85         doReturn(Collections.emptyMap()).when(yangStoreSnapshot).getModulesToSources();
86         doReturn(Collections.emptyMap()).when(yangStoreSnapshot).getQNamesToIdentitiesToModuleMXBeanEntries();
87     }
88
89     @Test
90     public void testCache() throws MalformedURLException, YangStoreException,
91             InterruptedException {
92         Bundle bundle = getMockedBundle(5, false);
93         tested.addingBundle(bundle, null);
94         bundle = getMockedBundle(2, false);
95         tested.addingBundle(bundle, null);
96         bundle = getMockedBundle(10, false);
97         tested.addingBundle(bundle, null);
98         YangStoreSnapshot returnedStore;
99
100         returnedStore = tested.getYangStoreSnapshot();
101
102
103         tested.removedBundle(bundle, null, null);
104         tested.getYangStoreSnapshot();
105
106         bundle = getMockedBundle(10, false);
107         tested.addingBundle(bundle, null);
108
109         for(int i = 0; i< 20; i++){
110             tested.getYangStoreSnapshot();
111         }
112
113         verify(parser, times(5)).parseYangFiles(anyCollectionOf(InputStream.class));
114
115         returnedStore = tested.getYangStoreSnapshot();
116
117         verifyNoMoreInteractions(parser);
118     }
119
120     int bundleCounter = 1;
121
122     private Bundle getMockedBundle(int sizeOfUrls, boolean system)
123             throws MalformedURLException {
124         Bundle mock = mock(Bundle.class);
125         doReturn(32).when(mock).getState();//mock just for logging
126
127         List<URL> urls = Lists.newArrayList();
128         for (int i = 0; i < sizeOfUrls; i++) {
129             urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
130         }
131         Enumeration<URL> abc = new TestEnumeration(urls);
132
133         doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
134         if (system)
135             doReturn(0L).when(mock).getBundleId();
136         else
137             doReturn(1L).when(mock).getBundleId();
138
139         doReturn("mockedBundle").when(mock).toString();
140         doReturn("mockedBundle").when(mock).getSymbolicName();
141
142         return mock;
143     }
144
145     private static final class TestEnumeration implements Enumeration<URL> {
146
147         private final List<URL> urls;
148         int currentPos = 0;
149
150         public TestEnumeration(List<URL> urls) {
151             this.urls = urls;
152         }
153
154         @Override
155         public boolean hasMoreElements() {
156             try {
157                 urls.get(currentPos);
158             } catch (IndexOutOfBoundsException e) {
159                 return false;
160             }
161             return true;
162         }
163
164         @Override
165         public URL nextElement() {
166             URL url = urls.get(currentPos++);
167             return url;
168         }
169
170     }
171 }