Cleanup DataTree interfaces and InMemmoryDataTreeFactory
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ErrorReportingTest.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.yangtools.yang.data.impl.schema.tree;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
19 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModifiedNodeDoesNotExistException;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22
23 public class ErrorReportingTest {
24
25     private DataTree tree;
26
27     @Before
28     public void setup() {
29         tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL,
30             TestModel.createTestContext());
31     }
32
33     @Test
34     public void writeWithoutParentExisting() {
35         DataTreeModification modification = tree.takeSnapshot().newModification();
36         // We write node without creating parent
37         modification.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
38             .build());
39         modification.ready();
40         try {
41             tree.validate(modification);
42             fail("ModifiedNodeDoesNotExistException should be raised");
43         } catch (ModifiedNodeDoesNotExistException e) {
44             assertEquals(TestModel.TEST_PATH, e.getPath());
45         } catch (DataValidationFailedException e) {
46             fail("ModifiedNodeDoesNotExistException expected");
47         }
48     }
49
50     @Test
51     public void parentConcurrentlyDeletedExisting() {
52         DataTreeModification initial = tree.takeSnapshot().newModification();
53         // We write node without creating parent
54         initial.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
55         initial.ready();
56         // We commit transaction
57         tree.commit(tree.prepare(initial));
58
59         final DataTreeModification writeTx = tree.takeSnapshot().newModification();
60         final DataTreeModification deleteTx = tree.takeSnapshot().newModification();
61         deleteTx.delete(TestModel.TEST_PATH);
62         deleteTx.ready();
63         // We commit delete modification
64         tree.commit(tree.prepare(deleteTx));
65
66         writeTx.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
67         writeTx.ready();
68         try {
69             tree.validate(writeTx);
70             fail("ConflictingModificationAppliedException should be raised");
71         } catch (ConflictingModificationAppliedException e) {
72             assertEquals(TestModel.TEST_PATH, e.getPath());
73         } catch (DataValidationFailedException e) {
74             fail("ConflictingModificationAppliedException expected");
75         }
76     }
77 }