Bug 164
[controller.git] / opendaylight / config / yang-store-impl / src / test / java / org / opendaylight / controller / config / yang / store / impl / ExtenderYangTrackerCustomizerTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.store.impl;
9
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;
21
22 import java.io.InputStream;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.List;
28 import java.util.regex.Pattern;
29
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;
39
40 public class ExtenderYangTrackerCustomizerTest {
41
42
43     private ExtenderYangTracker tested;
44     @Mock
45     private MbeParser parser;
46     @Mock
47     private YangStoreSnapshotImpl yangStoreSnapshot;
48     @Mock
49     private BundleContext bundleContext;
50
51     @Before
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();
63     }
64
65     @Test
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;
75
76         returnedStore = tested.getYangStoreSnapshot();
77
78         assertEquals(yangStoreSnapshot, returnedStore);
79
80         tested.removedBundle(bundle, null, null);
81         tested.getYangStoreSnapshot();
82
83         bundle = getMockedBundle(10, false);
84         tested.addingBundle(bundle, null);
85
86         for(int i = 0; i< 20; i++){
87             tested.getYangStoreSnapshot();
88         }
89
90         verify(parser, times(7)).parseYangFiles(anyCollectionOf(InputStream.class));
91
92         returnedStore = tested.getYangStoreSnapshot();
93
94         verifyNoMoreInteractions(parser);
95     }
96
97     int bundleCounter = 1;
98
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
103
104         List<URL> urls = Lists.newArrayList();
105         for (int i = 0; i < sizeOfUrls; i++) {
106             urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
107         }
108         Enumeration<URL> abc = new TestEnumeration(urls);
109
110         doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
111         if (system)
112             doReturn(0L).when(mock).getBundleId();
113         else
114             doReturn(1L).when(mock).getBundleId();
115
116         doReturn("mockedBundle").when(mock).toString();
117         doReturn("mockedBundle").when(mock).getSymbolicName();
118
119         return mock;
120     }
121
122     private static final class TestEnumeration implements Enumeration<URL> {
123
124         private final List<URL> urls;
125         int currentPos = 0;
126
127         public TestEnumeration(List<URL> urls) {
128             this.urls = urls;
129         }
130
131         @Override
132         public boolean hasMoreElements() {
133             try {
134                 urls.get(currentPos);
135             } catch (IndexOutOfBoundsException e) {
136                 return false;
137             }
138             return true;
139         }
140
141         @Override
142         public URL nextElement() {
143             URL url = urls.get(currentPos++);
144             return url;
145         }
146
147     }
148 }