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;
16 import com.google.common.io.CharSource;
17 import com.google.common.util.concurrent.Futures;
18 import java.util.Collections;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import scala.concurrent.Await;
30 import scala.concurrent.duration.FiniteDuration;
32 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class RemoteYangTextSourceProviderImplTest {
34 private static final SourceIdentifier ID = new SourceIdentifier("Test", "2015-10-30");
37 private SchemaRepository mockedLocalRepository;
39 private RemoteYangTextSourceProviderImpl remoteRepository;
40 private final Set<SourceIdentifier> providedSources = Collections.singleton(ID);
44 remoteRepository = new RemoteYangTextSourceProviderImpl(mockedLocalRepository, providedSources);
48 public void testGetExistingYangTextSchemaSource() throws Exception {
49 var schemaSource = YangTextSchemaSource.delegateForCharSource(ID, CharSource.wrap("Test source."));
51 doReturn(Futures.immediateFuture(schemaSource)).when(mockedLocalRepository)
52 .getSchemaSource(ID, YangTextSchemaSource.class);
54 var retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
55 assertTrue(retrievedSourceFuture.isCompleted());
56 var resultSchemaSource = Await.result(retrievedSourceFuture, FiniteDuration.Zero()).getRepresentation();
57 assertEquals(resultSchemaSource.getIdentifier(), schemaSource.getIdentifier());
58 assertEquals(resultSchemaSource.read(), schemaSource.read());
62 public void testGetNonExistentYangTextSchemaSource() throws Exception {
63 final var exception = new SchemaSourceException("Source is not provided");
65 doReturn(Futures.immediateFailedFuture(exception)).when(mockedLocalRepository)
66 .getSchemaSource(ID, YangTextSchemaSource.class);
68 var retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
69 assertTrue(retrievedSourceFuture.isCompleted());
71 final var ex = assertThrows(SchemaSourceException.class,
72 () -> Await.result(retrievedSourceFuture, FiniteDuration.Zero()));
73 assertSame(ex, exception);
77 public void testGetProvidedSources() throws Exception {
78 var remoteProvidedSources = Await.result(remoteRepository.getProvidedSources(), FiniteDuration.Zero());
79 assertEquals(providedSources, remoteProvidedSources);