Bharat saraswal
Committed by Gerrit Code Review

[ONOS-3908] YANG container translator.

Change-Id: I4e239509df747238905ca0995f41019679093627
Showing 19 changed files with 528 additions and 108 deletions
......@@ -26,6 +26,7 @@ import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/*-
* Reference RFC 6020.
......@@ -472,17 +473,21 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
@Override
public void generateJavaCodeEntry() throws IOException {
YangNode parent = getParent();
String modPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), getName());
setPackage(modPkg);
String contPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName());
setPackage(contPkg);
CachedFileHandle handle = null;
try {
FileSystemUtil.createPackage(getPackage(), getName());
FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName());
handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
handle.setFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/"));
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
addLeavesAttributes();
addLeafListAttributes();
addAttributeInParent();
}
......@@ -498,8 +503,6 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
@Override
public void generateJavaCodeExit() throws IOException {
addLeavesAttributes();
addLeafListAttributes();
getFileHandle().close();
return;
}
......
......@@ -26,6 +26,7 @@ import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.CodeGenerator;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/*-
......@@ -558,18 +559,19 @@ public class YangModule extends YangNode
CachedFileHandle handle = null;
try {
FileSystemUtil.createPackage(getPackage(), getName());
FileSystemUtil.createPackage(UtilConstants.YANG_GEN_DIR + getPackage(), getName());
handle = FileSystemUtil.createSourceFiles(getPackage(), getName(), GeneratedFileType.ALL);
handle.setFilePath(UtilConstants.YANG_GEN_DIR + getPackage().replace(".", "/"));
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
addLeavesAttributes();
addLeafListAttributes();
}
@Override
public void generateJavaCodeExit() throws IOException {
addLeavesAttributes();
addLeafListAttributes();
getFileHandle().close();
return;
}
......
......@@ -49,4 +49,11 @@ public interface CachedFileHandle {
* @param pkg child's package path
*/
void setChildsPackage(String pkg);
/**
* Sets directory package path for code generation.
*
* @param filePath directory package path for code generation
*/
void setFilePath(String filePath);
}
......
......@@ -19,6 +19,7 @@ package org.onosproject.yangutils.translator.tojava;
import java.io.Serializable;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
/**
* Maintains the attribute info corresponding to class/interface generated.
......@@ -71,7 +72,10 @@ public class AttributeInfo implements Serializable {
* @param type the data type info of attribute.
*/
public void setAttributeType(YangType<?> type) {
attrType = type;
if (type != null) {
attrType = AttributesJavaDataType.getJavaDataType(type);
}
}
/**
......
......@@ -42,21 +42,22 @@ public final class JavaCodeGenerator {
TraversalType curTraversal = TraversalType.ROOT;
while (!(curNode == null)) {
if (curTraversal != TraversalType.PARENT || curTraversal == TraversalType.SIBILING) {
if (curTraversal != TraversalType.PARENT) {
curNode.generateJavaCodeEntry();
}
if (curTraversal != TraversalType.PARENT && !(curNode.getChild() == null)) {
if (curTraversal != TraversalType.PARENT && (curNode.getChild() != null)) {
curTraversal = TraversalType.CHILD;
curNode = curNode.getChild();
} else if (curTraversal == TraversalType.PARENT && !(curNode.getNextSibling() == null)) {
} else if ((curNode.getNextSibling() != null)) {
curNode.generateJavaCodeExit();
curTraversal = TraversalType.SIBILING;
curNode = curNode.getNextSibling();
} else {
curNode.generateJavaCodeExit();
curTraversal = TraversalType.PARENT;
curNode.generateJavaCodeExit();
curNode = curNode.getParent();
}
}
}
}
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.AttributeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
/**
* Provides java data types corresponding to YANG type.
*/
public final class AttributesJavaDataType {
/**
* Default constructor.
*/
private AttributesJavaDataType() {
}
/**
* Returns YANG type.
*
* @param yangType YANG type
* @return YANG type
*/
public static YangType<?> getJavaDataType(YangType<?> yangType) {
yangType.setDataTypeName(yangType.getDataTypeName().replace("\"", ""));
if (yangType.getDataType() != null) {
yangType.setDataTypeName(parseYangDataType(yangType.getDataType()));
}
return yangType;
}
/**
* Returns list string as attribute name when attribute is a list.
*
* @param attr attribute info.
* @return list attribute
*/
@SuppressWarnings("rawtypes")
public static YangType<?> getListString(AttributeInfo attr) {
String listString = JavaCodeSnippetGen.getListAttribute(attr.getAttributeType().getDataTypeName());
YangType<?> type = new YangType();
type.setDataTypeName(listString);
attr.setAttributeType(type);
return type;
}
/**
* Parses YANG data type and returns corresponding java data type.
*
* @param type YANG data type
* @return java data type
*/
private static String parseYangDataType(YangDataTypes type) {
if (type.equals(YangDataTypes.INT8)) {
return UtilConstants.BYTE;
} else if (type.equals(YangDataTypes.INT16)) {
return UtilConstants.SHORT;
} else if (type.equals(YangDataTypes.INT32)) {
return UtilConstants.INT;
} else if (type.equals(YangDataTypes.INT64)) {
return UtilConstants.LONG;
} else if (type.equals(YangDataTypes.UINT8)) {
return UtilConstants.SHORT;
} else if (type.equals(YangDataTypes.UINT16)) {
return UtilConstants.INT;
} else if (type.equals(YangDataTypes.UINT32)) {
return UtilConstants.LONG;
} else if (type.equals(YangDataTypes.UINT64)) {
//TODO: BIGINTEGER.
} else if (type.equals(YangDataTypes.DECIMAL64)) {
//TODO: DECIMAL64.
} else if (type.equals(YangDataTypes.STRING)) {
return UtilConstants.STRING;
} else if (type.equals(YangDataTypes.BOOLEAN)) {
return UtilConstants.BOOLEAN;
} else if (type.equals(YangDataTypes.ENUMERATION)) {
//TODO: ENUMERATION.
} else if (type.equals(YangDataTypes.BITS)) {
//TODO:BITS
} else if (type.equals(YangDataTypes.BINARY)) {
//TODO:BINARY
} else if (type.equals(YangDataTypes.LEAFREF)) {
//TODO:LEAFREF
} else if (type.equals(YangDataTypes.IDENTITYREF)) {
//TODO:IDENTITYREF
} else if (type.equals(YangDataTypes.EMPTY)) {
//TODO:EMPTY
} else if (type.equals(YangDataTypes.UNION)) {
//TODO:UNION
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
//TODO:DERIVED
}
return null;
}
}
......@@ -16,9 +16,6 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.util.List;
import java.util.SortedSet;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
......@@ -50,17 +47,6 @@ public final class JavaCodeSnippetGen {
}
/**
* reorder the import list based on the ONOS import rules.
*
* @param importInfo the set of classes/interfaces to be imported.
* @return string of import info.
*/
public List<ImportInfo> sortImportOrder(SortedSet<ImportInfo> importInfo) {
/* TODO: reorder the import list based on the ONOS import rules. */
return null;
}
/**
* Get the textual java code information corresponding to the import list.
*
* @param importInfo import info.
......
......@@ -50,7 +50,7 @@ public final class JavaIdentifierSyntax {
pkg = pkg + UtilConstants.PERIOD;
pkg = pkg + getYangRevisionStr(revision);
return pkg;
return pkg.toLowerCase();
}
/**
......@@ -126,11 +126,11 @@ public final class JavaIdentifierSyntax {
* Get the package from parent's package and string.
*
* @param parentPkg parent's package.
* @param childName child's name.
* @param parentName parent's name.
* @return package string.
*/
public static String getPackageFromParent(String parentPkg, String childName) {
return parentPkg + UtilConstants.PERIOD + getSubPkgFromName(childName);
public static String getPackageFromParent(String parentPkg, String parentName) {
return (parentPkg + UtilConstants.PERIOD + getSubPkgFromName(parentName)).toLowerCase();
}
/**
......
......@@ -109,6 +109,7 @@ public final class MethodsGenerator {
* @param returnType return type of method
* @return constructed method impl
*/
@SuppressWarnings("rawtypes")
public static String constructMethodInfo(GeneratedFileType genFileTypes, String yangName,
GeneratedMethodTypes methodTypes, YangType<?> returnType) {
......@@ -187,8 +188,7 @@ public final class MethodsGenerator {
attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
String setterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
attr.getAttributeName(), GeneratedMethodTypes.SETTER, attr.getAttributeType());
return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString
+ UtilConstants.NEW_LINE;
return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString;
}
/**
......@@ -201,8 +201,7 @@ public final class MethodsGenerator {
return UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
+ UtilConstants.NEW_LINE + JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType())
+ UtilConstants.NEW_LINE;
attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
}
/**
......@@ -400,8 +399,7 @@ public final class MethodsGenerator {
}
getAttrInfo().clear();
}
return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET
+ UtilConstants.NEW_LINE;
return constructor + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
/**
......
......@@ -54,7 +54,6 @@ public final class FileSystemUtil {
return false;
}
/**
* Create a package structure with package info java file if not present.
*
......
......@@ -170,8 +170,8 @@ public final class JavaDocGen {
* @return javaDocs.
*/
private static String generateForBuilderClass(String className) {
return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC
+ className + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.BUILDER_CLASS_JAVA_DOC + className
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
}
/**
......@@ -181,8 +181,8 @@ public final class JavaDocGen {
* @return javaDocs.
*/
private static String generateForInterface(String interfaceName) {
return (UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC
+ interfaceName + UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.INTERFACE_JAVA_DOC + interfaceName
+ UtilConstants.PERIOD + UtilConstants.NEW_LINE + UtilConstants.JAVA_DOC_END_LINE);
}
/**
......
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.utils.io.impl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
......@@ -32,19 +33,19 @@ import java.util.ArrayList;
import java.util.List;
/**
* Provides storage for serialized data while traversing data model tree for code generation.
* Provides storage for Temp data while traversing data model tree for code generation.
*/
public final class SerializedDataStore {
public final class TempDataStore {
/**
* Data Store types.
*/
public static enum SerializedDataStoreType {
public static enum TempDataStoreType {
/**
* Methods.
*/
INTERFACE_METHODS,
GETTER_METHODS,
/**
* Methods.
......@@ -73,42 +74,42 @@ public final class SerializedDataStore {
}
/**
* File name string for serialized files of methods.
* File name string for Temp files of methods.
*/
private static final String INTERFACE_METHOD_FILE_NAME = "SerializedInterfaceMethodDataStore";
private static final String GETTER_METHOD_FILE_NAME = "TempGetterMethodDataStore";
/**
* File name string for serialized files of methods.
* File name string for Temp files of methods.
*/
private static final String BUILDER_METHOD_FILE_NAME = "SerializedBuilderMethodDataStore";
private static final String BUILDER_METHOD_FILE_NAME = "TempBuilderMethodDataStore";
/**
* File name string for serialized files of methods.
* File name string for Temp files of methods.
*/
private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "SerializedBuilderInterfaceMethodDataStore";
private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "TempBuilderInterfaceMethodDataStore";
/**
* File name string for serialized files of methods.
* File name string for Temp files of methods.
*/
private static final String IMPL_METHOD_FILE_NAME = "SerializedImplMethodDataStore";
private static final String IMPL_METHOD_FILE_NAME = "TempImplMethodDataStore";
/**
* File name string for serialized files of attributes.
* File name string for Temp files of attributes.
*/
private static final String ATTRIBUTE_FILE_NAME = "SerializedAttributeDataStore";
private static final String ATTRIBUTE_FILE_NAME = "TempAttributeDataStore";
/**
* File name string for serialized files of imports.
* File name string for Temp files of imports.
*/
private static final String IMPORT_FILE_NAME = "SerializedImportDataStore";
private static final String IMPORT_FILE_NAME = "TempImportDataStore";
/**
* File extension of serialized files.
* File extension of Temp files.
*/
private static final String SERIALIZE_FILE_EXTENSION = ".ser";
private static final String FILE_EXTENSION = ".tmp";
/**
* Directory for generating Serialized files.
* Directory for generating Temp files.
*/
private static final String GEN_DIR = "target/";
......@@ -120,35 +121,38 @@ public final class SerializedDataStore {
/**
* Default constructor.
*/
private SerializedDataStore() {
private TempDataStore() {
}
/**
* Writes specific info to a serialized file.
* Writes specific info to a Temp file.
*
* @param data data to be stored
* @param type type of serialized data store
* @throws IOException when fails to create a serialized data file.
* @param type type of Temp data store
* @param className class name
* @throws IOException when fails to create a Temp data file.
*/
public static void setSerializeData(String data, SerializedDataStoreType type) throws IOException {
public static void setTempData(String data, TempDataStoreType type, String className) throws IOException {
String fileName = "";
if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
if (type.equals(TempDataStoreType.ATTRIBUTE)) {
fileName = ATTRIBUTE_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
fileName = INTERFACE_METHOD_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
} else if (type.equals(TempDataStoreType.GETTER_METHODS)) {
fileName = GETTER_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) {
fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
} else if (type.equals(TempDataStoreType.BUILDER_METHODS)) {
fileName = BUILDER_METHOD_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
} else if (type.equals(TempDataStoreType.IMPL_METHODS)) {
fileName = IMPL_METHOD_FILE_NAME;
} else {
fileName = IMPORT_FILE_NAME;
}
File dir = new File(GEN_DIR + className + File.separator);
dir.mkdirs();
try {
OutputStream file = new FileOutputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
OutputStream file = new FileOutputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION);
OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
ObjectOutput output = new ObjectOutputStream(buffer);
......@@ -163,33 +167,34 @@ public final class SerializedDataStore {
}
/**
* Get the serialized data.
* Get the Temp data.
*
* @param type type of serialized data store
* @param type type of Temp data store
* @param className name of the class.
* @return list of attribute info.
* @throws IOException when fails to read from the file.
* @throws ClassNotFoundException when class is missing.
* @throws FileNotFoundException when file is missing.
*/
public static List<String> getSerializeData(SerializedDataStoreType type)
public static List<String> getTempData(TempDataStoreType type, String className)
throws IOException, FileNotFoundException, ClassNotFoundException {
String fileName = "";
if (type.equals(SerializedDataStoreType.ATTRIBUTE)) {
if (type.equals(TempDataStoreType.ATTRIBUTE)) {
fileName = ATTRIBUTE_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.INTERFACE_METHODS)) {
fileName = INTERFACE_METHOD_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.BUILDER_INTERFACE_METHODS)) {
} else if (type.equals(TempDataStoreType.GETTER_METHODS)) {
fileName = GETTER_METHOD_FILE_NAME;
} else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) {
fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.BUILDER_METHODS)) {
} else if (type.equals(TempDataStoreType.BUILDER_METHODS)) {
fileName = BUILDER_METHOD_FILE_NAME;
} else if (type.equals(SerializedDataStoreType.IMPL_METHODS)) {
} else if (type.equals(TempDataStoreType.IMPL_METHODS)) {
fileName = IMPL_METHOD_FILE_NAME;
} else {
fileName = IMPORT_FILE_NAME;
}
try {
InputStream file = new FileInputStream(GEN_DIR + fileName + SERIALIZE_FILE_EXTENSION);
InputStream file = new FileInputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION);
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);
try {
......@@ -204,7 +209,7 @@ public final class SerializedDataStore {
} catch (FileNotFoundException ex) {
throw new FileNotFoundException("No such file or directory.");
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("failed to fetch the serialized data file.");
throw new ClassNotFoundException("failed to fetch the Temp data file.");
}
}
}
......
......@@ -33,7 +33,6 @@ public final class YangFileScanner {
private YangFileScanner() {
}
/**
* Returns the list of java files.
*
......
......@@ -69,10 +69,9 @@ public final class YangIoUtils {
public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
if (pack.contains(UtilConstants.YANG_GEN_DIR)) {
String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR + UtilConstants.SLASH);
pack = strArray[1];
}
String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR);
pack = strArray[1];
}
try {
File packageInfo = new File(path + File.separator + "package-info.java");
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.tojava;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/**
* Unit test case for cached java file handle.
*/
public class CachedJavaFileHandleTest {
private static final String DIR_PKG = "target/unit/cachedfile/";
private static final String PKG = "org.onosproject.unittest";
private static final String CHILD_PKG = "target/unit/cachedfile/child";
private static final String YANG_NAME = "Test1";
private static final GeneratedFileType GEN_TYPE = GeneratedFileType.ALL;
/**
* Unit test case for add attribute info.
*
* @throws IOException when fails to add an attribute.
*/
@Test
public void testForAddAttributeInfo() throws IOException {
AttributeInfo attr = getAttr();
attr.setListAttr(false);
getFileHandle().addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
}
/**
* Unit test case for close of cached files.
*
* @throws IOException when fails to generate files.
*/
@Test
public void testForClose() throws IOException {
CopyrightHeader.parseCopyrightHeader();
AttributeInfo attr = getAttr();
attr.setListAttr(false);
CachedFileHandle handle = getFileHandle();
handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
handle.close();
assertThat(true, is(getStubDir().exists()));
assertThat(true, is(getStubPkgInfo().exists()));
assertThat(true, is(getStubInterfaceFile().exists()));
assertThat(true, is(getStubBuilderFile().exists()));
}
/**
* Unit test case for setting child's package.
*
* @throws IOException when fails to add child's package
*/
@Test
public void testForSetChildsPackage() throws IOException {
AttributeInfo attr = getAttr();
attr.setListAttr(false);
CachedFileHandle handle = getFileHandle();
handle.addAttributeInfo(attr.getAttributeType(), attr.getAttributeName(), attr.isListAttr());
handle.setChildsPackage(CHILD_PKG);
}
/**
* Returns attribute info.
*
* @return attribute info
*/
@SuppressWarnings("rawtypes")
private AttributeInfo getAttr() {
YangType<?> type = new YangType();
YangDataTypes dataType = YangDataTypes.STRING;
type.setDataTypeName("string");
type.setDataType(dataType);
AttributeInfo attr = new AttributeInfo();
attr.setAttributeName("testAttr");
attr.setAttributeType(type);
return attr;
}
/**
* Returns cached java file handle.
*
* @return java file handle.
*/
private CachedFileHandle getFileHandle() throws IOException {
CopyrightHeader.parseCopyrightHeader();
FileSystemUtil.createPackage(DIR_PKG + File.separator + PKG, YANG_NAME);
CachedFileHandle fileHandle = FileSystemUtil.createSourceFiles(PKG, YANG_NAME, GEN_TYPE);
fileHandle.setFilePath(DIR_PKG + PKG.replace(".", "/"));
return fileHandle;
}
/**
* Returns stub directory file object.
*
* @return stub directory file
*/
private File getStubDir() {
return new File(DIR_PKG);
}
/**
* Returns stub package-info file object.
*
* @return stub package-info file
*/
private File getStubPkgInfo() {
return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator
+ "package-info.java");
}
/**
* Returns stub interface file object.
*
* @return stub interface file
*/
private File getStubInterfaceFile() {
return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME
+ ".java");
}
/**
* Returns stub builder file.
*
* @return stub builder file
*/
private File getStubBuilderFile() {
return new File(DIR_PKG + PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH) + File.separator + YANG_NAME
+ "Builder.java");
}
}
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.tojava.utils;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
import org.onosproject.yangutils.translator.tojava.ImportInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
/**
* Unit test cases for java code snippet generator.
*/
public class JavaCodeSnippetGenTest {
private static final String PKG_INFO = "org.onosproject.unittest";
private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
private static final GeneratedFileType FILE_GEN_TYPE = GeneratedFileType.INTERFACE;
private static final GeneratedMethodTypes METHOD_GEN_TYPE = GeneratedMethodTypes.GETTER;
private static final String YANG_NAME = "Test";
private static final String STRING = "String";
/**
* Unit test case for import text.
*/
@Test
public void testForImportText() {
ImportInfo importInfo = new ImportInfo();
importInfo.setPkgInfo(PKG_INFO);
importInfo.setClassInfo(CLASS_INFO);
String imports = JavaCodeSnippetGen.getImportText(importInfo);
assertThat(true, is(imports.equals(UtilConstants.IMPORT + PKG_INFO + UtilConstants.PERIOD + CLASS_INFO
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
}
/**
* Unit test case for java class definition start.
*/
@Test
public void testForJavaClassDefStart() {
String classDef = JavaCodeSnippetGen.getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
assertThat(true,
is(classDef.equals(UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE
+ UtilConstants.SPACE + YANG_NAME + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE)));
}
/**
* Unit test case for java attribute info.
*/
@SuppressWarnings("rawtypes")
@Test
public void testForJavaAttributeInfo() {
String attributeWithType = JavaCodeSnippetGen.getJavaAttributeInfo(FILE_GEN_TYPE, YANG_NAME, getType());
assertThat(true, is(attributeWithType.equals(UtilConstants.PRIVATE + UtilConstants.SPACE
+ getType().getDataTypeName() + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
String attributeWithoutType = JavaCodeSnippetGen.getJavaAttributeInfo(FILE_GEN_TYPE, YANG_NAME, null);
assertThat(true,
is(attributeWithoutType.equals(
UtilConstants.PRIVATE + UtilConstants.SPACE + JavaIdentifierSyntax.getCaptialCase(YANG_NAME)
+ UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN)));
}
/**
* Unit test case for list attribute.
*/
@Test
public void testForListAttribute() {
String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING);
assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING
+ UtilConstants.DIAMOND_CLOSE_BRACKET)));
}
/**
* Unit test case for java method info.
*/
@Test
public void testForJavaMethodInfo() {
String method = JavaCodeSnippetGen.getJavaMethodInfo(FILE_GEN_TYPE, YANG_NAME, METHOD_GEN_TYPE, getType());
assertThat(true,
is(method.equals(UtilConstants.FOUR_SPACE_INDENTATION
+ JavaIdentifierSyntax.getCaptialCase(getType().getDataTypeName()) + UtilConstants.SPACE
+ UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(YANG_NAME)
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SEMI_COLAN)));
}
/**
* Unit test case for java class definition close.
*/
@Test
public void testForJavaClassDefClose() {
String classDef = JavaCodeSnippetGen.getJavaClassDefClose(FILE_GEN_TYPE, YANG_NAME);
assertThat(true, is(classDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
}
/**
* Returns YANG type.
*
* @return type
*/
@SuppressWarnings("rawtypes")
private YangType<?> getType() {
YangType<?> type = new YangType();
type.setDataTypeName(STRING);
type.setDataType(YangDataTypes.STRING);
return type;
}
}
......@@ -19,7 +19,7 @@ package org.onosproject.yangutils.utils.io.impl;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.utils.io.impl.SerializedDataStore.SerializedDataStoreType;
import org.onosproject.yangutils.utils.io.impl.TempDataStore.TempDataStoreType;
import java.io.FileNotFoundException;
import java.io.IOException;
......@@ -36,11 +36,12 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertNotNull;
/**
* Unit tests for the serialized data store for its contents.
* Unit tests for the Tempd data store for its contents.
*/
public final class SerializedDataStoreTest {
public final class TempDataStoreTest {
private final Logger log = getLogger(getClass());
private static final String CLASS_NAME = "YANG";
@Rule
public ExpectedException thrown = ExpectedException.none();
......@@ -59,7 +60,7 @@ public final class SerializedDataStoreTest {
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {SerializedDataStore.class };
Class<?>[] classesToConstruct = {TempDataStore.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
......@@ -74,12 +75,12 @@ public final class SerializedDataStoreTest {
public void insertAttributeDataTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String attributeData = "attribute content lists this";
SerializedDataStore.setSerializeData(attributeData, SerializedDataStoreType.ATTRIBUTE);
List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.ATTRIBUTE);
TempDataStore.setTempData(attributeData, TempDataStoreType.ATTRIBUTE, CLASS_NAME);
List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.ATTRIBUTE, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(attributeData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
SerializedDataStoreType.valueOf(SerializedDataStoreType.ATTRIBUTE.toString());
TempDataStoreType.valueOf(TempDataStoreType.ATTRIBUTE.toString());
}
/**
......@@ -89,10 +90,8 @@ public final class SerializedDataStoreTest {
public void insertBuilderInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String builderInterfaceMethodsData = "builder interface methods content lists this";
SerializedDataStore.setSerializeData(builderInterfaceMethodsData,
SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
List<String> attributeInfo = SerializedDataStore
.getSerializeData(SerializedDataStoreType.BUILDER_INTERFACE_METHODS);
TempDataStore.setTempData(builderInterfaceMethodsData, TempDataStoreType.BUILDER_INTERFACE_METHODS, CLASS_NAME);
List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.BUILDER_INTERFACE_METHODS, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(builderInterfaceMethodsData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
......@@ -105,8 +104,8 @@ public final class SerializedDataStoreTest {
public void insertBuilderMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String builderMethodsData = "builder methods content lists this";
SerializedDataStore.setSerializeData(builderMethodsData, SerializedDataStoreType.BUILDER_METHODS);
List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.BUILDER_METHODS);
TempDataStore.setTempData(builderMethodsData, TempDataStoreType.BUILDER_METHODS, CLASS_NAME);
List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.BUILDER_METHODS, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(builderMethodsData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
......@@ -119,8 +118,8 @@ public final class SerializedDataStoreTest {
public void insertImplMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String implMethodsData = "impl methods content lists this";
SerializedDataStore.setSerializeData(implMethodsData, SerializedDataStoreType.IMPL_METHODS);
List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPL_METHODS);
TempDataStore.setTempData(implMethodsData, TempDataStoreType.IMPL_METHODS, CLASS_NAME);
List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.IMPL_METHODS, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(implMethodsData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
......@@ -133,8 +132,8 @@ public final class SerializedDataStoreTest {
public void insertImportTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String importData = "interface methods content lists this";
SerializedDataStore.setSerializeData(importData, SerializedDataStoreType.IMPORT);
List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.IMPORT);
TempDataStore.setTempData(importData, TempDataStoreType.IMPORT, CLASS_NAME);
List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.IMPORT, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(importData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
......@@ -147,8 +146,8 @@ public final class SerializedDataStoreTest {
public void insertInterfaceMethodsTest() throws IOException, ClassNotFoundException, FileNotFoundException {
String interfaceMethodsData = "interface methods content lists this";
SerializedDataStore.setSerializeData(interfaceMethodsData, SerializedDataStoreType.INTERFACE_METHODS);
List<String> attributeInfo = SerializedDataStore.getSerializeData(SerializedDataStoreType.INTERFACE_METHODS);
TempDataStore.setTempData(interfaceMethodsData, TempDataStoreType.GETTER_METHODS, CLASS_NAME);
List<String> attributeInfo = TempDataStore.getTempData(TempDataStoreType.GETTER_METHODS, CLASS_NAME);
List<String> expectedinfo = new LinkedList<>();
expectedinfo.add(interfaceMethodsData);
assertThat(true, is(attributeInfo.equals(expectedinfo)));
......