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.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);
39 private static void verifySchemas(final LibraryModulesSchemas libraryModulesSchemas) throws MalformedURLException {
40 final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
41 assertThat(resolvedModulesSchema.size(), is(3));
43 assertTrue(resolvedModulesSchema.containsKey(new SourceIdentifier("module-with-revision", "2014-04-08")));
44 assertThat(resolvedModulesSchema.get(new SourceIdentifier("module-with-revision", "2014-04-08")),
45 is(new URL("http://localhost:8181/yanglib/schemas/module-with-revision/2014-04-08")));
47 assertTrue(resolvedModulesSchema.containsKey(
48 new SourceIdentifier("another-module-with-revision", "2013-10-21")));
49 assertThat(resolvedModulesSchema.get(new SourceIdentifier("another-module-with-revision", "2013-10-21")),
50 is(new URL("http://localhost:8181/yanglib/schemas/another-module-with-revision/2013-10-21")));
52 assertTrue(resolvedModulesSchema.containsKey(new SourceIdentifier("module-without-revision")));
53 assertThat(resolvedModulesSchema.get(new SourceIdentifier("module-without-revision")),
54 is(new URL("http://localhost:8181/yanglib/schemas/module-without-revision/")));
58 public void testCreateInvalidModulesEntries() throws Exception {
59 LibraryModulesSchemas libraryModulesSchemas =
60 LibraryModulesSchemas.create(getClass().getResource("/yang-library-fail.xml").toString());
62 final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
63 assertThat(resolvedModulesSchema.size(), is(1));
65 assertFalse(resolvedModulesSchema.containsKey(new SourceIdentifier("module-with-bad-url")));
66 //See BUG 8071 https://bugs.opendaylight.org/show_bug.cgi?id=8071
67 //assertFalse(resolvedModulesSchema.containsKey(
68 // RevisionSourceIdentifier.create("module-with-bad-revision", "bad-revision")));
69 assertTrue(resolvedModulesSchema.containsKey(new SourceIdentifier("good-ol-module")));
73 public void testCreateFromInvalidAll() throws Exception {
74 // test bad yang lib url
75 LibraryModulesSchemas libraryModulesSchemas = LibraryModulesSchemas.create("ObviouslyBadUrl");
76 assertThat(libraryModulesSchemas.getAvailableModels(), is(Collections.emptyMap()));
78 // TODO test also fail on json and xml parsing. But can we fail not on runtime exceptions?