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 org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.Mock;
15 import org.mockito.MockitoAnnotations;
16 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
18 import org.osgi.framework.Bundle;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.BundleListener;
22 import java.io.InputStream;
23 import java.net.MalformedURLException;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.List;
28 import java.util.regex.Pattern;
30 import static org.junit.Assert.assertEquals;
31 import static org.mockito.Matchers.any;
32 import static org.mockito.Matchers.anyCollectionOf;
33 import static org.mockito.Mockito.doNothing;
34 import static org.mockito.Mockito.doReturn;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.times;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.verifyNoMoreInteractions;
40 public class ExtenderYangTrackerCustomizerTest {
43 private ExtenderYangTracker tested;
45 private MbeParser parser;
47 private YangStoreSnapshotImpl yangStoreSnapshot;
49 private BundleContext bundleContext;
52 public void setUp() throws YangStoreException {
53 MockitoAnnotations.initMocks(this);
54 doNothing().when(bundleContext).addBundleListener(any(BundleListener.class));
55 doReturn(new Bundle[0]).when(bundleContext).getBundles();
56 tested = new ExtenderYangTracker(parser, Optional.<Pattern>absent(), bundleContext);
57 doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
58 anyCollectionOf(InputStream.class));
59 doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
60 doReturn("mock yang store").when(yangStoreSnapshot).toString();
61 doNothing().when(yangStoreSnapshot).close();
62 doReturn(Collections.emptyMap()).when(yangStoreSnapshot).getModuleMap();
66 public void testCache() throws MalformedURLException, YangStoreException,
67 InterruptedException {
68 Bundle bundle = getMockedBundle(5, false);
69 tested.addingBundle(bundle, null);
70 bundle = getMockedBundle(2, false);
71 tested.addingBundle(bundle, null);
72 bundle = getMockedBundle(10, false);
73 tested.addingBundle(bundle, null);
74 YangStoreSnapshot returnedStore;
76 returnedStore = tested.getYangStoreSnapshot();
78 assertEquals(yangStoreSnapshot, returnedStore);
80 tested.removedBundle(bundle, null, null);
81 tested.getYangStoreSnapshot();
83 bundle = getMockedBundle(10, false);
84 tested.addingBundle(bundle, null);
86 for(int i = 0; i< 20; i++){
87 tested.getYangStoreSnapshot();
90 verify(parser, times(7)).parseYangFiles(anyCollectionOf(InputStream.class));
92 returnedStore = tested.getYangStoreSnapshot();
94 verifyNoMoreInteractions(parser);
97 int bundleCounter = 1;
99 private Bundle getMockedBundle(int sizeOfUrls, boolean system)
100 throws MalformedURLException {
101 Bundle mock = mock(Bundle.class);
102 doReturn(32).when(mock).getState();//mock just for logging
104 List<URL> urls = Lists.newArrayList();
105 for (int i = 0; i < sizeOfUrls; i++) {
106 urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
108 Enumeration<URL> abc = new TestEnumeration(urls);
110 doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
112 doReturn(0L).when(mock).getBundleId();
114 doReturn(1L).when(mock).getBundleId();
116 doReturn("mockedBundle").when(mock).toString();
117 doReturn("mockedBundle").when(mock).getSymbolicName();
122 private static final class TestEnumeration implements Enumeration<URL> {
124 private final List<URL> urls;
127 public TestEnumeration(List<URL> urls) {
132 public boolean hasMoreElements() {
134 urls.get(currentPos);
135 } catch (IndexOutOfBoundsException e) {
142 public URL nextElement() {
143 URL url = urls.get(currentPos++);