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 akka.dispatch.ExecutionContexts;
16 import akka.dispatch.Futures;
17 import com.google.common.base.Optional;
18 import com.google.common.io.ByteSource;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.io.IOException;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
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;
30 public class RemoteSchemaProviderTest {
32 private static final SourceIdentifier ID = SourceIdentifier.create("Test", Optional.of("2015-10-30"));
34 private RemoteSchemaProvider remoteSchemaProvider;
35 private RemoteYangTextSourceProvider mockedRemoteSchemaRepository;
39 mockedRemoteSchemaRepository = Mockito.mock(RemoteYangTextSourceProvider.class);
40 ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService());
41 remoteSchemaProvider = new RemoteSchemaProvider(mockedRemoteSchemaRepository,
42 ExecutionContexts.fromExecutorService(MoreExecutors.newDirectExecutorService()));
46 public void getExistingYangTextSchemaSource() throws IOException, SchemaSourceException {
47 String source = "Test";
48 YangTextSchemaSource schemaSource = YangTextSchemaSource.delegateForByteSource(ID, ByteSource.wrap(source.getBytes()));
49 YangTextSchemaSourceSerializationProxy sourceProxy = new YangTextSchemaSourceSerializationProxy(schemaSource);
50 Mockito.when(mockedRemoteSchemaRepository.getYangTextSchemaSource(ID)).thenReturn(Futures.successful(sourceProxy));
52 YangTextSchemaSource providedSource = remoteSchemaProvider.getSource(ID).checkedGet();
53 assertEquals(providedSource.getIdentifier(), ID);
54 assertArrayEquals(providedSource.read(), schemaSource.read());
57 @Test(expected = SchemaSourceException.class)
58 public void getNonExistingSchemaSource() throws Exception {
59 Futures.failed(new Exception("halo"));
61 Mockito.when(mockedRemoteSchemaRepository.getYangTextSchemaSource(ID)).thenReturn(
62 Futures.<YangTextSchemaSourceSerializationProxy>failed(new SchemaSourceException("Source not provided")));
64 CheckedFuture<?, ?> sourceFuture = remoteSchemaProvider.getSource(ID);
65 assertTrue(sourceFuture.isDone());
66 sourceFuture.checkedGet();