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.collect.Lists;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.Mock;
14 import org.mockito.MockitoAnnotations;
15 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
16 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
17 import org.osgi.framework.Bundle;
19 import java.io.InputStream;
20 import java.net.MalformedURLException;
22 import java.util.Enumeration;
23 import java.util.List;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.Matchers.anyCollectionOf;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyNoMoreInteractions;
34 public class ExtenderYangTrackerCustomizerTest {
37 private ExtenderYangTrackerCustomizer tested;
39 private MbeParser parser;
41 private YangStoreSnapshot yangStoreSnapshot;
44 public void setUp() throws YangStoreException {
45 MockitoAnnotations.initMocks(this);
47 tested = new ExtenderYangTrackerCustomizer(parser);
48 doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
49 anyCollectionOf(InputStream.class));
50 doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
51 doReturn("mock yang store").when(yangStoreSnapshot).toString();
52 doNothing().when(yangStoreSnapshot).close();
56 public void testCache() throws MalformedURLException, YangStoreException,
57 InterruptedException {
58 Bundle bundle = getMockedBundle(5, false);
59 tested.addingBundle(bundle, null);
60 bundle = getMockedBundle(2, false);
61 tested.addingBundle(bundle, null);
62 bundle = getMockedBundle(10, false);
63 tested.addingBundle(bundle, null);
64 YangStoreSnapshot returnedStore;
66 returnedStore = tested.getYangStoreSnapshot();
68 assertEquals(yangStoreSnapshot, returnedStore);
70 tested.removedBundle(bundle, null, null);
71 tested.getYangStoreSnapshot();
73 bundle = getMockedBundle(10, false);
74 tested.addingBundle(bundle, null);
76 for(int i = 0; i< 10; i++){
77 tested.getYangStoreSnapshot();
80 verify(parser, times(5)).parseYangFiles(
81 anyCollectionOf(InputStream.class));
83 returnedStore = tested.getYangStoreSnapshot();
85 verifyNoMoreInteractions(parser);
86 assertEquals(yangStoreSnapshot, returnedStore);
89 int bundleCounter = 1;
91 private Bundle getMockedBundle(int sizeOfUrls, boolean system)
92 throws MalformedURLException {
93 Bundle mock = mock(Bundle.class);
94 doReturn(32).when(mock).getState();//mock just for logging
96 List<URL> urls = Lists.newArrayList();
97 for (int i = 0; i < sizeOfUrls; i++) {
98 urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
100 Enumeration<URL> abc = new TestEnumeration(urls);
102 doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
104 doReturn(0L).when(mock).getBundleId();
106 doReturn(1L).when(mock).getBundleId();
108 doReturn("mockedBundle").when(mock).toString();
113 private static final class TestEnumeration implements Enumeration<URL> {
115 private final List<URL> urls;
118 public TestEnumeration(List<URL> urls) {
123 public boolean hasMoreElements() {
125 urls.get(currentPos);
126 } catch (IndexOutOfBoundsException e) {
133 public URL nextElement() {
134 URL url = urls.get(currentPos++);