Remove CheckedFuture from RemoteSchemaProvider 36/85036/5
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 10 Oct 2019 12:05:49 +0000 (14:05 +0200)
committerStephen Kitt <skitt@redhat.com>
Thu, 10 Oct 2019 17:19:42 +0000 (17:19 +0000)
The API contract we are implementing is defined in terms of
ListenableFuture, hence we can drop the use of CheckedFuture.

Change-Id: I9d99b2e42c3995c4c11b44e595a81a93b2b31b7e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/schema/provider/impl/RemoteSchemaProvider.java
opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/schema/provider/impl/RemoteSchemaProviderTest.java

index 3208e390a44104e2c245ca8c47b448bb175611d4..88e58421c1d590681e72c4fc6ef8252c33fe630a 100644 (file)
@@ -10,12 +10,9 @@ package org.opendaylight.controller.cluster.schema.provider.impl;
 
 import akka.dispatch.OnComplete;
 import com.google.common.annotations.Beta;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
-import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
 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.SchemaSourceProvider;
@@ -29,27 +26,19 @@ import scala.concurrent.Future;
  */
 @Beta
 public class RemoteSchemaProvider implements SchemaSourceProvider<YangTextSchemaSource> {
-
     private static final Logger LOG = LoggerFactory.getLogger(RemoteSchemaProvider.class);
 
     private final RemoteYangTextSourceProvider remoteRepo;
     private final ExecutionContext executionContext;
 
-    private static final ExceptionMapper<SchemaSourceException> MAPPER = new ExceptionMapper<SchemaSourceException>(
-            "schemaDownload", SchemaSourceException.class) {
-        @Override
-        protected SchemaSourceException newWithCause(final String message, final Throwable throwable) {
-            return new SchemaSourceException(message, throwable);
-        }
-    };
-
-    public RemoteSchemaProvider(RemoteYangTextSourceProvider remoteRepo, ExecutionContext executionContext) {
+    public RemoteSchemaProvider(final RemoteYangTextSourceProvider remoteRepo,
+            final ExecutionContext executionContext) {
         this.remoteRepo = remoteRepo;
         this.executionContext = executionContext;
     }
 
     @Override
-    public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(SourceIdentifier sourceIdentifier) {
+    public ListenableFuture<YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
         LOG.trace("Getting yang schema source for {}", sourceIdentifier.getName());
 
         Future<YangTextSchemaSourceSerializationProxy> result = remoteRepo.getYangTextSchemaSource(sourceIdentifier);
@@ -57,8 +46,8 @@ public class RemoteSchemaProvider implements SchemaSourceProvider<YangTextSchema
         final SettableFuture<YangTextSchemaSource> res = SettableFuture.create();
         result.onComplete(new OnComplete<YangTextSchemaSourceSerializationProxy>() {
             @Override
-            public void onComplete(Throwable throwable,
-                    YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) {
+            public void onComplete(final Throwable throwable,
+                    final YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) {
                 if (yangTextSchemaSourceSerializationProxy != null) {
                     res.set(yangTextSchemaSourceSerializationProxy.getRepresentation());
                 }
@@ -66,9 +55,8 @@ public class RemoteSchemaProvider implements SchemaSourceProvider<YangTextSchema
                     res.setException(throwable);
                 }
             }
-
         }, executionContext);
 
-        return Futures.makeChecked(res, MAPPER);
+        return res;
     }
 }
index 0b41cdfa4ed8ea300b47222feb855c7fc49b7398..06d33690f039b7f90789038e718bc10dcffea537 100644 (file)
@@ -5,22 +5,27 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.controller.cluster.schema.provider.impl;
 
+import static org.hamcrest.Matchers.instanceOf;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
 
 import akka.dispatch.ExecutionContexts;
 import akka.dispatch.Futures;
-import com.google.common.io.ByteSource;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.io.CharSource;
+import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.ExecutionException;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mockito;
 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
@@ -29,7 +34,6 @@ import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 
 public class RemoteSchemaProviderTest {
-
     private static final SourceIdentifier ID = RevisionSourceIdentifier.create("Test", Revision.of("2015-10-30"));
 
     private RemoteSchemaProvider remoteSchemaProvider;
@@ -37,36 +41,35 @@ public class RemoteSchemaProviderTest {
 
     @Before
     public void setUp() {
-        mockedRemoteSchemaRepository = Mockito.mock(RemoteYangTextSourceProvider.class);
-        ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService());
+        mockedRemoteSchemaRepository = mock(RemoteYangTextSourceProvider.class);
         remoteSchemaProvider = new RemoteSchemaProvider(mockedRemoteSchemaRepository,
-                ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService()));
+                ExecutionContexts.fromExecutor(MoreExecutors.directExecutor()));
     }
 
     @Test
-    public void getExistingYangTextSchemaSource() throws IOException, SchemaSourceException {
-        String source = "Test";
-        YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(
-                ID, ByteSource.wrap(source.getBytes()));
-        YangTextSchemaSourceSerializationProxy sourceProxy = new YangTextSchemaSourceSerializationProxy(schemaSource);
-        Mockito.when(mockedRemoteSchemaRepository.getYangTextSchemaSource(ID))
-            .thenReturn(Futures.successful(sourceProxy));
+    public void getExistingYangTextSchemaSource() throws IOException, InterruptedException, ExecutionException {
+        YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(ID,
+            CharSource.wrap("Test").asByteSource(StandardCharsets.UTF_8));
+        doReturn(Futures.successful(new YangTextSchemaSourceSerializationProxy(schemaSource)))
+            .when(mockedRemoteSchemaRepository).getYangTextSchemaSource(ID);
 
-        YangTextSchemaSource providedSource = remoteSchemaProvider.getSource(ID).checkedGet();
-        assertEquals(providedSource.getIdentifier(), ID);
-        assertArrayEquals(providedSource.read(), schemaSource.read());
+        YangTextSchemaSource providedSource = remoteSchemaProvider.getSource(ID).get();
+        assertEquals(ID, providedSource.getIdentifier());
+        assertArrayEquals(schemaSource.read(), providedSource.read());
     }
 
-    @Test(expected = SchemaSourceException.class)
-    public void getNonExistingSchemaSource() throws Exception {
-        Futures.failed(new Exception("halo"));
-
-        Mockito.when(mockedRemoteSchemaRepository.getYangTextSchemaSource(ID)).thenReturn(
-                Futures.failed(
-                        new SchemaSourceException("Source not provided")));
+    @Test
+    public void getNonExistingSchemaSource() throws InterruptedException {
+        doReturn(Futures.failed(new SchemaSourceException("Source not provided")))
+            .when(mockedRemoteSchemaRepository).getYangTextSchemaSource(ID);
 
-        CheckedFuture<?, ?> sourceFuture = remoteSchemaProvider.getSource(ID);
+        ListenableFuture<YangTextSchemaSource> sourceFuture = remoteSchemaProvider.getSource(ID);
         assertTrue(sourceFuture.isDone());
-        sourceFuture.checkedGet();
+        try {
+            sourceFuture.get();
+            fail("Expected a failure to occur");
+        } catch (ExecutionException e) {
+            assertThat(e.getCause(), instanceOf(SchemaSourceException.class));
+        }
     }
 }