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.hamcrest.CoreMatchers;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.netconf.sal.connect.netconf.LibraryModulesSchemas;
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 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(new SourceIdentifier("module-with-revision", "2014-04-08")));
45 Assert.assertThat(resolvedModulesSchema.get(
46 new SourceIdentifier("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 new SourceIdentifier("another-module-with-revision", "2013-10-21")));
51 Assert.assertThat(resolvedModulesSchema.get(
52 new SourceIdentifier("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(new SourceIdentifier("module-without-revision")));
56 Assert.assertThat(resolvedModulesSchema.get(
57 new SourceIdentifier("module-without-revision")),
58 is(new URL("http://localhost:8181/yanglib/schemas/module-without-revision/")));
62 public void testCreateInvalidModulesEntries() throws Exception {
63 LibraryModulesSchemas libraryModulesSchemas =
64 LibraryModulesSchemas.create(getClass().getResource("/yang-library-fail.xml").toString());
66 final Map<SourceIdentifier, URL> resolvedModulesSchema = libraryModulesSchemas.getAvailableModels();
67 Assert.assertThat(resolvedModulesSchema.size(), is(1));
69 Assert.assertFalse(resolvedModulesSchema.containsKey(new SourceIdentifier("module-with-bad-url")));
70 Assert.assertFalse(resolvedModulesSchema.containsKey(
71 new SourceIdentifier("module-with-bad-revision", "bad-revision")));
72 Assert.assertTrue(resolvedModulesSchema.containsKey(new SourceIdentifier("good-ol-module")));
77 public void testCreateFromInvalidAll() throws Exception {
78 // test bad yang lib url
79 LibraryModulesSchemas libraryModulesSchemas = LibraryModulesSchemas.create("ObviouslyBadUrl");
80 Assert.assertThat(libraryModulesSchemas.getAvailableModels(), is(Collections.emptyMap()));
82 // TODO test also fail on json and xml parsing. But can we fail not on runtime exceptions?