2 * Copyright (c) 2013 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.controller.config.yang.store.impl;
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;
24 import java.io.InputStream;
25 import java.net.MalformedURLException;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.List;
31 import java.util.regex.Pattern;
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;
42 public class ExtenderYangTrackerCustomizerTest {
45 private ExtenderYangTracker tested;
47 private MbeParser parser;
49 private YangStoreSnapshotImpl yangStoreSnapshot;
51 private BundleContext bundleContext;
53 private Map<String, Map.Entry<Module, String>> moduleMap = Maps.newHashMap();
56 public void setUp() throws YangStoreException {
58 moduleMap.put("1", new Map.Entry<Module, String>() {
60 public Module getKey() {
61 return mock(Module.class);
65 public String getValue() {
70 public String setValue(String value) {
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();
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;
100 returnedStore = tested.getYangStoreSnapshot();
103 tested.removedBundle(bundle, null, null);
104 tested.getYangStoreSnapshot();
106 bundle = getMockedBundle(10, false);
107 tested.addingBundle(bundle, null);
109 for(int i = 0; i< 20; i++){
110 tested.getYangStoreSnapshot();
113 verify(parser, times(5)).parseYangFiles(anyCollectionOf(InputStream.class));
115 returnedStore = tested.getYangStoreSnapshot();
117 verifyNoMoreInteractions(parser);
120 int bundleCounter = 1;
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
127 List<URL> urls = Lists.newArrayList();
128 for (int i = 0; i < sizeOfUrls; i++) {
129 urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
131 Enumeration<URL> abc = new TestEnumeration(urls);
133 doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
135 doReturn(0L).when(mock).getBundleId();
137 doReturn(1L).when(mock).getBundleId();
139 doReturn("mockedBundle").when(mock).toString();
140 doReturn("mockedBundle").when(mock).getSymbolicName();
145 private static final class TestEnumeration implements Enumeration<URL> {
147 private final List<URL> urls;
150 public TestEnumeration(List<URL> urls) {
155 public boolean hasMoreElements() {
157 urls.get(currentPos);
158 } catch (IndexOutOfBoundsException e) {
165 public URL nextElement() {
166 URL url = urls.get(currentPos++);