Remove yang-test
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / AbstractYangTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yangjmxgenerator;
9
10 import static org.hamcrest.CoreMatchers.is;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThat;
13
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Sets;
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.YangModelSearchUtils;
26 import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32
33 public abstract class AbstractYangTest {
34     protected SchemaContext context;
35     protected Map<String, Module> namesToModules; // are module names globally
36                                                   // unique?
37     protected Module configModule, rpcContextModule, threadsModule,
38             threadsJavaModule, bgpListenerJavaModule, ietfInetTypesModule,
39             jmxModule, jmxImplModule, testFilesModule, testFiles1Module;
40
41     public static final String EVENTBUS_MXB_NAME = "eventbus";
42     public static final String ASYNC_EVENTBUS_MXB_NAME = "async-eventbus";
43     public static final String THREADFACTORY_NAMING_MXB_NAME = "threadfactory-naming";
44     public static final String THREADPOOL_DYNAMIC_MXB_NAME = "threadpool-dynamic";
45     public static final String THREADPOOL_REGISTRY_IMPL_NAME = "threadpool-registry-impl";
46
47     public static final String BGP_LISTENER_IMPL_MXB_NAME = "bgp-listener-impl";
48
49     @Before
50     public void loadYangFiles() throws Exception {
51         final List<String> yangResources = new ArrayList<>();
52         yangResources.add("/test-config-threads.yang");
53         yangResources.add("/test-config-threads-java.yang");
54         yangResources.add("/config-bgp-listener-impl.yang");
55         yangResources.add("/ietf-inet-types.yang");
56         yangResources.add("/config-jmx-it.yang");
57         yangResources.add("/config-jmx-it-impl.yang");
58         yangResources.add("/test-config-files.yang");
59         yangResources.add("/test-config-files1.yang");
60
61         yangResources.addAll(getConfigApiYangs());
62
63         this.context = YangParserTestUtils.parseYangResources(AbstractYangTest.class, yangResources);
64
65         this.namesToModules = YangModelSearchUtils.mapModulesByNames(this.context
66                 .getModules());
67         this.configModule = this.namesToModules.get(ConfigConstants.CONFIG_MODULE);
68         this.rpcContextModule = this.namesToModules.get(ConfigConstants.CONFIG_MODULE);
69         this.threadsModule = this.namesToModules
70                 .get(ConfigConstants.CONFIG_THREADS_MODULE);
71         this.threadsJavaModule = this.namesToModules.get("config-threads-java");
72         this.bgpListenerJavaModule = this.namesToModules.get("config-bgp-listener-impl");
73         this.ietfInetTypesModule = this.namesToModules
74                 .get(ConfigConstants.IETF_INET_TYPES);
75         this.jmxModule = this.namesToModules.get("config-jmx-it");
76         this.jmxImplModule = this.namesToModules.get("config-jmx-it-impl");
77         this.testFilesModule = this.namesToModules.get("test-config-files");
78         this.testFiles1Module = this.namesToModules.get("test-config-files1");
79     }
80
81     public static List<String> getConfigApiYangs() {
82         return ImmutableList.of("/META-INF/yang/config@2013-04-05.yang", "/META-INF/yang/rpc-context@2013-06-17.yang");
83     }
84
85     public Map<QName, IdentitySchemaNode> mapIdentitiesByQNames(final Module module) {
86         final Map<QName, IdentitySchemaNode> result = new HashMap<>();
87         for (final IdentitySchemaNode identitySchemaNode : module.getIdentities()) {
88             final QName qName = identitySchemaNode.getQName();
89             Preconditions.checkArgument(
90                     result.containsKey(qName) == false,
91                     "Two identities of %s contain same qname %s",
92                             module, qName);
93             result.put(qName, identitySchemaNode);
94         }
95         return result;
96     }
97
98     protected static List<InputStream> getStreams(final String... paths) {
99         final List<InputStream> result = new ArrayList<>();
100         for (final String path : paths) {
101             final InputStream is = AbstractYangTest.class.getResourceAsStream(path);
102             assertNotNull(path + " is null", is);
103             result.add(is);
104         }
105         return result;
106     }
107
108     protected Map<QName, ServiceInterfaceEntry>  loadThreadsServiceInterfaceEntries(final String packageName) {
109         final Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
110         return ServiceInterfaceEntry.create(this.threadsModule, packageName,identitiesToSIs);
111     }
112
113     protected Map<String /* identity local name */, ModuleMXBeanEntry> loadThreadsJava(
114             final Map<QName, ServiceInterfaceEntry> modulesToSIEs, final String packageName) {
115         final Map<String /* identity local name */, ModuleMXBeanEntry> namesToMBEs = ModuleMXBeanEntry
116                 .create(this.threadsJavaModule, modulesToSIEs, this.context, new TypeProviderWrapper(new TypeProviderImpl
117                 (this.context)), packageName);
118         Assert.assertNotNull(namesToMBEs);
119         final Set<String> expectedMXBeanNames = Sets.newHashSet(EVENTBUS_MXB_NAME,
120                 ASYNC_EVENTBUS_MXB_NAME, THREADFACTORY_NAMING_MXB_NAME,
121                 THREADPOOL_DYNAMIC_MXB_NAME, THREADPOOL_REGISTRY_IMPL_NAME);
122         assertThat(namesToMBEs.keySet(), is(expectedMXBeanNames));
123         return namesToMBEs;
124     }
125
126 }