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