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.base.Optional;
16 import com.google.common.io.ByteSource;
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.mockito.Mockito;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import scala.concurrent.Await;
28 import scala.concurrent.Future;
29 import scala.concurrent.duration.Duration;
31 public class RemoteYangTextSourceProviderImplTest {
33 private static final SourceIdentifier ID = SourceIdentifier.create("Test", Optional.of("2015-10-30"));
35 private RemoteYangTextSourceProviderImpl remoteRepository;
36 private SchemaRepository mockedLocalRepository;
37 private Set<SourceIdentifier> providedSources = Collections.singleton(ID);
41 mockedLocalRepository = Mockito.mock(SchemaRepository.class);
43 remoteRepository = new RemoteYangTextSourceProviderImpl(mockedLocalRepository, providedSources);
47 public void testGetExistingYangTextSchemaSource() throws Exception {
48 String source = "Test source.";
49 YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(ID, ByteSource.wrap(source.getBytes()));
50 Mockito.when(mockedLocalRepository.getSchemaSource(ID, YangTextSchemaSource.class)).thenReturn(
51 Futures.<YangTextSchemaSource, SchemaSourceException>immediateCheckedFuture(schemaSource));
53 Future<YangTextSchemaSourceSerializationProxy> retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
54 assertTrue(retrievedSourceFuture.isCompleted());
55 YangTextSchemaSource resultSchemaSource = Await.result(retrievedSourceFuture, Duration.Zero()).getRepresentation();
56 assertEquals(resultSchemaSource.getIdentifier(), schemaSource.getIdentifier());
57 assertArrayEquals(resultSchemaSource.read(), schemaSource.read());
60 @Test(expected = SchemaSourceException.class)
61 public void testGetNonExistentYangTextSchemaSource() throws Exception {
62 Mockito.when(mockedLocalRepository.getSchemaSource(ID, YangTextSchemaSource.class)).thenReturn(
63 Futures.<YangTextSchemaSource, SchemaSourceException>immediateFailedCheckedFuture(
64 new SchemaSourceException("Source is not provided")));
67 Future<YangTextSchemaSourceSerializationProxy> retrievedSourceFuture = remoteRepository.getYangTextSchemaSource(ID);
68 assertTrue(retrievedSourceFuture.isCompleted());
69 Await.result(retrievedSourceFuture, Duration.Zero());
73 public void testGetProvidedSources() throws Exception {
74 Set<SourceIdentifier> remoteProvidedSources = Await.result(remoteRepository.getProvidedSources(), Duration.Zero());
75 assertEquals(providedSources, remoteProvidedSources);