common-authorization/src/test/java/org/gcube/common/authorization/library/policies/SerializationTest.java

48 lines
1.7 KiB
Java

package org.gcube.common.authorization.library.policies;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class SerializationTest {
static JAXBContext context;
@BeforeClass
public static void before() throws Exception{
context = JAXBContext.newInstance(EnvironmentPolicy.class, UserPolicy.class, ServicePolicy.class);
}
@Test
public void serializeEnvironmentPolicy() throws Exception{
EnvironmentPolicy ep = new EnvironmentPolicy("/gcube", new ServiceAccess());
StringWriter sw = new StringWriter();
context.createMarshaller().marshal(ep, sw);
EnvironmentPolicy epCopy = (EnvironmentPolicy)context.createUnmarshaller().unmarshal(new StringReader(sw.toString()));
Assert.assertEquals(ep, epCopy);
}
@Test
public void serializeUserPolicy() throws Exception{
UserPolicy up = new UserPolicy("/gcube", new ServiceAccess("ServiceName", "ServiceClass","serviceID"), "userID");
StringWriter sw = new StringWriter();
context.createMarshaller().marshal(up, sw);
UserPolicy upCopy = (UserPolicy)context.createUnmarshaller().unmarshal(new StringReader(sw.toString()));
Assert.assertEquals(up, upCopy);
}
@Test
public void serializeServicePolicy() throws Exception{
ServicePolicy sp = new ServicePolicy("/gcube", new ServiceAccess("ServiceName","ServiceClass"),"ServiceName:ServiceClass");
StringWriter sw = new StringWriter();
context.createMarshaller().marshal(sp, sw);
ServicePolicy spCopy = (ServicePolicy)context.createUnmarshaller().unmarshal(new StringReader(sw.toString()));
Assert.assertEquals(sp, spCopy);
}
}