dataminer-invocation-model/src/test/java/DataMinerInvocationTest.java

208 lines
7.2 KiB
Java

/**
*
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBException;
import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager;
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
import org.gcube.data.analysis.dminvocation.model.DataMinerParam;
import org.gcube.data.analysis.dminvocation.model.DataMinerParamList;
import org.gcube.data.analysis.dminvocation.model.DataMinerParameters;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Dec 4, 2018
*/
public class DataMinerInvocationTest {
static String operatorID = "[THE_OPERATOR_ID]";
static Map<String,String> parameters = new HashMap<String,String>();
static DataMinerInvocationManager dmMng;
@Before
public void init() throws JAXBException, IOException, SAXException{
parameters.put("fileId", "http://publicLinkToFile");
parameters.put("[key2]", "[value2]");
dmMng = DataMinerInvocationManager.getInstance();
}
//@Test
public void marshallingTest() throws JAXBException, IOException, SAXException {
System.out.println("marshallingTest called");
//LOADING PARAMETERS
List<DataMinerParam> inParams = new ArrayList<DataMinerParam>();
for (String pm : parameters.keySet()) {
inParams.add(new DataMinerParam(pm, parameters.get(pm)));
}
// DataMinerInputParams inputParams = new DataMinerInputParams(inParams);
// DataMinerOutputParams outputParams = new DataMinerOutputParams(null);
// Map<String, String> inputList = new HashMap<String, String>();
// inputList.putAll(parameters);
//new DataMinerParamList(parameters)
DataMinerParameters parameters = new DataMinerParameters(new DataMinerParamList(inParams), null);
DataMinerInvocation dmInvocation = new DataMinerInvocation();
dmInvocation.setOperatorId(operatorID);
dmInvocation.setParameters(parameters);
System.out.println(dmInvocation);
// ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON);
// System.out.println(new String(outStreamJSON.toByteArray()));
String marshXML = dmMng.marshalingXML(dmInvocation, true, true);
System.out.println(marshXML);
String marshJSON = dmMng.marshalingJSON(dmInvocation, true, true);
System.out.println(marshJSON);
}
//@Test
public void marshallingTest2() throws JAXBException, IOException, SAXException {
System.out.println("marshallingTest called");
//LOADING PARAMETERS
List<DataMinerParam> inParams = new ArrayList<DataMinerParam>();
for (String pm : parameters.keySet()) {
inParams.add(new DataMinerParam(pm, parameters.get(pm)));
}
// DataMinerInputParams inputParams = new DataMinerInputParams(inParams);
// DataMinerOutputParams outputParams = new DataMinerOutputParams(null);
// Map<String, String> inputList = new HashMap<String, String>();
// inputList.putAll(parameters);
//new DataMinerParamList(parameters)
DataMinerParameters parameters = new DataMinerParameters(new DataMinerParamList(inParams), null);
DataMinerInvocation dmInvocation = new DataMinerInvocation();
// dmInvocation.setOperatorId(operatorID);
// dmInvocation.setParameters(parameters);
// System.out.println(dmInvocation);
// ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON);
// System.out.println(new String(outStreamJSON.toByteArray()));
// String marshXML = dmMng.marshalingXML(dmInvocation, true);
// System.out.println(marshXML);
String marshJSON = dmMng.marshalingJSON(dmInvocation, true, true);
System.out.println(marshJSON);
}
//@Test
public void unmarshallingXMLTest() throws JAXBException, IOException, SAXException{
System.out.println("unmarshallingXMLTest called");
FileInputStream dmInvocationXMLFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.xml"));
DataMinerInvocation dmInvocation = dmMng.unmarshalingXML(dmInvocationXMLFile, true);
System.out.println(dmInvocation);
String marshXML = dmMng.marshalingXML(dmInvocation, true, true);
System.out.println("TO XML: \n"+marshXML);
//
String marshJSON = dmMng.marshalingJSON(dmInvocation, true, true);
System.out.println("TO JSON: \n"+marshJSON);
}
@Test
public void unmarshallingJSONTest() throws JAXBException, IOException, SAXException{
System.out.println("unmarshallingJSONTest called");
FileInputStream dmInvocationJSONFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.json"));
DataMinerInvocation dmInvocation = dmMng.unmarshalingJSON(dmInvocationJSONFile, true);
System.out.println(dmInvocation);
String marshXML = dmMng.marshalingXML(dmInvocation, true, true);
System.out.println("TO XML: \n"+marshXML);
//
String marshJSON = dmMng.marshalingJSON(dmInvocation, true, true);
System.out.println("TO JSON: \n"+marshJSON);
}
/*
public static void main(String[] args) {
try{
System.out.println("marshallingTest called");
//LOADING PARAMETERS
List<DataMinerParam> inParams = new ArrayList<DataMinerParam>();
for (String pm : parameters.keySet()) {
inParams.add(new DataMinerParam(pm, parameters.get(pm)));
}
DataMinerInvocation dm = new DataMinerInvocation();
dm.setOperatorId("the operator");
DataMinerParameters parameters = new DataMinerParameters(null, null);
dm.setParameters(parameters);
List<String> errors = new ArrayList<String>();
checkRequiredFiels(dm, errors);
System.out.println(errors);
}catch(Exception e){
e.printStackTrace();
}
}
private static void checkRequiredFiels(Object theInstance, List<String> errors) throws IllegalArgumentException, IllegalAccessException{
System.out.println("Checking instance: "+theInstance);
for(Field field : theInstance.getClass().getDeclaredFields()){
Class<?> type = field.getType();
String name = field.getName();
Annotation[] annotations = field.getDeclaredAnnotations();
System.out.println("Field: "+name);
for (Annotation annotation : annotations) {
if(annotation instanceof XmlElement){
field.setAccessible(true);
XmlElement xmlEl = (XmlElement) annotation;
Object value = field.get(theInstance);
if(value==null){
String error = "Required field '"+xmlEl.name() + "' of "+type+" is null";
System.err.println("Required field '"+xmlEl.name() + "' is null");
errors.add(error);
}else if (String.class.equals(type) && ((String) value).isEmpty()){
String error = "Required field '"+xmlEl.name() + "' of "+type+" is empty";
System.err.println("Required field '"+xmlEl.name() + "' is null");
errors.add(error);
}else if(!type.isPrimitive() && type != String.class){
System.out.println("The field "+xmlEl.name()+ " is not a primitive or a String\n\n");
checkRequiredFiels(value, errors);
}
//System.out.println("XmlElement "+annotation.annotationType()+" is required: "+xmlEl.required());
}else
System.out.println("Another annotation: "+annotation);
//if (annotation.annotationType() instanceof XmlElement.c)
}
}
}
*/
}