public void getParents () throws Exception {
Long time = new Date().getTime();
CategoryAPI categoryAPI = APILocator.getCategoryAPI();
PermissionAPI permissionAPI = APILocator.getPermissionAPI();
ContentletAPI contentletAPI = APILocator.getContentletAPI();
List<Category> categories = new ArrayList<Category>();
//***************************************************************
//Creating new categories
HibernateUtil.startTransaction();
//Adding the parent category
Category parentCategory = new Category();
parentCategory.setCategoryName( "Movies" + time );
parentCategory.setKey( "movies" + time );
parentCategory.setCategoryVelocityVarName( "movies" + time );
parentCategory.setSortOrder( (String) null );
parentCategory.setKeywords( null );
//Saving it
categoryAPI.save( null, parentCategory, user, false );
//Creating child categories
//New Child category
Category childCategory1 = new Category();
childCategory1.setCategoryName( "Action" + time );
childCategory1.setKey( "action" + time );
childCategory1.setCategoryVelocityVarName( "action" + time );
childCategory1.setSortOrder( (String) null );
childCategory1.setKeywords( null );
//Saving it
categoryAPI.save( parentCategory, childCategory1, user, false );
categories.add( childCategory1 );
//New Child category
Category childCategory2 = new Category();
childCategory2.setCategoryName( "Drama" + time );
childCategory2.setKey( "drama" + time );
childCategory2.setCategoryVelocityVarName( "drama" + time );
childCategory2.setSortOrder( (String) null );
childCategory2.setKeywords( null );
//Saving it
categoryAPI.save( parentCategory, childCategory2, user, false );
categories.add( childCategory2 );
HibernateUtil.commitTransaction();
//***************************************************************
//Verify If we find the parent for the categories we just added categories
List<Category> parents = categoryAPI.getParents( childCategory1, user, false );
assertNotNull( parents );
assertTrue( parents.size() > 0 );
assertEquals( parents.get( 0 ), parentCategory );
parents = categoryAPI.getParents( childCategory2, user, false );
assertNotNull( parents );
assertTrue( parents.size() > 0 );
assertEquals( parents.get( 0 ), parentCategory );
//***************************************************************
//Set up a new structure with categories
HibernateUtil.startTransaction();
//Create the new structure
Structure testStructure = createStructure( "JUnit Test Categories Structure_" + String.valueOf( new Date().getTime() ), "junit_test_categories_structure_" + String.valueOf( new Date().getTime() ) );
//Add a Text field
Field textField = new Field( "JUnit Test Text", Field.FieldType.TEXT, Field.DataType.TEXT, testStructure, false, true, true, 1, false, false, false );
FieldFactory.saveField( textField );
//Add a Category field
Field categoryField = new Field( "JUnit Movies", Field.FieldType.CATEGORY, Field.DataType.TEXT, testStructure, true, true, true, 2, false, false, true );
categoryField.setValues( parentCategory.getInode() );
FieldFactory.saveField( categoryField );
//***************************************************************
//Set up a content for the categories structure
Contentlet contentlet = new Contentlet();
contentlet.setStructureInode( testStructure.getInode() );
contentlet.setHost( defaultHost.getIdentifier() );
contentlet.setLanguageId( APILocator.getLanguageAPI().getDefaultLanguage().getId() );
//Validate if the contenlet is OK
contentletAPI.validateContentlet( contentlet, categories );
//Saving the contentlet
contentlet = APILocator.getContentletAPI().checkin( contentlet, categories, permissionAPI.getPermissions( contentlet, false, true ), user, false );
APILocator.getContentletAPI().isInodeIndexed( contentlet.getInode() );
APILocator.getVersionableAPI().setLive( contentlet );
HibernateUtil.commitTransaction();