From 10a43a53218104650bdcd42eed9b91be87582776 Mon Sep 17 00:00:00 2001 From: "matus.matok" Date: Thu, 13 Jul 2023 11:27:10 +0200 Subject: [PATCH] Migrate yang-repo-spi to JUnit5 Migrated all tests to use JUnit5 Assertions, using openrewrite:rewrite-testing-frameworks. JIRA: YANGTOOLS-1521 Change-Id: I99202bffab5035110a3c2b3d9228d64d933c26c3 Signed-off-by: matus.matok Signed-off-by: Robert Varga --- yang/yang-repo-spi/pom.xml | 5 ++ .../repo/spi/GuavaSchemaSourceCacheTest.java | 59 +++++++++--------- .../repo/spi/PotentialSchemaSourceTest.java | 37 ++++++------ .../repo/spi/RefcountedRegistrationTest.java | 41 +++++-------- .../repo/spi/SchemaSourceTransformerTest.java | 51 ++++++++-------- .../repo/spi/SoftSchemaSourceCacheTest.java | 60 +++++++++---------- 6 files changed, 119 insertions(+), 134 deletions(-) diff --git a/yang/yang-repo-spi/pom.xml b/yang/yang-repo-spi/pom.xml index bb75b18768..e4b4aacd14 100644 --- a/yang/yang-repo-spi/pom.xml +++ b/yang/yang-repo-spi/pom.xml @@ -42,5 +42,10 @@ org.opendaylight.yangtools yang-repo-api + + + org.opendaylight.yangtools + mockito-configuration + diff --git a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java index 8853b4a8cd..0b5f885bba 100644 --- a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java +++ b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java @@ -7,66 +7,60 @@ */ package org.opendaylight.yangtools.yang.model.repo.spi; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import com.google.common.base.MoreObjects.ToStringHelper; -import java.io.Reader; import java.io.StringReader; import java.util.Optional; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; @Deprecated -@RunWith(MockitoJUnitRunner.StrictStubs.class) -public class GuavaSchemaSourceCacheTest { - public static final Class REPRESENTATION = YangSchemaSourceRepresentation.class; - public static final long LIFETIME = 1000L; - public static final TimeUnit UNITS = TimeUnit.MILLISECONDS; +@ExtendWith(MockitoExtension.class) +class GuavaSchemaSourceCacheTest { + private static final Class REPRESENTATION = YangSchemaSourceRepresentation.class; + private static final long LIFETIME = 1000L; + private static final TimeUnit UNITS = TimeUnit.MILLISECONDS; @Mock public SchemaSourceRegistry registry; @Mock public SchemaSourceRegistration registration; - @Before - public void setUp() { - doNothing().when(registration).close(); - doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class), - any(PotentialSchemaSource.class)); - } - @Test - public void inMemorySchemaSourceCacheTest1() { + void inMemorySchemaSourceCacheTest1() { try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) { assertNotNull(cache); } } @Test - public void inMemorySchemaSourceCacheTest2() { + void inMemorySchemaSourceCacheTest2() { try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION, LIFETIME, UNITS)) { assertNotNull(cache); } } @Test - public void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception { + void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception { + doNothing().when(registration).close(); + doReturn(registration).when(registry).registerSchemaSource(any(), any()); + try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) { - final String content = "content"; - final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content); + final var content = "content"; + final var source = new TestingYangSource("test", "2012-12-12", content); cache.offer(source); final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12"); final var checkedSource = cache .getSource(sourceIdentifier); @@ -78,21 +72,24 @@ public class GuavaSchemaSourceCacheTest { } @Test - public void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception { + void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception { try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) { final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12"); final var checkedSource = cache.getSource(sourceIdentifier); assertNotNull(checkedSource); - assertThrows(ExecutionException.class, () -> checkedSource.get()); + assertThrows(ExecutionException.class, checkedSource::get); } } @Test - public void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException { + void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException { + doNothing().when(registration).close(); + doReturn(registration).when(registry).registerSchemaSource(any(), any()); + try (var cache1 = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) { try (var cache2 = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION, LIFETIME, UNITS)) { - final String content = "content"; - final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content); + final var content = "content"; + final var source = new TestingYangSource("test", "2012-12-12", content); cache1.offer(source); cache2.offer(source); @@ -116,7 +113,7 @@ public class GuavaSchemaSourceCacheTest { } @Override - public Reader openStream() { + public StringReader openStream() { return new StringReader(content); } diff --git a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/PotentialSchemaSourceTest.java b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/PotentialSchemaSourceTest.java index 10d28f1fb4..d0435649d1 100644 --- a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/PotentialSchemaSourceTest.java +++ b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/PotentialSchemaSourceTest.java @@ -7,21 +7,20 @@ */ package org.opendaylight.yangtools.yang.model.repo.spi; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertThrows; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation; -@RunWith(MockitoJUnitRunner.StrictStubs.class) -public class PotentialSchemaSourceTest { +@ExtendWith(MockitoExtension.class) +class PotentialSchemaSourceTest { private interface TestSchemaSourceRepresentation extends YangSchemaSourceRepresentation { @Override default Class getType() { @@ -35,8 +34,8 @@ public class PotentialSchemaSourceTest { @SuppressWarnings("exports") public PotentialSchemaSource same; - @Before - public void before() { + @BeforeEach + void before() { source = PotentialSchemaSource.create(sourceIdentifier, TestSchemaSourceRepresentation.class, PotentialSchemaSource.Costs.LOCAL_IO.getValue()); same = PotentialSchemaSource.create(source.getSourceIdentifier(), source.getRepresentation(), @@ -44,24 +43,24 @@ public class PotentialSchemaSourceTest { } @Test - public void testNegativeCost() { + void testNegativeCost() { assertThrows(IllegalArgumentException.class, () -> PotentialSchemaSource.create(sourceIdentifier, TestSchemaSourceRepresentation.class, -1)); } @Test - public void testMethods() { + void testMethods() { assertEquals(PotentialSchemaSource.Costs.LOCAL_IO.getValue(), source.getCost()); assertSame(sourceIdentifier, source.getSourceIdentifier()); assertSame(TestSchemaSourceRepresentation.class, source.getRepresentation()); assertEquals(same.hashCode(), source.hashCode()); - assertFalse(source.equals(null)); - assertTrue(source.equals(source)); - assertTrue(source.equals(same)); + assertNotEquals(null, source); + assertEquals(source, source); + assertEquals(source, same); } @Test - public void testIntern() { + void testIntern() { assertSame(source, source.cachedReference()); assertSame(source, same.cachedReference()); } diff --git a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java index ec8f6bdf0f..a3124d2f55 100644 --- a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java +++ b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java @@ -7,49 +7,40 @@ */ package org.opendaylight.yangtools.yang.model.repo.spi; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) -public class RefcountedRegistrationTest { +@ExtendWith(MockitoExtension.class) +class RefcountedRegistrationTest { @Mock - public SchemaSourceRegistration reg; - - @Before - public void before() { - doNothing().when(reg).close(); - } + private SchemaSourceRegistration reg; @Test - public void refcountDecTrue() { - final RefcountedRegistration ref = new RefcountedRegistration(reg); + void refcountDecTrue() { + final var ref = new RefcountedRegistration(reg); + doNothing().when(reg).close(); assertTrue(ref.decRef()); - verify(reg, times(1)).close(); } @Test - public void refcountIncDecFalse() { - final RefcountedRegistration ref = new RefcountedRegistration(reg); + void refcountIncDecFalse() { + final var ref = new RefcountedRegistration(reg); ref.incRef(); assertFalse(ref.decRef()); - verify(reg, times(0)).close(); } @Test - public void refcountIncDecTrue() { - final RefcountedRegistration ref = new RefcountedRegistration(reg); + void refcountIncDecTrue() { + final var ref = new RefcountedRegistration(reg); ref.incRef(); assertFalse(ref.decRef()); + doNothing().when(reg).close(); assertTrue(ref.decRef()); - verify(reg, times(1)).close(); } } diff --git a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformerTest.java b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformerTest.java index 8fe0350d0f..f7d198dfa8 100644 --- a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformerTest.java +++ b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformerTest.java @@ -7,19 +7,18 @@ */ package org.opendaylight.yangtools.yang.model.repo.spi; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import com.google.common.util.concurrent.AsyncFunction; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; import java.util.Arrays; -import java.util.concurrent.Future; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory; import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration; import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository; @@ -29,8 +28,8 @@ import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresenta import org.opendaylight.yangtools.yang.model.repo.api.YinXmlSchemaSource; import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs; -@RunWith(MockitoJUnitRunner.class) -public class SchemaSourceTransformerTest { +@ExtendWith(MockitoExtension.class) +class SchemaSourceTransformerTest { public static final Class SRC_CLASS = YangSchemaSourceRepresentation.class; public static final Class DST_CLASS = YinXmlSchemaSource.class; @@ -46,7 +45,7 @@ public class SchemaSourceTransformerTest { public SchemaSourceTransformer schema; @Test - public void schemaSourceTransformerTest() { + void schemaSourceTransformerTest() { schema = new SchemaSourceTransformer<>( provider, SchemaSourceTransformerTest.SRC_CLASS, consumer, SchemaSourceTransformerTest.DST_CLASS, function); @@ -54,49 +53,49 @@ public class SchemaSourceTransformerTest { } @Test - public void schemaSourceTransformerGetSourceTest() { - final Provider p = new Provider(); - final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, + void schemaSourceTransformerGetSourceTest() { + final var p = new Provider(); + final var reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.IMMEDIATE); - final SourceIdentifier sourceIdentifier = new SourceIdentifier("source"); + final var sourceIdentifier = new SourceIdentifier("source"); reg.register(sourceIdentifier); schema = new SchemaSourceTransformer<>(p, SchemaSourceTransformerTest.SRC_CLASS, consumer, SchemaSourceTransformerTest.DST_CLASS, function); - final SchemaSourceProvider prov = schema; - final Future source = prov.getSource(sourceIdentifier); + final var prov = schema; + final var source = prov.getSource(sourceIdentifier); assertNotNull(source); source.cancel(true); assertTrue(source.isDone()); } @Test - public void schemaSourceRegAndUnregSchemaSourceTest() { - final SourceIdentifier sourceIdentifier = new SourceIdentifier("source"); - final Foo foo = new Foo<>(sourceIdentifier, + void schemaSourceRegAndUnregSchemaSourceTest() { + final var sourceIdentifier = new SourceIdentifier("source"); + final var foo = new Foo<>(sourceIdentifier, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.COMPUTATION); - final Provider p = new Provider(); + final var p = new Provider(); - final Registrator reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, + final var reg = new Registrator(p, SchemaSourceTransformerTest.SRC_CLASS, PotentialSchemaSource.Costs.IMMEDIATE); reg.register(sourceIdentifier); - final Consumer c = new Consumer(); + final var c = new Consumer(); schema = new SchemaSourceTransformer<>(p, SchemaSourceTransformerTest.SRC_CLASS, c, SchemaSourceTransformerTest.DST_CLASS, function); - final SchemaSourceListener listener = schema; + final var listener = schema; p.registerSchemaSourceListener(listener); - final PotentialSchemaSource[] potList = { foo.getPotentialSchemSource() }; - final Iterable> sources = Arrays.asList(potList); + final var potList = new PotentialSchemaSource[]{ foo.getPotentialSchemSource() }; + final var sources = Arrays.asList(potList); listener.schemaSourceRegistered(sources); - final ListenableFuture source = schema.getSource(sourceIdentifier); + final var source = schema.getSource(sourceIdentifier); assertNotNull(source); listener.schemaSourceUnregistered(foo.getPotentialSchemSource()); - final ListenableFuture source2 = schema.getSource(sourceIdentifier); + final var source2 = schema.getSource(sourceIdentifier); assertNotNull(source2); } diff --git a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java index 604143db50..2dae5fb1a7 100644 --- a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java +++ b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java @@ -7,58 +7,49 @@ */ package org.opendaylight.yangtools.yang.model.repo.spi; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import com.google.common.base.MoreObjects.ToStringHelper; -import java.io.Reader; import java.io.StringReader; import java.util.Optional; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; -@RunWith(MockitoJUnitRunner.StrictStubs.class) -public class SoftSchemaSourceCacheTest { - public static final Class REPRESENTATION = YangSchemaSourceRepresentation.class; - public static final long LIFETIME = 1000L; - public static final TimeUnit UNITS = TimeUnit.MILLISECONDS; +@ExtendWith(MockitoExtension.class) +class SoftSchemaSourceCacheTest { + private static final Class REPRESENTATION = YangSchemaSourceRepresentation.class; @Mock - public SchemaSourceRegistry registry; + private SchemaSourceRegistry registry; @Mock - public SchemaSourceRegistration registration; - - @Before - public void setUp() { - doNothing().when(registration).close(); - doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class), - any(PotentialSchemaSource.class)); - } + private SchemaSourceRegistration registration; @Test - public void inMemorySchemaSourceCacheTest() { + void inMemorySchemaSourceCacheTest() { try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) { assertNotNull(cache); } } @Test - public void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception { + void inMemorySchemaSourceCacheOfferAndGetSourcestest() throws Exception { + doNothing().when(registration).close(); + doReturn(registration).when(registry).registerSchemaSource(any(), any()); + try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) { - final String content = "content"; - final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content); + final var content = "content"; + final var source = new TestingYangSource("test", "2012-12-12", content); cache.offer(source); final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12"); final var checkedSource = cache .getSource(sourceIdentifier); @@ -70,21 +61,24 @@ public class SoftSchemaSourceCacheTest { } @Test - public void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception { + void inMemorySchemaSourceCacheNullGetSourcestest() throws Exception { try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) { final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12"); final var checkedSource = cache.getSource(sourceIdentifier); assertNotNull(checkedSource); - assertThrows(ExecutionException.class, () -> checkedSource.get()); + assertThrows(ExecutionException.class, checkedSource::get); } } @Test - public void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException { + void inMemorySchemaSourceCache3test() throws InterruptedException, ExecutionException { + doNothing().when(registration).close(); + doReturn(registration).when(registry).registerSchemaSource(any(), any()); + try (var cache1 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) { try (var cache2 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) { - final String content = "content"; - final YangTextSchemaSource source = new TestingYangSource("test", "2012-12-12", content); + final var content = "content"; + final var source = new TestingYangSource("test", "2012-12-12", content); cache1.offer(source); cache2.offer(source); @@ -108,7 +102,7 @@ public class SoftSchemaSourceCacheTest { } @Override - public Reader openStream() { + public StringReader openStream() { return new StringReader(content); } -- 2.36.6