Clean up CachedYangTextSchemaSource
[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.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.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.jupiter.api.Test;
21 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
22 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
24
25 public class LibrarySchemaYangSourceProviderTest {
26     private final SourceIdentifier workingSid = new SourceIdentifier("abc");
27     private final LibrarySchemaSourceProvider yangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
28         new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)),
29         Map.of(workingSid, LibrarySchemaYangSourceProviderTest.class.getResource("/schemas/config-test-rpc.yang")));
30
31     @Test
32     void testGetSource() throws Exception {
33         var source = yangLibrarySchemaYangSourceProvider.getSource(workingSid);
34         assertThat(source.get().read(), startsWith("module config-test-rpc"));
35     }
36
37     @Test
38     void testGetSourceFailure() throws InterruptedException, MalformedURLException {
39         final var sourceIdentifierURLMap = Map.of(workingSid, new URL("http://non-existing-entity.yang"));
40         final var failingYangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
41             new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)), sourceIdentifierURLMap);
42
43         final var future = failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
44         final var ex = assertThrows(ExecutionException.class, () -> future.get());
45         assertInstanceOf(SchemaSourceException.class, ex.getCause());
46     }
47
48     @Test
49     void testGetSourceNotAvailable() {
50         assertThrows(IllegalArgumentException.class,
51             () -> yangLibrarySchemaYangSourceProvider.getSource(new SourceIdentifier("aaaaa")));
52     }
53 }