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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertThrows;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.net.InetSocketAddress;
17 import java.net.MalformedURLException;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Collections;
22 import java.util.Optional;
23 import java.util.concurrent.ExecutionException;
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 {
33 private SourceIdentifier workingSid;
34 private YangLibrarySchemaYangSourceProvider yangLibrarySchemaYangSourceProvider;
37 public void setUp() throws Exception {
38 final URL url = getClass().getResource("/schemas/config-test-rpc.yang");
39 workingSid = RevisionSourceIdentifier.create("abc", Optional.empty());
40 final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
41 final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
42 yangLibrarySchemaYangSourceProvider = new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
46 public void testGetSource() throws Exception {
47 ListenableFuture<? extends YangTextSchemaSource> source = yangLibrarySchemaYangSourceProvider
48 .getSource(workingSid);
50 final String x = source.get().asCharSource(StandardCharsets.UTF_8).read();
51 assertThat(x, 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);
62 final ListenableFuture<?> future = failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
63 final ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
64 assertThat(ex.getCause(), instanceOf(SchemaSourceException.class));
68 public void testGetSourceNotAvailable() throws Exception {
69 assertThrows(IllegalArgumentException.class,
70 () -> yangLibrarySchemaYangSourceProvider.getSource(RevisionSourceIdentifier.create("aaaaa")));