Bump upstreams
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / LibrarySchemaYangSourceProviderTest.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.client.mdsal;
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 java.net.InetSocketAddress;
16 import java.net.MalformedURLException;
17 import java.net.URL;
18 import java.util.Map;
19 import java.util.concurrent.ExecutionException;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
23 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
25
26 public class LibrarySchemaYangSourceProviderTest {
27     private SourceIdentifier workingSid;
28     private LibrarySchemaSourceProvider yangLibrarySchemaYangSourceProvider;
29
30     @Before
31     public void setUp() throws Exception {
32         workingSid = new SourceIdentifier("abc");
33         yangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
34             new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)),
35             Map.of(workingSid, getClass().getResource("/schemas/config-test-rpc.yang")));
36     }
37
38     @Test
39     public void testGetSource() throws Exception {
40         var source = yangLibrarySchemaYangSourceProvider.getSource(workingSid);
41         final String x = source.get().read();
42         assertThat(x, containsString("module config-test-rpc"));
43     }
44
45     @Test
46     public void testGetSourceFailure() throws InterruptedException, MalformedURLException {
47         final var sourceIdentifierURLMap = Map.of(workingSid, new URL("http://non-existing-entity.yang"));
48         final var failingYangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
49             new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)), sourceIdentifierURLMap);
50
51         final var future = failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
52         final var ex = assertThrows(ExecutionException.class, () -> future.get());
53         assertThat(ex.getCause(), instanceOf(SchemaSourceException.class));
54     }
55
56     @Test
57     public void testGetSourceNotAvailable() {
58         assertThrows(IllegalArgumentException.class,
59             () -> yangLibrarySchemaYangSourceProvider.getSource(new SourceIdentifier("aaaaa")));
60     }
61 }