1634c1db44867420540cf1e744eb95c61b1ab1d3
[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.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;
31
32 public class YangLibrarySchemaYangSourceProviderTest {
33     private SourceIdentifier workingSid;
34     private YangLibrarySchemaYangSourceProvider yangLibrarySchemaYangSourceProvider;
35
36     @Before
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);
43     }
44
45     @Test
46     public void testGetSource() throws Exception {
47         ListenableFuture<? extends YangTextSchemaSource> source = yangLibrarySchemaYangSourceProvider
48                 .getSource(workingSid);
49
50         final String x = source.get().asCharSource(StandardCharsets.UTF_8).read();
51         assertThat(x, containsString("module config-test-rpc"));
52     }
53
54     @Test
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);
61
62         final ListenableFuture<?> future = failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
63         final ExecutionException ex = assertThrows(ExecutionException.class, () -> future.get());
64         assertThat(ex.getCause(), instanceOf(SchemaSourceException.class));
65     }
66
67     @Test
68     public void testGetSourceNotAvailable() throws Exception {
69         assertThrows(IllegalArgumentException.class,
70             () -> yangLibrarySchemaYangSourceProvider.getSource(RevisionSourceIdentifier.create("aaaaa")));
71     }
72 }