f66cf81cc59d518aa56615803cdb08ad2294267f
[mdsal.git] / binding / mdsal-binding-test-utils / src / test / java / org / opendaylight / mdsal / binding / testutils / AssertNonDataObjectsTest.java
1 /*
2  * Copyright (c) 2016 Red Hat, 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.mdsal.binding.testutils;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertThrows;
14
15 import ch.vorburger.xtendbeans.AssertBeans;
16 import org.junit.ComparisonFailure;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19
20 /**
21  * Tests that the {@link AssertDataObjects} utility also works for any Java
22  * object that is not a {@link DataObject}, like the {@link AssertBeans} which
23  * it's based on. There is absolutely no particular reason why it wouldn't,
24  * because {@link AssertDataObjects} is essentially just a customization of
25  * {@link AssertBeans} - this is just to make sure none of the base
26  * functionality gets broken in the customization.
27  *
28  * @author Michael Vorburger
29  */
30 public class AssertNonDataObjectsTest {
31
32     public static class SomeBean {
33         private String name;
34
35         public String getName() {
36             return name;
37         }
38
39         public void setName(final String name) {
40             this.name = name;
41         }
42
43         @Override
44         public boolean equals(final Object obj) {
45             if (this == obj) {
46                 return true;
47             }
48             if (obj == null || getClass() != obj.getClass()) {
49                 return false;
50             }
51
52             SomeBean someBean = (SomeBean) obj;
53
54             return name != null ? name.equals(someBean.name) : someBean.name == null;
55         }
56
57         @Override
58         public int hashCode() {
59             return name != null ? name.hashCode() : 0;
60         }
61     }
62
63     @Test
64     public void testString() {
65         AssertDataObjects.assertEqualBeans("hello, world", "hello, world");
66     }
67
68     @Test
69     public void testSomeBean() {
70         SomeBean first = new SomeBean();
71         first.setName("hello, world");
72
73         SomeBean second = new SomeBean();
74         second.setName("hello, world");
75
76         AssertDataObjects.assertEqualBeans(first, second);
77     }
78
79     @Test
80     public void testSomeBeanMismatch() {
81         SomeBean expected = new SomeBean();
82         expected.setName("hello, world 1");
83
84         SomeBean actual = new SomeBean();
85         actual.setName("hello, world 2");
86
87         ComparisonFailure ex = assertThrows(ComparisonFailure.class,
88             () -> AssertDataObjects.assertEqualBeans(expected, actual));
89         assertThat(ex.getMessage(), startsWith("Expected and actual beans do not match"));
90         assertThat(ex.getExpected(), containsString("hello, world 1"));
91         assertThat(ex.getActual(), containsString("hello, world 2"));
92     }
93 }