134 lines
4.0 KiB
Java
134 lines
4.0 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.infrastructure.detachedres.detachedreslibrary;
|
|
|
|
import java.io.File;
|
|
import java.io.StringWriter;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.ArrayList;
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
import javax.xml.bind.Marshaller;
|
|
import javax.xml.bind.Unmarshaller;
|
|
|
|
import org.gcube.infrastructure.detachedres.detachedreslibrary.server.DetachedREsClient;
|
|
import org.gcube.infrastructure.detachedres.detachedreslibrary.server.is.obj.DetachedREsJAXB;
|
|
import org.gcube.infrastructure.detachedres.detachedreslibrary.server.is.obj.GatewayJAXB;
|
|
import org.gcube.infrastructure.detachedres.detachedreslibrary.server.is.obj.VOJAXB;
|
|
import org.gcube.infrastructure.detachedres.detachedreslibrary.server.is.obj.VREJAXB;
|
|
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.Constants;
|
|
import org.gcube.infrastructure.detachedres.detachedreslibrary.shared.re.DetachedREs;
|
|
import org.junit.Test;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
/**
|
|
*
|
|
* @author Giancarlo Panichi
|
|
*
|
|
*
|
|
*/
|
|
public class DetachedREsTest extends TestCase {
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(DetachedREsTest.class);
|
|
|
|
@Test
|
|
public void testDetachedREsResource() {
|
|
if (Constants.TEST_ENABLE) {
|
|
logger.debug("Test Enabled");
|
|
|
|
try {
|
|
DetachedREsClient detachedREsClient=new DetachedREsClient();
|
|
DetachedREs detachedREs= detachedREsClient.getDetachedREs();
|
|
logger.debug("DetachedREs: " + detachedREs);
|
|
assertTrue(true);
|
|
|
|
} catch (Exception e) {
|
|
logger.error(e.getLocalizedMessage(), e);
|
|
assertTrue("Error searching the resource!", false);
|
|
}
|
|
|
|
} else {
|
|
logger.debug("Test Disabled");
|
|
assertTrue(true);
|
|
}
|
|
}
|
|
|
|
@Test
|
|
public void testDetachedREsMarshaller() {
|
|
if (Constants.TEST_ENABLE) {
|
|
logger.debug("Test Enabled");
|
|
|
|
try {
|
|
logger.info("Check Marshalling");
|
|
|
|
GatewayJAXB blueBridgeGateway = new GatewayJAXB();
|
|
blueBridgeGateway.setScope("-1");
|
|
blueBridgeGateway.setName("BlueBridge Gateway");
|
|
|
|
VOJAXB gCubeApps = new VOJAXB();
|
|
gCubeApps.setScope("/d4science.research-infrastructures.eu/gCubeApps");
|
|
gCubeApps.setName("gCubeApps");
|
|
|
|
VREJAXB blueBridgeProject = new VREJAXB();
|
|
blueBridgeProject.setScope("/d4science.research-infrastructures.eu/gcubeApps/BlueBridgeProject");
|
|
blueBridgeProject.setName("BlueBridgeProject");
|
|
|
|
ArrayList<VREJAXB> vres=new ArrayList<>();
|
|
vres.add(blueBridgeProject);
|
|
gCubeApps.setVres(vres);
|
|
|
|
ArrayList<VOJAXB> vos=new ArrayList<>();
|
|
vos.add(gCubeApps);
|
|
blueBridgeGateway.setVos(vos);
|
|
|
|
ArrayList<GatewayJAXB> gateways=new ArrayList<>();
|
|
gateways.add(blueBridgeGateway);
|
|
|
|
DetachedREsJAXB detachedREs = new DetachedREsJAXB();
|
|
detachedREs.setEnabled(true);
|
|
detachedREs.setGateways(gateways);
|
|
|
|
JAXBContext jaxbContext = JAXBContext.newInstance(DetachedREsJAXB.class);
|
|
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
|
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
|
|
|
// Print XML String to Console
|
|
StringWriter sw = new StringWriter();
|
|
|
|
// Write XML to StringWriter
|
|
jaxbMarshaller.marshal(detachedREs, sw);
|
|
|
|
// Verify XML Content
|
|
String xmlContent = sw.toString();
|
|
logger.debug(xmlContent);
|
|
|
|
logger.info("Check Unmarshalling");
|
|
Path path = Files.createTempFile("DetachedREsData", ".xml");
|
|
File file = path.toFile();
|
|
jaxbMarshaller.marshal(detachedREs, file);
|
|
|
|
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
|
|
DetachedREsJAXB configUnmarshalled = (DetachedREsJAXB) jaxbUnmarshaller
|
|
.unmarshal(file);
|
|
logger.debug("DetachedREs unmarshallded: " + configUnmarshalled);
|
|
file.delete();
|
|
logger.info("Success!");
|
|
assertTrue(true);
|
|
|
|
} catch (Throwable e) {
|
|
logger.error(e.getLocalizedMessage(), e);
|
|
assertTrue("Error in DetachedREs Marshal!", false);
|
|
}
|
|
} else {
|
|
logger.debug("Test Disabled");
|
|
assertTrue(true);
|
|
}
|
|
}
|
|
|
|
}
|