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