Project

General

Profile

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
import static java.time.format.DateTimeFormatter.ofPattern;
21

    
22
/**
23
 * RecordInfoGenerator Tester.
24
 *
25
 * @author <Authors name>
26
 * @version 1.0
27
 * @since <pre>Apr 6, 2016</pre>
28
 */
29
public class RecordInfoGeneratorTest {
30

    
31
	private RecordInfoGenerator gen = new RecordInfoGenerator();
32
	private String filePath = "/eu/dnetlib/data/oai/store/mongo/testRecord.xml";
33
	private Binary binaryXML;
34

    
35
	@Before
36
	public void before() throws Exception {
37
		String testRecord = IOUtils.toString(new ClassPathResource(filePath).getInputStream());
38
		binaryXML = createCompressRecord(testRecord);
39
	}
40

    
41
	/**
42
	 * Method: decompressRecord(final byte[] input)
43
	 */
44
	@Test
45
	public void testDecompressRecord() throws Exception {
46
		long timeStart = System.currentTimeMillis();
47
		String record = gen.decompressRecord(binaryXML.getData());
48
		long timeEnd = System.currentTimeMillis();
49
		System.out.println("Decompressed record in ms " + (timeEnd - timeStart));
50
		System.out.println(record);
51
	}
52

    
53
	@Test
54
	public void parseDatestamp(){
55
		String thedate = "2018-02-13T15:02:16.122Z";
56
		//Because DateTimeFormatter.ISO_INSTANT does not work
57
		DateTimeFormatter dtf = ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
58
		//DateTimeFormatter formatter = new DateTimeFormatterBuilder(DateTimeFormatter.ISO_INSTANT).appendInstant(3).toFormatter();
59
		LocalDateTime d = LocalDateTime.parse(thedate, dtf);
60
		Date utilDate = Date.from(d.atZone(ZoneId.systemDefault()).toInstant());
61
	}
62

    
63
	@Test
64
	public void parseDatestamp1() throws ParseException {
65
		String theDate = "2018-04-07T04:23:31.8Z";
66
//		org.apache.commons.lang.time.DateUtils.parseDate(
67
//				theDate,
68
//				new String[]{ "yyyy-MM-dd'T'HH:mm:ss.SX" });
69
		gen.parseDate(theDate);
70
	}
71

    
72
	@Test
73
	public void parseDatestamp2() throws ParseException {
74
		String theDate = "2018-02-13T15:02:16.122Z";
75
		//		org.apache.commons.lang.time.DateUtils.parseDate(
76
		//				theDate,
77
		//				new String[]{ "yyyy-MM-dd'T'HH:mm:ss.SSSX" });
78
		gen.parseDate(theDate);
79
	}
80

    
81

    
82
	@Test
83
	public void parseDatestamp3() throws ParseException {
84
		String theDate = "2017-12-12T17:06:28.537+01:00";
85
//		LocalDateTime d = LocalDateTime.parse(theDate, DateTimeFormatter.ISO_ZONED_DATE_TIME);
86
//		Date utilDate = Date.from(d.atZone(ZoneId.systemDefault()).toInstant());
87
		gen.parseDate(theDate);
88
	}
89

    
90

    
91
	private Binary createCompressRecord(final String record) throws IOException {
92
		ByteArrayOutputStream os = new ByteArrayOutputStream();
93
		ZipOutputStream zos = new ZipOutputStream(os);
94
		ZipEntry entry = new ZipEntry(OAIConfigurationReader.BODY_FIELD);
95
		zos.putNextEntry(entry);
96
		zos.write(record.getBytes());
97
		zos.closeEntry();
98
		//zos.flush();
99
		zos.close();
100
		return new Binary(os.toByteArray());
101
	}
102

    
103
}
(4-4/4)