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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.