X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-store-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fstore%2Fimpl%2FExtenderYangTrackerTest.java;fp=opendaylight%2Fconfig%2Fyang-store-impl%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyang%2Fstore%2Fimpl%2FExtenderYangTrackerTest.java;h=e40d7348a06119d1b62b4082f82a38f1358a63b3;hb=9fb64948564e252018f9b1e13e7cea2c92f991aa;hp=0000000000000000000000000000000000000000;hpb=1742b3894614be478c333a1043ced8ef1bc5dc84;p=controller.git diff --git a/opendaylight/config/yang-store-impl/src/test/java/org/opendaylight/controller/config/yang/store/impl/ExtenderYangTrackerTest.java b/opendaylight/config/yang-store-impl/src/test/java/org/opendaylight/controller/config/yang/store/impl/ExtenderYangTrackerTest.java new file mode 100644 index 0000000000..e40d7348a0 --- /dev/null +++ b/opendaylight/config/yang-store-impl/src/test/java/org/opendaylight/controller/config/yang/store/impl/ExtenderYangTrackerTest.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.config.yang.store.impl; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyCollectionOf; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Enumeration; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.config.yang.store.api.YangStoreException; +import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; + +import com.google.common.collect.Lists; + +public class ExtenderYangTrackerTest { + + @Mock + private BundleContext context; + private ExtenderYangTracker tested; + @Mock + private MbeParser parser; + @Mock + private YangStoreSnapshot yangStoreSnapshot; + + @Before + public void setUp() throws YangStoreException { + MockitoAnnotations.initMocks(this); + doReturn("context").when(context).toString(); + tested = new ExtenderYangTracker(context, parser); + doReturn(yangStoreSnapshot).when(parser).parseYangFiles( + anyCollectionOf(InputStream.class)); + doReturn(22).when(yangStoreSnapshot).countModuleMXBeanEntries(); + doReturn("mock yang store").when(yangStoreSnapshot).toString(); + } + + @Test + public void testCache() throws MalformedURLException, YangStoreException, + InterruptedException { + Bundle bundle = getMockedBundle(5, false); + tested.addingBundle(bundle, null); + bundle = getMockedBundle(2, false); + tested.addingBundle(bundle, null); + bundle = getMockedBundle(10, false); + tested.addingBundle(bundle, null); + YangStoreSnapshot returnedStore; + + returnedStore = tested.getYangStoreSnapshot(); + + assertEquals(yangStoreSnapshot, returnedStore); + + tested.removedBundle(bundle, null, null); + tested.getYangStoreSnapshot(); + + bundle = getMockedBundle(10, false); + tested.addingBundle(bundle, null); + + tested.getYangStoreSnapshot(); + + verify(parser, times(3)).parseYangFiles( + anyCollectionOf(InputStream.class)); + + returnedStore = tested.getYangStoreSnapshot(); + + verifyNoMoreInteractions(parser); + assertEquals(yangStoreSnapshot, returnedStore); + } + + int bundleCounter = 1; + + private Bundle getMockedBundle(int sizeOfUrls, boolean system) + throws MalformedURLException { + Bundle mock = mock(Bundle.class); + + List urls = Lists.newArrayList(); + for (int i = 0; i < sizeOfUrls; i++) { + urls.add(new URL("http://127.0." + bundleCounter++ + "." + i)); + } + Enumeration abc = new TestEnumeration(urls); + + doReturn(abc).when(mock).findEntries("META-INF/yang", "*.yang", false); + if (system) + doReturn(0L).when(mock).getBundleId(); + else + doReturn(1L).when(mock).getBundleId(); + + doReturn("mockedBundle").when(mock).toString(); + + return mock; + } + + private static final class TestEnumeration implements Enumeration { + + private final List urls; + int currentPos = 0; + + public TestEnumeration(List urls) { + this.urls = urls; + } + + @Override + public boolean hasMoreElements() { + try { + urls.get(currentPos); + } catch (IndexOutOfBoundsException e) { + return false; + } + return true; + } + + @Override + public URL nextElement() { + URL url = urls.get(currentPos++); + return url; + } + + } +}