Bharat saraswal
Committed by Gerrit Code Review

UT test cases fixes in YANG translator.

Change-Id: I8408280c663dda016956b76db27285f466f24fad
......@@ -63,6 +63,21 @@ public final class JavaAttributeInfo {
}
/**
* Construct object of java attribute info.
*
* @param attrType YANG type
* @param name attribute name
* @param isListAttr is list attribute
* @param isQualifiedName is qualified name
*/
public JavaAttributeInfo(YangType<?> attrType, String name, boolean isListAttr, boolean isQualifiedName) {
this.attrType = attrType;
this.name = name;
this.isListAttr = isListAttr;
this.isQualifiedName = isQualifiedName;
}
/**
* Get the data type info of attribute.
*
* @return the data type info of attribute
......
......@@ -176,21 +176,10 @@ public final class YangIoUtils {
}
/**
* Returns backspaced string.
*
* @param charString char string
* @return backspace string
*/
public static String deleteLastChar(String charString) {
return charString.substring(0, charString.length() - 1);
}
/**
* Get the directory path of the package in canonical form.
*
* @param baseCodeGenPath base path where the generated files needs to be
* put.
* put
* @param pathOfJavaPkg java package of the file being generated
* @return absolute path of the package in canonical form
*/
......@@ -211,7 +200,7 @@ public final class YangIoUtils {
* Get the absolute path of the package in canonical form.
*
* @param baseCodeGenPath base path where the generated files needs to be
* put.
* put
* @param pathOfJavaPkg java package of the file being generated
* @return absolute path of the package in canonical form
*/
......
/*
* 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 java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangType;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.onosproject.yangutils.datamodel.YangDataTypes.BOOLEAN;
import static org.onosproject.yangutils.datamodel.YangDataTypes.INT32;
import static org.onosproject.yangutils.datamodel.YangDataTypes.STRING;
import static org.onosproject.yangutils.datamodel.YangDataTypes.UINT8;
import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaDataType;
import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportClass;
import static org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType.getJavaImportPackage;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
/**
* Unit test case for attribute java data type.
*/
public class AttributesJavaDataTypeTest {
private static final YangDataTypes TYPE1 = STRING;
private static final YangDataTypes TYPE2 = INT32;
private static final YangDataTypes TYPE3 = BOOLEAN;
private static final YangDataTypes TYPE4 = UINT8;
private static final String CLASS_INFO1 = "String";
private static final String CLASS_INFO2 = "int";
private static final String CLASS_INFO3 = "boolean";
private static final String CLASS_INFO4 = "short";
private static final String CLASS_INFO5 = "Integer";
private static String test = "";
/**
* Unit test for private constructor.
*
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {AttributesJavaDataType.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
assertNotNull(constructor.newInstance());
}
}
/**
* Unit test for java class info method test.
*/
@Test
public void testgetJavaClassInfo() {
test = getJavaImportClass(getStubYangType(TYPE1), false);
assertThat(true, is(test.equals(CLASS_INFO1)));
test = getJavaImportClass(getStubYangType(TYPE2), true);
assertThat(true, is(test.equals(CLASS_INFO5)));
test = getJavaImportClass(getStubYangType(TYPE3), false);
assertThat(null, is(test));
test = getJavaImportClass(getStubYangType(TYPE4), false);
assertThat(null, is(test));
}
/**
* Unit test for java data type method.
*/
@Test
public void testgetJavaDataType() {
test = getJavaDataType(getStubYangType(TYPE1));
assertThat(true, is(test.equals(CLASS_INFO1)));
test = getJavaDataType(getStubYangType(TYPE2));
assertThat(true, is(test.equals(CLASS_INFO2)));
test = getJavaDataType(getStubYangType(TYPE3));
assertThat(true, is(test.equals(CLASS_INFO3)));
test = getJavaDataType(getStubYangType(TYPE4));
assertThat(true, is(test.equals(CLASS_INFO4)));
}
/**
* Unit test for java package info method.
*/
@Test
public void testgetJavaPkgInfo() {
test = getJavaImportPackage(getStubYangType(TYPE1), false, CLASS_INFO1);
assertThat(true, is(test.equals(JAVA_LANG)));
test = getJavaImportPackage(getStubYangType(TYPE2), true, CLASS_INFO5);
assertThat(true, is(test.equals(JAVA_LANG)));
test = getJavaImportPackage(getStubYangType(TYPE3), false, CLASS_INFO3);
assertThat(null, is(test));
test = getJavaImportPackage(getStubYangType(TYPE4), false, CLASS_INFO4);
assertThat(null, is(test));
}
/**
* Returns stub YANG type for test.
*
* @param dataTypes YANG data types
* @return YANG type
*/
private YangType<?> getStubYangType(YangDataTypes dataTypes) {
YangType<?> type = new YangType();
type.setDataType(dataTypes);
return type;
}
}
......@@ -30,24 +30,19 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
/**
* Unit tests for class definition generator for generated files.
*/
public final class ClassDefinitionGeneratorTest {
private static final String CLASS_NAME = "testclass";
private static final String CLASS_NAME = "TestClass";
private static final String INTERFACE_CLASS_DEF = "public interface TestClass {\n";
private static final String BULDER_INTERFACE_CLASS_DEF = "interface TestClassBuilder {\n\n";
private static final String BUILDER_CLASS_DEF = "public class TestClassBuilder implements "
+ "TestClass.TestClassBuilder {\n";
private static final String IMPL_CLASS_DEF = "public final class TestClassImpl implements TestClass {\n";
private static final String TYPE_DEF_CLASS_DEF = "public final class TestClass {\n";
/**
* Unit test for private constructor.
......@@ -78,9 +73,7 @@ public final class ClassDefinitionGeneratorTest {
public void generateBuilderClassDefinitionTest() {
String builderClassDefinition = generateClassDefinition(BUILDER_CLASS_MASK, CLASS_NAME);
assertThat(true, is(builderClassDefinition.equals(
PUBLIC + SPACE + CLASS + SPACE + CLASS_NAME + BUILDER + SPACE + IMPLEMENTS + SPACE + CLASS_NAME + PERIOD
+ CLASS_NAME + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
assertThat(true, is(builderClassDefinition.equals(BUILDER_CLASS_DEF)));
}
/**
......@@ -90,8 +83,7 @@ public final class ClassDefinitionGeneratorTest {
public void generateBuilderInterfaceDefinitionTest() {
String builderInterfaceDefinition = generateClassDefinition(BUILDER_INTERFACE_MASK, CLASS_NAME);
assertThat(true, is(builderInterfaceDefinition
.equals(INTERFACE + SPACE + CLASS_NAME + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE)));
assertThat(true, is(builderInterfaceDefinition.equals(BULDER_INTERFACE_CLASS_DEF)));
}
/**
......@@ -101,9 +93,7 @@ public final class ClassDefinitionGeneratorTest {
public void generateImplDefinitionTest() {
String implDefinition = generateClassDefinition(IMPL_CLASS_MASK, CLASS_NAME);
assertThat(true, is(implDefinition.equals(
PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + CLASS_NAME + IMPL + SPACE + IMPLEMENTS + SPACE
+ CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
assertThat(true, is(implDefinition.equals(IMPL_CLASS_DEF)));
}
/**
......@@ -113,8 +103,7 @@ public final class ClassDefinitionGeneratorTest {
public void generateinterfaceDefinitionTest() {
String interfaceDefinition = generateClassDefinition(INTERFACE_MASK, CLASS_NAME);
assertThat(true, is(interfaceDefinition
.equals(PUBLIC + SPACE + INTERFACE + SPACE + CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
assertThat(true, is(interfaceDefinition.equals(INTERFACE_CLASS_DEF)));
}
/**
......@@ -124,7 +113,6 @@ public final class ClassDefinitionGeneratorTest {
public void generateTypeDefTest() {
String typeDef = generateClassDefinition(GENERATE_TYPEDEF_CLASS, CLASS_NAME);
assertThat(true, is(typeDef.equals(
PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + CLASS_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
assertThat(true, is(typeDef.equals(TYPE_DEF_CLASS_DEF)));
}
}
......
/*
* Copyright 2016 Open Networking Laboratory
*
......@@ -16,70 +17,78 @@
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.UtilConstants;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.apache.maven.project.MavenProject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.utils.UtilConstants;
import org.sonatype.plexus.build.incremental.BuildContext;
import org.sonatype.plexus.build.incremental.DefaultBuildContext;
import org.slf4j.Logger;
import static org.slf4j.LoggerFactory.getLogger;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertNotNull;
import static org.hamcrest.core.Is.is;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
/**
* Unit tests for YANG io utils.
*/
public final class YangIoUtilsTest {
public static String baseDir = "target/UnitTestCase";
public static String createPath = baseDir + File.separator + "dir1/dir2/dir3/dir4/";
private final Logger log = getLogger(getClass());
private static final String BASE_DIR = "target/UnitTestCase";
private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
private static final String CHECK_STRING = "one, two, three, four, five, six";
private static final String TRIM_STRING = "one, two, three, four, five, ";
/**
* Expected exceptions.
*/
@Rule
public ExpectedException thrown = ExpectedException.none();
/**
* This test case checks whether the package-info file is created.
*
* @throws IOException when fails to do IO operations for test case
*/
@Test
public void addPackageInfoTest() throws IOException {
File dirPath = new File(createPath);
File dirPath = new File(CREATE_PATH);
dirPath.mkdirs();
CopyrightHeader.parseCopyrightHeader();
YangIoUtils.addPackageInfo(dirPath, "check1", createPath);
addPackageInfo(dirPath, "check1", CREATE_PATH);
File filePath = new File(dirPath + File.separator + "package-info.java");
assertThat(filePath.isFile(), is(true));
}
/**
* This test case checks with an additional info in the path.
*
* @throws IOException when fails to do IO operations for test case
*/
@Test
public void addPackageInfoWithPathTest() throws IOException {
File dirPath = new File(createPath);
File dirPath = new File(CREATE_PATH);
dirPath.mkdirs();
CopyrightHeader.parseCopyrightHeader();
YangIoUtils.addPackageInfo(dirPath, "check1", "src/main/yangmodel/" + createPath);
addPackageInfo(dirPath, "check1", "src/main/yangmodel/" + CREATE_PATH);
File filePath = new File(dirPath + File.separator + "package-info.java");
assertThat(filePath.isFile(), is(true));
}
/**
* This test case checks whether the package-info file is created when invalid path is given.
*
* @throws IOException when fails to do IO operations for test case
*/
@Test
public void addPackageInfoWithEmptyPathTest() throws IOException {
......@@ -87,7 +96,7 @@ public final class YangIoUtilsTest {
File dirPath = new File("invalid/check");
thrown.expect(IOException.class);
thrown.expectMessage("Exception occured while creating package info file.");
YangIoUtils.addPackageInfo(dirPath, "check1", createPath);
addPackageInfo(dirPath, "check1", CREATE_PATH);
File filePath1 = new File(dirPath + File.separator + "package-info.java");
assertThat(filePath1.isFile(), is(false));
}
......@@ -116,26 +125,30 @@ public final class YangIoUtilsTest {
/**
* This test case checks if the directory is cleaned.
*
* @throws IOException when fails to do IO operations for test case
*/
@Test
public void cleanGeneratedDirTest() throws IOException {
File baseDirPath = new File(baseDir);
File createNewDir = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR);
File baseDirPath = new File(BASE_DIR);
File createNewDir = new File(BASE_DIR + File.separator + UtilConstants.YANG_GEN_DIR);
createNewDir.mkdirs();
File createFile = new File(createNewDir + File.separator + "check1.java");
createFile.createNewFile();
YangIoUtils.clean(baseDirPath.getAbsolutePath());
clean(baseDirPath.getAbsolutePath());
}
/**
* This test case checks the cleaning method when an invalid path is provided.
*
* @throws IOException when fails to do IO operations for test case
*/
@Test
public void cleanWithInvalidDirTest() throws IOException {
File baseDirPath = new File(baseDir + "invalid");
YangIoUtils.clean(baseDirPath.getAbsolutePath());
File baseDirPath = new File(BASE_DIR + "invalid");
clean(baseDirPath.getAbsolutePath());
}
/**
......@@ -144,20 +157,30 @@ public final class YangIoUtilsTest {
@Test
public void createDirectoryTest() {
File dirPath = YangIoUtils.createDirectories(createPath);
File dirPath = createDirectories(CREATE_PATH);
assertThat(dirPath.isDirectory(), is(true));
}
/**
* This testcase checks whether the source is getting added.
* This test case checks whether the source is getting added.
*/
@Test
public void testForAddSource() {
MavenProject project = new MavenProject();
BuildContext context = new DefaultBuildContext();
File sourceDir = new File(baseDir + File.separator + "yang");
File sourceDir = new File(BASE_DIR + File.separator + "yang");
sourceDir.mkdirs();
YangIoUtils.addToSource(sourceDir.toString(), project, context);
addToSource(sourceDir.toString(), project, context);
}
/*
* Unit test case for trim at last method.
*/
@Test
public void testForTrimAtLast() {
String test = trimAtLast(CHECK_STRING, "six");
assertThat(test.contains(TRIM_STRING), is(true));
}
}
......