1818import java .util .List ;
1919import java .util .stream .Collectors ;
2020
21+ import static io .frictionlessdata .datapackage .Profile .PROFILE_TABULAR_DATA_RESOURCE ;
22+
2123/**
2224 * Ensure datapackages are written in a valid format and can be read back. Compare data to see it matches
2325 */
2426public class RoundtripTest {
2527 private static final CSVFormat csvFormat = TableDataSource
2628 .getDefaultCsvFormat ().builder ().setDelimiter ('\t' ).get ();
2729
28- private static final String resourceContent = "[\n " +
29- " {\n " +
30- "\t \" city\" : \" london\" ,\n " +
31- "\t \" year\" : 2017,\n " +
32- "\t \" population\" : 8780000\n " +
33- "\t },\n " +
34- "\t {\n " +
35- "\t \" city\" : \" paris\" ,\n " +
36- "\t \" year\" : 2017,\n " +
37- "\t \" population\" : 2240000\n " +
38- "\t },\n " +
39- "\t {\n " +
40- "\t \" city\" : \" rome\" ,\n " +
41- "\t \" year\" : 2017,\n " +
42- "\t \" population\" : 2860000\n " +
43- "\t }\n " +
44- " ]" ;
45-
4630 @ Test
4731 @ DisplayName ("Roundtrip test - write datapackage, read again and compare data" )
4832 // this lead to the discovery of https://github.com/frictionlessdata/specs/issues/666, just in case
@@ -87,16 +71,73 @@ public void dogfoodingTest() throws Exception {
8771 void validateResourceRoundtrip () throws Exception {
8872 Path resourcePath = TestUtil .getResourcePath ("/fixtures/datapackages/roundtrip/datapackage.json" );
8973 Package dp = new Package (resourcePath , true );
90- StringBuffer buf = new StringBuffer ();
91- Resource v4Resource = dp .getResource ("test2" );
92- List <Object []> testData = v4Resource .getData (false , false , true , false );
93- String data = createCSV (v4Resource .getHeaders (), testData );
94- Resource v5Resource = new CSVDataResource (v4Resource .getName (),data );
95- v5Resource .setDescription (v4Resource .getDescription ());
96- v5Resource .setSchema (v4Resource .getSchema ());
97- v5Resource .setSerializationFormat (Resource .FORMAT_CSV );
98- List data1 = v5Resource .getData (false , false , true , false );
99- Assertions .assertArrayEquals (testData .toArray (), data1 .toArray ());
74+ Resource referenceResource = dp .getResource ("test2" );
75+ List <Object []> referenceData = referenceResource .getData (false , false , true , false );
76+ String data = createCSV (referenceResource .getHeaders (), referenceData );
77+ Resource newResource = new CSVDataResource (referenceResource .getName (),data );
78+ newResource .setDescription (referenceResource .getDescription ());
79+ newResource .setSchema (referenceResource .getSchema ());
80+ newResource .setSerializationFormat (Resource .FORMAT_CSV );
81+ List testData = newResource .getData (false , false , true , false );
82+ Assertions .assertArrayEquals (referenceData .toArray (), testData .toArray ());
83+ }
84+
85+ @ Test
86+ @ DisplayName ("Create data resource, compare descriptor" )
87+ void validateCreateResourceDescriptorRoundtrip () throws Exception {
88+ Path resourcePath = TestUtil .getResourcePath ("/fixtures/datapackages/roundtrip/datapackage.json" );
89+ Package pkg = new Package (resourcePath , true );
90+ JSONDataResource resource = new JSONDataResource ("test3" , resourceContent );
91+ resource .setSchema (Schema .fromJson (
92+ new File (TestUtil .getBasePath ().toFile (), "/schema/population_schema.json" ), true ));
93+
94+ resource .setShouldSerializeToFile (false );
95+ resource .setSerializationFormat (Resource .FORMAT_CSV );
96+ resource .setDialect (Dialect .fromCsvFormat (csvFormat ));
97+ resource .setProfile (PROFILE_TABULAR_DATA_RESOURCE );
98+ resource .setEncoding ("utf-8" );
99+ pkg .addResource (resource );
100+ Path tempDirPath = Files .createTempDirectory ("datapackage-" );
101+ File createdFile = new File (tempDirPath .toFile (), "test_save_datapackage.zip" );
102+ pkg .write (createdFile , true );
103+ System .out .println (tempDirPath );
104+
105+ // create new Package from the serialized form and check they are equal
106+ Package testPkg = new Package (createdFile .toPath (), true );
107+ String json = testPkg .asJson ();
108+ Assertions .assertEquals (
109+ descriptorContentInlined .replaceAll ("[\n \r ]+" , "\n " ),
110+ json .replaceAll ("[\n \r ]+" , "\n " )
111+ );
112+ }
113+
114+
115+ @ Test
116+ @ DisplayName ("Create data resource and make it write to file, compare descriptor" )
117+ void validateCreateResourceDescriptorRoundtrip2 () throws Exception {
118+ Path resourcePath = TestUtil .getResourcePath ("/fixtures/datapackages/roundtrip/datapackage.json" );
119+ Package pkg = new Package (resourcePath , true );
120+ JSONDataResource resource = new JSONDataResource ("test3" , resourceContent );
121+ resource .setSchema (Schema .fromJson (
122+ new File (TestUtil .getBasePath ().toFile (), "/schema/population_schema.json" ), true ));
123+
124+ resource .setShouldSerializeToFile (true );
125+ resource .setSerializationFormat (Resource .FORMAT_CSV );
126+ resource .setDialect (Dialect .fromCsvFormat (csvFormat ));
127+ resource .setProfile (PROFILE_TABULAR_DATA_RESOURCE );
128+ resource .setEncoding ("utf-8" );
129+ pkg .addResource (resource );
130+ Path tempDirPath = Files .createTempDirectory ("datapackage-" );
131+ File createdFile = new File (tempDirPath .toFile (), "test_save_datapackage.zip" );
132+ pkg .write (createdFile , true );
133+
134+ // create new Package from the serialized form and check they are equal
135+ Package testPkg = new Package (createdFile .toPath (), true );
136+ String json = testPkg .asJson ();
137+ Assertions .assertEquals (
138+ descriptorContent .replaceAll ("[\n \r ]+" , "\n " ),
139+ json .replaceAll ("[\n \r ]+" , "\n " )
140+ );
100141 }
101142
102143 private static String createCSV (String [] headers , List <Object []> data ) {
@@ -111,4 +152,103 @@ private static String createCSV(String[] headers, List<Object[]> data) {
111152 }
112153 return sb .toString ();
113154 }
155+
156+
157+ private static final String resourceContent = "[\n " +
158+ " {\n " +
159+ "\t \" city\" : \" london\" ,\n " +
160+ "\t \" year\" : 2017,\n " +
161+ "\t \" population\" : 8780000\n " +
162+ "\t },\n " +
163+ "\t {\n " +
164+ "\t \" city\" : \" paris\" ,\n " +
165+ "\t \" year\" : 2017,\n " +
166+ "\t \" population\" : 2240000\n " +
167+ "\t },\n " +
168+ "\t {\n " +
169+ "\t \" city\" : \" rome\" ,\n " +
170+ "\t \" year\" : 2017,\n " +
171+ "\t \" population\" : 2860000\n " +
172+ "\t }\n " +
173+ " ]" ;
174+
175+ private static final String descriptorContent = "{\n " +
176+ " \" name\" : \" foreign-keys\" ,\n " +
177+ " \" profile\" : \" data-package\" ,\n " +
178+ " \" resources\" : [ {\n " +
179+ " \" name\" : \" test2\" ,\n " +
180+ " \" profile\" : \" data-resource\" ,\n " +
181+ " \" schema\" : \" schema/test2.json\" ,\n " +
182+ " \" path\" : \" data/test2.csv\" \n " +
183+ " }, {\n " +
184+ " \" name\" : \" test3\" ,\n " +
185+ " \" profile\" : \" tabular-data-resource\" ,\n " +
186+ " \" encoding\" : \" utf-8\" ,\n " +
187+ " \" dialect\" : \" dialect/test3.json\" ,\n " +
188+ " \" schema\" : \" schema/population_schema.json\" ,\n " +
189+ " \" path\" : \" data/test3.csv\" \n " +
190+ " } ]\n " +
191+ "}" ;
192+
193+
194+ private static String descriptorContentInlined ="{\n " +
195+ " \" name\" : \" foreign-keys\" ,\n " +
196+ " \" profile\" : \" data-package\" ,\n " +
197+ " \" resources\" : [ {\n " +
198+ " \" name\" : \" test2\" ,\n " +
199+ " \" profile\" : \" data-resource\" ,\n " +
200+ " \" schema\" : \" schema/test2.json\" ,\n " +
201+ " \" path\" : \" data/test2.csv\" \n " +
202+ " }, {\n " +
203+ " \" name\" : \" test3\" ,\n " +
204+ " \" profile\" : \" tabular-data-resource\" ,\n " +
205+ " \" encoding\" : \" utf-8\" ,\n " +
206+ " \" format\" : \" json\" ,\n " +
207+ " \" dialect\" : {\n " +
208+ " \" caseSensitiveHeader\" : false,\n " +
209+ " \" quoteChar\" : \" \\ \" \" ,\n " +
210+ " \" doubleQuote\" : true,\n " +
211+ " \" delimiter\" : \" \\ t\" ,\n " +
212+ " \" lineTerminator\" : \" \\ r\\ n\" ,\n " +
213+ " \" nullSequence\" : \" \" ,\n " +
214+ " \" header\" : true,\n " +
215+ " \" csvddfVersion\" : 1.2,\n " +
216+ " \" skipInitialSpace\" : true\n " +
217+ " },\n " +
218+ " \" schema\" : {\n " +
219+ " \" fields\" : [ {\n " +
220+ " \" name\" : \" city\" ,\n " +
221+ " \" title\" : \" city\" ,\n " +
222+ " \" type\" : \" string\" ,\n " +
223+ " \" format\" : \" default\" ,\n " +
224+ " \" description\" : \" The city.\" \n " +
225+ " }, {\n " +
226+ " \" name\" : \" year\" ,\n " +
227+ " \" title\" : \" year\" ,\n " +
228+ " \" type\" : \" year\" ,\n " +
229+ " \" format\" : \" default\" ,\n " +
230+ " \" description\" : \" The year.\" \n " +
231+ " }, {\n " +
232+ " \" name\" : \" population\" ,\n " +
233+ " \" title\" : \" population\" ,\n " +
234+ " \" type\" : \" integer\" ,\n " +
235+ " \" format\" : \" default\" ,\n " +
236+ " \" description\" : \" The population.\" \n " +
237+ " } ]\n " +
238+ " },\n " +
239+ " \" data\" : [ {\n " +
240+ " \" city\" : \" london\" ,\n " +
241+ " \" year\" : 2017,\n " +
242+ " \" population\" : 8780000\n " +
243+ " }, {\n " +
244+ " \" city\" : \" paris\" ,\n " +
245+ " \" year\" : 2017,\n " +
246+ " \" population\" : 2240000\n " +
247+ " }, {\n " +
248+ " \" city\" : \" rome\" ,\n " +
249+ " \" year\" : 2017,\n " +
250+ " \" population\" : 2860000\n " +
251+ " } ]\n " +
252+ " } ]\n " +
253+ "}" ;
114254}
0 commit comments