Bump upstream versions
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / schema / YangLibrarySchemaYangSourceProviderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.schema;
9
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;
14
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.net.InetSocketAddress;
17 import java.net.MalformedURLException;
18 import java.net.URL;
19 import java.nio.charset.StandardCharsets;
20 import java.util.Collections;
21 import java.util.Map;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.netconf.sal.connect.util.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;
29
30 public class YangLibrarySchemaYangSourceProviderTest {
31     private SourceIdentifier workingSid;
32     private YangLibrarySchemaYangSourceProvider yangLibrarySchemaYangSourceProvider;
33
34     @Before
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);
41     }
42
43     @Test
44     public void testGetSource() throws Exception {
45         ListenableFuture<? extends YangTextSchemaSource> source = yangLibrarySchemaYangSourceProvider
46                 .getSource(workingSid);
47
48         final String x = source.get().asCharSource(StandardCharsets.UTF_8).read();
49         assertThat(x, containsString("module config-test-rpc"));
50     }
51
52     @Test
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);
59
60         final ListenableFuture<?> future = failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
61         final ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
62         assertThat(ex.getCause(), instanceOf(SchemaSourceException.class));
63     }
64
65     @Test
66     public void testGetSourceNotAvailable() {
67         assertThrows(IllegalArgumentException.class,
68             () -> yangLibrarySchemaYangSourceProvider.getSource(new SourceIdentifier("aaaaa")));
69     }
70 }