blob: 927a2d6639f4c40fae565576a8f608453039302d [file] [log] [blame]
Gaurav Agrawala04483c2016-02-13 14:23:40 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawala04483c2016-02-13 14:23:40 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import org.junit.Rule;
20import org.junit.Test;
21import org.junit.rules.ExpectedException;
22import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.YangSubModule;
24import org.onosproject.yangutils.parser.exceptions.ParserException;
25import org.onosproject.yangutils.parser.impl.CustomExceptionMatcher;
26import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
27
28import java.io.IOException;
29
30import static org.hamcrest.core.Is.is;
31import static org.junit.Assert.assertThat;
32
33/**
34 * Test cases for testing belongsto listener functionality.
35 */
36public class BelongstoListenerTest {
37
38 @Rule
39 public ExpectedException thrown = ExpectedException.none();
40
41 private final YangUtilsParserManager manager = new YangUtilsParserManager();
42
43 /**
44 * Checks if mandatory belongsto parameter "prefix" is not present.
45 */
46 @Test
47 public void processBelongsToWithoutPrefix() throws IOException, ParserException {
48 thrown.expect(ParserException.class);
49 thrown.expectMessage("mismatched input '}' expecting 'prefix'");
50 thrown.expect(CustomExceptionMatcher.errorLocation(4, 0));
51 YangNode node = manager.getDataModel("src/test/resources/BelongsToWithoutPrefix.yang");
52 }
53
54 /**
55 * Checks that prefix must be present only once in belongsto.
56 */
57 @Test
58 public void processBelongsToDualPrefix() throws IOException, ParserException {
59 thrown.expect(ParserException.class);
60 thrown.expectMessage("mismatched input 'prefix' expecting '}'");
61 thrown.expect(CustomExceptionMatcher.errorLocation(5, 0));
62 YangNode node = manager.getDataModel("src/test/resources/BelongsToDualPrefix.yang");
63 }
64
65 /**
66 * Checks if belongsto listener updates the date model tree.
67 */
68 @Test
69 public void processBelongsToWithPrefix() throws IOException, ParserException {
70
71 YangNode node = manager.getDataModel("src/test/resources/BelongsToWithPrefix.yang");
72 YangSubModule yangNode = (YangSubModule) node;
73 assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
74 }
75
76 /**
77 * Checks if mandatory parameter "belongsto" is present.
78 */
79 @Test
80 public void processSubModuleWithoutBelongsTo() throws IOException, ParserException {
81 thrown.expect(ParserException.class);
82 thrown.expectMessage("mismatched input '}' expecting 'belongs-to'");
83 thrown.expect(CustomExceptionMatcher.errorLocation(3, 0));
84 YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutBelongsTo.yang");
85 }
86}