Bharat saraswal | c46ee2a | 2016-02-25 02:26:43 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.translator.tojava.utils; |
| 18 | |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.yangutils.datamodel.YangDataTypes; |
| 21 | import org.onosproject.yangutils.datamodel.YangType; |
| 22 | import org.onosproject.yangutils.translator.GeneratedFileType; |
| 23 | import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes; |
| 24 | import org.onosproject.yangutils.translator.tojava.ImportInfo; |
| 25 | import org.onosproject.yangutils.utils.UtilConstants; |
| 26 | |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.core.Is.is; |
| 29 | |
| 30 | /** |
| 31 | * Unit test cases for java code snippet generator. |
| 32 | */ |
| 33 | public class JavaCodeSnippetGenTest { |
| 34 | |
| 35 | private static final String PKG_INFO = "org.onosproject.unittest"; |
| 36 | private static final String CLASS_INFO = "JavaCodeSnippetGenTest"; |
| 37 | private static final GeneratedFileType FILE_GEN_TYPE = GeneratedFileType.INTERFACE; |
| 38 | 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() { |
| 75 | |
| 76 | String attributeWithType = JavaCodeSnippetGen.getJavaAttributeInfo(FILE_GEN_TYPE, YANG_NAME, getType()); |
| 77 | assertThat(true, is(attributeWithType.equals(UtilConstants.PRIVATE + UtilConstants.SPACE |
| 78 | + getType().getDataTypeName() + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN))); |
| 79 | |
| 80 | String attributeWithoutType = JavaCodeSnippetGen.getJavaAttributeInfo(FILE_GEN_TYPE, YANG_NAME, null); |
| 81 | assertThat(true, |
| 82 | is(attributeWithoutType.equals( |
| 83 | UtilConstants.PRIVATE + UtilConstants.SPACE + JavaIdentifierSyntax.getCaptialCase(YANG_NAME) |
| 84 | + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN))); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Unit test case for list attribute. |
| 90 | */ |
| 91 | @Test |
| 92 | public void testForListAttribute() { |
| 93 | String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING); |
| 94 | assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING |
| 95 | + UtilConstants.DIAMOND_CLOSE_BRACKET))); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Unit test case for java method info. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testForJavaMethodInfo() { |
| 103 | |
| 104 | String method = JavaCodeSnippetGen.getJavaMethodInfo(FILE_GEN_TYPE, YANG_NAME, METHOD_GEN_TYPE, getType()); |
| 105 | assertThat(true, |
| 106 | is(method.equals(UtilConstants.FOUR_SPACE_INDENTATION |
| 107 | + JavaIdentifierSyntax.getCaptialCase(getType().getDataTypeName()) + UtilConstants.SPACE |
| 108 | + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(YANG_NAME) |
| 109 | + UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS |
| 110 | + UtilConstants.SEMI_COLAN))); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Unit test case for java class definition close. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testForJavaClassDefClose() { |
| 118 | String classDef = JavaCodeSnippetGen.getJavaClassDefClose(FILE_GEN_TYPE, YANG_NAME); |
| 119 | assertThat(true, is(classDef.equals(UtilConstants.CLOSE_CURLY_BRACKET))); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns YANG type. |
| 124 | * |
| 125 | * @return type |
| 126 | */ |
| 127 | @SuppressWarnings("rawtypes") |
| 128 | private YangType<?> getType() { |
| 129 | YangType<?> type = new YangType(); |
| 130 | type.setDataTypeName(STRING); |
| 131 | type.setDataType(YangDataTypes.STRING); |
| 132 | return type; |
| 133 | } |
| 134 | } |