Rename opendaylight.mdsal.binding.runtime.spi
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / 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.tree.impl;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.fail;
12
13 import org.junit.jupiter.api.BeforeEach;
14 import org.junit.jupiter.api.Test;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
17 import org.opendaylight.yangtools.yang.data.tree.api.ConflictingModificationAppliedException;
18 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
21 import org.opendaylight.yangtools.yang.data.tree.api.ModifiedNodeDoesNotExistException;
22 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
23
24 class ErrorReportingTest extends AbstractTestModelTest {
25     private DataTree tree;
26
27     @BeforeEach
28     void setup() {
29         tree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, SCHEMA_CONTEXT);
30     }
31
32     @Test
33     void writeWithoutParentExisting() {
34         final var modification = tree.takeSnapshot().newModification();
35         // We write node without creating parent
36         modification.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.newSystemMapBuilder()
37             .withNodeIdentifier(new NodeIdentifier(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     void parentConcurrentlyDeletedExisting() throws DataValidationFailedException {
52         final var initial = tree.takeSnapshot().newModification();
53         // We write node without creating parent
54         initial.write(TestModel.TEST_PATH,
55             ImmutableNodes.newContainerBuilder().withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build());
56         initial.ready();
57         // We commit transaction
58         tree.commit(tree.prepare(initial));
59
60         final var writeTx = tree.takeSnapshot().newModification();
61         final var deleteTx = tree.takeSnapshot().newModification();
62         deleteTx.delete(TestModel.TEST_PATH);
63         deleteTx.ready();
64         // We commit delete modification
65         tree.commit(tree.prepare(deleteTx));
66
67         writeTx.write(TestModel.OUTER_LIST_PATH, ImmutableNodes.newSystemMapBuilder()
68             .withNodeIdentifier(new NodeIdentifier(TestModel.OUTER_LIST_QNAME))
69             .build());
70         writeTx.ready();
71         try {
72             tree.validate(writeTx);
73             fail("ConflictingModificationAppliedException should be raised");
74         } catch (ConflictingModificationAppliedException e) {
75             assertEquals(TestModel.TEST_PATH, e.getPath());
76         } catch (DataValidationFailedException e) {
77             fail("ConflictingModificationAppliedException expected");
78         }
79     }
80 }