package org.gcube.data_catalogue.grsf_publish_ws; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag; import org.gcube.data_catalogue.grsf_publish_ws.json.input.DatabaseSource; import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord; import org.gcube.data_catalogue.grsf_publish_ws.json.input.StockRecord; import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Abundance_Level; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse; import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpGet; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient; import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder; public class JTests { private static final String TEST_TOKEN = ""; //@Test public void test() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException { FisheryRecord recordFishery = new FisheryRecord(); recordFishery.setType(Type.Fishing_Description); recordFishery.setDatabaseSources(new ArrayList()); recordFishery.setStatus(Status.Pending); List tags = new ArrayList(); List groupsTitles = new ArrayList(); Map extras = new HashMap(); // bottom up, looks up for Tag/Group fields Class current = recordFishery.getClass(); do{ System.out.println("Class is " + current.getCanonicalName()); Field[] fields = current.getDeclaredFields(); for (Field field : fields) { if(field.isAnnotationPresent(Tag.class)){ Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(recordFishery); if(f != null){ tags.add(f.toString()); } } if(field.isAnnotationPresent(Group.class)){ Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(recordFishery); if(f != null){ groupsTitles.add(f.toString()); } } if(field.isAnnotationPresent(CustomField.class)){ Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(recordFishery); if(f != null){ // get the key to put into the map first extras.put(field.getAnnotation(CustomField.class).key(), f); } } } } while((current = current.getSuperclass())!=null); // print System.out.println("TAGS " + tags); System.out.println("GROUPS " + groupsTitles); System.out.println("EXTRAS " + extras); } //@Test public void testJsonSerializer(){ // // String pendingAsString = "pending"; // Status resSatus = Status.onDeserialize(pendingAsString); // System.out.println("Res is " + resSatus); // // System.out.println("To string is " + resSatus.onSerialize()); Abundance_Level type = Abundance_Level.onDeserialize("Uncertain Not assessed"); System.out.println("Res is " + type.onSerialize()); } //@Test public void testNameToStringEnum(){ Abundance_Level elem = Abundance_Level.Uncertain_Not_Assessed; System.out.println("Enum name is = " + elem.name() + ", enum to string is = " + elem.toString() + ", enum on serialize = " + elem.onSerialize()); // try deserializer String deserialize = "uncertain not assessed"; Abundance_Level res = Abundance_Level.onDeserialize(deserialize); System.out.println(res.name()); } //@Test public void testJSONMapping() throws IOException{ FisheryRecord record = new FisheryRecord(); // record.setType(Type.Assessment_Unit); // record.setFisheryId("sajhdskajda"); // record.setScientificName("assadsadada"); // // record.setExploitationRate(Exploitation_Rate.High_Fishing_Mortality); // // record.setAbundanceLevel(Abundance_Level.Uncertain_Not_Assessed); // ArrayList list = new ArrayList(); // list.add(new DatabaseSource("http", null, Source.FIRMS)); // list.add(new DatabaseSource("http", null, Source.FIRMS)); // list.add(new DatabaseSource("http", null, Source.FIRMS)); // list.add(new DatabaseSource("http", null, Source.FIRMS)); // list.add(new DatabaseSource("http", null, Source.FISHSOURCE)); // list.add(new DatabaseSource("http", null, Source.RAM)); // record.setManagementEntity("management ashdskad"); // record.setProductionSystemType(Production_System_Type.Artisanal); // record.setDatabaseSources(list); // record.setAuthor("Costantino Perciante"); // record.setMaintainer("Costantino Perciante"); // record.setAuthorContact("costantino.perciante@isti.cnr.it"); // record.setStatus(Status.Pending); // record.setVersion(new Long(1)); // // record.setSpeciesScientificName("Katsuwonus pelamis (or SKJ)"); // record.setCatchesOrLandings("Catch - 18962 - ton - 2014"); // record.setDataOwner("Giancarlo Panichi"); // // List groups = new ArrayList(); // // // check group generation // HelperMethods.getGroups(groups, record); // // for (String group : groups) { // System.out.println("Group is " + group); // } // // List tags = new ArrayList(); // // // check group generation // HelperMethods.getTags(tags, record); // // for (String tag : tags) { // System.out.println("Tag is " + tag); // } // // Map extras = new HashMap(); // HelperMethods.getExtras(extras, record); // // Iterator> it = extras.entrySet().iterator(); // // while (it.hasNext()) { // Map.Entry entry = (Map.Entry) it // .next(); // System.out.println("Extra is " + entry); // } // List sourcesOfInformation = recordStock.getSourceOfInformation(); // for (Resource sourceOfinformation : sourcesOfInformation) { // Set> violationsSourceOfinformationsBean = validator.validate(sourceOfinformation); // for (ConstraintViolation constraintViolation : violationsSourceOfinformationsBean) { // System.out.println("Violation is about " + constraintViolation.getPropertyPath() + ", message error is " + constraintViolation.getMessage()); // } // } // // ObjectMapper mapper = new ObjectMapper(); //Object to JSON in String String jsonInString = mapper.writeValueAsString(record); System.out.println(jsonInString); // // // JSON back to object // StockRecord converted = mapper.readValue(jsonInString, recordStock.getClass()); // System.out.println(converted); } //@Test public void instanciateGCoreReader() throws Exception{ String baseUrl = org.gcube.data_catalogue.grsf_publish_ws.utils.GcoreEndpointReaderSocialWS.getUrlSocialWS("/gcube/devNext/NextNext"); System.out.println("Base url is " + baseUrl); try(CloseableHttpClient client = HttpClientBuilder.create().build();){ String url = baseUrl.replace("80", "") + "/users/getUserEmail?gcube-token=" + TEST_TOKEN; System.out.println("Request url is " + url); HttpGet getRequest = new HttpGet(url); HttpResponse response = client.execute(getRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader( new InputStreamReader((response.getEntity().getContent()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } System.out.println(response.toString()); }catch(Exception e){ System.err.println("error while performing post method " + e.toString()); } } //@Test public void testFromScopeToOrgName(){ System.out.println("Valid ? " + HelperMethods.isValid("this is not valid")); // System.out.println(HelperMethods.retrieveOrgNameFromScope("/gcube/devNext/NextNext")); } //@Test public void testCustomFieldAnnotation() throws JsonProcessingException{ StockRecord record = new StockRecord(); record.setWaterArea(Arrays.asList("aa", "bb", "cc", "dd")); Map> extras = new HashMap>(); HelperMethods.getExtras(extras , record); System.out.println("Extras = " + extras); ObjectMapper mapper = new ObjectMapper(); //Object to JSON in String String jsonInString = mapper.writeValueAsString(record); System.out.println(jsonInString); } }