2 * Copyright (c) 2016 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.netconf.sal.connect.netconf.schema;
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
13 import com.google.common.io.ByteStreams;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.net.InetSocketAddress;
16 import java.net.MalformedURLException;
18 import java.util.Collections;
20 import java.util.Optional;
21 import java.util.concurrent.ExecutionException;
22 import org.hamcrest.CoreMatchers;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32 public class YangLibrarySchemaYangSourceProviderTest {
34 private SourceIdentifier workingSid;
35 private YangLibrarySchemaYangSourceProvider yangLibrarySchemaYangSourceProvider;
38 public void setUp() throws Exception {
39 final URL url = getClass().getResource("/schemas/config-test-rpc.yang");
40 workingSid = RevisionSourceIdentifier.create("abc", Optional.empty());
41 final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
42 final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
43 yangLibrarySchemaYangSourceProvider = new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
47 public void testGetSource() throws Exception {
48 ListenableFuture<? extends YangTextSchemaSource> source = yangLibrarySchemaYangSourceProvider
49 .getSource(workingSid);
50 final String x = new String(ByteStreams.toByteArray(source.get().openStream()));
51 Assert.assertThat(x, CoreMatchers.containsString("module config-test-rpc"));
55 public void testGetSourceFailure() throws InterruptedException, MalformedURLException {
56 final URL url = new URL("http://non-existing-entity.yang");
57 final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
58 final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
59 final YangLibrarySchemaYangSourceProvider failingYangLibrarySchemaYangSourceProvider =
60 new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
63 failingYangLibrarySchemaYangSourceProvider.getSource(workingSid).get();
65 } catch (ExecutionException e) {
66 final Throwable cause = e.getCause();
67 assertTrue(cause instanceof SchemaSourceException);
71 @Test(expected = IllegalArgumentException.class)
72 public void testGetSourceNotAvailable() throws Exception {
73 yangLibrarySchemaYangSourceProvider.getSource(RevisionSourceIdentifier.create("aaaaa"));