gFeed/oai-harvester/src/test/java/org/gcube/application/gfeed/oai/ParsingTests.java

89 lines
2.8 KiB
Java
Raw Normal View History

2020-05-07 17:43:22 +02:00
package org.gcube.application.gfeed.oai;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
2020-05-08 14:34:12 +02:00
import java.io.PrintWriter;
2020-06-05 16:05:49 +02:00
import java.io.StringReader;
2020-05-07 17:43:22 +02:00
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
2020-05-08 14:34:12 +02:00
import org.gcube.data.publishing.gCatFeeder.model.InternalConversionException;
import org.gcube.data.publishing.gCatFeeder.utils.Files;
import org.gcube.data.publishing.gCatFeeder.utils.IOUtils;
2020-05-07 17:43:22 +02:00
import org.gcube.data.publishing.gFeed.collectors.oai.model.DCRecordMetadata;
import org.gcube.data.publishing.gFeed.collectors.oai.model.MetadataHolder;
import org.gcube.data.publishing.gFeed.collectors.oai.model.OAIMetadata;
import org.gcube.data.publishing.gFeed.collectors.oai.model.OAIRecord;
import org.gcube.data.publishing.gFeed.collectors.oai.model.OAI_PMH;
2020-05-08 14:34:12 +02:00
import org.gcube.data.publishing.gFeed.collectors.oai.model.ckan.GCatModel;
import org.gcube.data.publishing.gFeed.collectors.oai.model.ckan.GCatTransformer;
2020-05-07 17:43:22 +02:00
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ParsingTests {
static JAXBContext jaxbContext;
@BeforeClass
public static void init() throws JAXBException {
jaxbContext = JAXBContext.newInstance(OAIRecord.class,
MetadataHolder.class,
OAIMetadata.class,
DCRecordMetadata.class,
OAI_PMH.class);
}
2020-06-05 16:05:49 +02:00
// @Test
public void parseDC() throws JAXBException, IOException {
2020-05-07 17:43:22 +02:00
2020-05-08 14:34:12 +02:00
OAI_PMH response=read();
2020-05-07 17:43:22 +02:00
Assert.assertNotNull(response);
Assert.assertNotNull(response.getResponseRecords().getRecords());
assertTrue(response.getResponseRecords().getRecords().size()>0);
for(OAIRecord record : response.getResponseRecords().getRecords()) {
log.debug("Record is "+record);
}
// System.out.println(response);
}
2020-06-05 16:05:49 +02:00
// @Test
2020-05-08 14:34:12 +02:00
public void marshal() throws JAXBException, InternalConversionException, IOException {
OAI_PMH resp=read();
File outF=File.createTempFile("json", ".json");
PrintWriter out = new PrintWriter(outF);
for(GCatModel gcat: new GCatTransformer().transform(resp.getResponseRecords().getRecords())) {
Assert.assertNotNull(gcat.getResources());
Assert.assertFalse(gcat.getResources().isEmpty());
out.println(gcat.toCatalogueFormat());
}
out.flush();
out.close();
System.out.println("Output at "+outF.getAbsolutePath());
2020-05-07 17:43:22 +02:00
}
2020-06-05 16:05:49 +02:00
private OAI_PMH read() throws JAXBException, IOException {
String toRead=IOUtils.readStream(
getClass().getClassLoader().getResourceAsStream("resp_dc.xml"));
2020-05-08 14:34:12 +02:00
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
2020-06-05 16:05:49 +02:00
return (OAI_PMH) jaxbUnmarshaller.unmarshal(new StringReader(toRead));
2020-05-08 14:34:12 +02:00
2020-05-07 17:43:22 +02:00
}
2020-05-08 14:34:12 +02:00
2020-05-07 17:43:22 +02:00
}