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