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