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;
12 import java.net.MalformedURLException;
14 import java.util.Collections;
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;
22 public class LibraryModulesSchemasTest {
25 public void testCreate() throws Exception {
26 // test create from xml
27 LibraryModulesSchemas libraryModulesSchemas =
28 LibraryModulesSchemas.create(getClass().getResource("/yang-library.xml").toString());
30 verifySchemas(libraryModulesSchemas);
32 // test create from json
33 LibraryModulesSchemas libraryModuleSchemas =
34 LibraryModulesSchemas.create(getClass().getResource("/yang-library.json").toString());
36 verifySchemas(libraryModulesSchemas);
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));
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")));
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")));
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/")));
64 public void testCreateInvalidModulesEntries() throws Exception {
65 LibraryModulesSchemas libraryModulesSchemas =
66 LibraryModulesSchemas.create(getClass().getResource("/yang-library-fail.xml").toString());
68 final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
69 Assert.assertThat(resolvedModulesSchema.size(), is(1));
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")));
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()));
87 // TODO test also fail on json and xml parsing. But can we fail not on runtime exceptions?