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