Remove CheckedFuture from RemoteSchemaProvider
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / schema / provider / impl / RemoteSchemaProviderTest.java
index 0b41cdf..06d3369 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));
+        }
     }
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.