Fix checkstyle warnings in yang-jmx-generator.
[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(Module module, 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.getBaseIdentity());
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 void assertAllIdentitiesAreExpected(
79             Module module,
80             Map<String /* identity name */, Optional<QName>> expectedIdentitiesToBases) {
81         Map<String /* identity name */, Optional<QName>> copyOfExpectedNames = new HashMap<>(
82                 expectedIdentitiesToBases);
83         for (IdentitySchemaNode id : module.getIdentities()) {
84             String localName = id.getQName().getLocalName();
85             assertTrue("Unexpected identity " + localName,
86                     copyOfExpectedNames.containsKey(localName));
87             Optional<QName> maybeExpectedBaseQName = copyOfExpectedNames
88                     .remove(localName);
89             if (maybeExpectedBaseQName.isPresent()) {
90                 assertEquals("Unexpected base identity of " + localName,
91                         maybeExpectedBaseQName.get(), id.getBaseIdentity()
92                                 .getQName());
93             }
94         }
95         assertEquals("Expected identities not found " + copyOfExpectedNames,
96                 Collections.emptyMap(), copyOfExpectedNames);
97     }
98
99     @Test
100     public void testReadingIdentities_threadsJavaModule() {
101         Map<String /* identity name */, Optional<QName>> expectedIdentitiesToBases = new HashMap<String, Optional<QName>>(){
102             private static final long serialVersionUID = 1L;
103
104             {
105                 put(ModuleMXBeanEntryTest.EVENTBUS_MXB_NAME, Optional.of(MODULE_TYPE_Q_NAME));
106                 put(ModuleMXBeanEntryTest.ASYNC_EVENTBUS_MXB_NAME, Optional.of(MODULE_TYPE_Q_NAME));
107                 put(ModuleMXBeanEntryTest.THREADFACTORY_NAMING_MXB_NAME, Optional.of(MODULE_TYPE_Q_NAME));
108                 put(ModuleMXBeanEntryTest.THREADPOOL_DYNAMIC_MXB_NAME, Optional.of(MODULE_TYPE_Q_NAME));
109                 put("thread-rpc-context", Optional.<QName>absent());
110                 put(ModuleMXBeanEntryTest.THREADPOOL_REGISTRY_IMPL_NAME, Optional.of(MODULE_TYPE_Q_NAME));
111             }
112         };
113
114         assertAllIdentitiesAreExpected(threadsJavaModule,
115                 expectedIdentitiesToBases);
116     }
117
118 }

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