Bug 6022: Deviation statement is not fully available in the YANG parser output
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / DeviationStmtTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.text.ParseException;
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Set;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
22 import org.opendaylight.yangtools.yang.model.api.DeviateDefinition;
23 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
24 import org.opendaylight.yangtools.yang.model.api.Deviation;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
33
34 public class DeviationStmtTest {
35
36     private static final StatementStreamSource FOO_MODULE =
37             new YangStatementSourceImpl("/deviation-stmt-test/foo.yang", false);
38
39     @Test
40     public void testDeviationAndDeviate() throws ReactorException, ParseException {
41         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
42         reactor.addSources(FOO_MODULE);
43
44         final SchemaContext schemaContext = reactor.buildEffective();
45         assertNotNull(schemaContext);
46
47         final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2016-06-23");
48
49         final Module testModule = schemaContext.findModuleByName("foo", revision);
50         assertNotNull(testModule);
51
52         final Set<Deviation> deviations = testModule.getDeviations();
53         assertEquals(4, deviations.size());
54
55         for (Deviation deviation : deviations) {
56             final List<DeviateDefinition> deviates = deviation.getDeviates();
57             final String targetLocalName = deviation.getTargetPath().getLastComponent().getLocalName();
58             if ("test-leaf".equals(targetLocalName)) {
59                 assertEquals("test-leaf is not supported", deviation.getDescription());
60                 assertEquals(1, deviates.size());
61                 assertEquals(DeviateKind.NOT_SUPPORTED, deviates.iterator().next().getDeviateType());
62             } else if ("test-leaf-2".equals(targetLocalName)) {
63                 assertEquals(1, deviates.size());
64                 assertEquals(DeviateKind.ADD, deviates.iterator().next().getDeviateType());
65                 assertEquals("added-def-val", deviates.iterator().next().getDeviatedDefault());
66                 assertFalse(deviates.iterator().next().getDeviatedConfig());
67             } else if ("test-leaf-list".equals(targetLocalName)) {
68                 assertEquals(3, deviates.size());
69                 for (DeviateDefinition deviate : deviates) {
70                     if (DeviateKind.ADD.equals(deviate.getDeviateType())) {
71                         assertEquals(12, deviate.getDeviatedMaxElements().intValue());
72                         assertTrue(deviate.getDeviatedMandatory());
73                     } else if (DeviateKind.REPLACE.equals(deviate.getDeviateType())) {
74                         assertEquals(5, deviate.getDeviatedMinElements().intValue());
75                         assertTrue(deviate.getDeviatedType() instanceof UnsignedIntegerTypeDefinition);
76                     } else {
77                         assertEquals(2, deviate.getDeviatedMusts().size());
78                         assertEquals("minutes", deviate.getDeviatedUnits());
79                     }
80                 }
81             } else {
82                 assertEquals(1, deviation.getDeviates().size());
83                 assertEquals(DeviateKind.DELETE, deviates.iterator().next().getDeviateType());
84                 assertEquals(2, deviates.iterator().next().getDeviatedUniques().size());
85             }
86         }
87     }
88 }