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