2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt;
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;
16 import java.text.ParseException;
17 import java.util.Date;
18 import java.util.List;
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;
34 public class DeviationStmtTest {
36 private static final StatementStreamSource FOO_MODULE =
37 new YangStatementSourceImpl("/deviation-stmt-test/foo.yang", false);
40 public void testDeviationAndDeviate() throws ReactorException, ParseException {
41 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
42 reactor.addSources(FOO_MODULE);
44 final SchemaContext schemaContext = reactor.buildEffective();
45 assertNotNull(schemaContext);
47 final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2016-06-23");
49 final Module testModule = schemaContext.findModuleByName("foo", revision);
50 assertNotNull(testModule);
52 final Set<Deviation> deviations = testModule.getDeviations();
53 assertEquals(4, deviations.size());
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);
77 assertEquals(2, deviate.getDeviatedMusts().size());
78 assertEquals("minutes", deviate.getDeviatedUnits());
82 assertEquals(1, deviation.getDeviates().size());
83 assertEquals(DeviateKind.DELETE, deviates.iterator().next().getDeviateType());
84 assertEquals(2, deviates.iterator().next().getDeviatedUniques().size());