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