Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / SchemaContextTest.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.core.Is.is;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.MODULE_TYPE_Q_NAME;
16 import static org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.SERVICE_TYPE_Q_NAME;
17
18 import com.google.common.base.Optional;
19 import com.google.common.collect.ImmutableMap;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
29
30 public class SchemaContextTest extends AbstractYangTest {
31
32     IdentitySchemaNode findIdentityByQName(final Module module, final QName qName) {
33         Map<QName, IdentitySchemaNode> mapIdentitiesByQNames = mapIdentitiesByQNames(module);
34         IdentitySchemaNode found = mapIdentitiesByQNames.get(qName);
35         assertNotNull(found);
36         return found;
37     }
38
39     @Test
40     public void testReadingIdentities_threadsModule() {
41
42         IdentitySchemaNode serviceType = findIdentityByQName(configModule,
43                 SERVICE_TYPE_Q_NAME);
44
45         Map<String /* identity name */, Optional<QName>> expectedIdentitiesToBases = ImmutableMap
46                 .of("eventbus", Optional.<QName>absent(), "threadfactory", Optional.<QName>absent(), "threadpool",
47                         Optional.<QName>absent(), "scheduled-threadpool", Optional.<QName>absent());
48
49         assertThat(threadsModule.getIdentities().size(),
50                 is(expectedIdentitiesToBases.size()));
51         assertAllIdentitiesAreExpected(threadsModule, expectedIdentitiesToBases);
52
53         IdentitySchemaNode eventBusSchemaNode = null;
54         for (IdentitySchemaNode id : threadsModule.getIdentities()) {
55             String localName = id.getQName().getLocalName();
56
57             if (localName.equals("eventbus")) {
58                 eventBusSchemaNode = id;
59             }
60             // all except scheduled-threadpool should have base set to
61             // serviceType
62             if (localName.equals("scheduled-threadpool") == false) {
63                 assertEquals(serviceType, id.getBaseIdentities().iterator().next());
64             }
65         }
66         assertNotNull(eventBusSchemaNode);
67         // check unknown schma nodes
68         List<UnknownSchemaNode> unknownSchemaNodes = eventBusSchemaNode
69                 .getUnknownSchemaNodes();
70         assertEquals(1, unknownSchemaNodes.size());
71         UnknownSchemaNode usn = unknownSchemaNodes.get(0);
72         assertEquals("com.google.common.eventbus.EventBus", usn.getQName()
73                 .getLocalName());
74         assertEquals(ConfigConstants.JAVA_CLASS_EXTENSION_QNAME,
75                 usn.getNodeType());
76     }
77
78     private static void assertAllIdentitiesAreExpected(final Module module,
79             final Map<String /* identity name */, Optional<QName>> expectedIdentitiesToBases) {
80         Map<String /* identity name */, Optional<QName>> copyOfExpectedNames = new HashMap<>(
81                 expectedIdentitiesToBases);
82         for (IdentitySchemaNode id : module.getIdentities()) {
83             String localName = id.getQName().getLocalName();
84             assertTrue("Unexpected identity " + localName,
85                     copyOfExpectedNames.containsKey(localName));
86             Optional<QName> maybeExpectedBaseQName = copyOfExpectedNames
87                     .remove(localName);
88             if (maybeExpectedBaseQName.isPresent()) {
89                 assertEquals("Unexpected base identity of " + localName,
90                         maybeExpectedBaseQName.get(), id.getBaseIdentities().iterator().next().getQName());
91             }
92         }
93         assertEquals("Expected identities not found " + copyOfExpectedNames,
94                 Collections.emptyMap(), copyOfExpectedNames);
95     }
96
97     @Test
98     public void testReadingIdentities_threadsJavaModule() {
99         Map<String /* identity name */, Optional<QName>> expected = new HashMap<>();
100         expected.put(ModuleMXBeanEntryTest.EVENTBUS_MXB_NAME, Optional.of(MODULE_TYPE_Q_NAME));
101         expected.put(ModuleMXBeanEntryTest.ASYNC_EVENTBUS_MXB_NAME, Optional.of(MODULE_TYPE_Q_NAME));
102         expected.put(ModuleMXBeanEntryTest.THREADFACTORY_NAMING_MXB_NAME, Optional.of(MODULE_TYPE_Q_NAME));
103         expected.put(ModuleMXBeanEntryTest.THREADPOOL_DYNAMIC_MXB_NAME, Optional.of(MODULE_TYPE_Q_NAME));
104         expected.put("thread-rpc-context", Optional.<QName>absent());
105         expected.put(ModuleMXBeanEntryTest.THREADPOOL_REGISTRY_IMPL_NAME, Optional.of(MODULE_TYPE_Q_NAME));
106
107         assertAllIdentitiesAreExpected(threadsJavaModule, expected);
108     }
109 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.