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
9 package org.opendaylight.controller.cluster.schema.provider.impl;
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.io.ByteSource;
16 import com.google.common.util.concurrent.Futures;
17 import java.util.Collections;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26 import scala.concurrent.Await;
27 import scala.concurrent.Future;
28 import scala.concurrent.duration.Duration;
30 public class RemoteYangTextSourceProviderImplTest {
32 private static final SourceIdentifier ID = new SourceIdentifier("Test", "2015-10-30");
34 private RemoteYangTextSourceProviderImpl remoteRepository;
35 private SchemaRepository mockedLocalRepository;
36 private Set<SourceIdentifier> providedSources = Collections.singleton(ID);
40 mockedLocalRepository = Mockito.mock(SchemaRepository.class);
42 remoteRepository = new RemoteYangTextSourceProviderImpl(mockedLocalRepository, providedSources);
46 public void testGetExistingYangTextSchemaSource() throws Exception {
47 String source = "Test source.";
48 YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(ID, ByteSource.wrap(source.getBytes()));
49 Mockito.when(mockedLocalRepository.getSchemaSource(ID, YangTextSchemaSource.class)).thenReturn(
50 Futures.<YangTextSchemaSource, SchemaSourceException>immediateCheckedFuture(schemaSource));
52 Future<YangTextSchemaSourceSerializationProxy> retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
53 assertTrue(retrievedSourceFuture.isCompleted());
54 YangTextSchemaSource resultSchemaSource = Await.result(retrievedSourceFuture, Duration.Zero()).getRepresentation();
55 assertEquals(resultSchemaSource.getIdentifier(), schemaSource.getIdentifier());
56 assertArrayEquals(resultSchemaSource.read(), schemaSource.read());
59 @Test(expected = SchemaSourceException.class)
60 public void testGetNonExistentYangTextSchemaSource() throws Exception {
61 Mockito.when(mockedLocalRepository.getSchemaSource(ID, YangTextSchemaSource.class)).thenReturn(
62 Futures.<YangTextSchemaSource, SchemaSourceException>immediateFailedCheckedFuture(
63 new SchemaSourceException("Source is not provided")));
66 Future<YangTextSchemaSourceSerializationProxy> retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
67 assertTrue(retrievedSourceFuture.isCompleted());
68 Await.result(retrievedSourceFuture, Duration.Zero());
72 public void testGetProvidedSources() throws Exception {
73 Set<SourceIdentifier> remoteProvidedSources = Await.result(remoteRepository.getProvidedSources(), Duration.Zero());
74 assertEquals(providedSources, remoteProvidedSources);