Adjust for yangtools normalizing module names
[controller.git] / opendaylight / config / yang-jmx-generator / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / ModuleMXBeanEntryNameConflictTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12 import com.google.common.base.Preconditions;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.NameConflictException;
23 import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.YangModelSearchUtils;
24 import org.opendaylight.mdsal.binding.yang.types.TypeProviderImpl;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class ModuleMXBeanEntryNameConflictTest extends AbstractYangTest {
31
32     private static final Logger LOG = LoggerFactory
33             .getLogger(ModuleMXBeanEntryNameConflictTest.class);
34
35     public static final String PACKAGE_NAME = "pack2";
36     Map<File, String> testedFilesToYangModules = new HashMap<>();
37     Map<String, String> testedYangModulesToExpectedConflictingName = new HashMap<>();
38
39     @Ignore
40     @Test
41     public void testNameConflicts() throws Exception {
42         prepareSamples();
43         prepareExceptionAssertions();
44
45         for (final Map.Entry<File, String> currentTestEntry : this.testedFilesToYangModules
46                 .entrySet()) {
47             final String moduleName = currentTestEntry.getValue();
48             final File yangFile = currentTestEntry.getKey();
49             final Module testedModule = loadYangs(yangFile, moduleName);
50
51             try {
52                 LOG.debug("Testing {}", yangFile);
53                 ModuleMXBeanEntry.create(testedModule,
54                         new HashMap<>(), this.context,
55                         new TypeProviderWrapper(new TypeProviderImpl(this.context)),
56                         PACKAGE_NAME);
57                 fail(yangFile.toString()
58                         + " did not cause a name conflict and should");
59             } catch (final NameConflictException e) {
60                 assertEquals(
61                         this.testedYangModulesToExpectedConflictingName
62                                 .get(moduleName),
63                         e.getConflictingName());
64             }
65         }
66     }
67
68     private void prepareSamples() {
69         final File first = new File(getClass().getResource(
70                 "/duplicates/config-test-duplicate-attribute-in-list.yang")
71                 .getFile());
72         final File dir = first.getParentFile();
73
74         for (final File testYang : dir.listFiles()) {
75             final String moduleName = getYangModuleName(testYang.getName());
76             this.testedFilesToYangModules.put(testYang, moduleName);
77         }
78     }
79
80     private void prepareExceptionAssertions() {
81         this.testedYangModulesToExpectedConflictingName.put(
82                 "config-test-duplicate-attribute", "DtoA");
83         this.testedYangModulesToExpectedConflictingName.put(
84                 "config-test-duplicate-attribute-in-list", "DtoA");
85         this.testedYangModulesToExpectedConflictingName.put(
86                 "config-test-duplicate-attribute-runtime-bean", "DtoA");
87         this.testedYangModulesToExpectedConflictingName.put(
88                 "config-test-generated-attributes-name-conflict", "StateB");
89         this.testedYangModulesToExpectedConflictingName.put(
90                 "config-test-runtime-bean-list-name-conflict",
91                 "StateARuntimeMXBean");
92         this.testedYangModulesToExpectedConflictingName.put(
93                 "config-test-runtime-bean-list-name-conflict2",
94                 "StateARuntimeMXBean");
95         this.testedYangModulesToExpectedConflictingName
96                 .put("config-test-runtime-bean-name-conflict", "StateARuntimeMXBean");
97         this.testedYangModulesToExpectedConflictingName.put(
98                 "config-test-runtime-bean-name-conflict2",
99                 "StateARuntimeMXBean");
100         this.testedYangModulesToExpectedConflictingName.put(
101                 "config-test-duplicate-attribute-in-runtime-and-mxbean",
102                 "port");
103     }
104
105     private static String getYangModuleName(final String name) {
106         final int startIndex = 0;
107         final int endIndex = name.indexOf(".yang");
108         return name.substring(startIndex, endIndex);
109     }
110
111     private Module loadYangs(final File testedModule, final String moduleName)
112             throws Exception {
113         final List<InputStream> yangISs = new ArrayList<>();
114         yangISs.addAll(getStreams("/ietf-inet-types.yang"));
115
116         yangISs.add(new FileInputStream(testedModule));
117
118         yangISs.addAll(getConfigApiYangInputStreams());
119
120         this.context =  YangParserTestUtils.parseYangStreams(yangISs);
121         // close ISs
122         for (final InputStream is : yangISs) {
123             is.close();
124         }
125         this.namesToModules = YangModelSearchUtils.mapModulesByNames(this.context
126                 .getModules());
127         this.configModule = this.namesToModules.get(ConfigConstants.CONFIG_MODULE);
128         final Module module = this.namesToModules.get(moduleName);
129         Preconditions.checkNotNull(module, "Cannot get module %s from %s",
130                 moduleName, this.namesToModules.keySet());
131         return module;
132     }
133
134 }