Merge "BUG-326: quickfix: Log offending type and fallback to strings"
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / util / BitImplTest.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.yangtools.yang.parser.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.util.*;
17
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.*;
21 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
23
24 public class BitImplTest {
25
26     @Test
27     public void test() {
28
29         // hashCode method test
30         URI uriA = null;
31         URI uriA1 = null;
32         URI uriA2 = null;
33         URI uriB = null;
34         URI uriB1 = null;
35         URI uriB2 = null;
36         boolean urisInitiallized = false;
37         try {
38             uriA = new URI("some:uriA");
39             uriA1 = new URI("some:uriA1");
40             uriA2 = new URI("some:uriA2");
41             uriB = new URI("some:uriB");
42             uriB1 = new URI("some:uriB1");
43             uriB2 = new URI("some:uriB2");
44             urisInitiallized = true;
45
46         } catch (URISyntaxException e) {
47             e.printStackTrace();
48             assertTrue("Not all required uri variables were instantiated.", urisInitiallized);
49
50         }
51         QName qnameA = new QName(uriA, new Date(5000000), "some name");
52
53         QName qnameA1 = new QName(uriA1, new Date(6000000), "some nameA1");
54         QName qnameA2 = new QName(uriA2, new Date(7000000), "some nameA2");
55         List<QName> qnamesA = new ArrayList<>();
56         qnamesA.add(qnameA1);
57         qnamesA.add(qnameA2);
58         SchemaPath schemaPathA = new SchemaPath(qnamesA, true);
59
60         QName qnameB = new QName(uriB, new Date(5000000), "some name");
61
62         QName qnameB1 = new QName(uriB1, new Date(6000000), "some nameB1");
63         QName qnameB2 = new QName(uriB2, new Date(7000000), "some nameB2");
64         List<QName> qnamesB = new ArrayList<>();
65         qnamesB.add(qnameB1);
66         qnamesB.add(qnameB2);
67         SchemaPath schemaPathB = new SchemaPath(qnamesB, true);
68
69         BitImpl biB = null;
70         BitImpl biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
71
72         assertEquals("biA should equals to itsefl", biA, biA);
73         assertFalse("biA shouldn't equal to null", biA.equals(null));
74         assertFalse("biA shouldn't equal to object of other type", biA.equals(new String("str")));
75
76         // test of equals method
77         // // test qname
78         biA = new BitImpl(55L, null, schemaPathA, "description", "reference", Status.CURRENT, null);
79         biB = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null);
80         assertFalse("biA shouldn't equal to biB", biA.equals(biB));
81
82         biA = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null);
83         biB = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null);
84         assertEquals("biA should equal to biB", biA, biB);
85
86         biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
87         biB = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null);
88         assertFalse("biA shouldn't equal to biB", biA.equals(biB));
89
90         // // test schemaPath
91         biA = new BitImpl(55L, qnameA, null, "description", "reference", Status.CURRENT, null);
92         biB = new BitImpl(55L, qnameA, schemaPathB, "description", "reference", Status.CURRENT, null);
93         assertFalse("biA shouldn't equal to biB", biA.equals(biB));
94
95         biA = new BitImpl(55L, qnameA, schemaPathB, "description", "reference", Status.CURRENT, null);
96         biB = new BitImpl(55L, qnameA, schemaPathB, "description", "reference", Status.CURRENT, null);
97         assertEquals("biA should equal to biB", biA, biB);
98
99         biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
100         biB = new BitImpl(55L, qnameA, schemaPathB, "description", "reference", Status.CURRENT, null);
101         assertFalse("biA shouldn't equal to biB", biA.equals(biB));
102
103         biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
104         biB = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
105         assertEquals("biA should equal to biB", biA, biB);
106
107         // test of hashCode method
108         biA = new BitImpl(null, null, null, "description", "reference", Status.CURRENT, null);
109         assertEquals("Incorrect hash code for biA.", 923522, biA.hashCode());
110
111         List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
112         SchemaPath path = org.mockito.Mockito.mock(SchemaPath.class);
113         UnknownSchemaNodeBuilder usnb = new UnknownSchemaNodeBuilder("module", 3, qnameB, path);
114         ModuleBuilder parent = org.mockito.Mockito.mock(ModuleBuilder.class);
115         usnb.setParent(parent);
116
117         unknownNodes.add(usnb.build());
118
119         biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, unknownNodes);
120
121         // test of getter methods
122         assertEquals("Incorrect value for qname.", qnameA, biA.getQName());
123         assertEquals("Incorrect value for schema path.", schemaPathA, biA.getPath());
124         assertEquals("Incorrect value for description.", "description", biA.getDescription());
125         assertEquals("Incorrect value for reference.", "reference", biA.getReference());
126         assertEquals("Incorrect value for status.", Status.CURRENT, biA.getStatus());
127         assertEquals("Incorrect value for unknown nodes.", unknownNodes, biA.getUnknownSchemaNodes());
128
129         // test of toString method
130         assertEquals("toString method doesn't return correct value", "Bit[name=some name, position=55]", biA.toString());
131
132     }
133 }