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