Migrate users of deprecated yang.common methods
[mdsal.git] / binding / yang-binding / src / test / java / org / opendaylight / yangtools / yang / binding / 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.yang.binding;
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
23 public class CodeHelpersTest {
24     @Test
25     public void testCheckedFieldCast() {
26         assertNull(CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", null));
27         assertSame(this, CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", this));
28
29         final IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
30             () -> CodeHelpers.checkFieldCast(CodeHelpersTest.class, "foo", new Object()));
31         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
32     }
33
34     @Test
35     public void testCheckListFieldCast() {
36         assertNull(CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", null));
37         assertSame(List.of(), CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of()));
38         final var list = List.of(this);
39         assertSame(list, CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", list));
40
41         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
42             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", Collections.singletonList(null)));
43         assertThat(iae.getCause(), instanceOf(NullPointerException.class));
44
45         iae = assertThrows(IllegalArgumentException.class,
46             () -> CodeHelpers.checkListFieldCast(CodeHelpersTest.class, "foo", List.of(new Object())));
47         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
48     }
49
50     @Test
51     public void testCheckSetFieldCast() {
52         assertNull(CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", null));
53         assertSame(Set.of(), CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of()));
54         final var list = Set.of(this);
55         assertSame(list, CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", list));
56
57         IllegalArgumentException iae = assertThrows(IllegalArgumentException.class,
58             () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Collections.singleton(null)));
59         assertThat(iae.getCause(), instanceOf(NullPointerException.class));
60
61         iae = assertThrows(IllegalArgumentException.class,
62             () -> CodeHelpers.checkSetFieldCast(CodeHelpersTest.class, "foo", Set.of(new Object())));
63         assertThat(iae.getCause(), instanceOf(ClassCastException.class));
64     }
65
66     @Test
67     public void testCheckEnumName() {
68         final var ex = assertThrows(IllegalArgumentException.class, () -> CodeHelpers.checkEnum(null, "xyzzy"));
69         assertEquals("\"xyzzy\" is not a valid name", ex.getMessage());
70
71         final var obj = mock(EnumTypeObject.class);
72         assertSame(obj, CodeHelpers.checkEnum(obj, "xyzzy"));
73     }
74
75     @Test
76     public void testCheckEnumValue() {
77         final var ex = assertThrows(IllegalArgumentException.class, () -> CodeHelpers.checkEnum(null, 1234));
78         assertEquals("1234 is not a valid value", ex.getMessage());
79
80         final var obj = mock(EnumTypeObject.class);
81         assertSame(obj, CodeHelpers.checkEnum(obj, 1234));
82     }
83 }