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

©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.