0da61b46c336043566d7b0b5c7b7da394771bde9
[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 java.net.URI;
15 import java.net.URISyntaxException;
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.util.Arrays;
19 import java.util.Date;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Map.Entry;
24 import java.util.Set;
25
26 import org.hamcrest.CoreMatchers;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.common.QName;
29
30 import com.google.common.collect.Sets;
31
32 public class ServiceInterfaceEntryTest extends AbstractYangTest {
33     public static final String PACKAGE_NAME = "packages.sis";
34     public static final List<String> expectedSIEFileNames = toFileNames("[EventBusServiceInterface"
35             + ".java, "
36             + "ScheduledThreadPoolServiceInterface"
37             + ".java, ThreadFactoryServiceInterface.java, ThreadPoolServiceInterface.java]");
38
39     private static final URI THREADS_NAMESPACE;
40     private static final Date THREADS_REVISION_DATE;
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<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
81                 .create(threadsModule, PACKAGE_NAME);
82         // expected eventbus, threadfactory, threadpool,
83         // scheduled-threadpool,thread-rpc-context
84         assertThat(namesToSIEntries.size(), is(expectedSIEFileNames.size()));
85
86         Set<QName> withNoBaseType = Sets.newHashSet(EVENTBUS_QNAME,
87                 THREADFACTORY_QNAME, THREADPOOL_QNAME,
88                 SCHEDULED_EXECUTOR_SERVICE_QNAME);
89         HashSet<QName> withBaseType = new HashSet<>();
90         for (Entry<QName, ServiceInterfaceEntry> entry : namesToSIEntries
91                 .entrySet()) {
92             QName qName = entry.getKey();
93             if (withNoBaseType.contains(qName)) {
94                 ServiceInterfaceEntry sie = namesToSIEntries.get(qName);
95                 assertNotNull(qName + " not found", sie);
96                 assertThat(qName + " should have empty base type", sie
97                         .getBase().isPresent(), is(false));
98                 assertThat(sie.getQName(), is(qName));
99             } else {
100                 withBaseType.add(qName);
101             }
102         }
103         // scheduled-threadpool has super type threadpool
104         assertThat(withBaseType,
105                 is(Sets.newHashSet(SCHEDULED_THREADPOOL_QNAME)));
106         assertThat(withBaseType.contains(SCHEDULED_THREADPOOL_QNAME), is(true));
107         ServiceInterfaceEntry scheduled = namesToSIEntries
108                 .get(SCHEDULED_THREADPOOL_QNAME);
109         assertNotNull(scheduled);
110         assertThat(scheduled.getQName(), is(SCHEDULED_THREADPOOL_QNAME));
111         ServiceInterfaceEntry threadPool = namesToSIEntries
112                 .get(THREADPOOL_QNAME);
113         assertNotNull(threadPool);
114         assertThat("scheduled-threadpool should extend threadpool", scheduled
115                 .getBase().get(), is(threadPool));
116
117         assertThat(scheduled.getExportedOsgiClassName(),
118                 is(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX
119                         + ".threadpool.ScheduledThreadPool"));
120         assertThat(threadPool.getExportedOsgiClassName(),
121                 is(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX
122                         + ".threadpool.ThreadPool"));
123
124         String expectedDescription = "An extension of the simple pool of threads able to schedule "
125                 + "work to be executed at some point in time.";
126         assertThat(trimInnerSpacesOrNull(scheduled.getNullableDescription()),
127                 is(expectedDescription));
128         assertThat(scheduled.getPackageName(), is(PACKAGE_NAME));
129         assertThat(scheduled.getTypeName(),
130                 is(SCHEDULED_THREADPOOL_INTERFACE_NAME));
131         assertThat(scheduled.getFullyQualifiedName(), is(PACKAGE_NAME + "."
132                 + SCHEDULED_THREADPOOL_INTERFACE_NAME));
133     }
134
135     static String trimInnerSpacesOrNull(String input) {
136         if (input == null)
137             return null;
138         return input.replaceAll("\\s{2,}", " ");
139     }
140 }