Bump upstream versions
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / LibraryModulesSchemasTest.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;
9
10 import static org.hamcrest.CoreMatchers.is;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
15 import java.net.MalformedURLException;
16 import java.net.URL;
17 import java.util.Collections;
18 import java.util.Map;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21
22 public class LibraryModulesSchemasTest {
23
24     @Test
25     public void testCreate() throws Exception {
26         // test create from xml
27         LibraryModulesSchemas libraryModulesSchemas =
28                 LibraryModulesSchemas.create(getClass().getResource("/yang-library.xml").toString());
29
30         verifySchemas(libraryModulesSchemas);
31
32         // test create from json
33         LibraryModulesSchemas libraryModuleSchemas =
34                 LibraryModulesSchemas.create(getClass().getResource("/yang-library.json").toString());
35
36         verifySchemas(libraryModulesSchemas);
37     }
38
39     private static void verifySchemas(final LibraryModulesSchemas libraryModulesSchemas) throws MalformedURLException {
40         final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
41         assertThat(resolvedModulesSchema.size(), is(3));
42
43         assertTrue(resolvedModulesSchema.containsKey(new SourceIdentifier("module-with-revision", "2014-04-08")));
44         assertThat(resolvedModulesSchema.get(new SourceIdentifier("module-with-revision", "2014-04-08")),
45                 is(new URL("http://localhost:8181/yanglib/schemas/module-with-revision/2014-04-08")));
46
47         assertTrue(resolvedModulesSchema.containsKey(
48                 new SourceIdentifier("another-module-with-revision", "2013-10-21")));
49         assertThat(resolvedModulesSchema.get(new SourceIdentifier("another-module-with-revision", "2013-10-21")),
50                 is(new URL("http://localhost:8181/yanglib/schemas/another-module-with-revision/2013-10-21")));
51
52         assertTrue(resolvedModulesSchema.containsKey(new SourceIdentifier("module-without-revision")));
53         assertThat(resolvedModulesSchema.get(new SourceIdentifier("module-without-revision")),
54                 is(new URL("http://localhost:8181/yanglib/schemas/module-without-revision/")));
55     }
56
57     @Test
58     public void testCreateInvalidModulesEntries() throws Exception {
59         LibraryModulesSchemas libraryModulesSchemas =
60                 LibraryModulesSchemas.create(getClass().getResource("/yang-library-fail.xml").toString());
61
62         final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
63         assertThat(resolvedModulesSchema.size(), is(1));
64
65         assertFalse(resolvedModulesSchema.containsKey(new SourceIdentifier("module-with-bad-url")));
66         //See BUG 8071 https://bugs.opendaylight.org/show_bug.cgi?id=8071
67         //assertFalse(resolvedModulesSchema.containsKey(
68         //        RevisionSourceIdentifier.create("module-with-bad-revision", "bad-revision")));
69         assertTrue(resolvedModulesSchema.containsKey(new SourceIdentifier("good-ol-module")));
70     }
71
72     @Test
73     public void testCreateFromInvalidAll() throws Exception {
74         // test bad yang lib url
75         LibraryModulesSchemas libraryModulesSchemas = LibraryModulesSchemas.create("ObviouslyBadUrl");
76         assertThat(libraryModulesSchemas.getAvailableModels(), is(Collections.emptyMap()));
77
78         // TODO test also fail on json and xml parsing. But can we fail not on runtime exceptions?
79     }
80 }