Merge "Complete implementation of DataChangeListenerProxy"
[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
16 import java.net.URI;
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;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Set;
28
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;
33
34 public class ServiceInterfaceEntryTest extends AbstractYangTest {
35     public static final String PACKAGE_NAME = "packages.sis";
36     public static final List<String> expectedSIEFileNames = toFileNames("[EventBusServiceInterface"
37             + ".java, "
38             + "ScheduledThreadPoolServiceInterface"
39             + ".java, ThreadFactoryServiceInterface.java, ThreadPoolServiceInterface.java]");
40
41     private static final URI THREADS_NAMESPACE;
42     private static final Date THREADS_REVISION_DATE;
43
44     static {
45         try {
46             THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
47                     + ":threads");
48         } catch (URISyntaxException e) {
49             throw new Error(e);
50         }
51         SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
52         try {
53             THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
54         } catch (ParseException e) {
55             throw new Error(e);
56         }
57     }
58
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";
71
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(", "));
78     }
79
80     @Test
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()));
89
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
95                 .entrySet()) {
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));
103             } else {
104                 withBaseType.add(qName);
105             }
106         }
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));
120
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"));
127
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));
137     }
138
139     static String trimInnerSpacesOrNull(final String input) {
140         if (input == null) {
141             return null;
142         }
143         return input.replaceAll("\\s{2,}", " ");
144     }
145 }