Bug 2343 - Sideload of models for nodes from remote yang sources repository
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / schema / YangLibrarySchemaYangSourceProviderTest.java
1 package org.opendaylight.netconf.sal.connect.netconf.schema;
2
3 import com.google.common.base.Optional;
4 import com.google.common.io.ByteStreams;
5 import com.google.common.util.concurrent.CheckedFuture;
6 import java.net.InetSocketAddress;
7 import java.net.URL;
8 import java.util.Collections;
9 import java.util.Map;
10 import org.hamcrest.CoreMatchers;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
15 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
16 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
17 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
18
19 public class YangLibrarySchemaYangSourceProviderTest {
20
21     private SourceIdentifier workingSid;
22     private YangLibrarySchemaYangSourceProvider yangLibrarySchemaYangSourceProvider;
23
24     @Before
25     public void setUp() throws Exception {
26         final URL url = getClass().getResource("/schemas/config-test-rpc.yang");
27         workingSid = new SourceIdentifier("abc", Optional.<String>absent());
28         final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
29         final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
30         yangLibrarySchemaYangSourceProvider = new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
31     }
32
33     @Test
34     public void testGetSource() throws Exception {
35         CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> source =
36                 yangLibrarySchemaYangSourceProvider.getSource(workingSid);
37         final String x = new String(ByteStreams.toByteArray(source.checkedGet().openStream()));
38         Assert.assertThat(x, CoreMatchers.containsString("module config-test-rpc"));
39     }
40
41     @Test(expected = SchemaSourceException.class)
42     public void testGetSourceFailure() throws Exception {
43         final URL url = new URL("http://non-existing-entity.yang");
44         final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
45         final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
46         final YangLibrarySchemaYangSourceProvider failingYangLibrarySchemaYangSourceProvider =
47                 new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
48
49         CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> source =
50                 failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
51         source.checkedGet();
52     }
53
54     @Test(expected = IllegalArgumentException.class)
55     public void testGetSourceNotAvailable() throws Exception {
56         yangLibrarySchemaYangSourceProvider.getSource(new SourceIdentifier("aaaaa", "0000-00-00"));
57     }
58 }