Bug 9060: TracingBroker printOpenTransactions
[controller.git] / opendaylight / md-sal / mdsal-trace / dom-impl / src / test / java / org / opendaylight / controller / md / sal / trace / closetracker / impl / tests / TracingBrokerTest.java
1 /*
2  * Copyright (c) 2017 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.controller.md.sal.trace.closetracker.impl.tests;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static java.nio.charset.StandardCharsets.UTF_8;
12 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
13 import static org.mockito.Mockito.mock;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.PrintStream;
17 import org.junit.Test;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
21 import org.opendaylight.controller.md.sal.trace.dom.impl.TracingBroker;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.Config;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.ConfigBuilder;
25
26 /**
27  * Test of {@link TracingBroker}.
28  *
29  * @author Michael Vorburger.ch
30  */
31 public class TracingBrokerTest {
32
33     @Test
34     @SuppressWarnings({ "resource", "unused" }) // Finding resource leaks is the point of this test
35     public void testPrintOpenTransactions() {
36         DOMDataBroker domDataBroker = mock(DOMDataBroker.class, RETURNS_DEEP_STUBS);
37         Config config = new ConfigBuilder().setTransactionDebugContextEnabled(true).build();
38         BindingNormalizedNodeSerializer codec = mock(BindingNormalizedNodeSerializer.class);
39         TracingBroker tracingBroker = new TracingBroker(domDataBroker, config, codec);
40
41         DOMTransactionChain txChain = tracingBroker.createTransactionChain(null);
42         DOMDataReadWriteTransaction tx = txChain.newReadWriteTransaction();
43
44         ByteArrayOutputStream baos = new ByteArrayOutputStream();
45         PrintStream ps = new PrintStream(baos);
46         boolean printReturnValue = tracingBroker.printOpenTransactions(ps);
47         String output = new String(baos.toByteArray(), UTF_8);
48
49         assertThat(printReturnValue).isTrue();
50         assertThat(output).contains("testPrintOpenTransactions(TracingBrokerTest.java:41)"); // in a stack trace
51
52         // We don't do any verify/times on the mocks,
53         // because the main point of the test is just to verify that
54         // printOpenTransactions runs through without any exceptions
55         // (e.g. it used to have a ClassCastException).
56     }
57
58 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.