Merge "Clean up netconf-parent root pom"
[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.common.Revision;
19 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
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
40     private static void verifySchemas(final LibraryModulesSchemas libraryModulesSchemas) throws MalformedURLException {
41         final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
42         Assert.assertThat(resolvedModulesSchema.size(), is(3));
43
44         Assert.assertTrue(resolvedModulesSchema.containsKey(RevisionSourceIdentifier.create("module-with-revision",
45                 Revision.of("2014-04-08"))));
46         Assert.assertThat(resolvedModulesSchema.get(
47                 RevisionSourceIdentifier.create("module-with-revision", Revision.of("2014-04-08"))),
48                 is(new URL("http://localhost:8181/yanglib/schemas/module-with-revision/2014-04-08")));
49
50         Assert.assertTrue(resolvedModulesSchema.containsKey(
51                 RevisionSourceIdentifier.create("another-module-with-revision", Revision.of("2013-10-21"))));
52         Assert.assertThat(resolvedModulesSchema.get(
53                 RevisionSourceIdentifier.create("another-module-with-revision", Revision.of("2013-10-21"))),
54                 is(new URL("http://localhost:8181/yanglib/schemas/another-module-with-revision/2013-10-21")));
55
56         Assert.assertTrue(resolvedModulesSchema.containsKey(
57                 RevisionSourceIdentifier.create("module-without-revision")));
58         Assert.assertThat(resolvedModulesSchema.get(
59                 RevisionSourceIdentifier.create("module-without-revision")),
60                 is(new URL("http://localhost:8181/yanglib/schemas/module-without-revision/")));
61     }
62
63     @Test
64     public void testCreateInvalidModulesEntries() throws Exception {
65         LibraryModulesSchemas libraryModulesSchemas =
66                 LibraryModulesSchemas.create(getClass().getResource("/yang-library-fail.xml").toString());
67
68         final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
69         Assert.assertThat(resolvedModulesSchema.size(), is(1));
70
71         Assert.assertFalse(resolvedModulesSchema.containsKey(
72                 RevisionSourceIdentifier.create("module-with-bad-url")));
73         //See BUG 8071 https://bugs.opendaylight.org/show_bug.cgi?id=8071
74         //Assert.assertFalse(resolvedModulesSchema.containsKey(
75         //        RevisionSourceIdentifier.create("module-with-bad-revision", "bad-revision")));
76         Assert.assertTrue(resolvedModulesSchema.containsKey(
77                 RevisionSourceIdentifier.create("good-ol-module")));
78     }
79
80
81     @Test
82     public void testCreateFromInvalidAll() throws Exception {
83         // test bad yang lib url
84         LibraryModulesSchemas libraryModulesSchemas = LibraryModulesSchemas.create("ObviouslyBadUrl");
85         Assert.assertThat(libraryModulesSchemas.getAvailableModels(), is(Collections.emptyMap()));
86
87         // TODO test also fail on json and xml parsing. But can we fail not on runtime exceptions?
88     }
89 }