sdi-interface/src/test/java/org/gcube/spatial/data/sdi/MarshallUnmarshallTest.java

141 lines
5.4 KiB
Java

package org.gcube.spatial.data.sdi;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import javax.xml.bind.JAXBException;
import org.apache.commons.collections.CollectionUtils;
import org.gcube.spatial.data.sdi.model.faults.ErrorMessage;
import org.gcube.spatial.data.sdi.model.metadata.MetadataReport;
import org.gcube.spatial.data.sdi.model.metadata.TemplateDescriptor;
import org.gcube.spatial.data.sdi.model.metadata.TemplateInvocation;
import org.junit.BeforeClass;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
public class MarshallUnmarshallTest {
static ObjectMapper mapper=null;
@BeforeClass
public static void init() throws JAXBException{
mapper=new ObjectMapper();
AnnotationIntrospector introspector=new JaxbAnnotationIntrospector(mapper.getTypeFactory());
mapper.setAnnotationIntrospector(introspector);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
public static boolean roundTrip(Object obj) throws JsonParseException, JsonMappingException, JsonProcessingException, IOException{
Object roundTripResult=mapper.readValue(marshal(obj), obj.getClass());
if(obj instanceof Collection) {
return CollectionUtils.isEqualCollection((Collection)obj, (Collection)roundTripResult);
}
return obj.equals(roundTripResult);
}
@Test
public void testHashAndEquals() throws IOException {
assertTrue(loadObject(TemplateInvocation.class).equals(loadObject(TemplateInvocation.class)));
assertTrue(loadObject(TemplateInvocation.class).hashCode()==loadObject(TemplateInvocation.class).hashCode());
assertTrue(loadObject(TemplateDescriptor.class).equals(loadObject(TemplateDescriptor.class)));
assertTrue(loadObject(TemplateDescriptor.class).hashCode()==loadObject(TemplateDescriptor.class).hashCode());
assertTrue(loadObject(MetadataReport.class).equals(loadObject(MetadataReport.class)));
assertTrue(loadObject(MetadataReport.class).hashCode()==loadObject(MetadataReport.class).hashCode());
assertTrue(loadObject(ErrorMessage.class).equals(loadObject(ErrorMessage.class)));
assertTrue(loadObject(ErrorMessage.class).hashCode()==loadObject(ErrorMessage.class).hashCode());
}
@Test
public void Marshall() throws IOException{
print(loadObject(TemplateInvocation.class));
print(loadObject(TemplateDescriptor.class));
print(loadObject(MetadataReport.class));
print(loadObject(ErrorMessage.class));
}
@Test
public void unMarshall() throws JsonParseException, JsonMappingException, JsonProcessingException, IOException{
assertTrue(roundTrip(loadObject(TemplateInvocation.class)));
assertTrue(roundTrip(loadObject(TemplateDescriptor.class)));
assertTrue(roundTrip(loadObject(MetadataReport.class)));
assertTrue(roundTrip(loadObject(ErrorMessage.class)));
}
@Test
public void toStringTest() throws IOException{
System.out.println(loadObject(TemplateInvocation.class));
System.out.println(loadObject(TemplateDescriptor.class));
System.out.println(loadObject(MetadataReport.class));
System.out.println(loadObject(ErrorMessage.class));
}
public static String marshal(Object toSerialize) throws JsonProcessingException {
return mapper.writeValueAsString(toSerialize);
}
public static void print(Object obj) throws JsonProcessingException {
System.out.println(marshal(obj));
}
// public static <T> T unmarshal(Class<T> resourceClass, String toRead) throws JsonParseException, JsonMappingException, IOException {
// return mapper.readValue(toRead, resourceClass);
// }
//
//
// private TemplateApplicationRequest getTemplateInvocations(){
// return new TemplateApplicationRequest(new TemplateInvocationBuilder().threddsOnlineResources("localhost", "myDataset.nc", "my Catalog").get());
// }
//
// private TemplateCollection getDescriptors(){
// HashSet<TemplateDescriptor> descriptors=new HashSet<>();
// descriptors.add(new TemplateDescriptor(TemplateInvocationBuilder.THREDDS_ONLINE.ID,"Thredds Online Resources","Online reousrce template for thredds resources","http://some.place.org/theTemplate", new HashMap<String,String>()));
// descriptors.add(new TemplateDescriptor(TemplateInvocationBuilder.THREDDS_ONLINE.ID,"Thredds Online Resources","Online reousrce template for thredds resources","http://some.place.org/theTemplate", new HashMap<String,String>()));
// return new TemplateCollection(descriptors);
// }
//
// private MetadataReport getReport(){
// return new MetadataReport("theUUID", 12335l, Collections.singleton(TemplateInvocationBuilder.THREDDS_ONLINE.ID));
// }
//
//
// private ErrorMessage getError(){
// ErrorMessage error=new ErrorMessage();
// error.setCode(500);
// error.setDeveloperMessage("Develop it better!");
// error.setMessage("You didn't see anything");
// error.setLink("www.sto.ca.z.z.o.org");
// return error;
// }
private <T> T loadObject(Class<T> objectClass) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get("src/test/resources/json/"+objectClass.getSimpleName()+".json"));
return mapper.readValue(encoded, objectClass);
}
}