Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / yang-store-impl / src / test / java / org / opendaylight / controller / config / yang / store / impl / ExtenderYangTrackerTest.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.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyNoMoreInteractions;
17
18 import java.io.InputStream;
19 import java.net.MalformedURLException;
20 import java.net.URL;
21 import java.util.Enumeration;
22 import java.util.List;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
29 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleContext;
32
33 import com.google.common.collect.Lists;
34
35 public class ExtenderYangTrackerTest {
36
37     @Mock
38     private BundleContext context;
39     private ExtenderYangTracker tested;
40     @Mock
41     private MbeParser parser;
42     @Mock
43     private YangStoreSnapshot yangStoreSnapshot;
44
45     @Before
46     public void setUp() throws YangStoreException {
47         MockitoAnnotations.initMocks(this);
48         doReturn("context").when(context).toString();
49         tested = new ExtenderYangTracker(context, parser);
50         doReturn(yangStoreSnapshot).when(parser).parseYangFiles(
51                 anyCollectionOf(InputStream.class));
52         doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries();
53         doReturn("mock yang store").when(yangStoreSnapshot).toString();
54     }
55
56     @Test
57     public void testCache() throws MalformedURLException, YangStoreException,
58             InterruptedException {
59         Bundle bundle = getMockedBundle(5, false);
60         tested.addingBundle(bundle, null);
61         bundle = getMockedBundle(2, false);
62         tested.addingBundle(bundle, null);
63         bundle = getMockedBundle(10, false);
64         tested.addingBundle(bundle, null);
65         YangStoreSnapshot returnedStore;
66
67         returnedStore = tested.getYangStoreSnapshot();
68
69         assertEquals(yangStoreSnapshot, returnedStore);
70
71         tested.removedBundle(bundle, null, null);
72         tested.getYangStoreSnapshot();
73
74         bundle = getMockedBundle(10, false);
75         tested.addingBundle(bundle, null);
76
77         tested.getYangStoreSnapshot();
78
79         verify(parser, times(3)).parseYangFiles(
80                 anyCollectionOf(InputStream.class));
81
82         returnedStore = tested.getYangStoreSnapshot();
83
84         verifyNoMoreInteractions(parser);
85         assertEquals(yangStoreSnapshot, returnedStore);
86     }
87
88     int bundleCounter = 1;
89
90     private Bundle getMockedBundle(int sizeOfUrls, boolean system)
91             throws MalformedURLException {
92         Bundle mock = mock(Bundle.class);
93
94         List<URL> urls = Lists.newArrayList();
95         for (int i = 0; i < sizeOfUrls; i++) {
96             urls.add(new URL("http://127.0." + bundleCounter++ + "." + i));
97         }
98         Enumeration<URL> abc = new TestEnumeration(urls);
99
100         doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false);
101         if (system)
102             doReturn(0L).when(mock).getBundleId();
103         else
104             doReturn(1L).when(mock).getBundleId();
105
106         doReturn("mockedBundle").when(mock).toString();
107
108         return mock;
109     }
110
111     private static final class TestEnumeration implements Enumeration<URL> {
112
113         private final List<URL> urls;
114         int currentPos = 0;
115
116         public TestEnumeration(List<URL> urls) {
117             this.urls = urls;
118         }
119
120         @Override
121         public boolean hasMoreElements() {
122             try {
123                 urls.get(currentPos);
124             } catch (IndexOutOfBoundsException e) {
125                 return false;
126             }
127             return true;
128         }
129
130         @Override
131         public URL nextElement() {
132             URL url = urls.get(currentPos++);
133             return url;
134         }
135
136     }
137 }