Bump upstreams
[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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import akka.dispatch.ExecutionContexts;
18 import akka.dispatch.Futures;
19 import com.google.common.io.CharSource;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import java.io.IOException;
23 import java.util.concurrent.ExecutionException;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
27 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30
31 public class RemoteSchemaProviderTest {
32     private static final SourceIdentifier ID = new SourceIdentifier("Test", "2015-10-30");
33
34     private RemoteSchemaProvider remoteSchemaProvider;
35     private RemoteYangTextSourceProvider mockedRemoteSchemaRepository;
36
37     @Before
38     public void setUp() {
39         mockedRemoteSchemaRepository = mock(RemoteYangTextSourceProvider.class);
40         remoteSchemaProvider = new RemoteSchemaProvider(mockedRemoteSchemaRepository,
41                 ExecutionContexts.fromExecutor(MoreExecutors.directExecutor()));
42     }
43
44     @Test
45     public void getExistingYangTextSchemaSource() throws IOException, InterruptedException, ExecutionException {
46         YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForCharSource(ID, CharSource.wrap("Test"));
47         doReturn(Futures.successful(new YangTextSchemaSourceSerializationProxy(schemaSource)))
48             .when(mockedRemoteSchemaRepository).getYangTextSchemaSource(ID);
49
50         YangTextSchemaSource providedSource = remoteSchemaProvider.getSource(ID).get();
51         assertEquals(ID, providedSource.getIdentifier());
52         assertEquals(schemaSource.read(), providedSource.read());
53     }
54
55     @Test
56     public void getNonExistingSchemaSource() throws InterruptedException {
57         final var exception = new SchemaSourceException("Source not provided");
58         doReturn(Futures.failed(exception)).when(mockedRemoteSchemaRepository).getYangTextSchemaSource(ID);
59
60         ListenableFuture<YangTextSchemaSource> sourceFuture = remoteSchemaProvider.getSource(ID);
61         assertTrue(sourceFuture.isDone());
62
63         final var cause = assertThrows(ExecutionException.class, sourceFuture::get).getCause();
64         assertSame(exception, cause);
65     }
66 }