206 lines
6.4 KiB
Java
206 lines
6.4 KiB
Java
package org.gcube.spatial.data.sdi;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import java.io.InputStream;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.Reader;
|
|
import java.io.StringReader;
|
|
import java.io.StringWriter;
|
|
import java.io.Writer;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
import javax.xml.bind.JAXBException;
|
|
import javax.xml.bind.Marshaller;
|
|
import javax.xml.bind.Unmarshaller;
|
|
import javax.xml.transform.Result;
|
|
import javax.xml.transform.Source;
|
|
import javax.xml.transform.stream.StreamResult;
|
|
import javax.xml.transform.stream.StreamSource;
|
|
|
|
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.TemplateApplicationRequest;
|
|
import org.gcube.spatial.data.sdi.model.metadata.TemplateCollection;
|
|
import org.gcube.spatial.data.sdi.model.metadata.TemplateDescriptor;
|
|
import org.gcube.spatial.data.sdi.model.metadata.TemplateInvocationBuilder;
|
|
import org.junit.BeforeClass;
|
|
import org.junit.Test;
|
|
|
|
public class MarshallUnmarshallTest {
|
|
|
|
static JAXBContext ctx =null;
|
|
|
|
|
|
|
|
@BeforeClass
|
|
public static void init() throws JAXBException{
|
|
ctx = JAXBContext.newInstance(
|
|
MetadataReport.class,
|
|
TemplateApplicationRequest.class,
|
|
TemplateCollection.class,
|
|
ErrorMessage.class);
|
|
}
|
|
|
|
public static boolean roundTrip(Object obj){
|
|
|
|
Object roundTripResult=unmarshal(obj.getClass(), new StringReader(marshal(obj,new StringWriter()).toString()));
|
|
return obj.equals(roundTripResult);
|
|
|
|
}
|
|
|
|
@Test
|
|
public void testHashAndEquals() {
|
|
assertTrue(getTemplateInvocations().equals(getTemplateInvocations()));
|
|
assertTrue(getTemplateInvocations().hashCode()==getTemplateInvocations().hashCode());
|
|
assertTrue(getDescriptors().equals(getDescriptors()));
|
|
assertTrue(getDescriptors().hashCode()==getDescriptors().hashCode());
|
|
assertTrue(getReport().equals(getReport()));
|
|
assertTrue(getReport().hashCode()==getReport().hashCode());
|
|
assertTrue(getError().equals(getError()));
|
|
assertTrue(getError().hashCode()==getError().hashCode());
|
|
}
|
|
|
|
|
|
@Test
|
|
public void Marshall(){
|
|
print(getTemplateInvocations());
|
|
print(getDescriptors());
|
|
print(getReport());
|
|
print(getError());
|
|
}
|
|
@Test
|
|
public void unMarshall(){
|
|
assertTrue(roundTrip(getTemplateInvocations()));
|
|
assertTrue(roundTrip(getDescriptors()));
|
|
assertTrue(roundTrip(getReport()));
|
|
assertTrue(roundTrip(getError()));
|
|
}
|
|
|
|
@Test
|
|
public void toStringTest(){
|
|
System.out.println(getTemplateInvocations());
|
|
System.out.println(getDescriptors());
|
|
System.out.println(getReport());
|
|
System.out.println(getError());
|
|
}
|
|
|
|
/**
|
|
* Write the serialisation of a given resource to a {@link Result}.
|
|
* @param resource the resource
|
|
* @param stream the result
|
|
* @return the result in input
|
|
*/
|
|
public static <T extends Result> T marshal(Object resource,T result) {
|
|
|
|
try {
|
|
// OLD XML MARSHALLING
|
|
// JAXBContext context = ctx;
|
|
// Marshaller m = context.createMarshaller();
|
|
// m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
|
//
|
|
// m.marshal(resource,result);
|
|
//
|
|
// return result;
|
|
|
|
|
|
JAXBContext jaxbContext = ctx;
|
|
Marshaller marshaller = jaxbContext.createMarshaller();
|
|
marshaller.setProperty("eclipselink.media-type", "application/json");
|
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
|
marshaller.marshal(resource, result);
|
|
return result;
|
|
}
|
|
catch(Exception e) {
|
|
throw new RuntimeException("serialisation error",e);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void print(Object resource) {
|
|
|
|
marshal(resource,new OutputStreamWriter(System.out));
|
|
}
|
|
|
|
/**
|
|
* Write the serialisation of a given resource to a given character stream.
|
|
* @param resource the resource
|
|
* @param stream the stream in input
|
|
*/
|
|
public static <T extends Writer> T marshal(Object resource,T stream) {
|
|
|
|
marshal(resource,new StreamResult(stream));
|
|
return stream;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Creates a resource of given class from its serialisation in a given {@link Reader}.
|
|
* @param resourceClass the class of the resource
|
|
* @param reader the reader
|
|
* @return the resource
|
|
*/
|
|
public static <T> T unmarshal(Class<T> resourceClass, Reader reader) {
|
|
return unmarshal(resourceClass,new StreamSource(reader));
|
|
}
|
|
|
|
/**
|
|
* Creates a resource of given class from its serialisation in a given {@link InputStream}.
|
|
* @param resourceClass the class of the resource
|
|
* @param stream the stream
|
|
* @return the resource
|
|
*/
|
|
public static <T> T unmarshal(Class<T> resourceClass, InputStream stream) {
|
|
return unmarshal(resourceClass,new StreamSource(stream));
|
|
}
|
|
|
|
/**
|
|
* Creates a resource of given class from its serialisation in a given {@link Source}.
|
|
* @param resourceClass the class of the resource
|
|
* @param source the source
|
|
* @return the resource
|
|
*/
|
|
public static <T> T unmarshal(Class<T> resourceClass,Source source) {
|
|
try {
|
|
Unmarshaller um = ctx.createUnmarshaller();
|
|
um.setProperty("eclipselink.media-type", "application/json");
|
|
return resourceClass.cast(um.unmarshal(source));
|
|
}
|
|
catch(Exception e) {
|
|
throw new RuntimeException("deserialisation error",e);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|