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