31be076851f45cd4ae3af734cce28a2cf177e93a
[netconf.git] / restconf / restconf-common / src / test / java / org / opendaylight / restconf / common / util / RestconfSchemaUtilTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.restconf.common.util;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.mockito.Mockito;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
18
19 /**
20  * Unit tests for {@link RestconfSchemaUtil}.
21  */
22 public class RestconfSchemaUtilTest {
23
24     @Test
25     public void findInCollectionTest() {
26         final SchemaNode origSchNode = mockSchemaNode("key");
27         final SchemaNode actualSch = findSchemaNodeInCollection("key", origSchNode);
28         Assert.assertEquals(origSchNode, actualSch);
29     }
30
31     @Test(expected = RuntimeException.class)
32     public void findInCollectionFailedTest() {
33         final SchemaNode origSchNode = mockSchemaNode("key");
34         findSchemaNodeInCollection("bad_key", origSchNode);
35     }
36
37     private static SchemaNode findSchemaNodeInCollection(final String key, final SchemaNode... origSchNode) {
38         final List<SchemaNode> collection = new ArrayList<>();
39         for (final SchemaNode element : origSchNode) {
40             collection.add(element);
41         }
42         return RestconfSchemaUtil.findSchemaNodeInCollection(collection, key);
43     }
44
45     private static SchemaNode mockSchemaNode(final String origKey) {
46         final SchemaNode mockSchNode = Mockito.mock(SchemaNode.class);
47         Mockito.when(mockSchNode.getQName())
48                 .thenReturn(QName.create("ns", "2016-10-10", origKey));
49         return mockSchNode;
50     }
51 }