blob: 6b7228da97544f502489b1a6b9029599a0e5e7dc [file] [log] [blame]
Bharat saraswal4bf8b152016-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;
18
19import java.io.File;
20import java.io.IOException;
21
22import org.junit.Test;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053023
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.core.Is.is;
26
Bharat saraswal4bf8b152016-02-25 02:26:43 +053027import org.onosproject.yangutils.datamodel.YangDataTypes;
28import org.onosproject.yangutils.datamodel.YangType;
29import org.onosproject.yangutils.translator.CachedFileHandle;
30import org.onosproject.yangutils.translator.GeneratedFileType;
31import org.onosproject.yangutils.utils.UtilConstants;
32import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
33import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
34
35/**
36 * Unit test case for cached java file handle.
37 */
38public class CachedJavaFileHandleTest {
39
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053040 private static final String DIR_PKG = "target/unit/cachedfile/yangmodel/";
Bharat saraswal4bf8b152016-02-25 02:26:43 +053041 private static final String PKG = "org.onosproject.unittest";
42 private static final String CHILD_PKG = "target/unit/cachedfile/child";
43 private static final String YANG_NAME = "Test1";
Vinod Kumar Sc4216002016-03-03 19:55:30 +053044 private static final int GEN_TYPE = GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER;
Bharat saraswal4bf8b152016-02-25 02:26:43 +053045
46 /**
47 * Unit test case for add attribute info.
48 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053049 * @throws IOException when fails to add an attribute
Bharat saraswal4bf8b152016-02-25 02:26:43 +053050 */
51 @Test
52 public void testForAddAttributeInfo() throws IOException {
53
54 AttributeInfo attr = getAttr();
55 attr.setListAttr(false);
56 getFileHandle().addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
57 }
58
59 /**
60 * Unit test case for close of cached files.
61 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053062 * @throws IOException when fails to generate files
Bharat saraswal4bf8b152016-02-25 02:26:43 +053063 */
64 @Test
65 public void testForClose() throws IOException {
66
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053067 CopyrightHeader.parseCopyrightHeader();
Bharat saraswal4bf8b152016-02-25 02:26:43 +053068
69 AttributeInfo attr = getAttr();
70 attr.setListAttr(false);
71 CachedFileHandle handle = getFileHandle();
72 handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053073 handle.close();
Bharat saraswal4bf8b152016-02-25 02:26:43 +053074
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053075 assertThat(true, is(getStubDir().exists()));
76 assertThat(true, is(getStubPkgInfo().exists()));
77 assertThat(true, is(getStubInterfaceFile().exists()));
78 assertThat(true, is(getStubBuilderFile().exists()));
Bharat saraswal4bf8b152016-02-25 02:26:43 +053079 }
80
81 /**
82 * Returns attribute info.
83 *
84 * @return attribute info
85 */
86 @SuppressWarnings("rawtypes")
87 private AttributeInfo getAttr() {
88 YangType<?> type = new YangType();
89 YangDataTypes dataType = YangDataTypes.STRING;
90
91 type.setDataTypeName("string");
92 type.setDataType(dataType);
93
94 AttributeInfo attr = new AttributeInfo();
95
96 attr.setAttributeName("testAttr");
97 attr.setAttributeType(type);
98 return attr;
99 }
100
101 /**
102 * Returns cached java file handle.
103 *
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530104 * @return java file handle
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530105 */
106 private CachedFileHandle getFileHandle() throws IOException {
107 CopyrightHeader.parseCopyrightHeader();
108 FileSystemUtil.createPackage(DIR_PKG + File.separator + PKG, YANG_NAME);
109 CachedFileHandle fileHandle = FileSystemUtil.createSourceFiles(PKG, YANG_NAME, GEN_TYPE);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530110 fileHandle.setRelativeFilePath(PKG.replace(".", "/"));
111 fileHandle.setCodeGenFilePath(DIR_PKG);
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530112 return fileHandle;
113 }
114
115 /**
116 * Returns stub directory file object.
117 *
118 * @return stub directory file
119 */
120 private File getStubDir() {
121 return new File(DIR_PKG);
122 }
123
124 /**
125 * Returns stub package-info file object.
126 *
127 * @return stub package-info file
128 */
129 private File getStubPkgInfo() {
130 return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator
131 + "package-info.java");
132 }
133
134 /**
135 * Returns stub interface file object.
136 *
137 * @return stub interface file
138 */
139 private File getStubInterfaceFile() {
140 return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME
141 + ".java");
142 }
143
144 /**
145 * Returns stub builder file.
146 *
147 * @return stub builder file
148 */
149 private File getStubBuilderFile() {
150 return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME
151 + "Builder.java");
152 }
153
154}