2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.schema.provider.impl;
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;
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;
30 public class RemoteSchemaProviderTest {
31 private static final SourceIdentifier ID = new SourceIdentifier("Test", "2015-10-30");
33 private RemoteSchemaProvider remoteSchemaProvider;
34 private RemoteYangTextSourceProvider mockedRemoteSchemaRepository;
38 mockedRemoteSchemaRepository = mock(RemoteYangTextSourceProvider.class);
39 remoteSchemaProvider = new RemoteSchemaProvider(mockedRemoteSchemaRepository,
40 ExecutionContexts.fromExecutor(MoreExecutors.directExecutor()));
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);
49 final var providedSource = remoteSchemaProvider.getSource(ID).get();
50 assertEquals(ID, providedSource.sourceId());
51 assertEquals(schemaSource.read(), providedSource.read());
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);
59 final var sourceFuture = remoteSchemaProvider.getSource(ID);
60 assertTrue(sourceFuture.isDone());
62 final var cause = assertThrows(ExecutionException.class, sourceFuture::get).getCause();
63 assertSame(exception, cause);