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.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyNoMoreInteractions;
18 import java.io.InputStream;
19 import java.net.MalformedURLException;
21 import java.util.Enumeration;
22 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
29 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleContext;
33 import com.google.common.collect.Lists;
35 public class ExtenderYangTrackerTest {
38 private BundleContext context;
39 private ExtenderYangTracker tested;
41 private MbeParser parser;
43 private YangStoreSnapshot yangStoreSnapshot;
46 public void setUp() throws YangStoreException {
47 MockitoAnnotations.initMocks(this);
48 doReturn("context").when(context).toString();
49 tested = new ExtenderYangTracker(context, parser);
50 doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
51 anyCollectionOf(InputStream.class));
52 doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
53 doReturn("mock yang store").when(yangStoreSnapshot).toString();
57 public void testCache() throws MalformedURLException, YangStoreException,
58 InterruptedException {
59 Bundle bundle = getMockedBundle(5, false);
60 tested.addingBundle(bundle, null);
61 bundle = getMockedBundle(2, false);
62 tested.addingBundle(bundle, null);
63 bundle = getMockedBundle(10, false);
64 tested.addingBundle(bundle, null);
65 YangStoreSnapshot returnedStore;
67 returnedStore = tested.getYangStoreSnapshot();
69 assertEquals(yangStoreSnapshot, returnedStore);
71 tested.removedBundle(bundle, null, null);
72 tested.getYangStoreSnapshot();
74 bundle = getMockedBundle(10, false);
75 tested.addingBundle(bundle, null);
77 tested.getYangStoreSnapshot();
79 verify(parser, times(3)).parseYangFiles(
80 anyCollectionOf(InputStream.class));
82 returnedStore = tested.getYangStoreSnapshot();
84 verifyNoMoreInteractions(parser);
85 assertEquals(yangStoreSnapshot, returnedStore);
88 int bundleCounter = 1;
90 private Bundle getMockedBundle(int sizeOfUrls, boolean system)
91 throws MalformedURLException {
92 Bundle mock = mock(Bundle.class);
94 List<URL> urls = Lists.newArrayList();
95 for (int i = 0; i < sizeOfUrls; i++) {
96 urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
98 Enumeration<URL> abc = new TestEnumeration(urls);
100 doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
102 doReturn(0L).when(mock).getBundleId();
104 doReturn(1L).when(mock).getBundleId();
106 doReturn("mockedBundle").when(mock).toString();
111 private static final class TestEnumeration implements Enumeration<URL> {
113 private final List<URL> urls;
116 public TestEnumeration(List<URL> urls) {
121 public boolean hasMoreElements() {
123 urls.get(currentPos);
124 } catch (IndexOutOfBoundsException e) {
131 public URL nextElement() {
132 URL url = urls.get(currentPos++);