Populate data/ hierarchy
[yangtools.git] / data / 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 extends AbstractTestModelTest {
24
25     private DataTree tree;
26
27     @Before
28     public void setup() {
29         tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
30     }
31
32     @Test
33     public void writeWithoutParentExisting() {
34         DataTreeModification modification = tree.takeSnapshot().newModification();
35         // We write node without creating parent
36         modification.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
37             .build());
38         modification.ready();
39         try {
40             tree.validate(modification);
41             fail("ModifiedNodeDoesNotExistException should be raised");
42         } catch (ModifiedNodeDoesNotExistException e) {
43             assertEquals(TestModel.TEST_PATH, e.getPath());
44         } catch (DataValidationFailedException e) {
45             fail("ModifiedNodeDoesNotExistException expected");
46         }
47     }
48
49     @Test
50     public void parentConcurrentlyDeletedExisting() throws DataValidationFailedException {
51         DataTreeModification initial = tree.takeSnapshot().newModification();
52         // We write node without creating parent
53         initial.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
54         initial.ready();
55         // We commit transaction
56         tree.commit(tree.prepare(initial));
57
58         final DataTreeModification writeTx = tree.takeSnapshot().newModification();
59         final DataTreeModification deleteTx = tree.takeSnapshot().newModification();
60         deleteTx.delete(TestModel.TEST_PATH);
61         deleteTx.ready();
62         // We commit delete modification
63         tree.commit(tree.prepare(deleteTx));
64
65         writeTx.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME).build());
66         writeTx.ready();
67         try {
68             tree.validate(writeTx);
69             fail("ConflictingModificationAppliedException should be raised");
70         } catch (ConflictingModificationAppliedException e) {
71             assertEquals(TestModel.TEST_PATH, e.getPath());
72         } catch (DataValidationFailedException e) {
73             fail("ConflictingModificationAppliedException expected");
74         }
75     }
76 }