2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yangjmxgenerator;
10 import static org.hamcrest.core.Is.is;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThat;
14 import com.google.common.collect.Sets;
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;
25 import java.util.Map.Entry;
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;
32 public class ServiceInterfaceEntryTest extends AbstractYangTest {
33 public static final String PACKAGE_NAME = "packages.sis";
34 public static final List<String> expectedSIEFileNames = toFileNames("[EventBusServiceInterface"
36 + "ScheduledThreadPoolServiceInterface"
37 + ".java, ThreadFactoryServiceInterface.java, ThreadPoolServiceInterface.java]");
39 private static final URI THREADS_NAMESPACE;
40 private static final Date THREADS_REVISION_DATE;
44 THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
46 } catch (URISyntaxException e) {
49 SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
51 THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
52 } catch (ParseException e) {
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";
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(", "));
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()));
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
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));
102 withBaseType.add(qName);
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));
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"));
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));
137 static String trimInnerSpacesOrNull(final String input) {
141 return input.replaceAll("\\s{2,}", " ");