2016-10-07 21:50:21 +02:00
|
|
|
package org.gcube.data_catalogue.grsf_publish_ws;
|
|
|
|
|
|
|
|
import java.beans.IntrospectionException;
|
|
|
|
import java.beans.PropertyDescriptor;
|
2016-10-09 12:36:25 +02:00
|
|
|
import java.io.IOException;
|
2016-10-07 21:50:21 +02:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2016-10-08 23:10:25 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2016-10-07 21:50:21 +02:00
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField;
|
2016-10-07 21:50:21 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group;
|
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
|
2016-10-08 20:46:17 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord;
|
2016-10-08 23:10:25 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Abundance_Level;
|
2016-10-07 21:50:21 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Source;
|
2016-10-08 23:10:25 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
|
2016-10-07 21:50:21 +02:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type;
|
|
|
|
|
2016-10-09 12:36:25 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
2016-10-07 21:50:21 +02:00
|
|
|
public class JTests {
|
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
//@Test
|
2016-10-07 21:50:21 +02:00
|
|
|
public void test() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException {
|
|
|
|
|
|
|
|
FisheryRecord recordFishery = new FisheryRecord();
|
|
|
|
recordFishery.setType(Type.Fishing_Description);
|
|
|
|
recordFishery.setDatabaseSources(Source.FIRMS);
|
2016-10-08 23:10:25 +02:00
|
|
|
recordFishery.setStatus(Status.Pending);
|
2016-10-07 21:50:21 +02:00
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
List<String> tags = new ArrayList<String>();
|
|
|
|
List<String> groupsTitles = new ArrayList<String>();
|
|
|
|
Map<String, Object> extras = new HashMap<String, Object>();
|
|
|
|
|
|
|
|
// bottom up, looks up for Tag/Group fields
|
|
|
|
Class<?> current = recordFishery.getClass();
|
2016-10-07 21:50:21 +02:00
|
|
|
do{
|
|
|
|
System.out.println("Class is " + current.getCanonicalName());
|
|
|
|
Field[] fields = current.getDeclaredFields();
|
2016-10-08 23:10:25 +02:00
|
|
|
|
2016-10-07 21:50:21 +02:00
|
|
|
for (Field field : fields) {
|
2016-10-08 23:10:25 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
2016-10-07 21:50:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while((current = current.getSuperclass())!=null);
|
2016-10-09 12:36:25 +02:00
|
|
|
|
2016-10-08 23:10:25 +02:00
|
|
|
// 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());
|
2016-10-07 21:50:21 +02:00
|
|
|
}
|
2016-10-09 12:36:25 +02:00
|
|
|
|
2016-10-08 23:18:45 +02:00
|
|
|
//@Test
|
|
|
|
public void testNameToStringEnum(){
|
2016-10-09 12:36:25 +02:00
|
|
|
|
2016-10-08 23:18:45 +02:00
|
|
|
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());
|
2016-10-09 12:36:25 +02:00
|
|
|
|
2016-10-08 23:18:45 +02:00
|
|
|
// try deserializer
|
|
|
|
String deserialize = "uncertain not assessed";
|
|
|
|
Abundance_Level res = Abundance_Level.onDeserialize(deserialize);
|
|
|
|
System.out.println(res.name());
|
|
|
|
}
|
2016-10-07 21:50:21 +02:00
|
|
|
|
2016-10-09 12:36:25 +02:00
|
|
|
//@Test
|
|
|
|
public void testJSONMapping() throws IOException{
|
|
|
|
|
|
|
|
FisheryRecord recordFishery = new FisheryRecord();
|
|
|
|
recordFishery.setType(Type.Fishing_Description);
|
|
|
|
recordFishery.setDatabaseSources(Source.FIRMS);
|
|
|
|
recordFishery.setStatus(Status.Pending);
|
|
|
|
recordFishery.setVersion(new Long(10));
|
|
|
|
HashMap<String, String> extras = new HashMap<String, String>();
|
|
|
|
extras.put("test1", "testValue");
|
|
|
|
extras.put("test2", "test2Value");
|
|
|
|
recordFishery.setProperties(extras);
|
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
|
|
|
//Object to JSON in String
|
|
|
|
String jsonInString = mapper.writeValueAsString(recordFishery);
|
|
|
|
|
|
|
|
System.out.println(jsonInString);
|
|
|
|
|
|
|
|
FisheryRecord converted = mapper.readValue(jsonInString, recordFishery.getClass());
|
|
|
|
|
|
|
|
System.out.println(converted);
|
|
|
|
}
|
|
|
|
|
2016-10-07 21:50:21 +02:00
|
|
|
}
|