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

©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.