From 662aedc0f3c2ba65a6afe28ce2b489e6e2c93ae0 Mon Sep 17 00:00:00 2001 From: Ivan Hrasko Date: Tue, 7 Mar 2023 12:24:39 +0100 Subject: [PATCH] Refactor YangLibProviderTest Refactor mock usage: remove not needed doNothing constructs, move mock setup to setUp method. Split test cases for registering empty and non-YANG resources, unregistering models with and without revision. JIRA: NETCONF-968 Change-Id: I5195d794f2949c2343908281488d46d27bcf07bd Signed-off-by: Ivan Hrasko --- .../yanglib/impl/YangLibProviderTest.java | 129 +++++++----------- 1 file changed, 53 insertions(+), 76 deletions(-) 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 f7728437f5..8430e5b63b 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 @@ -7,15 +7,10 @@ */ package org.opendaylight.yanglib.impl; -import static org.junit.Assert.assertEquals; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture; import java.io.File; @@ -31,7 +26,6 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.mdsal.binding.api.DataBroker; @@ -56,6 +50,7 @@ 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.model.repo.spi.PotentialSchemaSource.Costs; import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory; @RunWith(MockitoJUnitRunner.StrictStubs.class) @@ -92,6 +87,9 @@ public class YangLibProviderTest { // 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()); @@ -100,9 +98,6 @@ public class YangLibProviderTest { @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( @@ -113,7 +108,6 @@ public class YangLibProviderTest { PotentialSchemaSource.create(new SourceIdentifier("with-revision", "2016-04-28"), YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue())); - doReturn(emptyFluentFuture()).when(writeTransaction).commit(); yangLibProvider.schemaSourceRegistered(list); Map newModulesList = new HashMap<>(); @@ -142,98 +136,81 @@ public class YangLibProviderTest { } @Test - public void testFilteringNonYangSchemaSourceRegistered() { + public void testFilteringEmptySchemaSourceRegistered() { yangLibProvider.init(); // test empty list of schema sources registered - List> potentialSources = Collections.emptyList(); - yangLibProvider.schemaSourceRegistered(potentialSources); - - verifyNoMoreInteractions(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(new SourceIdentifier("yin-source-representation"), - YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue())); - - potentialSources.add( - PotentialSchemaSource.create(new SourceIdentifier("asts-schema-source"), - YangIRSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue())); - - yangLibProvider.schemaSourceRegistered(potentialSources); verifyNoMoreInteractions(dataBroker, writeTransaction); - - // add yang schema source to list - potentialSources.add( - PotentialSchemaSource.create(new SourceIdentifier("yang-schema-source"), - YangTextSchemaSource.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue())); - - when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); - doReturn(emptyFluentFuture()).when(writeTransaction).commit(); - 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).commit(); } @Test - public void testNonYangSchemaSourceUnregistered() { + public void testFilteringNonYangSchemaSourceRegistered() { yangLibProvider.init(); - final PotentialSchemaSource nonYangSource = - PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"), - YinSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.IMMEDIATE.getValue()); - - 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 + // expected behavior is to do nothing verifyNoMoreInteractions(dataBroker, writeTransaction); } @Test - public void testSchemaSourceUnregistered() { + public void testSchemaSourceWithRevisionUnregistered() { yangLibProvider.init(); - 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); - doReturn(emptyFluentFuture()).when(writeTransaction).commit(); + // 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(new SourceIdentifier("unregistered-yang-schema-without-revision"), - 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); + // 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"), - LegacyRevisionUtils.emptyRevision())))); - + eq(InstanceIdentifier.create(ModulesState.class) + .child(Module.class, + new ModuleKey(new YangIdentifier("unregistered-yang-schema-without-revision"), + LegacyRevisionUtils.emptyRevision())))); verify(writeTransaction).commit(); + } - yangUnregistererSource = - PotentialSchemaSource.create(new SourceIdentifier("unregistered-yang-with-revision", "2016-04-28"), - YangTextSchemaSource.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue()); - - yangLibProvider.schemaSourceUnregistered(yangUnregistererSource); + @Test + public void testNonYangSchemaSourceUnregistered() { + yangLibProvider.init(); - 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 Revision(new RevisionIdentifier("2016-04-28")))))); + // try to unregister non-YANG source + final var nonYangSources = PotentialSchemaSource.create(new SourceIdentifier("yin-source-representation"), + YinSchemaSourceRepresentation.class, Costs.IMMEDIATE.getValue()); + yangLibProvider.schemaSourceUnregistered(nonYangSources); - verify(writeTransaction, times(2)).commit(); + // expected behaviour is to do nothing if non yang based source is unregistered + verifyNoMoreInteractions(dataBroker, writeTransaction); } } -- 2.36.6