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.concurrent.ExecutionException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
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 YangLibrarySchemaYangSourceProviderTest {
31 private SourceIdentifier workingSid;
32 private YangLibrarySchemaYangSourceProvider yangLibrarySchemaYangSourceProvider;
35 public void setUp() throws Exception {
36 final URL url = getClass().getResource("/schemas/config-test-rpc.yang");
37 workingSid = new SourceIdentifier("abc");
38 final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
39 final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
40 yangLibrarySchemaYangSourceProvider = new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
44 public void testGetSource() throws Exception {
45 ListenableFuture<? extends YangTextSchemaSource> source = yangLibrarySchemaYangSourceProvider
46 .getSource(workingSid);
48 final String x = source.get().asCharSource(StandardCharsets.UTF_8).read();
49 assertThat(x, containsString("module config-test-rpc"));
53 public void testGetSourceFailure() throws InterruptedException, MalformedURLException {
54 final URL url = new URL("http://non-existing-entity.yang");
55 final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
56 final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
57 final YangLibrarySchemaYangSourceProvider failingYangLibrarySchemaYangSourceProvider =
58 new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
60 final ListenableFuture<?> future = failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
61 final ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
62 assertThat(ex.getCause(), instanceOf(SchemaSourceException.class));
66 public void testGetSourceNotAvailable() {
67 assertThrows(IllegalArgumentException.class,
68 () -> yangLibrarySchemaYangSourceProvider.getSource(new SourceIdentifier("aaaaa")));