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