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