Fix DataObjectReference javadoc
[yangtools.git] / binding / binding-spec / src / test / java / org / opendaylight / yangtools / binding / lib / CodeHelpersTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.binding.lib;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertThrows;
16 import static org.mockito.Mockito.mock;
17
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Set;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.binding.EnumTypeObject;
23
24 public class CodeHelpersTest {
25     @Test
26     public void testCheckedFieldCast() {
27         assertNull(CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", null));
28         assertSame(this, CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", this));
29
30         final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
31             () -> CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", new Object()));
32         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
33     }
34
35     @Test
36     public void testCheckListFieldCast() {
37         assertNull(CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", null));
38         assertSame(List.of(), CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of()));
39         final var list = List.of(this);
40         assertSame(list, CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", list));
41
42         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
43             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", Collections.singletonList(null)));
44         assertThat(iae.getCause(), instanceOf(NullPointerException.class));
45
46         iae = assertThrows(IllegalArgumentException.class,
47             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of(new Object())));
48         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
49     }
50
51     @Test
52     public void testCheckSetFieldCast() {
53         assertNull(CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", null));
54         assertSame(Set.of(), CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of()));
55         final var list = Set.of(this);
56         assertSame(list, CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", list));
57
58         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
59             () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Collections.singleton(null)));
60         assertThat(iae.getCause(), instanceOf(NullPointerException.class));
61
62         iae = assertThrows(IllegalArgumentException.class,
63             () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of(new Object())));
64         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
65     }
66
67     @Test
68     public void testCheckEnumName() {
69         final var ex = assertThrows(IllegalArgumentException.class, () -> CodeHelpers.checkEnum(null, "xyzzy"));
70         assertEquals("\"xyzzy\" is not a valid name", ex.getMessage());
71
72         final var obj = mock(EnumTypeObject.class);
73         assertSame(obj, CodeHelpers.checkEnum(obj, "xyzzy"));
74     }
75
76     @Test
77     public void testCheckEnumValue() {
78         final var ex = assertThrows(IllegalArgumentException.class, () -> CodeHelpers.checkEnum(null, 1234));
79         assertEquals("1234 is not a valid value", ex.getMessage());
80
81         final var obj = mock(EnumTypeObject.class);
82         assertSame(obj, CodeHelpers.checkEnum(obj, 1234));
83     }
84 }