Added test case

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/widgets/ws-thredds-sync-widget@165040 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-03-14 10:30:57 +00:00
parent dba7f8f501
commit f983e1a5ba
1 changed files with 198 additions and 0 deletions

View File

@ -0,0 +1,198 @@
/**
*
*/
package org.gcube.portlets.widgets.wsthreddssync;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceItem;
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.widgets.wsthreddssync.server.SyncronizeWithThredds;
/**
* The Class UnSyncThreddsFolders.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Mar 13, 2018
*/
public class UnSyncThreddsFolders {
public static String DEFAULT_SCOPE = "/gcube"; //DEV
public static String TEST_USER = "francesco.mangiacrapa";
//public static String FOLDER_SYNC = "d6dae663-91d1-4da7-b13d-959de6fb2f86"; //TestSync is root Folder
//token user: Francesco Mangiacrapa - /gcube (root)
//user.token=0e2c7963-8d3e-4ea6-a56d-ffda530dd0fa-98187548
public static String TEST_USER_TOKEN = "0e2c7963-8d3e-4ea6-a56d-ffda530dd0fa-98187548";
// public static String FOLDER_A = "983d4ab9-e8fd-4c6d-869e-734a730e3e50";
// public static String FOLDER_B = "94995b10-6fcb-4ed8-a3ea-4a6dd7d33a7a";
// public static String FOLDER_C = "e945fcfd-9da9-45a0-98a9-d1940d1720bb";
private static List<String> lstUnSynchedItem = new ArrayList<String>();
private static List<String> lstUnSynchedFailedItem = new ArrayList<String>();
private static List<String> lstErrorItem = new ArrayList<String>();
private static long totalAttempts = 0;
public static SyncronizeWithThredds syncService = new SyncronizeWithThredds();
/**
* Gets the workspace.
*
* @return the workspace
* @throws InternalErrorException the internal error exception
* @throws HomeNotFoundException the home not found exception
* @throws WorkspaceFolderNotFoundException the workspace folder not found exception
*/
public static Workspace getWorkspace() throws InternalErrorException, HomeNotFoundException, WorkspaceFolderNotFoundException
{
System.out.println("Get Workspace scope: "+DEFAULT_SCOPE + " username: "+TEST_USER);
ScopeProvider.instance.set(DEFAULT_SCOPE);
Workspace workspace = HomeLibrary.getUserWorkspace(TEST_USER);
return workspace;
}
/**
* The main method.
*
* @param args the arguments
* @throws WorkspaceFolderNotFoundException the workspace folder not found exception
* @throws InternalErrorException the internal error exception
* @throws HomeNotFoundException the home not found exception
*/
public static void main(String[] args) throws WorkspaceFolderNotFoundException, InternalErrorException, HomeNotFoundException {
Workspace ws = getWorkspace();
unsycFirstLevel(ws, ws.getRoot().getId(), false);
System.out.println("UnSync completed");
System.out.println("\nUnsync attempted: "+totalAttempts);
System.out.println("\nTotal failed unsync: "+lstUnSynchedFailedItem.size());
for (String string : args) {
System.out.println("Failed unsync: "+string);
}
System.out.println("\nUnsynched "+lstUnSynchedItem.size() +" item/s");
for (String string : lstUnSynchedItem) {
System.out.println("Unsynched id: "+string);
}
System.out.println("\nErros on "+lstErrorItem.size() +" item/s");
for (String string : lstErrorItem) {
System.out.println("Error on id: "+string);
}
}
/**
* Unsyc first level.
*
* @param ws the ws
* @param itemId the item id
* @param depthUnsync the depth unsync
*/
public static void unsycFirstLevel(Workspace ws, String itemId, boolean depthUnsync){
WorkspaceItem item;
try {
item = ws.getItem(itemId);
if(item.isFolder()){
List<? extends WorkspaceItem> children = item.getChildren();
for (WorkspaceItem workspaceItem : children) {
if(depthUnsync)
unsycFirstLevel(ws, workspaceItem.getId(), depthUnsync);
unsynFolder(ws, workspaceItem.getId());
}
}
}catch (ItemNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
lstErrorItem.add(itemId);
}catch (InternalErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
lstErrorItem.add(itemId);
}
}
/**
* Unsyn workspace sub tree.
*
* @param ws the ws
* @param itemId the item id
*/
public static void unsynFolder(Workspace ws, String itemId) {
if(itemId==null)
return;
try{
if(syncService.isItemSynched(itemId, DEFAULT_SCOPE, TEST_USER)){
Boolean unsynched = syncService.doUnSync(itemId, false, DEFAULT_SCOPE, TEST_USER_TOKEN);
totalAttempts++;
if(unsynched)
lstUnSynchedItem.add(itemId);
else
lstUnSynchedFailedItem.add(itemId);
}
}catch(Exception e){
e.printStackTrace();
lstErrorItem.add(itemId);
}
// WorkspaceItem item;
// try {
//
// item = ws.getItem(itemId);
//
// if(item.isFolder()){
//
// List<? extends WorkspaceItem> children = item.getChildren();
// for (WorkspaceItem workspaceItem : children) {
// unsynWorkspaceSubTree(ws, workspaceItem.getId());
// }
// }
//
// }catch (ItemNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// lstErrorItem.add(itemId);
//
// }catch (InternalErrorException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// lstErrorItem.add(itemId);
// }
}
}