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