Merge "Bug 5581: Optimize subtree filtering"
[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 com.google.common.base.Optional;
11 import com.google.common.io.ByteStreams;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import java.net.InetSocketAddress;
14 import java.net.URL;
15 import java.util.Collections;
16 import java.util.Map;
17 import org.hamcrest.CoreMatchers;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
22 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26
27 public class YangLibrarySchemaYangSourceProviderTest {
28
29     private SourceIdentifier workingSid;
30     private YangLibrarySchemaYangSourceProvider yangLibrarySchemaYangSourceProvider;
31
32     @Before
33     public void setUp() throws Exception {
34         final URL url = getClass().getResource("/schemas/config-test-rpc.yang");
35         workingSid = RevisionSourceIdentifier.create("abc", Optional.<String>absent());
36         final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
37         final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
38         yangLibrarySchemaYangSourceProvider = new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
39     }
40
41     @Test
42     public void testGetSource() throws Exception {
43         CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> source =
44                 yangLibrarySchemaYangSourceProvider.getSource(workingSid);
45         final String x = new String(ByteStreams.toByteArray(source.checkedGet().openStream()));
46         Assert.assertThat(x, CoreMatchers.containsString("module config-test-rpc"));
47     }
48
49     @Test(expected = SchemaSourceException.class)
50     public void testGetSourceFailure() throws Exception {
51         final URL url = new URL("http://non-existing-entity.yang");
52         final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
53         final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
54         final YangLibrarySchemaYangSourceProvider failingYangLibrarySchemaYangSourceProvider =
55                 new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
56
57         CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> source =
58                 failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
59         source.checkedGet();
60     }
61
62     @Test(expected = IllegalArgumentException.class)
63     public void testGetSourceNotAvailable() throws Exception {
64         yangLibrarySchemaYangSourceProvider.getSource(RevisionSourceIdentifier.create("aaaaa"));
65     }
66 }