git-svn-id: http://svn.research-infrastructures.eu/d4science/gcube/trunk/spatial-data/geonetwork@146446 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
4f378159e1
commit
f0b1c6b005
|
@ -1,178 +0,0 @@
|
|||
package org.gcube.spatial.data.geonetwork.test;
|
||||
|
||||
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 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.geonetwork.GeoNetworkAdministration;
|
||||
import org.gcube.spatial.data.geonetwork.LoginLevel;
|
||||
import org.gcube.spatial.data.geonetwork.model.Account;
|
||||
import org.gcube.spatial.data.geonetwork.model.Group;
|
||||
import org.gcube.spatial.data.geonetwork.model.ScopeConfiguration;
|
||||
import org.gcube.spatial.data.geonetwork.model.User;
|
||||
import org.gcube.spatial.data.geonetwork.model.faults.AuthorizationException;
|
||||
import org.gcube.spatial.data.geonetwork.model.faults.EncryptionException;
|
||||
import org.gcube.spatial.data.geonetwork.model.faults.MissingConfigurationException;
|
||||
import org.gcube.spatial.data.geonetwork.model.faults.MissingServiceEndpointException;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import it.geosolutions.geonetwork.exception.GNLibException;
|
||||
import it.geosolutions.geonetwork.exception.GNServerException;
|
||||
|
||||
public class ModelMarshalling {
|
||||
|
||||
private static JAXBContext ctx =null;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void init() throws JAXBException{
|
||||
ctx = JAXBContext.newInstance(
|
||||
ScopeConfiguration.class,
|
||||
Account.class,
|
||||
Group.class,
|
||||
User.class);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setScope(){
|
||||
TokenSetter.set("/gcube/devsec");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Marshall() throws MissingConfigurationException, GNLibException, GNServerException, MissingServiceEndpointException, EncryptionException, AuthorizationException{
|
||||
ScopeConfiguration config=createFakeConfig();
|
||||
print(config);
|
||||
print(config.getAccounts().values().iterator().next());
|
||||
print(getGroup());
|
||||
print(getUser());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unMarshall() throws MissingConfigurationException, MissingServiceEndpointException, EncryptionException, GNLibException, GNServerException, AuthorizationException{
|
||||
ScopeConfiguration config=createFakeConfig();
|
||||
assertTrue(roundTrip(config));
|
||||
assertTrue(roundTrip(config.getAccounts().values().iterator().next()));
|
||||
assertTrue(roundTrip(getGroup()));
|
||||
assertTrue(roundTrip(getUser()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
private ScopeConfiguration createFakeConfig() throws MissingConfigurationException, MissingServiceEndpointException, EncryptionException, GNLibException, GNServerException, AuthorizationException{
|
||||
return TestConfiguration.getClient().getConfiguration().getScopeConfiguration();
|
||||
}
|
||||
|
||||
private Group getGroup() throws MissingConfigurationException, GNLibException, GNServerException, MissingServiceEndpointException, EncryptionException, AuthorizationException{
|
||||
return TestConfiguration.getClient().getGroups().iterator().next();
|
||||
}
|
||||
|
||||
private User getUser() throws MissingConfigurationException, GNLibException, GNServerException, MissingServiceEndpointException, EncryptionException, AuthorizationException{
|
||||
GeoNetworkAdministration admin=TestConfiguration.getClient();
|
||||
admin.login(LoginLevel.ADMIN);
|
||||
return admin.getUsers().iterator().next();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean roundTrip(Object obj){
|
||||
Object roundTripResult=unmarshal(obj.getClass(), new StringReader(marshal(obj,new StringWriter()).toString()));
|
||||
return obj.equals(roundTripResult);
|
||||
}
|
||||
|
||||
//********************** MARSHALL / UNMARSHALL
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
JAXBContext context = ctx;
|
||||
Marshaller m = context.createMarshaller();
|
||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
|
||||
m.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();
|
||||
return resourceClass.cast(um.unmarshal(source));
|
||||
}
|
||||
catch(Exception e) {
|
||||
throw new RuntimeException("deserialisation error",e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue