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.util.Arrays;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
22 import java.util.Map.Entry;
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;
30 public class ServiceInterfaceEntryTest extends AbstractYangTest {
31 public static final String PACKAGE_NAME = "packages.sis";
32 public static final List<String> expectedSIEFileNames = toFileNames("[EventBusServiceInterface"
34 + "ScheduledThreadPoolServiceInterface"
35 + ".java, ThreadFactoryServiceInterface.java, ThreadPoolServiceInterface.java]");
37 private static final URI THREADS_NAMESPACE;
38 private static final Revision THREADS_REVISION_DATE = Revision.of("2013-04-09");
42 THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE + ":threads");
43 } catch (URISyntaxException e) {
44 throw new ExceptionInInitializerError(e);
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";
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(", "));
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()));
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
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));
93 withBaseType.add(qName);
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));
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"));
117 String expectedDescription = "An extension of the simple pool of threads able to schedule\n"
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));
128 static String trimInnerSpacesOrNull(final String input) {
132 return input.replaceAll("\\s{2,}", " ");