From 71eef30d2ab797d1869bdb7ce2e8dfb3100676f5 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 26 Feb 2019 09:55:09 +0000 Subject: [PATCH] Added phd test git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/infrastructure-tests@177269 82a268e6-3cf1-43bd-a215-b396298e98cf --- src/test/java/org/gcube/phd/Statistics.java | 132 ++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/test/java/org/gcube/phd/Statistics.java diff --git a/src/test/java/org/gcube/phd/Statistics.java b/src/test/java/org/gcube/phd/Statistics.java new file mode 100644 index 0000000..4d2136d --- /dev/null +++ b/src/test/java/org/gcube/phd/Statistics.java @@ -0,0 +1,132 @@ +package org.gcube.phd; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.gcube.common.resources.gcore.GCoreEndpoint; +import org.gcube.common.resources.gcore.HostingNode; +import org.gcube.common.resources.gcore.Resource; +import org.gcube.common.resources.gcore.Resources; +import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; +import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; +import org.gcube.informationsystem.utils.ISMapper; +import org.gcube.resourcemanagement.model.reference.entities.resources.EService; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; +import org.gcube.resources.discovery.icclient.ICFactory; +import org.gcube.testutility.ScopedTest; +import org.junit.Test; + +public class Statistics extends ScopedTest { + + private File directory; + + + private void printLineToFile(String line, File file) throws IOException { + synchronized (file) { + try (FileWriter fw = new FileWriter(file, true); + BufferedWriter bw = new BufferedWriter(fw); + PrintWriter out = new PrintWriter(bw)) { + out.println(line); + out.flush(); + } catch (IOException e) { + throw e; + } + } + } + + protected String getUnmarshalledResource(Resource gr) { + StringWriter stringWriter = new StringWriter(); + Resources.marshal(gr, stringWriter); + return stringWriter.toString(); + } + + private void addToCSV(UUID uuid, Resource gr, org.gcube.informationsystem.model.reference.entities.Resource r, File statisticsFile, boolean addHeader) throws IOException { + StringBuffer stringBuffer = new StringBuffer(); + if(addHeader) { + stringBuffer.append(gr.getClass().getSimpleName()); + stringBuffer.append("(byte size)"); + stringBuffer.append(","); + stringBuffer.append(r.getClass().getSimpleName()); + stringBuffer.append("(byte size)"); + printLineToFile(stringBuffer.toString(), statisticsFile); + stringBuffer = new StringBuffer(); + } + String unmarshalledGR = getUnmarshalledResource(gr); + File xmlFile = new File(directory, uuid.toString()+".xml"); + printLineToFile(unmarshalledGR, xmlFile); + + final byte[] grUTF8Bytes = unmarshalledGR.getBytes("UTF-8"); + stringBuffer.append(grUTF8Bytes.length); + stringBuffer.append(","); + String unmarshalledR = ISMapper.marshal(r); + File jsonFile = new File(directory, uuid.toString()+".json"); + printLineToFile(unmarshalledR, jsonFile); + + final byte[] rUTF8Bytes = unmarshalledR.getBytes("UTF-8"); + stringBuffer.append(rUTF8Bytes.length); + printLineToFile(stringBuffer.toString(), statisticsFile); + } + + + @Test + public void test() throws Exception { + ScopedTest.setContext(GCUBE); + + boolean first = true; + + directory = new File("/home/lucafrosini/Dropbox/Dottorato/Review Tesi"); + + File eServicesFile = new File(directory, "EService.csv"); + File hostingNodeFile = new File(directory, "HostingNode.csv"); + + List serviceIDs = new ArrayList<>(); + // Data Tansfer Service + serviceIDs.add(UUID.fromString("d33619ab-4084-492f-800c-a2197a610132")); + // Smart Executor Service + serviceIDs.add(UUID.fromString("f7b0030a-4608-4560-bb6e-c1f33a7ad9f6")); + // Web Hosting Node Service (WHNMAnager) + serviceIDs.add(UUID.fromString("3aa75ba7-27d0-49a2-8ffc-356eb012d305")); + DiscoveryClient client = ICFactory.clientFor(GCoreEndpoint.class); + + ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create(); + + for(UUID uuid : serviceIDs){ + SimpleQuery query = ICFactory.queryFor(GCoreEndpoint.class); + query.addCondition(String.format("$resource/ID/text() eq '%1s'", uuid.toString())); + List gces = client.submit(query); + GCoreEndpoint gCoreEndpoint = gces.get(0); + + EService eService = resourceRegistryClient.getInstance(EService.class, uuid); + addToCSV(uuid, gCoreEndpoint, eService, eServicesFile, first); + first= false; + } + + + + + UUID hostingNodeUUID = UUID.fromString("e81b39b6-0ee4-49cd-805f-ac133544b6fe"); + DiscoveryClient hnClient = ICFactory.clientFor(HostingNode.class); + SimpleQuery query = ICFactory.queryFor(HostingNode.class); + query.addCondition(String.format("$resource/ID/text() eq '%1s'", hostingNodeUUID.toString())); + List hostingNodes = hnClient.submit(query); + HostingNode hostingNode = hostingNodes.get(0); + + org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode hn = + resourceRegistryClient.getInstance(org.gcube.resourcemanagement.model.reference.entities.resources.HostingNode.class, hostingNodeUUID); + addToCSV(hostingNodeUUID, hostingNode, hn, hostingNodeFile, true); + + + } + +}