Apply style rules on whole sal-rest-connector
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / MultipleEqualNamesForDataNodesTest.java
1 /*
2  * Copyright (c) 2014 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.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.fail;
13
14 import java.util.List;
15 import java.util.Set;
16 import javax.ws.rs.ext.MessageBodyReader;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
19 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
20 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
21 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
22 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
23 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26
27 /**
28  * If more then one data element with equal name exists where container or leaf schema node should be present the
29  * RestconfDocumentedException has to be raised
30  *
31  * Tests for BUG 1204
32  */
33 public class MultipleEqualNamesForDataNodesTest {
34
35     @Test
36     public void multipleEqualNameDataNodeTestForContainerJsonTest() {
37         multipleEqualNameDataNodeTest("/equal-data-node-names/equal-name-data-for-container.json",
38                 ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, JsonToCompositeNodeProvider.INSTANCE);
39     }
40
41     @Test
42     public void multipleEqualNameDataNodeTestForLeafJsonTest() {
43         multipleEqualNameDataNodeTest("/equal-data-node-names/equal-name-data-for-leaf.json", ErrorType.PROTOCOL,
44                 ErrorTag.MALFORMED_MESSAGE, JsonToCompositeNodeProvider.INSTANCE);
45     }
46
47     @Test
48     public void multipleEqualNameDataNodeTestForContainerXmlTest() {
49         multipleEqualNameDataNodeTest("/equal-data-node-names/equal-name-data-for-container.xml",
50                 ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, XmlToCompositeNodeProvider.INSTANCE);
51     }
52
53     @Test
54     public void multipleEqualNameDataNodeTestForLeafXmlTest() {
55         multipleEqualNameDataNodeTest("/equal-data-node-names/equal-name-data-for-leaf.xml", ErrorType.APPLICATION,
56                 ErrorTag.BAD_ELEMENT, XmlToCompositeNodeProvider.INSTANCE);
57     }
58
59     private void multipleEqualNameDataNodeTest(String path, ErrorType errorType, ErrorTag errorTag,
60             MessageBodyReader<CompositeNode> messageBodyReader) {
61         try {
62             CompositeNode compositeNode = TestUtils.readInputToCnSn(path, false, messageBodyReader);
63             assertNotNull(compositeNode);
64
65             Set<Module> modules = null;
66             modules = TestUtils.loadModulesFrom("/equal-data-node-names/yang");
67             assertNotNull(modules);
68
69             TestUtils.normalizeCompositeNode(compositeNode, modules, "equal-data-node-names" + ":" + "cont");
70             fail("Exception RestconfDocumentedException should be raised");
71         } catch (RestconfDocumentedException e) {
72             List<RestconfError> errors = e.getErrors();
73             assertNotNull(errors);
74
75             assertEquals(1, errors.size());
76
77             RestconfError restconfError = errors.get(0);
78
79             assertEquals(errorType, restconfError.getErrorType());
80             assertEquals(errorTag, restconfError.getErrorTag());
81         }
82     }
83
84 }