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