Migrate yang-repo-fs to JUnit5 66/101366/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 30 May 2022 16:38:09 +0000 (18:38 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 30 May 2022 16:38:09 +0000 (18:38 +0200)
These are simple tests, migrate them.

Change-Id: Ied27283b229ae27f8d5bef6b8af0554b5ade1bc9
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-repo-fs/src/test/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCacheIntegrationTest.java
yang/yang-repo-fs/src/test/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCacheTest.java

index 1850f305c7f380457ee13c5a86966deda0fa6375..5a66878e84fab4f08bd86f4af83910bbe6dd476f 100644 (file)
@@ -9,19 +9,17 @@ package org.opendaylight.yangtools.yang.model.repo.fs;
 
 import static org.hamcrest.CoreMatchers.both;
 import static org.hamcrest.CoreMatchers.hasItem;
+import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.collect.Lists;
 import com.google.common.io.Files;
-import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.MoreExecutors;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
@@ -32,9 +30,8 @@ import java.util.Arrays;
 import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
@@ -46,7 +43,7 @@ import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
 
 public class FilesystemSchemaSourceCacheIntegrationTest {
     @Test
-    public void testWithCacheStartup() throws Exception {
+    public void testWithCacheStartup() throws IOException {
         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
 
         class CountingSchemaListener implements SchemaSourceListener {
@@ -100,7 +97,7 @@ public class FilesystemSchemaSourceCacheIntegrationTest {
     }
 
     @Test
-    public void testWithCacheRunning() throws Exception {
+    public void testWithCacheRunning() {
         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
 
         final File storageDir = Files.createTempDir();
@@ -138,30 +135,14 @@ public class FilesystemSchemaSourceCacheIntegrationTest {
         final ListenableFuture<EffectiveModelContext> schemaFuture = sharedSchemaRepository
                 .createEffectiveModelContextFactory()
                 .createEffectiveModelContext(runningId);
-        Futures.addCallback(schemaFuture, new FutureCallback<SchemaContext>() {
-            @Override
-            public void onSuccess(final SchemaContext result) {
-                fail("Creation of schema context should fail from non-regular sources");
-            }
 
-            @Override
-            public void onFailure(final Throwable cause) {
-                // Creation of schema context fails, since we do not provide regular sources, but we just want
-                // to check cache
-                final List<File> cachedSchemas = Arrays.asList(storageDir.listFiles());
-                assertEquals(1, cachedSchemas.size());
-                assertEquals(Files.getNameWithoutExtension(cachedSchemas.get(0).getName()), "running@2012-12-12");
-            }
-        }, MoreExecutors.directExecutor());
-
-        try {
-            schemaFuture.get();
-        } catch (final ExecutionException e) {
-            assertNotNull(e.getCause());
-            assertEquals(MissingSchemaSourceException.class, e.getCause().getClass());
-            return;
-        }
+        final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(schemaFuture)).getCause();
+        assertThat(cause, instanceOf(MissingSchemaSourceException.class));
 
-        fail("Creation of schema context should fail from non-regular sources");
+        // Creation of schema context fails, since we do not provide regular sources, but we just want
+        // to check cache
+        final List<File> cachedSchemas = Arrays.asList(storageDir.listFiles());
+        assertEquals(1, cachedSchemas.size());
+        assertEquals(Files.getNameWithoutExtension(cachedSchemas.get(0).getName()), "running@2012-12-12");
     }
 }
index 8a20352b3b244f2a84a46c725e544e38b9016d1e..ef6841ea404d69c700cfcb361ff6a10658f80de5 100644 (file)
@@ -12,10 +12,10 @@ import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.either;
 import static org.hamcrest.CoreMatchers.hasItem;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-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.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.times;
@@ -23,23 +23,25 @@ import static org.mockito.Mockito.verify;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.collect.Collections2;
-import com.google.common.io.Files;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.BeforeEach;
+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.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
@@ -47,7 +49,8 @@ import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
 public class FilesystemSchemaSourceCacheTest {
     @Mock
     public SchemaSourceRegistry registry;
@@ -56,9 +59,9 @@ public class FilesystemSchemaSourceCacheTest {
 
     public File storageDir;
 
-    @Before
+    @BeforeEach
     public void setUp() throws Exception {
-        storageDir = Files.createTempDir();
+        storageDir = Files.createTempDirectory(null).toFile();
         doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
             any(PotentialSchemaSource.class));
     }
@@ -82,9 +85,9 @@ public class FilesystemSchemaSourceCacheTest {
 
         assertThat(fileNames, both(hasItem("test2")).and(hasItem("test@2012-12-12")));
 
-        assertThat(Files.asCharSource(storedFiles.get(0), StandardCharsets.UTF_8).read(),
+        assertThat(Files.readString(storedFiles.get(0).toPath()),
             either(containsString(content)).or(containsString(content2)));
-        assertThat(Files.asCharSource(storedFiles.get(1), StandardCharsets.UTF_8).read(),
+        assertThat(Files.readString(storedFiles.get(1).toPath()),
             either(containsString(content)).or(containsString(content2)));
 
         verify(registry, times(2)).registerSchemaSource(any(SchemaSourceProvider.class),
@@ -101,7 +104,11 @@ public class FilesystemSchemaSourceCacheTest {
     }
 
     private static Collection<String> filesToFilenamesWithoutRevision(final List<File> storedFiles) {
-        return Collections2.transform(storedFiles, input -> Files.getNameWithoutExtension(input.getName()));
+        return Collections2.transform(storedFiles, input -> {
+            final String fileName = input.getName();
+            final int dotIndex = fileName.lastIndexOf('.');
+            return dotIndex == -1 ? fileName : fileName.substring(0, dotIndex);
+        });
     }
 
     @Test
@@ -190,7 +197,6 @@ public class FilesystemSchemaSourceCacheTest {
 
     @Test
     public void test() throws Exception {
-
         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(registry,
                 YangTextSchemaSource.class, storageDir);
         final String content = "content1";
@@ -199,14 +205,13 @@ public class FilesystemSchemaSourceCacheTest {
         final SourceIdentifier sourceIdentifier = new SourceIdentifier("test", "2013-12-12");
         final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
         assertNotNull(checked);
+        assertTrue(checked.isDone());
         final YangTextSchemaSource checkedGet = checked.get();
         assertEquals(sourceIdentifier, checkedGet.getIdentifier());
-        assertTrue(checked.isDone());
     }
 
     @Test
     public void test1() throws Exception {
-
         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(registry,
                 YangTextSchemaSource.class, storageDir);
         final String content = "content1";