Remove CheckedFuture from RemoteSchemaProvider
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / cluster / schema / provider / impl / RemoteSchemaProviderTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.schema.provider.impl;
9
10 import static org.hamcrest.Matchers.instanceOf;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18
19 import akka.dispatch.ExecutionContexts;
20 import akka.dispatch.Futures;
21 import com.google.common.io.CharSource;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
26 import java.util.concurrent.ExecutionException;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
30 import org.opendaylight.yangtools.yang.common.Revision;
31 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
32 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
33 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
34 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
35
36 public class RemoteSchemaProviderTest {
37     private static final SourceIdentifier ID = RevisionSourceIdentifier.create("Test", Revision.of("2015-10-30"));
38
39     private RemoteSchemaProvider remoteSchemaProvider;
40     private RemoteYangTextSourceProvider mockedRemoteSchemaRepository;
41
42     @Before
43     public void setUp() {
44         mockedRemoteSchemaRepository = mock(RemoteYangTextSourceProvider.class);
45         remoteSchemaProvider = new RemoteSchemaProvider(mockedRemoteSchemaRepository,
46                 ExecutionContexts.fromExecutor(MoreExecutors.directExecutor()));
47     }
48
49     @Test
50     public void getExistingYangTextSchemaSource() throws IOException, InterruptedException, ExecutionException {
51         YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(ID,
52             CharSource.wrap("Test").asByteSource(StandardCharsets.UTF_8));
53         doReturn(Futures.successful(new YangTextSchemaSourceSerializationProxy(schemaSource)))
54             .when(mockedRemoteSchemaRepository).getYangTextSchemaSource(ID);
55
56         YangTextSchemaSource providedSource = remoteSchemaProvider.getSource(ID).get();
57         assertEquals(ID, providedSource.getIdentifier());
58         assertArrayEquals(schemaSource.read(), providedSource.read());
59     }
60
61     @Test
62     public void getNonExistingSchemaSource() throws InterruptedException {
63         doReturn(Futures.failed(new SchemaSourceException("Source not provided")))
64             .when(mockedRemoteSchemaRepository).getYangTextSchemaSource(ID);
65
66         ListenableFuture<YangTextSchemaSource> sourceFuture = remoteSchemaProvider.getSource(ID);
67         assertTrue(sourceFuture.isDone());
68         try {
69             sourceFuture.get();
70             fail("Expected a failure to occur");
71         } catch (ExecutionException e) {
72             assertThat(e.getCause(), instanceOf(SchemaSourceException.class));
73         }
74     }
75 }