Refactor YangLibProviderTest
[netconf.git] / netconf / yanglib / src / test / java / org / opendaylight / yanglib / impl / YangLibProviderTest.java
index 516767165f0212d9be9930091a9daecd18f4bd3a..8430e5b63b849096a421ea67561b8deaf89de4b1 100644 (file)
@@ -7,49 +7,55 @@
  */
 package org.opendaylight.yanglib.impl;
 
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.times;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
 
-import com.google.common.util.concurrent.Futures;
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import org.apache.commons.io.FileUtils;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
-import org.mockito.ArgumentCaptor;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.WriteTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesState;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.ModulesStateBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.OptionalRevision;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.RevisionIdentifier;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.Module;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev160409.module.list.ModuleKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.LegacyRevisionUtils;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesState;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.ModulesStateBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.RevisionIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.CommonLeafs.Revision;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.Module;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.module.list.ModuleKey;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.YangIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.yanglib.impl.rev141210.YanglibConfigBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangIRSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.api.YinSchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
-import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
-import org.opendaylight.yangtools.yang.parser.util.ASTSchemaSource;
+import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
+import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class YangLibProviderTest {
-
-    @Mock
-    private BindingAwareBroker.ProviderContext context;
+    private static final File CACHE_DIR = new File("target/yanglib");
 
     @Mock
     private DataBroker dataBroker;
@@ -57,176 +63,154 @@ public class YangLibProviderTest {
     @Mock
     private WriteTransaction writeTransaction;
 
-    private TestingYangLibProvider yangLibProvider;
+    private YangLibProvider yangLibProvider;
+
+    @BeforeClass
+    public static void staticSetup() {
+        if (!CACHE_DIR.exists() && !CACHE_DIR.mkdirs()) {
+            throw new RuntimeException("Failed to create " + CACHE_DIR);
+        }
+    }
+
+    @AfterClass
+    public static void staticCleanup() {
+        FileUtils.deleteQuietly(CACHE_DIR);
+    }
 
     @Before
     public void setUp() {
-        MockitoAnnotations.initMocks(this);
-        yangLibProvider = new TestingYangLibProvider(new SharedSchemaRepository("test"), "www.fake.com", 300);
-        when(context.getSALService(eq(DataBroker.class))).thenReturn(dataBroker);
+        try {
+            if (CACHE_DIR.exists()) {
+                FileUtils.cleanDirectory(CACHE_DIR);
+            }
+        } catch (IOException e) {
+            // Ignore
+        }
+
+        doReturn(emptyFluentFuture()).when(writeTransaction).commit();
+        doReturn(writeTransaction).when(dataBroker).newWriteOnlyTransaction();
+
+        final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com")
+                .setBindingPort(Uint32.valueOf(300)).setCacheFolder(CACHE_DIR.getAbsolutePath()).build();
+        yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new DefaultYangParserFactory());
     }
 
     @Test
     public void testSchemaSourceRegistered() {
-        yangLibProvider.onSessionInitiated(context);
-        when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
-        doNothing().when(writeTransaction)
-                .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any());
+        yangLibProvider.init();
 
         List<PotentialSchemaSource<?>> list = new ArrayList<>();
         list.add(
-                PotentialSchemaSource.create(
-                        RevisionSourceIdentifier.create("no-revision"),
+                PotentialSchemaSource.create(new SourceIdentifier("no-revision"),
                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
 
         list.add(
-                PotentialSchemaSource.create(
-                        RevisionSourceIdentifier.create("with-revision", "2016-04-28"),
+                PotentialSchemaSource.create(new SourceIdentifier("with-revision", "2016-04-28"),
                         YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
 
-        when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
         yangLibProvider.schemaSourceRegistered(list);
 
-        List<Module> newModulesList = new ArrayList<>();
+        Map<ModuleKey, Module> newModulesList = new HashMap<>();
 
         Module newModule = new ModuleBuilder()
                 .setName(new YangIdentifier("no-revision"))
-                .setRevision(new OptionalRevision(""))
+                .setRevision(LegacyRevisionUtils.emptyRevision())
                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/"))
                 .build();
 
-        newModulesList.add(newModule);
+        newModulesList.put(newModule.key(), newModule);
 
         newModule = new ModuleBuilder()
                 .setName(new YangIdentifier("with-revision"))
-                .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28")))
+                .setRevision(new Revision(new RevisionIdentifier("2016-04-28")))
                 .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28"))
                 .build();
 
-        newModulesList.add(newModule);
+        newModulesList.put(newModule.key(), newModule);
 
         verify(dataBroker).newWriteOnlyTransaction();
         verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
                 eq(InstanceIdentifier.create(ModulesState.class)),
                 eq(new ModulesStateBuilder().setModule(newModulesList).build()));
-        verify(writeTransaction).submit();
+        verify(writeTransaction).commit();
     }
 
     @Test
-    public void testFilteringNonYangSchemaSourceRegistered() {
-        yangLibProvider.onSessionInitiated(context);
+    public void testFilteringEmptySchemaSourceRegistered() {
+        yangLibProvider.init();
 
         // test empty list of schema sources registered
-        List<PotentialSchemaSource<?>> potentialSources = Collections.emptyList();
-        yangLibProvider.schemaSourceRegistered(potentialSources);
-
-        verifyZeroInteractions(dataBroker, writeTransaction);
-
-        // test list of non yang schema sources registered
+        yangLibProvider.schemaSourceRegistered(Collections.emptyList());
         // expected behavior is to do nothing
-        potentialSources = new ArrayList<>();
-        potentialSources.add(
-                PotentialSchemaSource.create(
-                        RevisionSourceIdentifier.create("yin-source-representation"),
-                        YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
-
-        potentialSources.add(
-                PotentialSchemaSource.create(
-                        RevisionSourceIdentifier.create("asts-schema-source"),
-                        ASTSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
-
-        yangLibProvider.schemaSourceRegistered(potentialSources);
-        verifyZeroInteractions(dataBroker, writeTransaction);
-
-        // add yang schema source to list
-        potentialSources.add(
-                PotentialSchemaSource.create(
-                        RevisionSourceIdentifier.create("yang-schema-source"),
-                        YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()));
-
-        when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
-        when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
-        yangLibProvider.schemaSourceRegistered(potentialSources);
-        verify(dataBroker).newWriteOnlyTransaction();
-
-        ArgumentCaptor<ModulesState> modulesStateCaptor = ArgumentCaptor.forClass(ModulesState.class);
-        verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL),
-                eq(InstanceIdentifier.create(ModulesState.class)), modulesStateCaptor.capture());
-        assertEquals(modulesStateCaptor.getValue().getModule().size(), 1);
-        verify(writeTransaction).submit();
+        verifyNoMoreInteractions(dataBroker, writeTransaction);
     }
 
     @Test
-    public void testNonYangSchemaSourceUnregistered() {
-        yangLibProvider.onSessionInitiated(context);
-
-        final PotentialSchemaSource<YinSchemaSourceRepresentation> nonYangSource =
-                PotentialSchemaSource.create(
-                        RevisionSourceIdentifier.create("yin-source-representation"),
-                YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue());
+    public void testFilteringNonYangSchemaSourceRegistered() {
+        yangLibProvider.init();
 
-        yangLibProvider.schemaSourceUnregistered(nonYangSource);
+        // test list of non yang schema sources registered
+        final var nonYangSources = new ArrayList<PotentialSchemaSource<?>>();
+        nonYangSources.add(PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
+            YinSchemaSourceRepresentation.class, Costs.IMMEDIATE.getValue()));
+        nonYangSources.add(PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"),
+            YangIRSchemaSource.class, Costs.IMMEDIATE.getValue()));
+        yangLibProvider.schemaSourceRegistered(nonYangSources);
 
-        // expected behaviour is to do nothing if non yang based source is unregistered
-        verifyZeroInteractions(dataBroker, writeTransaction);
+        // expected behavior is to do nothing
+        verifyNoMoreInteractions(dataBroker, writeTransaction);
     }
 
     @Test
-    public void testSchemaSourceUnregistered() {
-        yangLibProvider.onSessionInitiated(context);
+    public void testSchemaSourceWithRevisionUnregistered() {
+        yangLibProvider.init();
 
-        when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
-        doNothing().when(writeTransaction)
-                .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class));
-
-        when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
-
-        PotentialSchemaSource<YangTextSchemaSource> yangUnregistererSource =
-                PotentialSchemaSource.create(
-                        RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"),
-                        YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
-
-        yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
+        // try to unregister YANG source with revision
+        final var schemaSourceWithRevision = PotentialSchemaSource.create(
+            new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"),
+            YangTextSchemaSource.class, Costs.LOCAL_IO.getValue());
+        yangLibProvider.schemaSourceUnregistered(schemaSourceWithRevision);
 
+        // source is unregistered
         verify(dataBroker).newWriteOnlyTransaction();
         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
-                eq(InstanceIdentifier.create(ModulesState.class)
-                        .child(Module.class,
-                                new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
-                                        new OptionalRevision("")))));
-
-        verify(writeTransaction).submit();
+            eq(InstanceIdentifier.create(ModulesState.class)
+                .child(Module.class,
+                    new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
+                        new Revision(new RevisionIdentifier("2016-04-28"))))));
+        verify(writeTransaction).commit();
+    }
 
-        yangUnregistererSource =
-                PotentialSchemaSource.create(
-                        RevisionSourceIdentifier.create("unregistered-yang-with-revision", "2016-04-28"),
-                        YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue());
+    @Test
+    public void testSchemaSourceWithoutRevisionUnregistered() {
+        yangLibProvider.init();
 
-        yangLibProvider.schemaSourceUnregistered(yangUnregistererSource);
+        // try to unregister YANG source without revision
+        final var schemaSourceWithoutRevision = PotentialSchemaSource.create(
+            new SourceIdentifier("unregistered-yang-schema-without-revision"), YangTextSchemaSource.class,
+            Costs.LOCAL_IO.getValue());
+        yangLibProvider.schemaSourceUnregistered(schemaSourceWithoutRevision);
 
-        verify(dataBroker, times(2)).newWriteOnlyTransaction();
+        // source is unregistered
+        verify(dataBroker).newWriteOnlyTransaction();
         verify(writeTransaction).delete(eq(LogicalDatastoreType.OPERATIONAL),
-                eq(InstanceIdentifier.create(ModulesState.class)
-                        .child(Module.class,
-                                new ModuleKey(new YangIdentifier("unregistered-yang-with-revision"),
-                                        new OptionalRevision(new RevisionIdentifier("2016-04-28"))))));
-
-        verify(writeTransaction, times(2)).submit();
+            eq(InstanceIdentifier.create(ModulesState.class)
+                .child(Module.class,
+                    new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"),
+                        LegacyRevisionUtils.emptyRevision()))));
+        verify(writeTransaction).commit();
     }
 
-    private static class TestingYangLibProvider extends YangLibProvider {
+    @Test
+    public void testNonYangSchemaSourceUnregistered() {
+        yangLibProvider.init();
 
-        TestingYangLibProvider(
-                SharedSchemaRepository schemaRepository, String bindingAddress, long bindingPort) {
-            super(schemaRepository, bindingAddress, bindingPort);
-        }
+        // try to unregister non-YANG source
+        final var nonYangSources = PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"),
+            YinSchemaSourceRepresentation.class, Costs.IMMEDIATE.getValue());
+        yangLibProvider.schemaSourceUnregistered(nonYangSources);
 
-        @Override
-        public void onSessionInitiated(BindingAwareBroker.ProviderContext providerContext) {
-            this.dataBroker = providerContext.getSALService(DataBroker.class);
-            schemaListenerRegistration = schemaRepository.registerSchemaSourceListener(this);
-        }
+        // expected behaviour is to do nothing if non yang based source is unregistered
+        verifyNoMoreInteractions(dataBroker, writeTransaction);
     }
-
-
 }