This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSbdWrapperTest.java
156 lines (115 loc) · 5.93 KB
/
SbdWrapperTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package no.difi.vefa.sbdh;
import com.google.common.io.ByteStreams;
import no.difi.vefa.sbdh.api.AsicExtractor;
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.codec.binary.Base64OutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import org.unece.cefact.namespaces.standardbusinessdocumentheader.ObjectFactory;
import org.unece.cefact.namespaces.standardbusinessdocumentheader.StandardBusinessDocumentHeader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import java.io.*;
import java.net.URL;
import static org.testng.Assert.*;
/**
* Created by soc on 16.09.2015.
*/
@SuppressWarnings("ALL")
public class SbdWrapperTest {
public static final Logger log = LoggerFactory.getLogger(SbdWrapperTest.class);
/**
* Experimental code to verify some assumptions, i.e. does not use the SbdWrapper
*
* @param asicInputStream
* @param sbdh
* @throws Exception
*/
@Test(dataProvider = "sampleData", dataProviderClass = SampleDataProvider.class)
public void wrapSampleDataExperiment(InputStream asicInputStream, StandardBusinessDocumentHeader sbdh) throws Exception {
// Establishes the output stream for our xml data
File outputFile = File.createTempFile("vefa-sbdh", ".xml");
log.debug("Writing sample data to " + outputFile.toString());
FileOutputStream outputStream = new FileOutputStream(outputFile);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")));
out.println("<?xml version=\"1.0\"?>\n" +
"<StandardBusinessDocument>");
// Emits the SBDH
JAXBContext jc = JAXBContext.newInstance(StandardBusinessDocumentHeader.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<StandardBusinessDocumentHeader> standardBusinessDocumentHeader = new ObjectFactory().createStandardBusinessDocumentHeader(sbdh);
marshaller.marshal(standardBusinessDocumentHeader, out);
// Now for the base64 encoded ASiC archive
out.println("\n<asic:asic xmlns:asic=\"urn:etsi.org:specification:02918:v1.2.1\" id=\"asic\">");
out.flush();
// Base64 contents goes here:
Base64OutputStream base64OutputStream = new Base64OutputStream(outputStream);
ByteStreams.copy(asicInputStream, base64OutputStream);
base64OutputStream.flush();
// Wrap up
out.println("\n</asic:asic>");
out.println("</StandardBusinessDocument>");
out.flush();
out.close();
log.debug("SBD written to " + outputFile);
}
/**
* Encodes the ASiC archive to base64 and decodes back to binary again.
*
* @throws Exception
*/
@Test
public void encodeAndDecode() throws Exception {
URL sampleAsicUrl = SbdWrapperTest.class.getClassLoader().getResource(SampleDataProvider.SAMPLE_ASIC);
assertNotNull(sampleAsicUrl);
File asicFile = new File(sampleAsicUrl.toURI());
File base64Copy = File.createTempFile("test-asic", ".b64");
FileOutputStream base64CopyOutputStream = new FileOutputStream(base64Copy);
Base64OutputStream base64OutputStream = new Base64OutputStream(base64CopyOutputStream);
// Converts from binary -> base64
ByteStreams.copy(new FileInputStream(asicFile), base64OutputStream);
base64OutputStream.close();
assertTrue(base64Copy.length() > asicFile.length(), "Base encoding failed");
// Converts back from base64 -> binary
File asicFileCopy = File.createTempFile("asic-copy", ".asice");
Base64InputStream base64InputStream = new Base64InputStream(new FileInputStream(base64Copy));
FileOutputStream outputStreamForAsicCopy = new FileOutputStream(asicFileCopy);
ByteStreams.copy(base64InputStream, outputStreamForAsicCopy);
outputStreamForAsicCopy.close();
// They should be identical
assertEquals(asicFile.length(), asicFileCopy.length());
log.debug("Wrote base64 encoded contents to " + base64Copy);
}
/**
* Performs the actual testing using the SbdWrapper class
*
* @param asicInputStream
* @param standardBusinessDocumentHeader
* @throws Exception
*/
@Test(dataProvider = "sampleData", dataProviderClass = SampleDataProvider.class)
public void wrapSampleData(InputStream asicInputStream, StandardBusinessDocumentHeader standardBusinessDocumentHeader) throws Exception {
SbdWrapper sbdWrapper = new SbdWrapper();
File outputFile = File.createTempFile("vefa-sbdh", ".xml");
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
sbdWrapper.wrapInputStream(standardBusinessDocumentHeader, asicInputStream, fileOutputStream);
fileOutputStream.close();
log.debug("Wrote sample StandardBusinessDocument into " + outputFile.toString());
// Extracts the data to verify
File binaryFile = File.createTempFile("vefa-recreated", ".asice");
OutputStream out = new FileOutputStream(binaryFile);
// Extracts the base64 encoded ASiC payload
AsicExtractor asicExtractor = AsicExtractorFactory.defaultAsicExtractor();
asicExtractor.extractAsic(new FileInputStream(outputFile), out);
out.close();
log.debug("Re-created the ASiC file into " + binaryFile);
URL originalAsicFileUrl = SbdWrapperTest.class.getClassLoader().getResource(SampleDataProvider.SAMPLE_ASIC);
assertNotNull(originalAsicFileUrl);
File originalAsicFile = new File(originalAsicFileUrl.toURI());
assertEquals(originalAsicFile.length(), binaryFile.length()," The wrapped-and-unwrapped ASiC file does not equal in size to the original" );
}
}