2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf;
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;
15 import java.net.MalformedURLException;
17 import java.util.Collections;
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;
24 public class LibraryModulesSchemasTest {
27 public void testCreate() throws Exception {
28 // test create from xml
29 LibraryModulesSchemas libraryModulesSchemas =
30 LibraryModulesSchemas.create(getClass().getResource("/yang-library.xml").toString());
32 verifySchemas(libraryModulesSchemas);
34 // test create from json
35 LibraryModulesSchemas libraryModuleSchemas =
36 LibraryModulesSchemas.create(getClass().getResource("/yang-library.json").toString());
38 verifySchemas(libraryModulesSchemas);
41 private static void verifySchemas(final LibraryModulesSchemas libraryModulesSchemas) throws MalformedURLException {
42 final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
43 assertThat(resolvedModulesSchema.size(), is(3));
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")));
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")));
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/")));
65 public void testCreateInvalidModulesEntries() throws Exception {
66 LibraryModulesSchemas libraryModulesSchemas =
67 LibraryModulesSchemas.create(getClass().getResource("/yang-library-fail.xml").toString());
69 final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
70 assertThat(resolvedModulesSchema.size(), is(1));
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")));
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()));
85 // TODO test also fail on json and xml parsing. But can we fail not on runtime exceptions?