[ONOS-3908] YANG container translator.
Change-Id: I4e239509df747238905ca0995f41019679093627
diff --git a/src/test/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandleTest.java b/src/test/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandleTest.java
new file mode 100644
index 0000000..9a62458
--- /dev/null
+++ b/src/test/java/org/onosproject/yangutils/translator/tojava/CachedJavaFileHandleTest.java
@@ -0,0 +1,170 @@
+/*
+ * 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");
+ }
+
+}
diff --git a/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java b/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java
new file mode 100644
index 0000000..bf73689
--- /dev/null
+++ b/src/test/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGenTest.java
@@ -0,0 +1,134 @@
+/*
+ * 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;
+ }
+}
diff --git a/src/test/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStoreTest.java b/src/test/java/org/onosproject/yangutils/utils/io/impl/TempDataStoreTest.java
similarity index 75%
rename from src/test/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStoreTest.java
rename to src/test/java/org/onosproject/yangutils/utils/io/impl/TempDataStoreTest.java
index 154dcdd..11e31c5 100644
--- a/src/test/java/org/onosproject/yangutils/utils/io/impl/SerializedDataStoreTest.java
+++ b/src/test/java/org/onosproject/yangutils/utils/io/impl/TempDataStoreTest.java
@@ -19,7 +19,7 @@
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.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 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 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 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 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 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 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 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)));