5813a0c4e9d7946e5851cfece0093a37f5daf7de
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / test / tests / AbstractDataBrokerTestTest.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.controller.md.sal.binding.test.tests;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
12 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.path;
13 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
14
15 import org.junit.Before;
16 import org.junit.FixMethodOrder;
17 import org.junit.Test;
18 import org.junit.runners.MethodSorters;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugmentBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.complex.from.grouping.ContainerWithUsesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.TopBuilder;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32 /**
33  * Integration tests the AbstractDataBrokerTest.
34  *
35  * @author Michael Vorburger
36  */
37 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
38 public class AbstractDataBrokerTestTest extends AbstractConcurrentDataBrokerTest {
39
40     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
41
42     @Before
43     public void before() {
44         assertThat(getDataBroker()).isNotNull();
45     }
46
47     @Test
48     public void aEnsureDataBrokerIsNotNull() {
49         assertThat(getDataBroker()).isNotNull();
50     }
51
52     @Test
53     public void bPutSomethingIntoDataStore() throws Exception {
54         writeInitialState();
55         assertThat(isTopInDataStore()).isTrue();
56     }
57
58     @Test
59     public void cEnsureDataStoreIsEmptyAgainInNewTest() throws ReadFailedException {
60         assertThat(isTopInDataStore()).isFalse();
61     }
62
63     // copy/pasted from Bug1125RegressionTest.writeInitialState()
64     private void writeInitialState() throws TransactionCommitFailedException {
65         WriteTransaction initialTx = getDataBroker().newWriteOnlyTransaction();
66         initialTx.put(LogicalDatastoreType.OPERATIONAL, TOP_PATH, new TopBuilder().build());
67         TreeComplexUsesAugment fooAugment = new TreeComplexUsesAugmentBuilder()
68                 .setContainerWithUses(new ContainerWithUsesBuilder().setLeafFromGrouping("foo").build()).build();
69         initialTx.put(LogicalDatastoreType.OPERATIONAL, path(TOP_FOO_KEY), topLevelList(TOP_FOO_KEY, fooAugment));
70         initialTx.submit().checkedGet();
71     }
72
73     private boolean isTopInDataStore() throws ReadFailedException {
74         ReadOnlyTransaction readTx = getDataBroker().newReadOnlyTransaction();
75         try {
76             return readTx.read(LogicalDatastoreType.OPERATIONAL, TOP_PATH).checkedGet().isPresent();
77         } finally {
78             readTx.close();
79         }
80     }
81
82 }