Project

General

Profile

1
package eu.dnetlib.data.oai.store.mongo;
2

    
3
import java.io.IOException;
4
import java.time.LocalDateTime;
5
import java.time.ZoneId;
6
import java.time.format.DateTimeFormatter;
7
import java.util.Date;
8
import java.util.zip.ZipEntry;
9
import java.util.zip.ZipOutputStream;
10

    
11
import eu.dnetlib.data.information.oai.publisher.conf.OAIConfigurationReader;
12
import org.apache.commons.io.IOUtils;
13
import org.apache.commons.io.output.ByteArrayOutputStream;
14
import org.bson.types.Binary;
15
import org.junit.Before;
16
import org.junit.Test;
17
import org.springframework.core.io.ClassPathResource;
18

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

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

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

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

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

    
60
	private Binary createCompressRecord(final String record) throws IOException {
61
		ByteArrayOutputStream os = new ByteArrayOutputStream();
62
		ZipOutputStream zos = new ZipOutputStream(os);
63
		ZipEntry entry = new ZipEntry(OAIConfigurationReader.BODY_FIELD);
64
		zos.putNextEntry(entry);
65
		zos.write(record.getBytes());
66
		zos.closeEntry();
67
		//zos.flush();
68
		zos.close();
69
		return new Binary(os.toByteArray());
70
	}
71

    
72
}
(4-4/4)