Add change notification functionality to yang-store.
[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 static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.anyCollectionOf;
12 import static org.mockito.Mockito.*;
13
14 import java.io.InputStream;
15 import java.net.MalformedURLException;
16 import java.net.URL;
17 import java.util.Enumeration;
18 import java.util.List;
19
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;
28
29 import com.google.common.collect.Lists;
30
31 public class ExtenderYangTrackerCustomizerTest {
32
33
34     private ExtenderYangTrackerCustomizer tested;
35     @Mock
36     private MbeParser parser;
37     @Mock
38     private YangStoreSnapshot yangStoreSnapshot;
39
40     @Before
41     public void setUp() throws YangStoreException {
42         MockitoAnnotations.initMocks(this);
43
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();
50     }
51
52     @Test
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;
62
63         returnedStore = tested.getYangStoreSnapshot();
64
65         assertEquals(yangStoreSnapshot, returnedStore);
66
67         tested.removedBundle(bundle, null, null);
68         tested.getYangStoreSnapshot();
69
70         bundle = getMockedBundle(10, false);
71         tested.addingBundle(bundle, null);
72
73         for(int i = 0; i< 10; i++){
74             tested.getYangStoreSnapshot();
75         }
76
77         verify(parser, times(5)).parseYangFiles(
78                 anyCollectionOf(InputStream.class));
79
80         returnedStore = tested.getYangStoreSnapshot();
81
82         verifyNoMoreInteractions(parser);
83         assertEquals(yangStoreSnapshot, returnedStore);
84     }
85
86     int bundleCounter = 1;
87
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
92
93         List<URL> urls = Lists.newArrayList();
94         for (int i = 0; i < sizeOfUrls; i++) {
95             urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
96         }
97         Enumeration<URL> abc = new TestEnumeration(urls);
98
99         doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
100         if (system)
101             doReturn(0L).when(mock).getBundleId();
102         else
103             doReturn(1L).when(mock).getBundleId();
104
105         doReturn("mockedBundle").when(mock).toString();
106
107         return mock;
108     }
109
110     private static final class TestEnumeration implements Enumeration<URL> {
111
112         private final List<URL> urls;
113         int currentPos = 0;
114
115         public TestEnumeration(List<URL> urls) {
116             this.urls = urls;
117         }
118
119         @Override
120         public boolean hasMoreElements() {
121             try {
122                 urls.get(currentPos);
123             } catch (IndexOutOfBoundsException e) {
124                 return false;
125             }
126             return true;
127         }
128
129         @Override
130         public URL nextElement() {
131             URL url = urls.get(currentPos++);
132             return url;
133         }
134
135     }
136 }