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;
28 import com.google.common.collect.Lists;
30 public class ExtenderYangTrackerCustomizerTest {
33 private ExtenderYangTrackerCustomizer tested;
35 private MbeParser parser;
37 private YangStoreSnapshot yangStoreSnapshot;
40 public void setUp() throws YangStoreException {
41 MockitoAnnotations.initMocks(this);
43 tested = new ExtenderYangTrackerCustomizer(parser);
44 doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
45 anyCollectionOf(InputStream.class));
46 doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
47 doReturn("mock yang store").when(yangStoreSnapshot).toString();
48 doNothing().when(yangStoreSnapshot).close();
52 public void testCache() throws MalformedURLException, YangStoreException,
53 InterruptedException {
54 Bundle bundle = getMockedBundle(5, false);
55 tested.addingBundle(bundle, null);
56 bundle = getMockedBundle(2, false);
57 tested.addingBundle(bundle, null);
58 bundle = getMockedBundle(10, false);
59 tested.addingBundle(bundle, null);
60 YangStoreSnapshot returnedStore;
62 returnedStore = tested.getYangStoreSnapshot();
64 assertEquals(yangStoreSnapshot, returnedStore);
66 tested.removedBundle(bundle, null, null);
67 tested.getYangStoreSnapshot();
69 bundle = getMockedBundle(10, false);
70 tested.addingBundle(bundle, null);
72 for(int i = 0; i< 10; i++){
73 tested.getYangStoreSnapshot();
76 verify(parser, times(5)).parseYangFiles(
77 anyCollectionOf(InputStream.class));
79 returnedStore = tested.getYangStoreSnapshot();
81 verifyNoMoreInteractions(parser);
82 assertEquals(yangStoreSnapshot, returnedStore);
85 int bundleCounter = 1;
87 private Bundle getMockedBundle(int sizeOfUrls, boolean system)
88 throws MalformedURLException {
89 Bundle mock = mock(Bundle.class);
90 doReturn(32).when(mock).getState();//mock just for logging
92 List<URL> urls = Lists.newArrayList();
93 for (int i = 0; i < sizeOfUrls; i++) {
94 urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
96 Enumeration<URL> abc = new TestEnumeration(urls);
98 doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
100 doReturn(0L).when(mock).getBundleId();
102 doReturn(1L).when(mock).getBundleId();
104 doReturn("mockedBundle").when(mock).toString();
109 private static final class TestEnumeration implements Enumeration<URL> {
111 private final List<URL> urls;
114 public TestEnumeration(List<URL> urls) {
119 public boolean hasMoreElements() {
121 urls.get(currentPos);
122 } catch (IndexOutOfBoundsException e) {
129 public URL nextElement() {
130 URL url = urls.get(currentPos++);