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

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