* fix : yang-generator can't find SIE if its base is another SIE
[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 com.google.common.collect.Sets;
11 import java.net.URI;
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;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
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;
30
31 public class ServiceInterfaceEntryTest extends AbstractYangTest {
32     public static final String PACKAGE_NAME = "packages.sis";
33     public static final List<String> expectedSIEFileNames = toFileNames("[EventBusServiceInterface"
34             + ".java, "
35             + "ScheduledThreadPoolServiceInterface"
36             + ".java, ThreadFactoryServiceInterface.java, ThreadPoolServiceInterface.java]");
37
38     private static final URI THREADS_NAMESPACE;
39     private static final Date THREADS_REVISION_DATE;
40
41     static {
42         try {
43             THREADS_NAMESPACE = new URI(ConfigConstants.CONFIG_NAMESPACE
44                     + ":threads");
45         } catch (URISyntaxException e) {
46             throw new Error(e);
47         }
48         SimpleDateFormat revisionFormat = new SimpleDateFormat("yyyy-MM-dd");
49         try {
50             THREADS_REVISION_DATE = revisionFormat.parse("2013-04-09");
51         } catch (ParseException e) {
52             throw new Error(e);
53         }
54     }
55
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";
68
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(", "));
75     }
76
77     @Test
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()));
86
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
92                 .entrySet()) {
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));
100             } else {
101                 withBaseType.add(qName);
102             }
103         }
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));
117
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"));
124
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));
134     }
135
136     static String trimInnerSpacesOrNull(String input) {
137         if (input == null)
138             return null;
139         return input.replaceAll("\\s{2,}", " ");
140     }
141 }