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