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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.