8bfcd6fc3382a12b3d1ca804dcd77adb10b254d4
[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 @Deprecated(since = "8.0.9", forRemoval = true)
31 public class AssertNonDataObjectsTest {
32
33     public static class SomeBean {
34         private String name;
35
36         public String getName() {
37             return name;
38         }
39
40         public void setName(final String name) {
41             this.name = name;
42         }
43
44         @Override
45         public boolean equals(final Object obj) {
46             if (this == obj) {
47                 return true;
48             }
49             if (obj == null || getClass() != obj.getClass()) {
50                 return false;
51             }
52
53             SomeBean someBean = (SomeBean) obj;
54
55             return name != null ? name.equals(someBean.name) : someBean.name == null;
56         }
57
58         @Override
59         public int hashCode() {
60             return name != null ? name.hashCode() : 0;
61         }
62     }
63
64     @Test
65     public void testString() {
66         AssertDataObjects.assertEqualBeans("hello, world", "hello, world");
67     }
68
69     @Test
70     public void testSomeBean() {
71         SomeBean first = new SomeBean();
72         first.setName("hello, world");
73
74         SomeBean second = new SomeBean();
75         second.setName("hello, world");
76
77         AssertDataObjects.assertEqualBeans(first, second);
78     }
79
80     @Test
81     public void testSomeBeanMismatch() {
82         SomeBean expected = new SomeBean();
83         expected.setName("hello, world 1");
84
85         SomeBean actual = new SomeBean();
86         actual.setName("hello, world 2");
87
88         ComparisonFailure ex = assertThrows(ComparisonFailure.class,
89             () -> AssertDataObjects.assertEqualBeans(expected, actual));
90         assertThat(ex.getMessage(), startsWith("Expected and actual beans do not match"));
91         assertThat(ex.getExpected(), containsString("hello, world 1"));
92         assertThat(ex.getActual(), containsString("hello, world 2"));
93     }
94 }