New test utility AssertDataObjects
[mdsal.git] / binding / mdsal-binding-test-utils / src / main / java / org / opendaylight / mdsal / binding / testutils / AssertDataObjects.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 ch.vorburger.xtendbeans.AssertBeans;
11 import org.junit.ComparisonFailure;
12 import org.opendaylight.yangtools.yang.binding.DataObject;
13
14 /**
15  * Assertion utilities for YANG {@link DataObject}s.
16  *
17  * <p>This compares two {@link DataObject}s by creating a textual representation of them,
18  * and comparing them.  If they are not equals, then the thrown ComparisonFailure provides
19  * for a very highly readable comparison due to a syntax which immediately makes the difference obvious.
20  *
21  * <p>The syntax used happens to be valid Xtend code, and as such could be directly copy/pasted
22  * into an *.xtend source file of an expected object definition.  This is optional though; this
23  * utility can very well be used with any object, not necessarily created by Xtend source code.
24  *
25  * <p>This also works for any Java object that is not a {@link DataObject},
26  * like the {@link AssertBeans} which this is based upon.
27  *
28  * @see AssertBeans for more background
29  *
30  * @author Michael Vorburger
31  */
32 public final class AssertDataObjects {
33
34     private static final XtendYangBeanGenerator GENERATOR = new XtendYangBeanGenerator();
35
36     private AssertDataObjects() {
37     }
38
39     /**
40      * Assert that an actual YANG DataObject (DataContainer) is equals to an expected one.
41      *
42      * <p>The argument types are intentionally of type Object instead of YANG DataContainer or DataObject.
43      * This is important so that this can be directly used on e.g. a List or Map etc. of DataObjects.
44      *
45      * @param expected the expected object
46      * @param actual the actual object to check against <code>expected</code>
47      *
48      * @see AssertBeans#assertEqualBeans(Object, Object)
49      */
50     public static void assertEqualBeans(Object expected, Object actual) throws ComparisonFailure {
51         String expectedText = GENERATOR.getExpression(expected);
52         assertEqualByText(expectedText, actual);
53     }
54
55     // package local method used only in the self tests of this utility (not intended for usage by client code)
56     static void assertEqualByText(String expectedText, Object actual) throws ComparisonFailure {
57         String actualText = GENERATOR.getExpression(actual);
58         if (!expectedText.equals(actualText)) {
59             throw new ComparisonFailure("Expected and actual beans do not match", expectedText, actualText);
60         }
61     }
62
63 }