X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fyanglib%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fyanglib%2Fimpl%2FYangLibProviderTest.java;h=794946ab1b375b067df89dd88d01715aa81ac978;hb=04c94a48416e0c87f2d62164e11a6266630e6956;hp=ab0cb2359c90576b5d8ea1c15862fb54ccb8db9a;hpb=6d7e12bf3ef64e5004703a1d540e7e26f30a9595;p=netconf.git diff --git a/netconf/yanglib/src/test/java/org/opendaylight/yanglib/impl/YangLibProviderTest.java b/netconf/yanglib/src/test/java/org/opendaylight/yanglib/impl/YangLibProviderTest.java index ab0cb2359c..794946ab1b 100644 --- a/netconf/yanglib/src/test/java/org/opendaylight/yanglib/impl/YangLibProviderTest.java +++ b/netconf/yanglib/src/test/java/org/opendaylight/yanglib/impl/YangLibProviderTest.java @@ -8,234 +8,212 @@ 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.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.clearInvocations; +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.List; -import org.apache.commons.io.FileUtils; -import org.junit.AfterClass; +import java.util.HashMap; 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.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.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 { - private static final File CACHE_DIR = new File("target/yanglib"); - @Mock private DataBroker dataBroker; - @Mock private WriteTransaction writeTransaction; 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); - - try { - if (CACHE_DIR.exists()) { - FileUtils.cleanDirectory(CACHE_DIR); - } - } catch (IOException e) { - // Ignore - } - - final YanglibConfig yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com") - .setBindingPort(300L).setCacheFolder(CACHE_DIR.getAbsolutePath()).build(); - yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new YangLibServiceImpl()); + doReturn(emptyFluentFuture()).when(writeTransaction).commit(); + doReturn(writeTransaction).when(dataBroker).newWriteOnlyTransaction(); + + final var yanglibConfig = new YanglibConfigBuilder().setBindingAddr("www.fake.com") + .setBindingPort(Uint32.valueOf(300)) + .setCacheFolder(YangLibProviderTest.class.getResource("/model").getPath()) + .build(); + yangLibProvider = new YangLibProvider(yanglibConfig, dataBroker, new DefaultYangParserFactory()); + // this will automatically register all models from /model directory + yangLibProvider.init(); } @Test public void testSchemaSourceRegistered() { - yangLibProvider.init(); - when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); - doNothing().when(writeTransaction) - .merge(eq(LogicalDatastoreType.OPERATIONAL), eq(InstanceIdentifier.create(ModulesState.class)), any()); - - List> list = new ArrayList<>(); - list.add( - PotentialSchemaSource.create( - RevisionSourceIdentifier.create("no-revision"), - YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue())); - - list.add( - PotentialSchemaSource.create( - RevisionSourceIdentifier.create("with-revision", "2016-04-28"), - YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue())); - - when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null)); - yangLibProvider.schemaSourceRegistered(list); - - List newModulesList = new ArrayList<>(); - - Module newModule = new ModuleBuilder() - .setName(new YangIdentifier("no-revision")) - .setRevision(new OptionalRevision("")) - .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/no-revision/")) - .build(); - - newModulesList.add(newModule); - - newModule = new ModuleBuilder() - .setName(new YangIdentifier("with-revision")) - .setRevision(new OptionalRevision(new RevisionIdentifier("2016-04-28"))) - .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/with-revision/2016-04-28")) - .build(); - - newModulesList.add(newModule); + // test that initial models are registered + final var newModulesMap = new HashMap(); + + final var model1 = new ModuleBuilder() + .setName(new YangIdentifier("model1")) + .setRevision(new Revision(new RevisionIdentifier("2023-02-21"))) + .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/model1/2023-02-21")) + .build(); + newModulesMap.put(model1.key(), model1); + + final var model2 = new ModuleBuilder() + .setName(new YangIdentifier("model2")) + .setRevision(LegacyRevisionUtils.emptyRevision()) + .setSchema(new Uri("http://www.fake.com:300/yanglib/schemas/model2")) + .build(); + newModulesMap.put(model2.key(), model2); verify(dataBroker).newWriteOnlyTransaction(); verify(writeTransaction).merge(eq(LogicalDatastoreType.OPERATIONAL), - eq(InstanceIdentifier.create(ModulesState.class)), - eq(new ModulesStateBuilder().setModule(newModulesList).build())); - verify(writeTransaction).submit(); + eq(InstanceIdentifier.create(ModulesState.class)), + eq(new ModulesStateBuilder().setModule(newModulesMap).build())); + verify(writeTransaction).commit(); } @Test - public void testFilteringNonYangSchemaSourceRegistered() { - yangLibProvider.init(); + public void testFilteringEmptySchemaSourceRegistered() { + clearInvocations(dataBroker, writeTransaction); // test empty list of schema sources registered - List> 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 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.init(); - - final PotentialSchemaSource nonYangSource = - PotentialSchemaSource.create( - RevisionSourceIdentifier.create("yin-source-representation"), - YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()); + public void testFilteringNonYangSchemaSourceRegistered() { + clearInvocations(dataBroker, writeTransaction); - yangLibProvider.schemaSourceUnregistered(nonYangSource); + // test list of non yang schema sources registered + final var nonYangSources = new ArrayList>(); + 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.init(); + public void testSchemaSourceWithRevisionUnregistered() { + clearInvocations(dataBroker, writeTransaction); - when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); - doNothing().when(writeTransaction) - .delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class)); + // 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); - when(writeTransaction.submit()).thenReturn(Futures.immediateCheckedFuture(null)); + // 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 Revision(new RevisionIdentifier("2016-04-28")))))); + verify(writeTransaction).commit(); + } - PotentialSchemaSource yangUnregistererSource = - PotentialSchemaSource.create( - RevisionSourceIdentifier.create("unregistered-yang-schema-without-revision"), - YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue()); + @Test + public void testSchemaSourceWithoutRevisionUnregistered() { + clearInvocations(dataBroker, writeTransaction); - 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); + // 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(""))))); + eq(InstanceIdentifier.create(ModulesState.class) + .child(Module.class, + new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"), + LegacyRevisionUtils.emptyRevision())))); + verify(writeTransaction).commit(); + } - verify(writeTransaction).submit(); + @Test + public void testNonYangSchemaSourceUnregistered() { + clearInvocations(dataBroker, writeTransaction); - yangUnregistererSource = - PotentialSchemaSource.create( - RevisionSourceIdentifier.create("unregistered-yang-with-revision", "2016-04-28"), - YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue()); + // try to unregister non-YANG source + final var nonYangSources = PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"), + YinSchemaSourceRepresentation.class, Costs.IMMEDIATE.getValue()); + yangLibProvider.schemaSourceUnregistered(nonYangSources); - yangLibProvider.schemaSourceUnregistered(yangUnregistererSource); + // expected behaviour is to do nothing if non yang based source is unregistered + verifyNoMoreInteractions(dataBroker, writeTransaction); + } - verify(dataBroker, times(2)).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")))))); + @Test + public void testGetSchemaWithRevision() { + final var modelWithRevision = yangLibProvider.getSchema("model1", "2023-02-21"); + assertNotNull(modelWithRevision); + assertEquals(""" + module model1 { + namespace "model:with:revision"; + prefix mwr; + + revision 2023-02-21 { + description + "Initial revision;"; + } + + container test { + leaf test-leaf { + type string; + } + } + } + """, modelWithRevision); + } - verify(writeTransaction, times(2)).submit(); + @Test + public void testGetSchemaWithoutRevision() { + final var modelWithoutRevision = yangLibProvider.getSchema("model2"); + assertNotNull(modelWithoutRevision); + assertEquals(""" + module model2 { + namespace "model:with:no:revision"; + prefix mwnr; + + container test { + leaf test-leaf { + type string; + } + } + } + """, modelWithoutRevision); } }