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