2 * Copyright (c) 2013 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
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import static org.junit.Assert.*;
12 import java.io.FileNotFoundException;
14 import java.text.DateFormat;
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
27 import org.opendaylight.yangtools.yang.model.api.Status;
29 public class YangParserSimpleTest {
31 private final URI snNS = URI.create("urn:opendaylight:simple-nodes");
34 private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
35 private Set<Module> modules;
38 public void init() throws FileNotFoundException, ParseException {
39 snRev = simpleDateFormat.parse("2013-07-30");
40 modules = TestUtils.loadModules(getClass().getResource("/simple-test").getPath());
44 public void testAnyXml() {
45 Module testModule = TestUtils.findModule(modules, "simple-nodes");
46 AnyXmlSchemaNode data = (AnyXmlSchemaNode) testModule.getDataChildByName("data");
47 assertNotNull("'anyxml data not found'", data);
49 // test SchemaNode args
50 QName qname = data.getQName();
51 assertEquals("data", qname.getLocalName());
52 assertEquals("sn", qname.getPrefix());
53 assertEquals(snNS, qname.getNamespace());
54 assertEquals(snRev, qname.getRevision());
55 assertEquals("anyxml desc", data.getDescription());
56 assertEquals("data ref", data.getReference());
57 assertEquals(Status.OBSOLETE, data.getStatus());
58 assertEquals(0, data.getUnknownSchemaNodes().size());
59 // test DataSchemaNode args
60 assertFalse(data.isAugmenting());
61 assertFalse(data.isConfiguration());
62 ConstraintDefinition constraints = data.getConstraints();
63 assertEquals("class != 'wheel'", constraints.getWhenCondition().toString());
64 Set<MustDefinition> mustConstraints = constraints.getMustConstraints();
65 assertEquals(2, constraints.getMustConstraints().size());
67 String must1 = "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"";
68 String errMsg1 = "An ethernet MTU must be 1500";
69 String must2 = "\"ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)\"";
70 String errMsg2 = "An atm MTU must be 64 .. 17966";
72 boolean found1 = false;
73 boolean found2 = false;
75 for (MustDefinition must : mustConstraints) {
76 System.out.println(must);
77 if (must1.equals(must.toString())) {
79 assertEquals(errMsg1, must.getErrorMessage());
80 } else if (must2.equals(must.toString())) {
82 assertEquals(errMsg2, must.getErrorMessage());
83 assertEquals("anyxml data error-app-tag", must.getErrorAppTag());
84 assertEquals("an error occured in data", must.getDescription());
85 assertEquals("data must ref", must.getReference());
91 assertTrue(constraints.isMandatory());
92 assertNull(constraints.getMinElements());
93 assertNull(constraints.getMaxElements());