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