From 1f5e26160fe30b31b6e8ffb471de805c728c5c53 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Fri, 14 Nov 2014 14:03:04 +0000 Subject: [PATCH] added testers git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/uri-resolver-manager@101619 82a268e6-3cf1-43bd-a215-b396298e98cf --- src/test/java/HttpRequestUtil.java | 84 +++++++++ src/test/java/UriResolverManagerTest.java | 113 ++++++++++++ src/test/java/UriResolverStressTest.java | 158 +++++++++++++++++ src/test/java/UriResolverTest.java | 206 ++++++++++++---------- 4 files changed, 463 insertions(+), 98 deletions(-) create mode 100644 src/test/java/HttpRequestUtil.java create mode 100644 src/test/java/UriResolverManagerTest.java create mode 100644 src/test/java/UriResolverStressTest.java diff --git a/src/test/java/HttpRequestUtil.java b/src/test/java/HttpRequestUtil.java new file mode 100644 index 0000000..29559b7 --- /dev/null +++ b/src/test/java/HttpRequestUtil.java @@ -0,0 +1,84 @@ + + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.SocketTimeoutException; +import java.net.URL; +import java.net.URLConnection; + +import org.apache.log4j.Logger; + + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Apr 26, 2013 + * + */ +public class HttpRequestUtil { + + private static final int CONNECTION_TIMEOUT = 1000; + public static Logger logger = Logger.getLogger(HttpRequestUtil.class); + + public static boolean urlExists(String urlConn) throws Exception { + + + if(urlConn==null || urlConn.isEmpty()) + return false; + + URL url; + + try { + url = new URL(urlConn); + + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(CONNECTION_TIMEOUT); + connection.setReadTimeout(CONNECTION_TIMEOUT+CONNECTION_TIMEOUT); + + logger.trace("open connection on: " + url); + + // Cast to a HttpURLConnection + if (connection instanceof HttpURLConnection) { + HttpURLConnection httpConnection = (HttpURLConnection) connection; + + httpConnection.setRequestMethod("GET"); + + int code = httpConnection.getResponseCode(); + + httpConnection.disconnect(); + + if (code == 200) { + logger.trace("status code is "+code+" - on url connection: "+urlConn); + return true; + }else + logger.warn("status code is "+code+" - on url connection: "+urlConn); + +// logger.trace("result: "+result); + + } else { + logger.error("error - not a http request!"); + } + + return false; + + } catch (SocketTimeoutException e) { + logger.error("Error SocketTimeoutException with url " +urlConn, e); + return true; + } catch (MalformedURLException e) { + logger.error("Error MalformedURLException with url " +urlConn, e); + throw new Exception("Error MalformedURLException"); + } catch (IOException e) { + logger.error("Error IOException with url " +urlConn, e); + throw new Exception("Error IOException"); + }catch (Exception e) { + logger.error("Error Exception with url " +urlConn, e); + throw new Exception("Error Exception"); + } + } + + + + public static void main(String[] args) throws Exception { + System.out.println(HttpRequestUtil.urlExists("http://geoserver2.d4science.research-infrastructures.eu/geoserver/wms")); + } +} diff --git a/src/test/java/UriResolverManagerTest.java b/src/test/java/UriResolverManagerTest.java new file mode 100644 index 0000000..4a88dad --- /dev/null +++ b/src/test/java/UriResolverManagerTest.java @@ -0,0 +1,113 @@ +import java.util.HashMap; +import java.util.Map; + +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; +import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException; +import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException; +import org.junit.Test; + +/** + * + */ + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Oct 20, 2014 + * + */ +public class UriResolverManagerTest { + +// @Test + public void testGIS() { + + try { + ScopeProvider.instance.set("/gcube/devsec/devVRE"); + UriResolverManager resolver = new UriResolverManager("GIS"); + Map params = new HashMap(); + params.put("gis-UUID", "5ac49f44-999f-4efe-a32b-af71da2b39ac"); + params.put("scope", "/gcube/devsec/devVRE"); + String shortLink = resolver.getLink(params, true); + System.out.println(shortLink); //true, link is shorted otherwise none + } catch (UriResolverMapException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + }catch (Exception e) { + e.printStackTrace(); + } + } + + @Test + public void testSMP() { + + try { + ScopeProvider.instance.set("/gcube/devsec/devVRE"); + UriResolverManager resolver = new UriResolverManager("SMP"); + Map params = new HashMap(); + params.put("smp-uri","smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y"); + params.put("fileName", "wikipediaLogo"); + params.put("contentType", "image/jpeg"); //true, link is shorted otherwise none + String shortLink = resolver.getLink(params, true); + System.out.println(shortLink); + } catch (UriResolverMapException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + }catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Thread safe + */ +// @Test + public void test2(){ + + + //create thread to print counter value + Thread t = new Thread(new Runnable() { + + @Override + public void run() { + while (true) { + try { + + ScopeProvider.instance.set("/gcube/devsec/devVRE"); + UriResolverManager resolver; + resolver = new UriResolverManager("GIS"); + + Map params = new HashMap(); + params.put("gis-UUID", "eb1a1b63-f324-47ee-9522-b8f5803e19ec"); + params.put("scope", "/gcube/devsec/devVRE"); + String shortLink = resolver.getLink(params, true); + System.out.println(shortLink); //true, link is shorted otherwise none + + System.out.println("Thread "+Thread.currentThread().getId() +" reading counter is: " + resolver.countReaders()); + Thread.sleep(1000); + } catch (InterruptedException ex) { + ex.printStackTrace(); + }catch (UriResolverMapException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + }); + + t.start(); + + try { + Thread.sleep(500000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + +} diff --git a/src/test/java/UriResolverStressTest.java b/src/test/java/UriResolverStressTest.java new file mode 100644 index 0000000..afb4e76 --- /dev/null +++ b/src/test/java/UriResolverStressTest.java @@ -0,0 +1,158 @@ +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +/** + * + */ + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Nov 12, 2014 + * + */ +public class UriResolverStressTest { + + // public static final String hostserviceURI = + // "http://dev.d4science.org/uri-resolver/smp?smp-uri=smp%3A%2F%2F"; + // public static final String fileName = "apache-tomcat-"; + // public static final String fileExtension = ".tar.gz"; + // + // public static final String smpURI = + // "Share%2FFFb6ca1678-9237-4e30-8d6b-268e330b6053%2Fapache-tomcat-6.0.41.tar.gz%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevSW1i4CWmmE07DVZLaoR9ZU3BAfo3xUYQEBCy28i2fxnrGYnbmjfm6hRCxd%2Fheeyp"; + // public static final String contentType = + // "contentType=application%2Fx-gzip"; + + public static final String hostserviceURI = "http://localhost:8080/uri-resolver/smp?smp-uri="; + public static final String fileName = "gattino02"; + public static final String fileExtension = ".jpg"; + + public static final String smpURI = "smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2Fgattino02.jpg%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D"; + public static final String contentType = "application%2Fx-gzip"; + + public static boolean isValidation = true; + +// http://dev.d4science.org/uri-resolver/smp?fileName=gattino02.jpg&contentType=audio%2Fmpeg&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2Fgattino02.jpg%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D + // http://dev.d4science.org/uri-resolver/smp?fileName=apache-tomcat-6.0.41.tar.gz&contentType=application%2Fx-gzip&smp-uri=smp%3A%2F%2FShare%2FFFb6ca1678-9237-4e30-8d6b-268e330b6053%2Fapache-tomcat-6.0.41.tar.gz%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevSW1i4CWmmE07DVZLaoR9ZU3BAfo3xUYQEBCy28i2fxnrGYnbmjfm6hRCxd%2Fheeyp + public static void main(String[] args) { + + startThead("test"); + } + + /** + * + */ + public void startStressTest() { + int j = 0; + + while (true) { + + for (int i = 0; i < 5; i++) { + String name = j + "-" + i; + startThead(name); + } + + j++; + + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public static void startThead(final String name) { + + new Thread(name) { + + + @Override + public void run() { + + InputStream inputStream = null; + FileOutputStream outputStream = null; + + String url = hostserviceURI + smpURI + + "&fileName="+fileName+ fileExtension + + "&contentType="+contentType; + + + if(isValidation){ + url+="&validation=true"; + } + System.out.println(url); + + try { + + //VALIDATE URL + if(isValidation){ + + try { + System.out.println("isValidation"); + HttpRequestUtil.urlExists(url); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return; + } + + //DOWNLOAD URL + URLConnection connection = new URL(url).openConnection(); + // InputStream response = connection.getInputStream(); + // read this file into InputStream + inputStream = connection.getInputStream(); + + System.out.println("Thread " + name + + " Total file size to read (in bytes) : " + + inputStream.available()); + + // write the inputStream to a FileOutputStream + outputStream = new FileOutputStream(new File( + "/home/francesco-mangiacrapa/Desktop/UriResolverDownloads/" + + fileName + name + fileExtension)); + + int read = 0; + byte[] bytes = new byte[1024]; + + while ((read = inputStream.read(bytes)) != -1) { + outputStream.write(bytes, 0, read); + } + + outputStream.flush(); + + System.out.println("Thread " + name + + " DOWNLOAD COMPLETED!"); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (outputStream != null) { + try { + // outputStream.flush(); + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + } + + }.start(); + + } + +} diff --git a/src/test/java/UriResolverTest.java b/src/test/java/UriResolverTest.java index c5ae0e1..241bd2d 100644 --- a/src/test/java/UriResolverTest.java +++ b/src/test/java/UriResolverTest.java @@ -1,11 +1,9 @@ -import java.util.HashMap; -import java.util.Map; - -import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.portlets.user.uriresolvermanager.UriResolverManager; -import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException; -import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException; -import org.junit.Test; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; /** * @@ -13,101 +11,113 @@ import org.junit.Test; /** * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * @Oct 20, 2014 - * + * @Nov 12, 2014 + * */ public class UriResolverTest { - - @Test - public void testGIS() { - - try { - ScopeProvider.instance.set("/gcube/devsec/devVRE"); - UriResolverManager resolver = new UriResolverManager("GIS"); - Map params = new HashMap(); - params.put("gis-UUID", "5ac49f44-999f-4efe-a32b-af71da2b39ac"); - params.put("scope", "/gcube/devsec/devVRE"); - String shortLink = resolver.getLink(params, true); - System.out.println(shortLink); //true, link is shorted otherwise none - } catch (UriResolverMapException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - }catch (Exception e) { - e.printStackTrace(); - } - } - - @Test - public void testSMP() { - - try { - ScopeProvider.instance.set("/gcube/devsec/devVRE"); - UriResolverManager resolver = new UriResolverManager("SMP"); - Map params = new HashMap(); - params.put("smp-uri","smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y"); - params.put("fileName", "wikipediaLogo"); - params.put("contentType", "image/jpeg"); //true, link is shorted otherwise none - String shortLink = resolver.getLink(params, true); - System.out.println(shortLink); - } catch (UriResolverMapException e) { - e.printStackTrace(); - } catch (IllegalArgumentException e) { - e.printStackTrace(); - }catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * Thread safe - */ -// @Test - public void test2(){ - - - //create thread to print counter value - Thread t = new Thread(new Runnable() { - @Override - public void run() { - while (true) { - try { - - ScopeProvider.instance.set("/gcube/devsec/devVRE"); - UriResolverManager resolver; - resolver = new UriResolverManager("GIS"); - - Map params = new HashMap(); - params.put("gis-UUID", "eb1a1b63-f324-47ee-9522-b8f5803e19ec"); - params.put("scope", "/gcube/devsec/devVRE"); - String shortLink = resolver.getLink(params, true); - System.out.println(shortLink); //true, link is shorted otherwise none - - System.out.println("Thread "+Thread.currentThread().getId() +" reading counter is: " + resolver.countReaders()); - Thread.sleep(1000); - } catch (InterruptedException ex) { - ex.printStackTrace(); - }catch (UriResolverMapException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } - - }); - - t.start(); - + // CASE IS NULL +// public static final String uri = "http://localhost:8080/uri-resolver/smp?fileName=HCAF_2050&contentType=text%2Fcsv&smp-uri=smp%3A%2F%2FShare%2Fcd8cb73f-feb6-4072-864c-3bb57f81ad56%2FHCAF+2050%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D"; +// public static String filename = "HCAF_2050"; +// public static String ext = "csv"; + + // CASE OK +// public static final String uri = "http://localhost:8080/uri-resolver/smp?fileName=gattino02.jpg&contentType=audio%2Fmpeg&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2Fgattino02.jpg%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D"; +// public static String filename ="gattino02"; +// public static String ext = "jpg"; + + +// ANOTHER CASE OK +// public static final String uri = "http://localhost:8080/uri-resolver/smp?fileName=org.gcube.portlets-user.messages-0.4.0-0.notifica&contentType=application%2Fx-gtar&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2FSharedFolder1%2Forg.gcube.portlets-user.messages-0.4.0-0.notifica%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D"; +// public static String filename ="org.gcube.portlets-user.messages-0.4.0-0.notifica"; +// public static String ext = "war"; + + + //SIZE 0 + public static final String uri = "http://localhost:8080/uri-resolver/smp?fileName=Untitled_Document&contentType=application%2Foctet-stream&smp-uri=smp%3A%2F%2FHome%2Ffrancesco.mangiacrapa%2FWorkspace%2FUntitled+Document%3F5ezvFfBOLqb3YESyI%2FkesN4T%2BZD0mtmc%2F4sZ0vGMrl0lgx7k85j8o2Q1vF0ezJi%2FTEYl7d%2BF4sKR7EwqeONAlQygGb2MgXevVwnFtqGknsyTZoV3fuG3iZ3%2BAsJaJDUH7F%2FELBV1lV8smBnSfc4vhDULwoWY6CWZ2tGj15BzeBI%3D"; + public static String filename ="Untitled_Document"; + public static String ext = "pplication%2Foctet-stream"; + + public static void main(String[] args) { + + startTest(); + } + + public static void startTest() { + + InputStream inputStream = null; + FileOutputStream outputStream = null; + + String url = uri; + + System.out.println(url); + try { - Thread.sleep(500000); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + + try { + System.out.println("Validating.."); + boolean isValid = HttpRequestUtil.urlExists(url+"&validation=true"); + + if(!isValid){ + System.out.println("url not valid, return!"); + return; + } + + System.out.println("URL is valid, continue.."); + + /* + // DOWNLOAD URL + URLConnection connection = new URL(url).openConnection(); + // InputStream response = connection.getInputStream(); + // read this file into InputStream + inputStream = connection.getInputStream(); + + System.out.println(" Total file size to read (in bytes) : " + + inputStream.available()); + + // write the inputStream to a FileOutputStream + outputStream = new FileOutputStream(new File( + "/home/francesco-mangiacrapa/Desktop/UriResolverDownloads/" + + filename + "." + ext)); + + int read = 0; + byte[] bytes = new byte[1024]; + + while ((read = inputStream.read(bytes)) != -1) { + outputStream.write(bytes, 0, read); + } + + outputStream.flush(); + + System.out.println("DOWNLOAD COMPLETED!"); +*/ + } catch (IOException e) { + e.printStackTrace(); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (outputStream != null) { + try { + // outputStream.flush(); + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } } + } }