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