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