730ed151cbc4db6817761ed26cda2f3200c846f9
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.model.statements.yang.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Arrays;
27
28 import org.junit.Test;
29
30 import org.oran.smo.yangtools.parser.model.statements.yang.YLeaf;
31 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
32 import org.oran.smo.yangtools.parser.model.statements.yang.YWhen;
33 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
34
35 public class WhenTest extends YangTestCommon {
36
37     @Test
38     public void testWhen() {
39         parseRelativeImplementsYangModels(Arrays.asList("when-test/when-test.yang"));
40         final YModule yModule = getModule("when-test");
41
42         final YLeaf leafWithoutWhen = getLeafUnderContainer(yModule, "cont1", "leaf11");
43         assertTrue(leafWithoutWhen.getWhens().isEmpty());
44         assertNoFindingsOnStatement(leafWithoutWhen);
45
46         final YLeaf leafWithWhen = getLeafUnderContainer(yModule, "cont1", "leaf12");
47         final YWhen when1 = leafWithWhen.getWhens().get(0);
48         assertTrue(when1 != null);
49         assertEquals("../leaf11 = 'Hello'", when1.getValue());
50         assertEquals(Boolean.FALSE, when1.appliesToParentSchemaNode());
51         assertNoFindingsOnStatement(leafWithWhen);
52         assertNoFindingsOnStatement(when1);
53
54         final YLeaf augmentedLeafWithWhen = getLeafUnderContainer(yModule, "cont2", "leaf22");
55         final YWhen when2 = augmentedLeafWithWhen.getWhens().get(0);
56         assertTrue(when2 != null);
57         assertEquals("/this:cont1/this:leaf11 = 'World'", when2.getValue());
58         assertEquals(Boolean.TRUE, when2.appliesToParentSchemaNode());
59         assertNoFindingsOnStatement(augmentedLeafWithWhen);
60         assertNoFindingsOnStatement(when2);
61
62         final YWhen newWhen = new YWhen(when2.getParentStatement(), when2.getDomElement());
63         newWhen.cloneFrom(when2);
64         assertTrue(newWhen.getParentStatement() == augmentedLeafWithWhen);
65     }
66 }