bcd2d7c923a2ce56f3886cb33c6eaa104f091396
[controller.git] / opendaylight / md-sal / mdsal-trace / dom-impl / src / test / java / org / opendaylight / controller / md / sal / trace / 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.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("mock", domDataBroker, config, codec);
40
41         for (int i = 0; i < 3; i++) {
42             DOMDataReadWriteTransaction tx = tracingBroker.newReadWriteTransaction();
43         }
44         DOMDataReadWriteTransaction anotherTx = tracingBroker.newReadWriteTransaction();
45
46         DOMTransactionChain txChain = tracingBroker.createTransactionChain(null);
47         DOMDataReadWriteTransaction txFromChain = txChain.newReadWriteTransaction();
48
49         ByteArrayOutputStream baos = new ByteArrayOutputStream();
50         PrintStream ps = new PrintStream(baos);
51         boolean printReturnValue = tracingBroker.printOpenTransactions(ps);
52         String output = new String(baos.toByteArray(), UTF_8);
53
54         assertThat(printReturnValue).isTrue();
55         // Assert expectations about stack trace
56         assertThat(output).contains("testPrintOpenTransactions(TracingBrokerTest.java");
57         assertThat(output).doesNotContain(TracingBroker.class.getName());
58
59         String previousLine = "";
60         for (String line : output.split("\n")) {
61             if (line.contains("(...")) {
62                 assertThat(previousLine.contains("(...)")).isFalse();
63             }
64             previousLine = line;
65         }
66
67         // assert that the sorting works - the x3 is shown before the x1
68         assertThat(output).contains("  DataBroker : newReadWriteTransaction()\n    3x");
69
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).
74     }
75
76 }