* @throws InterruptedException
*/
@Test
public void importFile () throws DotDataException, DotSecurityException, IOException, InterruptedException {
ContentletAPI contentletAPI = APILocator.getContentletAPI();
//Create a test structure
String structure1Suffix = String.valueOf( new Date().getTime() );
Structure structure1 = new Structure();
structure1.setName( "Import Test " + structure1Suffix );
structure1.setVelocityVarName( "ImportTest_" + structure1Suffix );
structure1.setDescription( "Testing import of csv files" );
StructureFactory.saveStructure( structure1 );
//Create test fields
Field textFileStructure1 = new Field( "Title", Field.FieldType.TEXT, Field.DataType.TEXT, structure1, true, true, true, 1, false, false, true );
FieldFactory.saveField( textFileStructure1 );
Field hostFileStructure1 = new Field( "Host", Field.FieldType.HOST_OR_FOLDER, Field.DataType.TEXT, structure1, true, true, true, 2, false, false, true );
FieldFactory.saveField( hostFileStructure1 );
//----------------PREVIEW = TRUE------------------------------------------
//------------------------------------------------------------------------
//Create the csv file to import
Reader reader = createTempFile( "Title, Host" + "\r\n" +
"Test1, " + defaultHost.getIdentifier() + "\r\n" +
"Test2, " + defaultHost.getIdentifier() + "\r\n" +
"Test3, " + defaultHost.getIdentifier() + "\r\n" +
"Test4, " + defaultHost.getIdentifier() + "\r\n" );
CsvReader csvreader = new CsvReader( reader );
csvreader.setSafetySwitch( false );
String[] csvHeaders = csvreader.getHeaders();
//Preview=true
HashMap<String, List<String>> results = ImportUtil.importFile( 0L, defaultHost.getInode(), structure1.getInode(), new String[]{}, true, false, user, defaultLanguage.getId(), csvHeaders, csvreader, -1, -1, reader );
//Validations
validate( results, true, false, true );
//As it was a preview nothing should be saved
List<Contentlet> savedData = contentletAPI.findByStructure( structure1.getInode(), user, false, 0, 0 );
//Validations
assertNotNull( savedData );
assertEquals( savedData.size(), 0 );
//----------------PREVIEW = FALSE-----------------------------------------
//------------------------------------------------------------------------
//Create the csv file to import
reader = createTempFile( "Title, Host" + "\r\n" +
"Test1, " + defaultHost.getIdentifier() + "\r\n" +
"Test2, " + defaultHost.getIdentifier() + "\r\n" +
"Test3, " + defaultHost.getIdentifier() + "\r\n" +
"Test4, " + defaultHost.getIdentifier() + "\r\n" );
csvreader = new CsvReader( reader );
csvreader.setSafetySwitch( false );
csvHeaders = csvreader.getHeaders();
//Preview=false
results = ImportUtil.importFile( 0L, defaultHost.getInode(), structure1.getInode(), new String[]{}, false, false, user, defaultLanguage.getId(), csvHeaders, csvreader, -1, -1, reader );
//Validations
validate( results, false, false, true );
//Now we should have saved data
savedData = contentletAPI.findByStructure( structure1.getInode(), user, false, 0, 0 );
//Validations
assertNotNull( savedData );
assertEquals( savedData.size(), 4 );
//----------------USING WRONG HOST IDENTIFIERS----------------------------
//------------------------------------------------------------------------
//Create the csv file to import
reader = createTempFile( "Title, Host" + "\r\n" +
"Test5, " + defaultHost.getIdentifier() + "\r\n" +
"Test6, " + "999-99999999-99999999-00000" + "\r\n" +
"Test7, " + "44444444-5555555555-2222" + "\r\n" );
csvreader = new CsvReader( reader );
csvreader.setSafetySwitch( false );
csvHeaders = csvreader.getHeaders();
//Preview=true
results = ImportUtil.importFile( 0L, defaultHost.getInode(), structure1.getInode(), new String[]{}, true, false, user, defaultLanguage.getId(), csvHeaders, csvreader, -1, -1, reader );
//Validations
validate( results, true, true, true );
//We should have the same amount on data
savedData = contentletAPI.findByStructure( structure1.getInode(), user, false, 0, 0 );
//Validations
assertNotNull( savedData );
assertEquals( savedData.size(), 4 );
//---------------USING KEY FIELDS-----------------------------------------
//------------------------------------------------------------------------
//Making sure the contentlets are in the indexes
List<ContentletSearch> contentletSearchResults;
int x = 0;
do {
Thread.sleep( 200 );
//Verify if it was added to the index
contentletSearchResults = contentletAPI.searchIndex( "+structureName:" + structure1.getVelocityVarName() + " +working:true +deleted:false +" + structure1.getVelocityVarName() + ".title:Test1 +languageId:1", 0, -1, null, user, true );
x++;
} while ( (contentletSearchResults == null || contentletSearchResults.isEmpty()) && x < 100 );
//Create the csv file to import
reader = createTempFile( "Title, Host" + "\r\n" +
"Test1, " + defaultHost.getIdentifier() + "\r\n" +
"Test2, " + defaultHost.getIdentifier() + "\r\n" );
csvreader = new CsvReader( reader );
csvreader.setSafetySwitch( false );
csvHeaders = csvreader.getHeaders();
//Preview=false
results = ImportUtil.importFile( 0L, defaultHost.getInode(), structure1.getInode(), new String[]{textFileStructure1.getInode()}, false, false, user, defaultLanguage.getId(), csvHeaders, csvreader, -1, -1, reader );
//Validations
validate( results, false, false, true );//We should expect warnings: Line #X. The key fields chosen match 1 existing content(s) - more than one match suggests key(s) are not properly unique
//We used the key fields, so the import process should update instead to add new records
savedData = contentletAPI.findByStructure( structure1.getInode(), user, false, 0, 0 );
//Validations
assertNotNull( savedData );
assertEquals( savedData.size(), 4 );
//---------------USING IDENTIFIER COLUMN----------------------------------
//------------------------------------------------------------------------
//Create the csv file to import
String id1 = null;
String id2 = null;
for ( Contentlet content : savedData ) {
if ( content.getMap().get( "title" ).equals( "Test1" ) ) {
id1 = content.getIdentifier();
} else if ( content.getMap().get( "title" ).equals( "Test2" ) ) {
id2 = content.getIdentifier();
}
}
reader = createTempFile( "Identifier, Title, Host" + "\r\n" +
id1 + ", Test1_edited, " + defaultHost.getIdentifier() + "\r\n" +
id2 + ", Test2_edited, " + defaultHost.getIdentifier() + "\r\n" );
csvreader = new CsvReader( reader );
csvreader.setSafetySwitch( false );
csvHeaders = csvreader.getHeaders();
//Preview=false
results = ImportUtil.importFile( 0L, defaultHost.getInode(), structure1.getInode(), new String[]{}, false, false, user, defaultLanguage.getId(), csvHeaders, csvreader, -1, -1, reader );
//Validations
validate( results, false, false, true );
//We used a identifier column, so the import process should update instead to add new records
savedData = contentletAPI.findByStructure( structure1.getInode(), user, false, 0, 0 );
//Validations
assertNotNull( savedData );
assertEquals( savedData.size(), 4 );
//-------------------------LANGUAGE AND KEY FIELDS------------------------
//------------------------------------------------------------------------
//Create the csv file to import
reader = createTempFile( "languageCode, countryCode, Title, Host" + "\r\n" +
"es, ES, Test1_edited, " + defaultHost.getIdentifier() + "\r\n" +
"es, ES, Test2_edited, " + defaultHost.getIdentifier() + "\r\n" );
csvreader = new CsvReader( reader );
csvreader.setSafetySwitch( false );
csvHeaders = csvreader.getHeaders();
int languageCodeHeaderColumn = 0;
int countryCodeHeaderColumn = 1;
//Preview=false
results = ImportUtil.importFile( 0L, defaultHost.getInode(), structure1.getInode(), new String[]{textFileStructure1.getInode()}, false, true, user, -1, csvHeaders, csvreader, languageCodeHeaderColumn, countryCodeHeaderColumn, reader );
//Validations
validate( results, false, false, false );
//We used the key fields, so the import process should update instead to add new records
savedData = contentletAPI.findByStructure( structure1.getInode(), user, false, 0, 0 );
//Validations
assertNotNull( savedData );
assertEquals( savedData.size(), 6 );
//Validate we saved the contentlets on spanish