Java 8: use lambdas and function references
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / SharedSchemaRepositoryTest.java
index 9a8790a428cf4f9eb1764d8c7941d86f1c9d2970..f9dff888633ea5be2aefeda2488fc485ede15044 100644 (file)
@@ -8,39 +8,40 @@
 
 package org.opendaylight.yangtools.yang.parser.repo;
 
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertSame;
-import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.fail;
+import static org.hamcrest.CoreMatchers.both;
+import static org.hamcrest.CoreMatchers.hasItem;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.matchers.JUnitMatchers.both;
-import static org.junit.matchers.JUnitMatchers.hasItem;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 
 import com.google.common.base.Function;
-import com.google.common.base.Objects;
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Optional;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
 import com.google.common.io.Files;
-import com.google.common.io.InputSupplier;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
-import org.apache.commons.io.IOUtils;
 import org.junit.Test;
 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.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
@@ -57,6 +58,27 @@ import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
 
 public class SharedSchemaRepositoryTest {
 
+    @Test
+    public void testSourceWithAndWithoutRevision() throws Exception {
+        final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
+
+        final SourceIdentifier idNoRevision = loadAndRegisterSource(sharedSchemaRepository, "/no-revision/imported.yang");
+        final SourceIdentifier id2 = loadAndRegisterSource(sharedSchemaRepository, "/no-revision/imported@2012-12-12.yang");
+
+        CheckedFuture<ASTSchemaSource, SchemaSourceException> source = sharedSchemaRepository.getSchemaSource(idNoRevision, ASTSchemaSource.class);
+        assertEquals(idNoRevision, source.checkedGet().getIdentifier());
+        source = sharedSchemaRepository.getSchemaSource(id2, ASTSchemaSource.class);
+        assertEquals(id2, source.checkedGet().getIdentifier());
+    }
+
+    private static SourceIdentifier loadAndRegisterSource(final SharedSchemaRepository sharedSchemaRepository, final String resourceName) throws Exception {
+        final SettableSchemaProvider<ASTSchemaSource> sourceProvider = getImmediateYangSourceProviderFromResource(resourceName);
+        sourceProvider.setResult();
+        final SourceIdentifier idNoRevision = sourceProvider.getId();
+        sourceProvider.register(sharedSchemaRepository);
+        return idNoRevision;
+    }
+
     @Test
     public void testSimpleSchemaContext() throws Exception {
         final SharedSchemaRepository sharedSchemaRepository = new SharedSchemaRepository("netconf-mounts");
@@ -203,16 +225,16 @@ public class SharedSchemaRepositoryTest {
         sharedSchemaRepository.registerSchemaSourceListener(listener);
 
         final File test = new File(storageDir, "test.yang");
-        Files.copy(new StringSupplier("content-test"), test);
+        Files.write("content-test", test, StandardCharsets.UTF_8);
 
         final File test2 = new File(storageDir, "test@2012-12-12.yang");
-        Files.copy(new StringSupplier("content-test-2012"), test2);
+        Files.write("content-test-2012", test2, StandardCharsets.UTF_8);
 
         final File test3 = new File(storageDir, "test@2013-12-12.yang");
-        Files.copy(new StringSupplier("content-test-2013"), test3);
+        Files.write("content-test-2013", test3, StandardCharsets.UTF_8);
 
         final File test4 = new File(storageDir, "module@2010-12-12.yang");
-        Files.copy(new StringSupplier("content-module-2010"), test4);
+        Files.write("content-module-2010", test4, StandardCharsets.UTF_8);
 
 
         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
@@ -220,17 +242,13 @@ public class SharedSchemaRepositoryTest {
 
         assertEquals(4, listener.registeredSources.size());
 
-        final Function<PotentialSchemaSource<?>, SourceIdentifier> potSourceToSID = new Function<PotentialSchemaSource<?>, SourceIdentifier>() {
-            @Override
-            public SourceIdentifier apply(final PotentialSchemaSource<?> input) {
-                return input.getSourceIdentifier();
-            }
-        };
+        final Function<PotentialSchemaSource<?>, SourceIdentifier> potSourceToSID =
+                PotentialSchemaSource::getSourceIdentifier;
         assertThat(Collections2.transform(listener.registeredSources, potSourceToSID),
-                both(hasItem(new SourceIdentifier("test", Optional.<String>absent())))
-                        .and(hasItem(new SourceIdentifier("test", Optional.of("2012-12-12"))))
-                        .and(hasItem(new SourceIdentifier("test", Optional.of("2013-12-12"))))
-                        .and(hasItem(new SourceIdentifier("module", Optional.of("2010-12-12"))))
+                both(hasItem(RevisionSourceIdentifier.create("test", Optional.absent())))
+                        .and(hasItem(RevisionSourceIdentifier.create("test", Optional.of("2012-12-12"))))
+                        .and(hasItem(RevisionSourceIdentifier.create("test", Optional.of("2013-12-12"))))
+                        .and(hasItem(RevisionSourceIdentifier.create("module", Optional.of("2010-12-12"))))
         );
     }
 
@@ -243,20 +261,20 @@ public class SharedSchemaRepositoryTest {
         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(sharedSchemaRepository, YangTextSchemaSource.class, storageDir);
         sharedSchemaRepository.registerSchemaSourceListener(cache);
 
-        final SourceIdentifier runningId = new SourceIdentifier("running", Optional.of("2012-12-12"));
+        final SourceIdentifier runningId = RevisionSourceIdentifier.create("running", Optional.of("2012-12-12"));
 
         sharedSchemaRepository.registerSchemaSource(new SchemaSourceProvider<YangTextSchemaSource>() {
             @Override
             public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
-                return Futures.<YangTextSchemaSource, SchemaSourceException>immediateCheckedFuture(new YangTextSchemaSource(runningId) {
+                return Futures.immediateCheckedFuture(new YangTextSchemaSource(runningId) {
                     @Override
-                    protected Objects.ToStringHelper addToStringAttributes(final Objects.ToStringHelper toStringHelper) {
+                    protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
                         return toStringHelper;
                     }
 
                     @Override
                     public InputStream openStream() throws IOException {
-                        return IOUtils.toInputStream("running");
+                        return new ByteArrayInputStream("running".getBytes(StandardCharsets.UTF_8));
                     }
                 });
             }
@@ -293,33 +311,20 @@ public class SharedSchemaRepositoryTest {
         fail("Creation of schema context should fail from non-regular sources");
     }
 
-    private void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
+    private static void assertSchemaContext(final SchemaContext schemaContext, final int moduleSize) {
         assertNotNull(schemaContext);
         assertEquals(moduleSize, schemaContext.getModules().size());
     }
 
-    private SettableSchemaProvider<ASTSchemaSource> getRemoteYangSourceProviderFromResource(final String resourceName) throws Exception {
+    static SettableSchemaProvider<ASTSchemaSource> getRemoteYangSourceProviderFromResource(final String resourceName) throws Exception {
         final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
         final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
         return SettableSchemaProvider.createRemote(aSTSchemaSource.get(), ASTSchemaSource.class);
     }
 
-    private SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName) throws Exception {
+    static SettableSchemaProvider<ASTSchemaSource> getImmediateYangSourceProviderFromResource(final String resourceName) throws Exception {
         final ResourceYangSource yangSource = new ResourceYangSource(resourceName);
         final CheckedFuture<ASTSchemaSource, SchemaSourceException> aSTSchemaSource = TextToASTTransformer.TRANSFORMATION.apply(yangSource);
         return SettableSchemaProvider.createImmediate(aSTSchemaSource.get(), ASTSchemaSource.class);
     }
-
-    private class StringSupplier implements InputSupplier<InputStream> {
-        private final String s;
-
-        public StringSupplier(final String s) {
-            this.s = s;
-        }
-
-        @Override
-        public InputStream getInput() throws IOException {
-            return IOUtils.toInputStream(s);
-        }
-    }
 }