Promote SchemaTracker to NormalizedNodeInferenceStack
[yangtools.git] / yang / rfc7952-parser-support / src / test / java / org / opendaylight / yangtools / rfc7952 / parser / AnnotationTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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 package org.opendaylight.yangtools.rfc7952.parser;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertTrue;
14
15 import java.io.IOException;
16 import java.util.Map;
17 import java.util.Optional;
18 import org.junit.AfterClass;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.rfc7952.model.api.AnnotationSchemaNode;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26 import org.opendaylight.yangtools.yang.model.spi.type.BaseTypes;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
33
34 public class AnnotationTest {
35     private static final QName LAST_MODIFIED_QNAME = QName.create("http://example.org/example-last-modified",
36             "last-modified");
37     private static CrossSourceStatementReactor reactor;
38
39     @BeforeClass
40     public static void createReactor() {
41         reactor = RFC7950Reactors.vanillaReactorBuilder()
42                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, AnnotationStatementSupport.getInstance())
43                 .build();
44     }
45
46     @AfterClass
47     public static void freeReactor() {
48         reactor = null;
49     }
50
51     @Test
52     public void testAnnotationResolution() throws ReactorException, IOException, YangSyntaxErrorException {
53         final BuildAction build = reactor.newBuild();
54         build.addSource(YangStatementStreamSource.create(
55             YangTextSchemaSource.forResource("/ietf-yang-metadata@2016-08-05.yang")));
56         build.addSource(YangStatementStreamSource.create(
57             YangTextSchemaSource.forResource("/example-last-modified.yang")));
58         final SchemaContext context = build.buildEffective();
59
60         final Map<QName, AnnotationSchemaNode> annotations = AnnotationSchemaNode.findAll(context);
61         assertEquals(1, annotations.size());
62         final AnnotationSchemaNode annotation = annotations.get(LAST_MODIFIED_QNAME);
63         assertNotNull(annotation);
64
65         final Optional<AnnotationSchemaNode> findAnnotation = AnnotationSchemaNode.find(context, LAST_MODIFIED_QNAME);
66         assertTrue(findAnnotation.isPresent());
67         assertSame(annotation, findAnnotation.get());
68
69         assertEquals(BaseTypes.stringType(), annotation.getType());
70         assertEquals(Optional.empty(), annotation.getReference());
71         assertEquals(Optional.of("This annotation contains the date and time when the\n"
72                 + "annotated instance was last modified (or created)."), annotation.getDescription());
73     }
74 }