Renamed Data(ReadOnly|WriteOnly) to DataTree(Read|Write)
[mdsal.git] / dom / mdsal-dom-api / src / test / java / org / opendaylight / controller / md / sal / dom / api / AbstractDOMDataTreeServiceTestSuite.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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.dom.api;
9
10 import static org.junit.Assert.assertNotNull;
11
12 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
13 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
14
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import java.net.URI;
22 import java.util.Collections;
23 import javax.annotation.Nonnull;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
30
31 /**
32  * Abstract test suite demonstrating various access patterns on how a {@link DOMDataTreeService}
33  * can be used.
34  */
35 public abstract class AbstractDOMDataTreeServiceTestSuite {
36     protected static final QNameModule TEST_MODULE = QNameModule.create(URI.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:store"), null);
37
38     protected static final YangInstanceIdentifier UNORDERED_CONTAINER_IID = YangInstanceIdentifier.create(
39         new NodeIdentifier(QName.create(TEST_MODULE, "lists")),
40         new NodeIdentifier(QName.create(TEST_MODULE, "unordered-container")));
41     protected static final DOMDataTreeIdentifier UNORDERED_CONTAINER_TREE = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, UNORDERED_CONTAINER_IID);
42
43     /**
44      * Return a reference to the service used in this test. The instance
45      * needs to be reused within the same test and must be isolated between
46      * tests.
47      *
48      * @return {@link DOMDataTreeService} instance.
49      */
50     protected abstract @Nonnull DOMDataTreeService service();
51
52     /**
53      * A simple unbound producer. It write some basic things into the data store based on the
54      * test model.
55      * @throws DOMDataTreeProducerException
56      * @throws TransactionCommitFailedException
57      */
58     @Test
59     public final void testBasicProducer() throws DOMDataTreeProducerException, TransactionCommitFailedException {
60         // Create a producer. It is an AutoCloseable resource, hence the try-with pattern
61         try (final DOMDataTreeProducer prod = service().createProducer(Collections.singleton(UNORDERED_CONTAINER_TREE))) {
62             assertNotNull(prod);
63
64             final DOMDataTreeWriteTransaction tx = prod.createTransaction(true);
65             assertNotNull(tx);
66
67             tx.put(LogicalDatastoreType.OPERATIONAL, UNORDERED_CONTAINER_IID, ImmutableContainerNodeBuilder.create().build());
68
69             final CheckedFuture<Void, TransactionCommitFailedException> f = tx.submit();
70             assertNotNull(f);
71
72             f.checkedGet();
73         }
74     }
75
76     // TODO: simple listener
77 }