Initial code drop of yang model driven configuration system
[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 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 java.util.Set;
21
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.impl.YangParserImpl;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import com.google.common.base.Preconditions;
33
34 public class ModuleMXBeanEntryNameConflictTest extends AbstractYangTest {
35
36     private static final Logger logger = LoggerFactory
37             .getLogger(ModuleMXBeanEntryNameConflictTest.class);
38
39     public static final String PACKAGE_NAME = "pack2";
40     Map<File, String> testedFilesToYangModules = new HashMap<>();
41     Map<String, String> testedYangModulesToExpectedConflictingName = new HashMap<>();
42
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                 logger.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     }
104
105     private String getYangModuleName(String name) {
106         int startIndex = 0;
107         int endIndex = name.indexOf(".yang");
108         return name.substring(startIndex, endIndex);
109     }
110
111     private Module loadYangs(File testedModule, String moduleName)
112             throws Exception {
113         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         YangParserImpl parser = new YangParserImpl();
121         Set<Module> modulesToBuild = parser.parseYangModelsFromStreams(yangISs);
122         // close ISs
123         for (InputStream is : yangISs) {
124             is.close();
125         }
126         context = parser.resolveSchemaContext(modulesToBuild);
127         namesToModules = YangModelSearchUtils.mapModulesByNames(context
128                 .getModules());
129         configModule = namesToModules.get(ConfigConstants.CONFIG_MODULE);
130         final Module module = namesToModules.get(moduleName);
131         Preconditions.checkNotNull(module, "Cannot get module %s from %s",
132                 moduleName, namesToModules.keySet());
133         return module;
134     }
135
136 }

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