2 * Copyright (c) 2017 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.controller.md.sal.trace.tests;
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;
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;
27 * Test of {@link TracingBroker}.
29 * @author Michael Vorburger.ch
31 public class TracingBrokerTest {
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("mock", domDataBroker, config, codec);
41 for (int i = 0; i < 3; i++) {
42 DOMDataReadWriteTransaction tx = tracingBroker.newReadWriteTransaction();
44 DOMDataReadWriteTransaction anotherTx = tracingBroker.newReadWriteTransaction();
46 DOMTransactionChain txChain = tracingBroker.createTransactionChain(null);
47 DOMDataReadWriteTransaction txFromChain = txChain.newReadWriteTransaction();
49 ByteArrayOutputStream baos = new ByteArrayOutputStream();
50 PrintStream ps = new PrintStream(baos);
51 boolean printReturnValue = tracingBroker.printOpenTransactions(ps, 1);
52 String output = new String(baos.toByteArray(), UTF_8);
54 assertThat(printReturnValue).isTrue();
55 // Assert expectations about stack trace
56 assertThat(output).contains("testPrintOpenTransactions(TracingBrokerTest.java");
57 assertThat(output).doesNotContain(TracingBroker.class.getName());
59 String previousLine = "";
60 for (String line : output.split("\n")) {
61 if (line.contains("(...")) {
62 assertThat(previousLine.contains("(...)")).isFalse();
67 // assert that the sorting works - the x3 is shown before the x1
68 assertThat(output).contains(" DataBroker : newReadWriteTransaction()\n 3x");
70 // We don't do any verify/times on the mocks,
71 // because the main point of the test is just to verify that
72 // printOpenTransactions runs through without any exceptions
73 // (e.g. it used to have a ClassCastException).