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.model.repo.api.RevisionSourceIdentifier;
19 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21 public class LibraryModulesSchemasTest {
24 public void testCreate() throws Exception {
25 // test create from xml
26 LibraryModulesSchemas libraryModulesSchemas =
27 LibraryModulesSchemas.create(getClass().getResource("/yang-library.xml").toString());
29 verifySchemas(libraryModulesSchemas);
31 // test create from json
32 LibraryModulesSchemas libraryModuleSchemas =
33 LibraryModulesSchemas.create(getClass().getResource("/yang-library.json").toString());
35 verifySchemas(libraryModulesSchemas);
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));
43 Assert.assertTrue(resolvedModulesSchema.containsKey(RevisionSourceIdentifier.create("module-with-revision",
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")));
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")));
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/")));
63 public void testCreateInvalidModulesEntries() throws Exception {
64 LibraryModulesSchemas libraryModulesSchemas =
65 LibraryModulesSchemas.create(getClass().getResource("/yang-library-fail.xml").toString());
67 final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
68 Assert.assertThat(resolvedModulesSchema.size(), is(1));
70 Assert.assertFalse(resolvedModulesSchema.containsKey(
71 RevisionSourceIdentifier.create("module-with-bad-url")));
72 Assert.assertFalse(resolvedModulesSchema.containsKey(
73 RevisionSourceIdentifier.create("module-with-bad-revision", "bad-revision")));
74 Assert.assertTrue(resolvedModulesSchema.containsKey(
75 RevisionSourceIdentifier.create("good-ol-module")));
80 public void testCreateFromInvalidAll() throws Exception {
81 // test bad yang lib url
82 LibraryModulesSchemas libraryModulesSchemas = LibraryModulesSchemas.create("ObviouslyBadUrl");
83 Assert.assertThat(libraryModulesSchemas.getAvailableModels(), is(Collections.emptyMap()));
85 // TODO test also fail on json and xml parsing. But can we fail not on runtime exceptions?