Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / ServiceInterfaceEntryTest.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.assertNotNull;
12 import static org.junit.Assert.assertThat;
13
14 import com.google.common.collect.Sets;
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.Set;
24 import org.hamcrest.CoreMatchers;
25 import org.junit.Test;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.Revision;
28 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
29
30 public class ServiceInterfaceEntryTest extends AbstractYangTest {
31     public static final String PACKAGE_NAME = "packages.sis";
32     public static final List<String> expectedSIEFileNames = toFileNames("[EventBusServiceInterface"
33             + ".java, "
34             + "ScheduledThreadPoolServiceInterface"
35             + ".java, ThreadFactoryServiceInterface.java, ThreadPoolServiceInterface.java]");
36
37     private static final URI THREADS_NAMESPACE;
38     private static final Revision THREADS_REVISION_DATE = Revision.of("2013-04-09");
39
40     static {
41         try {
42             THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE + ":threads");
43         } catch (URISyntaxException e) {
44             throw new ExceptionInInitializerError(e);
45         }
46     }
47
48     public static final QName EVENTBUS_QNAME = QName.create(THREADS_NAMESPACE,
49             THREADS_REVISION_DATE, "eventbus");
50     public static final QName THREADFACTORY_QNAME = QName.create(
51             THREADS_NAMESPACE, THREADS_REVISION_DATE, "threadfactory");
52     public static final QName THREADPOOL_QNAME = QName.create(THREADS_NAMESPACE,
53             THREADS_REVISION_DATE, "threadpool");
54     public static final QName SCHEDULED_THREADPOOL_QNAME = QName.create(
55             THREADS_NAMESPACE, THREADS_REVISION_DATE, "scheduled-threadpool");
56     public static final QName SCHEDULED_EXECUTOR_SERVICE_QNAME = QName.create(
57             THREADS_NAMESPACE, THREADS_REVISION_DATE,
58             "scheduled-executor-service");
59     public static final String SCHEDULED_THREADPOOL_INTERFACE_NAME = "ScheduledThreadPoolServiceInterface";
60
61     public static List<String> toFileNames(String fileNameString) {
62         assertThat(fileNameString.startsWith("["), CoreMatchers.is(true));
63         assertThat(fileNameString.endsWith("]"), CoreMatchers.is(true));
64         fileNameString = fileNameString.substring(1,
65                 fileNameString.length() - 1);
66         return Arrays.asList(fileNameString.split(", "));
67     }
68
69     @Test
70     public void testCreateFromIdentities() {
71         // each identity has to have a base that leads to service-type
72         Map<IdentitySchemaNode, ServiceInterfaceEntry> definedIdentities = new HashMap<>();
73         Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
74                 .create(threadsModule, PACKAGE_NAME,definedIdentities);
75         // expected eventbus, threadfactory, threadpool,
76         // scheduled-threadpool,thread-rpc-context
77         assertThat(namesToSIEntries.size(), is(expectedSIEFileNames.size()));
78
79         Set<QName> withNoBaseType = Sets.newHashSet(EVENTBUS_QNAME,
80                 THREADFACTORY_QNAME, THREADPOOL_QNAME,
81                 SCHEDULED_EXECUTOR_SERVICE_QNAME);
82         HashSet<QName> withBaseType = new HashSet<>();
83         for (Entry<QName, ServiceInterfaceEntry> entry : namesToSIEntries
84                 .entrySet()) {
85             QName qName = entry.getKey();
86             if (withNoBaseType.contains(qName)) {
87                 ServiceInterfaceEntry sie = namesToSIEntries.get(qName);
88                 assertNotNull(qName + " not found", sie);
89                 assertThat(qName + " should have empty base type", sie
90                         .getBase().isPresent(), is(false));
91                 assertThat(sie.getQName(), is(qName));
92             } else {
93                 withBaseType.add(qName);
94             }
95         }
96         // scheduled-threadpool has super type threadpool
97         assertThat(withBaseType,
98                 is(Sets.newHashSet(SCHEDULED_THREADPOOL_QNAME)));
99         assertThat(withBaseType.contains(SCHEDULED_THREADPOOL_QNAME), is(true));
100         ServiceInterfaceEntry scheduled = namesToSIEntries
101                 .get(SCHEDULED_THREADPOOL_QNAME);
102         assertNotNull(scheduled);
103         assertThat(scheduled.getQName(), is(SCHEDULED_THREADPOOL_QNAME));
104         ServiceInterfaceEntry threadPool = namesToSIEntries
105                 .get(THREADPOOL_QNAME);
106         assertNotNull(threadPool);
107         assertThat("scheduled-threadpool should extend threadpool", scheduled
108                 .getBase().get(), is(threadPool));
109
110         assertThat(scheduled.getExportedOsgiClassName(),
111                 is(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX
112                         + ".threadpool.ScheduledThreadPool"));
113         assertThat(threadPool.getExportedOsgiClassName(),
114                 is(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX
115                         + ".threadpool.ThreadPool"));
116
117         String expectedDescription = "An extension of the simple pool of threads able to schedule "
118                 + "work to be executed at some point in time.";
119         assertThat(trimInnerSpacesOrNull(scheduled.getNullableDescription()),
120                 is(expectedDescription));
121         assertThat(scheduled.getPackageName(), is(PACKAGE_NAME));
122         assertThat(scheduled.getTypeName(),
123                 is(SCHEDULED_THREADPOOL_INTERFACE_NAME));
124         assertThat(scheduled.getFullyQualifiedName(), is(PACKAGE_NAME + "."
125                 + SCHEDULED_THREADPOOL_INTERFACE_NAME));
126     }
127
128     static String trimInnerSpacesOrNull(final String input) {
129         if (input == null) {
130             return null;
131         }
132         return input.replaceAll("\\s{2,}", " ");
133     }
134 }

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