1
|
package eu.dnetlib.data.oai.store.mongo;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.text.ParseException;
|
5
|
import java.time.LocalDateTime;
|
6
|
import java.time.ZoneId;
|
7
|
import java.time.format.DateTimeFormatter;
|
8
|
import java.util.Date;
|
9
|
import java.util.zip.ZipEntry;
|
10
|
import java.util.zip.ZipOutputStream;
|
11
|
|
12
|
import eu.dnetlib.data.information.oai.publisher.conf.OAIConfigurationReader;
|
13
|
import org.apache.commons.io.IOUtils;
|
14
|
import org.apache.commons.io.output.ByteArrayOutputStream;
|
15
|
import org.bson.types.Binary;
|
16
|
import org.junit.Before;
|
17
|
import org.junit.Test;
|
18
|
import org.springframework.core.io.ClassPathResource;
|
19
|
|
20
|
/**
|
21
|
* RecordInfoGenerator Tester.
|
22
|
*
|
23
|
* @author <Authors name>
|
24
|
* @version 1.0
|
25
|
* @since <pre>Apr 6, 2016</pre>
|
26
|
*/
|
27
|
public class RecordInfoGeneratorTest {
|
28
|
|
29
|
private RecordInfoGenerator gen = new RecordInfoGenerator();
|
30
|
private String filePath = "/eu/dnetlib/data/oai/store/mongo/testRecord.xml";
|
31
|
private Binary binaryXML;
|
32
|
|
33
|
@Before
|
34
|
public void before() throws Exception {
|
35
|
String testRecord = IOUtils.toString(new ClassPathResource(filePath).getInputStream());
|
36
|
binaryXML = createCompressRecord(testRecord);
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Method: decompressRecord(final byte[] input)
|
41
|
*/
|
42
|
@Test
|
43
|
public void testDecompressRecord() throws Exception {
|
44
|
long timeStart = System.currentTimeMillis();
|
45
|
String record = gen.decompressRecord(binaryXML.getData());
|
46
|
long timeEnd = System.currentTimeMillis();
|
47
|
System.out.println("Decompressed record in ms " + (timeEnd - timeStart));
|
48
|
System.out.println(record);
|
49
|
}
|
50
|
|
51
|
@Test
|
52
|
public void parseDatestamp(){
|
53
|
String thedate = "2018-02-13T15:02:16.122Z";
|
54
|
//Because DateTimeFormatter.ISO_INSTANT does not work
|
55
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
|
56
|
//DateTimeFormatter formatter = new DateTimeFormatterBuilder(DateTimeFormatter.ISO_INSTANT).appendInstant(3).toFormatter();
|
57
|
LocalDateTime d = LocalDateTime.parse(thedate, dtf);
|
58
|
Date utilDate = Date.from(d.atZone(ZoneId.systemDefault()).toInstant());
|
59
|
}
|
60
|
|
61
|
@Test
|
62
|
public void parseDatestamp1() throws ParseException {
|
63
|
String theDate = "2018-04-07T04:23:31.8Z";
|
64
|
// org.apache.commons.lang.time.DateUtils.parseDate(
|
65
|
// theDate,
|
66
|
// new String[]{ "yyyy-MM-dd'T'HH:mm:ss.SX" });
|
67
|
gen.parseDate(theDate);
|
68
|
}
|
69
|
|
70
|
@Test
|
71
|
public void parseDatestamp2() throws ParseException {
|
72
|
String theDate = "2018-02-13T15:02:16.122Z";
|
73
|
// org.apache.commons.lang.time.DateUtils.parseDate(
|
74
|
// theDate,
|
75
|
// new String[]{ "yyyy-MM-dd'T'HH:mm:ss.SX" });
|
76
|
gen.parseDate(theDate);
|
77
|
}
|
78
|
|
79
|
|
80
|
|
81
|
private Binary createCompressRecord(final String record) throws IOException {
|
82
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
83
|
ZipOutputStream zos = new ZipOutputStream(os);
|
84
|
ZipEntry entry = new ZipEntry(OAIConfigurationReader.BODY_FIELD);
|
85
|
zos.putNextEntry(entry);
|
86
|
zos.write(record.getBytes());
|
87
|
zos.closeEntry();
|
88
|
//zos.flush();
|
89
|
zos.close();
|
90
|
return new Binary(os.toByteArray());
|
91
|
}
|
92
|
|
93
|
}
|