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