2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.testutils;
10 import ch.vorburger.xtendbeans.AssertBeans;
11 import org.junit.ComparisonFailure;
12 import org.opendaylight.yangtools.yang.binding.DataObject;
15 * Assertion utilities for YANG {@link DataObject}s.
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.
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.
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.
28 * @see AssertBeans for more background
30 * @author Michael Vorburger
32 public final class AssertDataObjects {
34 private static final XtendYangBeanGenerator GENERATOR = new XtendYangBeanGenerator();
36 private AssertDataObjects() {
40 * Assert that an actual YANG DataObject (DataContainer) is equals to an expected one.
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.
45 * @param expected the expected object
46 * @param actual the actual object to check against <code>expected</code>
48 * @see AssertBeans#assertEqualBeans(Object, Object)
50 public static void assertEqualBeans(Object expected, Object actual) throws ComparisonFailure {
51 String expectedText = GENERATOR.getExpression(expected);
52 assertEqualByText(expectedText, actual);
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);