Migrate yang-data-tree-ri to JUnit5
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / impl / CaseExclusionTest.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.assertThrows;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.leafNode;
13
14 import org.junit.jupiter.api.AfterAll;
15 import org.junit.jupiter.api.BeforeAll;
16 import org.junit.jupiter.api.BeforeEach;
17 import org.junit.jupiter.api.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
23 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
25 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27
28 class CaseExclusionTest {
29     private static EffectiveModelContext SCHEMA_CONTEXT;
30
31     private DataTree inMemoryDataTree;
32
33     @BeforeAll
34     static void beforeClass() {
35         SCHEMA_CONTEXT = TestModel.createTestContext("/case-exclusion-test.yang");
36     }
37
38     @AfterAll
39     static void afterClass() {
40         SCHEMA_CONTEXT = null;
41     }
42
43     @BeforeEach
44     void before() {
45         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION,
46             SCHEMA_CONTEXT);
47     }
48
49     @Test
50     void testCorrectCaseWrite() throws DataValidationFailedException {
51         final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
52
53         final var container = Builders
54                 .containerBuilder()
55                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
56                 .withChild(
57                         Builders.choiceBuilder().withNodeIdentifier(choice1Id)
58                                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
59                                 .build()).build();
60         final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
61         modificationTree.write(TestModel.TEST_PATH, container);
62         modificationTree.ready();
63
64         inMemoryDataTree.validate(modificationTree);
65         final var prepare = inMemoryDataTree.prepare(modificationTree);
66         inMemoryDataTree.commit(prepare);
67     }
68
69     @Test
70     void testCaseExclusion() {
71         assertThrows(IllegalArgumentException.class, () -> {
72             final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
73
74             final var container = Builders
75                 .containerBuilder()
76                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
77                 .withChild(
78                     Builders.choiceBuilder()
79                         .withNodeIdentifier(choice1Id)
80                         .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
81                         .withChild(
82                             ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont")))
83                         .build()).build();
84             try {
85                 final var modificationTree = inMemoryDataTree.takeSnapshot().newModification();
86                 modificationTree.write(TestModel.TEST_PATH, container);
87                 modificationTree.ready();
88
89                 inMemoryDataTree.validate(modificationTree);
90                 final var prepare = inMemoryDataTree.prepare(modificationTree);
91                 inMemoryDataTree.commit(prepare);
92             } catch (IllegalArgumentException e) {
93                 assertTrue(e.getMessage().contains("implies non-presence of child"));
94                 throw e;
95             }
96         });
97     }
98
99     @Test
100     void testCaseExclusionOnChoiceWrite() {
101         assertThrows(IllegalArgumentException.class, () -> {
102             // Container write
103             final var container = Builders.containerBuilder()
104                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME)).build();
105
106             final var modificationTree1 = inMemoryDataTree.takeSnapshot().newModification();
107             modificationTree1.write(TestModel.TEST_PATH, container);
108             modificationTree1.ready();
109
110             inMemoryDataTree.validate(modificationTree1);
111             final var prepare1 = inMemoryDataTree.prepare(modificationTree1);
112             inMemoryDataTree.commit(prepare1);
113
114             // Choice write
115             final var choice1Id = new NodeIdentifier(QName.create(TestModel.TEST_QNAME, "choice1"));
116             final var choice = Builders.choiceBuilder().withNodeIdentifier(choice1Id)
117                 .withChild(leafNode(QName.create(TestModel.TEST_QNAME, "case1-leaf1"), "leaf-value"))
118                 .withChild(ImmutableNodes.containerNode(QName.create(TestModel.TEST_QNAME, "case2-cont"))).build();
119
120             try {
121                 final var modificationTree2 = inMemoryDataTree.takeSnapshot().newModification();
122                 modificationTree2.write(TestModel.TEST_PATH.node(choice1Id), choice);
123                 modificationTree2.ready();
124
125                 inMemoryDataTree.validate(modificationTree2);
126
127                 final var prepare2 = inMemoryDataTree.prepare(modificationTree2);
128                 inMemoryDataTree.commit(prepare2);
129             } catch (IllegalArgumentException e) {
130                 assertTrue(e.getMessage().contains("implies non-presence of child"));
131                 throw e;
132             }
133         });
134     }
135 }