blob: fbd418339b39479ea4059e4af7c9bd0e799fc2b8 [file] [log] [blame]
Bharat saraswalc46ee2a2016-02-25 02:26:43 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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.translator.tojava.utils;
18
19import org.junit.Test;
20import org.onosproject.yangutils.datamodel.YangDataTypes;
21import org.onosproject.yangutils.datamodel.YangType;
22import org.onosproject.yangutils.translator.GeneratedFileType;
23import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
24import org.onosproject.yangutils.translator.tojava.ImportInfo;
25import org.onosproject.yangutils.utils.UtilConstants;
26
27import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.core.Is.is;
29
30/**
31 * Unit test cases for java code snippet generator.
32 */
33public class JavaCodeSnippetGenTest {
34
35 private static final String PKG_INFO = "org.onosproject.unittest";
36 private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
Vinod Kumar S08710982016-03-03 19:55:30 +053037 private static final int FILE_GEN_TYPE = GeneratedFileType.INTERFACE_MASK;
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053038 private static final GeneratedMethodTypes METHOD_GEN_TYPE = GeneratedMethodTypes.GETTER;
39 private static final String YANG_NAME = "Test";
40 private static final String STRING = "String";
41
42 /**
43 * Unit test case for import text.
44 */
45 @Test
46 public void testForImportText() {
47 ImportInfo importInfo = new ImportInfo();
48 importInfo.setPkgInfo(PKG_INFO);
49 importInfo.setClassInfo(CLASS_INFO);
50
51 String imports = JavaCodeSnippetGen.getImportText(importInfo);
52
53 assertThat(true, is(imports.equals(UtilConstants.IMPORT + PKG_INFO + UtilConstants.PERIOD + CLASS_INFO
54 + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
55 }
56
57 /**
58 * Unit test case for java class definition start.
59 */
60 @Test
61 public void testForJavaClassDefStart() {
62 String classDef = JavaCodeSnippetGen.getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
63 assertThat(true,
64 is(classDef.equals(UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE
65 + UtilConstants.SPACE + YANG_NAME + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
66 + UtilConstants.NEW_LINE)));
67 }
68
69 /**
70 * Unit test case for java attribute info.
71 */
72 @SuppressWarnings("rawtypes")
73 @Test
74 public void testForJavaAttributeInfo() {
Vinod Kumar S08710982016-03-03 19:55:30 +053075 // TODO: need to update for new framework
76 // String attributeWithType
77 // = JavaCodeSnippetGen.getJavaAttributeDefination(FILE_GEN_TYPE, YANG_NAME, getType());
78 // assertThat(true, is(attributeWithType.equals(UtilConstants.PRIVATE + UtilConstants.SPACE
79 // + getType().getDataTypeName() + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
80 //
81 // String attributeWithoutType
82 // = JavaCodeSnippetGen.getJavaAttributeDefination(FILE_GEN_TYPE, YANG_NAME, null);
83 // assertThat(true,
84 // is(attributeWithoutType.equals(
85 // UtilConstants.PRIVATE
86 // + UtilConstants.SPACE + JavaIdentifierSyntax.getCaptialCase(YANG_NAME)
87 // + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
Bharat saraswalc46ee2a2016-02-25 02:26:43 +053088
89 }
90
91 /**
92 * Unit test case for list attribute.
93 */
94 @Test
95 public void testForListAttribute() {
96 String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING);
97 assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING
98 + UtilConstants.DIAMOND_CLOSE_BRACKET)));
99 }
100
101 /**
102 * Unit test case for java method info.
103 */
104 @Test
105 public void testForJavaMethodInfo() {
Vinod Kumar S08710982016-03-03 19:55:30 +0530106 //TODO: update to new framework.
107 // String method
108 // = JavaCodeSnippetGen.getJavaMethodInfo(FILE_GEN_TYPE, YANG_NAME, METHOD_GEN_TYPE, getType());
109 // assertThat(true,
110 // is(method.equals(UtilConstants.FOUR_SPACE_INDENTATION
111 // + JavaIdentifierSyntax.getCaptialCase(getType().getDataTypeName())
112 // + UtilConstants.SPACE
113 // + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(YANG_NAME)
114 // + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS
115 // + UtilConstants.SEMI_COLAN)));
Bharat saraswalc46ee2a2016-02-25 02:26:43 +0530116 }
117
118 /**
119 * Unit test case for java class definition close.
120 */
121 @Test
122 public void testForJavaClassDefClose() {
123 String classDef = JavaCodeSnippetGen.getJavaClassDefClose(FILE_GEN_TYPE, YANG_NAME);
124 assertThat(true, is(classDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
125 }
126
127 /**
128 * Returns YANG type.
129 *
130 * @return type
131 */
132 @SuppressWarnings("rawtypes")
133 private YangType<?> getType() {
134 YangType<?> type = new YangType();
135 type.setDataTypeName(STRING);
136 type.setDataType(YangDataTypes.STRING);
137 return type;
138 }
139}