Merge "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 com.google.common.collect.Maps;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mock;
16 import org.mockito.MockitoAnnotations;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
18 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.BundleListener;
23
24 import java.io.InputStream;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.Collections;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.regex.Pattern;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.mockito.Matchers.any;
35 import static org.mockito.Matchers.anyCollectionOf;
36 import static org.mockito.Mockito.doNothing;
37 import static org.mockito.Mockito.doReturn;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.times;
40 import static org.mockito.Mockito.verify;
41 import static org.mockito.Mockito.verifyNoMoreInteractions;
42
43 public class ExtenderYangTrackerCustomizerTest {
44
45
46     private ExtenderYangTracker tested;
47     @Mock
48     private MbeParser parser;
49     @Mock
50     private YangStoreSnapshotImpl yangStoreSnapshot;
51     @Mock
52     private BundleContext bundleContext;
53
54     private Map<String, Map.Entry<Module, String>> moduleMap = Maps.newHashMap();
55
56     @Before
57     public void setUp() throws YangStoreException {
58
59         moduleMap.put("1", new Map.Entry<Module, String>() {
60             @Override
61             public Module getKey() {
62                 return mock(Module.class);
63             }
64
65             @Override
66             public String getValue() {
67                 return "v";
68             }
69
70             @Override
71             public String setValue(String value) {
72                 return "v";
73             }
74         });
75
76         MockitoAnnotations.initMocks(this);
77         doNothing().when(bundleContext).addBundleListener(any(BundleListener.class));
78         doReturn(new Bundle[0]).when(bundleContext).getBundles();
79         tested = new ExtenderYangTracker(parser, Optional.<Pattern>absent(), bundleContext);
80         doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
81                 anyCollectionOf(InputStream.class));
82         doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
83         doReturn("mock yang store").when(yangStoreSnapshot).toString();
84         doNothing().when(yangStoreSnapshot).close();
85         doReturn(moduleMap).when(yangStoreSnapshot).getModuleMap();
86         doReturn(Collections.emptyMap()).when(yangStoreSnapshot).getModuleMXBeanEntryMap();
87     }
88
89     @Test
90     public void testCache() throws MalformedURLException, YangStoreException,
91             InterruptedException {
92         Bundle bundle = getMockedBundle(5, false);
93         tested.addingBundle(bundle, null);
94         bundle = getMockedBundle(2, false);
95         tested.addingBundle(bundle, null);
96         bundle = getMockedBundle(10, false);
97         tested.addingBundle(bundle, null);
98         YangStoreSnapshot returnedStore;
99
100         returnedStore = tested.getYangStoreSnapshot();
101
102         assertEquals(yangStoreSnapshot.getModuleMap(), returnedStore.getModuleMap());
103
104         tested.removedBundle(bundle, null, null);
105         tested.getYangStoreSnapshot();
106
107         bundle = getMockedBundle(10, false);
108         tested.addingBundle(bundle, null);
109
110         for(int i = 0; i< 20; i++){
111             tested.getYangStoreSnapshot();
112         }
113
114         verify(parser, times(5)).parseYangFiles(anyCollectionOf(InputStream.class));
115
116         returnedStore = tested.getYangStoreSnapshot();
117
118         verifyNoMoreInteractions(parser);
119     }
120
121     int bundleCounter = 1;
122
123     private Bundle getMockedBundle(int sizeOfUrls, boolean system)
124             throws MalformedURLException {
125         Bundle mock = mock(Bundle.class);
126         doReturn(32).when(mock).getState();//mock just for logging
127
128         List<URL> urls = Lists.newArrayList();
129         for (int i = 0; i < sizeOfUrls; i++) {
130             urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
131         }
132         Enumeration<URL> abc = new TestEnumeration(urls);
133
134         doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
135         if (system)
136             doReturn(0L).when(mock).getBundleId();
137         else
138             doReturn(1L).when(mock).getBundleId();
139
140         doReturn("mockedBundle").when(mock).toString();
141         doReturn("mockedBundle").when(mock).getSymbolicName();
142
143         return mock;
144     }
145
146     private static final class TestEnumeration implements Enumeration<URL> {
147
148         private final List<URL> urls;
149         int currentPos = 0;
150
151         public TestEnumeration(List<URL> urls) {
152             this.urls = urls;
153         }
154
155         @Override
156         public boolean hasMoreElements() {
157             try {
158                 urls.get(currentPos);
159             } catch (IndexOutOfBoundsException e) {
160                 return false;
161             }
162             return true;
163         }
164
165         @Override
166         public URL nextElement() {
167             URL url = urls.get(currentPos++);
168             return url;
169         }
170
171     }
172 }