bc94ff2f9ec55f539fdaafb77d26284d8a196ce5
[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.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import com.google.common.io.ByteStreams;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.net.InetSocketAddress;
16 import java.net.MalformedURLException;
17 import java.net.URL;
18 import java.util.Collections;
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.concurrent.ExecutionException;
22 import org.hamcrest.CoreMatchers;
23 import org.junit.Assert;
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
34     private SourceIdentifier workingSid;
35     private YangLibrarySchemaYangSourceProvider yangLibrarySchemaYangSourceProvider;
36
37     @Before
38     public void setUp() throws Exception {
39         final URL url = getClass().getResource("/schemas/config-test-rpc.yang");
40         workingSid = RevisionSourceIdentifier.create("abc", Optional.empty());
41         final Map<SourceIdentifier, URL> sourceIdentifierURLMap = Collections.singletonMap(workingSid, url);
42         final RemoteDeviceId id = new RemoteDeviceId("id", new InetSocketAddress("localhost", 22));
43         yangLibrarySchemaYangSourceProvider = new YangLibrarySchemaYangSourceProvider(id, sourceIdentifierURLMap);
44     }
45
46     @Test
47     public void testGetSource() throws Exception {
48         ListenableFuture<? extends YangTextSchemaSource> source = yangLibrarySchemaYangSourceProvider
49                 .getSource(workingSid);
50         final String x = new String(ByteStreams.toByteArray(source.get().openStream()));
51         Assert.assertThat(x, CoreMatchers.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         try {
63             failingYangLibrarySchemaYangSourceProvider.getSource(workingSid).get();
64             fail();
65         } catch (ExecutionException e) {
66             final Throwable cause = e.getCause();
67             assertTrue(cause instanceof SchemaSourceException);
68         }
69     }
70
71     @Test(expected = IllegalArgumentException.class)
72     public void testGetSourceNotAvailable() throws Exception {
73         yangLibrarySchemaYangSourceProvider.getSource(RevisionSourceIdentifier.create("aaaaa"));
74     }
75 }