Merge "Bug 1112: Update toaster to use async best practices"
[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
29  * leaf schema node should be present the RestconfDocumentedException has to
30  * be raised
31  *
32  * Tests for BUG 1204
33  */
34 public class MultipleEqualNamesForDataNodesTest {
35
36     @Test
37     public void multipleEqualNameDataNodeTestForContainerJsonTest() {
38         multipleEqualNameDataNodeTest("/equal-data-node-names/equal-name-data-for-container.json", ErrorType.APPLICATION,
39                 ErrorTag.BAD_ELEMENT,JsonToCompositeNodeProvider.INSTANCE);
40     }
41
42     @Test
43     public void multipleEqualNameDataNodeTestForLeafJsonTest() {
44         multipleEqualNameDataNodeTest("/equal-data-node-names/equal-name-data-for-leaf.json", ErrorType.PROTOCOL,
45                 ErrorTag.MALFORMED_MESSAGE,JsonToCompositeNodeProvider.INSTANCE);
46     }
47
48     @Test
49     public void multipleEqualNameDataNodeTestForContainerXmlTest() {
50         multipleEqualNameDataNodeTest("/equal-data-node-names/equal-name-data-for-container.xml", ErrorType.APPLICATION,
51                 ErrorTag.BAD_ELEMENT,XmlToCompositeNodeProvider.INSTANCE);
52     }
53
54     @Test
55     public void multipleEqualNameDataNodeTestForLeafXmlTest() {
56         multipleEqualNameDataNodeTest("/equal-data-node-names/equal-name-data-for-leaf.xml", ErrorType.APPLICATION,
57                 ErrorTag.BAD_ELEMENT,XmlToCompositeNodeProvider.INSTANCE);
58     }
59
60     private void multipleEqualNameDataNodeTest(String path, ErrorType errorType, ErrorTag errorTag,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 }