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 static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.anyCollectionOf;
12 import static org.mockito.Mockito.*;
14 import java.io.InputStream;
15 import java.net.MalformedURLException;
17 import java.util.Enumeration;
18 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
25 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
26 import org.osgi.framework.Bundle;
27 import org.osgi.framework.BundleContext;
29 import com.google.common.collect.Lists;
31 public class ExtenderYangTrackerCustomizerTest {
34 private ExtenderYangTrackerCustomizer tested;
36 private MbeParser parser;
38 private YangStoreSnapshot yangStoreSnapshot;
41 public void setUp() throws YangStoreException {
42 MockitoAnnotations.initMocks(this);
44 tested = new ExtenderYangTrackerCustomizer(parser);
45 doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
46 anyCollectionOf(InputStream.class));
47 doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
48 doReturn("mock yang store").when(yangStoreSnapshot).toString();
49 doNothing().when(yangStoreSnapshot).close();
53 public void testCache() throws MalformedURLException, YangStoreException,
54 InterruptedException {
55 Bundle bundle = getMockedBundle(5, false);
56 tested.addingBundle(bundle, null);
57 bundle = getMockedBundle(2, false);
58 tested.addingBundle(bundle, null);
59 bundle = getMockedBundle(10, false);
60 tested.addingBundle(bundle, null);
61 YangStoreSnapshot returnedStore;
63 returnedStore = tested.getYangStoreSnapshot();
65 assertEquals(yangStoreSnapshot, returnedStore);
67 tested.removedBundle(bundle, null, null);
68 tested.getYangStoreSnapshot();
70 bundle = getMockedBundle(10, false);
71 tested.addingBundle(bundle, null);
73 for(int i = 0; i< 10; i++){
74 tested.getYangStoreSnapshot();
77 verify(parser, times(5)).parseYangFiles(
78 anyCollectionOf(InputStream.class));
80 returnedStore = tested.getYangStoreSnapshot();
82 verifyNoMoreInteractions(parser);
83 assertEquals(yangStoreSnapshot, returnedStore);
86 int bundleCounter = 1;
88 private Bundle getMockedBundle(int sizeOfUrls, boolean system)
89 throws MalformedURLException {
90 Bundle mock = mock(Bundle.class);
91 doReturn(32).when(mock).getState();//mock just for logging
93 List<URL> urls = Lists.newArrayList();
94 for (int i = 0; i < sizeOfUrls; i++) {
95 urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
97 Enumeration<URL> abc = new TestEnumeration(urls);
99 doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
101 doReturn(0L).when(mock).getBundleId();
103 doReturn(1L).when(mock).getBundleId();
105 doReturn("mockedBundle").when(mock).toString();
110 private static final class TestEnumeration implements Enumeration<URL> {
112 private final List<URL> urls;
115 public TestEnumeration(List<URL> urls) {
120 public boolean hasMoreElements() {
122 urls.get(currentPos);
123 } catch (IndexOutOfBoundsException e) {
130 public URL nextElement() {
131 URL url = urls.get(currentPos++);