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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.model.statements.yang.test;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import java.util.Arrays;
28 import org.junit.Test;
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;
35 public class WhenTest extends YangTestCommon {
38 public void testWhen() {
39 parseRelativeImplementsYangModels(Arrays.asList("when-test/when-test.yang"));
40 final YModule yModule = getModule("when-test");
42 final YLeaf leafWithoutWhen = getLeafUnderContainer(yModule, "cont1", "leaf11");
43 assertTrue(leafWithoutWhen.getWhens().isEmpty());
44 assertNoFindingsOnStatement(leafWithoutWhen);
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);
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);
62 final YWhen newWhen = new YWhen(when2.getParentStatement(), when2.getDomElement());
63 newWhen.cloneFrom(when2);
64 assertTrue(newWhen.getParentStatement() == augmentedLeafWithWhen);