c6c5985d50cafb13308e696a09e3542e1ac731fb
[mdsal.git] / dom / mdsal-dom-api / src / test / java / org / opendaylight / mdsal / 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.mdsal.dom.api;
9
10 import static org.junit.Assert.assertNotNull;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.net.URI;
14 import java.util.Collections;
15 import java.util.concurrent.ExecutionException;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
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 @Deprecated
30 public abstract class AbstractDOMDataTreeServiceTestSuite {
31     protected static final QNameModule TEST_MODULE =
32             QNameModule.create(URI.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:test:store"));
33
34     protected static final YangInstanceIdentifier UNORDERED_CONTAINER_IID = YangInstanceIdentifier.create(
35         new NodeIdentifier(QName.create(TEST_MODULE, "lists")),
36         new NodeIdentifier(QName.create(TEST_MODULE, "unordered-container")));
37     protected static final DOMDataTreeIdentifier UNORDERED_CONTAINER_TREE
38         = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, UNORDERED_CONTAINER_IID);
39
40     /**
41      * Return a reference to the service used in this test. The instance
42      * needs to be reused within the same test and must be isolated between
43      * tests.
44      *
45      * @return {@link DOMDataTreeService} instance.
46      */
47     protected abstract @NonNull DOMDataTreeService service();
48
49     /**
50      * A simple unbound producer. It write some basic things into the data store based on the
51      * test model.
52      *
53      * @throws DOMDataTreeProducerException when this exceptional condition happens
54      */
55     @Test
56     public final void testBasicProducer() throws DOMDataTreeProducerException, InterruptedException,
57            ExecutionException {
58         // Create a producer. It is an AutoCloseable resource, hence the try-with pattern
59         try (DOMDataTreeProducer prod =
60                 service().createProducer(Collections.singleton(UNORDERED_CONTAINER_TREE))) {
61             assertNotNull(prod);
62
63             final DOMDataTreeCursorAwareTransaction tx = prod.createTransaction(true);
64             assertNotNull(tx);
65
66             final DOMDataTreeWriteCursor cursor =
67                     tx.createCursor(new DOMDataTreeIdentifier(
68                             LogicalDatastoreType.OPERATIONAL, UNORDERED_CONTAINER_IID));
69             assertNotNull(cursor);
70             cursor.write(UNORDERED_CONTAINER_IID.getLastPathArgument(), ImmutableContainerNodeBuilder.create().build());
71             cursor.close();
72
73             final ListenableFuture<?> f = tx.commit();
74             assertNotNull(f);
75
76             f.get();
77         }
78     }
79
80     // TODO: simple listener
81 }