2 * Copyright (c) 2014 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.yangtools.it.yang.runtime.tests;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import java.io.IOException;
17 import java.util.Enumeration;
18 import java.util.Vector;
20 import org.junit.Test;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
22 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
23 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
24 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
25 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
26 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import com.google.common.base.Optional;
30 import com.google.common.collect.ImmutableSet;
32 public class ModelDiscoveryTest {
34 public static final YangModuleInfo TOPOLOGY_OLD_MODULE = org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev130712.$YangModuleInfoImpl
36 public static final YangModuleInfo TOPOLOGY_NEW_MODULE = org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.$YangModuleInfoImpl
39 public static final String METAINF_PATH = "META-INF/services/" + YangModelBindingProvider.class.getName();
42 public void discoveryUsing_BindingReflections_TCCL() {
44 ImmutableSet<YangModuleInfo> moduleInfoSet = BindingReflections.loadModuleInfos();
45 assertNotNull(moduleInfoSet);
46 assertFalse(moduleInfoSet.isEmpty());
47 assertTrue(moduleInfoSet.contains(TOPOLOGY_NEW_MODULE));
51 public void discoveryUsing_BindingReflections_classloader_partialServiceMetadata() throws Exception {
53 ClassLoader topologyModelClassLoader = new ClassLoader(Thread.currentThread().getContextClassLoader()) {
56 public Enumeration<URL> getResources(String name) throws IOException {
57 if (METAINF_PATH.equals(name)) {
58 Vector<URL> topologyUrlVector = new Vector<>();
59 topologyUrlVector.add(TOPOLOGY_NEW_MODULE.getClass().getResource("/" + METAINF_PATH));
60 return topologyUrlVector.elements();
62 return super.getResources(name);
66 ImmutableSet<YangModuleInfo> moduleInfoSet = BindingReflections.loadModuleInfos(topologyModelClassLoader);
67 assertNotNull(moduleInfoSet);
68 assertFalse(moduleInfoSet.isEmpty());
69 assertTrue(moduleInfoSet.contains(TOPOLOGY_NEW_MODULE));
73 public void moduleInfoBackedContextTCCL() throws Exception {
75 ModuleInfoBackedContext context = ModuleInfoBackedContext.create(GeneratedClassLoadingStrategy.getAlwaysFailClassLoadingStrategy());
77 ImmutableSet<YangModuleInfo> moduleInfoSet = BindingReflections.loadModuleInfos();
79 context.addModuleInfos(moduleInfoSet);
80 assertNotNull(moduleInfoSet);
81 assertFalse(moduleInfoSet.isEmpty());
82 assertTrue(moduleInfoSet.contains(TOPOLOGY_NEW_MODULE));
84 Class<?> linkClass = context.loadClass(Link.class.getName());
85 assertNotNull(linkClass);
86 assertEquals(Link.class, linkClass);
87 Optional<SchemaContext> schemaContext = context.tryToCreateSchemaContext();
88 assertNotNull(schemaContext);
89 assertNotNull(schemaContext.get());