16
16
17
17
package org .springframework .batch .extensions .bigquery .unit .writer ;
18
18
19
- import com .fasterxml .jackson .core .JsonFactory ;
20
- import com .fasterxml .jackson .databind .ObjectWriter ;
21
19
import com .google .cloud .bigquery .BigQuery ;
22
20
import com .google .cloud .bigquery .Field ;
23
21
import com .google .cloud .bigquery .FormatOptions ;
36
34
import org .springframework .batch .extensions .bigquery .common .TestConstants ;
37
35
import org .springframework .batch .extensions .bigquery .unit .base .AbstractBigQueryTest ;
38
36
import org .springframework .batch .extensions .bigquery .writer .BigQueryJsonItemWriter ;
39
- import org .springframework .core .convert .converter .Converter ;
37
+ import org .springframework .batch .item .json .GsonJsonObjectMarshaller ;
38
+ import org .springframework .batch .item .json .JacksonJsonObjectMarshaller ;
39
+ import org .springframework .batch .item .json .JsonObjectMarshaller ;
40
40
41
41
import java .lang .invoke .MethodHandles ;
42
42
import java .util .List ;
@@ -47,34 +47,15 @@ class BigQueryJsonItemWriterTest extends AbstractBigQueryTest {
47
47
private static final TableId TABLE_ID = TableId .of ("1" , "2" );
48
48
49
49
@ Test
50
- void testDoInitializeProperties () throws IllegalAccessException , NoSuchFieldException {
51
- TestWriter writer = new TestWriter ();
52
- List <PersonDto > items = TestConstants .CHUNK .getItems ();
53
- MethodHandles .Lookup handle = MethodHandles .privateLookupIn (BigQueryJsonItemWriter .class , MethodHandles .lookup ());
54
-
55
- // Exception
56
- Assertions .assertThrows (IllegalStateException .class , () -> writer .testInitializeProperties (List .of ()));
57
-
58
- // No exception
59
- writer .testInitializeProperties (items );
60
- Assertions .assertEquals (
61
- PersonDto .class .getSimpleName (),
62
- ((Class <PersonDto >) handle .findVarHandle (BigQueryJsonItemWriter .class , "itemClass" , Class .class ).get (writer )).getSimpleName ()
63
- );
64
- ObjectWriter objectWriter = (ObjectWriter ) handle .findVarHandle (BigQueryJsonItemWriter .class , "objectWriter" , ObjectWriter .class ).get (writer );
65
- Assertions .assertInstanceOf (JsonFactory .class , objectWriter .getFactory ());
66
- }
67
-
68
- @ Test
69
- void testSetRowMapper () throws IllegalAccessException , NoSuchFieldException {
50
+ void testSetMarshaller () throws IllegalAccessException , NoSuchFieldException {
70
51
BigQueryJsonItemWriter <PersonDto > reader = new BigQueryJsonItemWriter <>();
71
52
MethodHandles .Lookup handle = MethodHandles .privateLookupIn (BigQueryJsonItemWriter .class , MethodHandles .lookup ());
72
- Converter <PersonDto , String > expected = source -> null ;
53
+ JsonObjectMarshaller <PersonDto > expected = new JacksonJsonObjectMarshaller <>() ;
73
54
74
- reader .setRowMapper (expected );
55
+ reader .setMarshaller (expected );
75
56
76
- Converter <PersonDto , String > actual = (Converter <PersonDto , String >) handle
77
- .findVarHandle (BigQueryJsonItemWriter .class , "rowMapper " , Converter .class )
57
+ JsonObjectMarshaller <PersonDto > actual = (JsonObjectMarshaller <PersonDto >) handle
58
+ .findVarHandle (BigQueryJsonItemWriter .class , "marshaller " , JsonObjectMarshaller .class )
78
59
.get (reader );
79
60
80
61
Assertions .assertEquals (expected , actual );
@@ -83,14 +64,23 @@ void testSetRowMapper() throws IllegalAccessException, NoSuchFieldException {
83
64
@ Test
84
65
void testConvertObjectsToByteArrays () {
85
66
TestWriter writer = new TestWriter ();
67
+ writer .setMarshaller (new JacksonJsonObjectMarshaller <>());
86
68
87
69
// Empty
88
70
Assertions .assertTrue (writer .testConvert (List .of ()).isEmpty ());
89
71
90
72
// Not empty
91
- writer .setRowMapper (Record ::toString );
73
+ writer .setMarshaller (Record ::toString );
92
74
List <byte []> actual = writer .testConvert (TestConstants .CHUNK .getItems ());
93
- List <byte []> expected = TestConstants .CHUNK .getItems ().stream ().map (PersonDto ::toString ).map (s -> s .concat ("\n " )).map (String ::getBytes ).toList ();
75
+
76
+ List <byte []> expected = TestConstants .CHUNK
77
+ .getItems ()
78
+ .stream ()
79
+ .map (PersonDto ::toString )
80
+ .map (s -> s .concat ("\n " ))
81
+ .map (String ::getBytes )
82
+ .toList ();
83
+
94
84
Assertions .assertEquals (expected .size (), actual .size ());
95
85
96
86
for (int i = 0 ; i < actual .size (); i ++) {
@@ -112,10 +102,15 @@ void testPerformFormatSpecificChecks() {
112
102
BigQuery bigQuery = prepareMockedBigQuery ();
113
103
Mockito .when (bigQuery .getTable (Mockito .any (TableId .class ))).thenReturn (table );
114
104
105
+ // marshaller
106
+ IllegalArgumentException actual = Assertions .assertThrows (IllegalArgumentException .class , writer ::testPerformFormatSpecificChecks );
107
+ Assertions .assertEquals ("Marshaller is mandatory" , actual .getMessage ());
108
+
115
109
// schema
110
+ writer .setMarshaller (new JacksonJsonObjectMarshaller <>());
116
111
writer .setBigQuery (bigQuery );
117
112
writer .setWriteChannelConfig (WriteChannelConfiguration .of (TABLE_ID , FormatOptions .csv ()));
118
- IllegalArgumentException actual = Assertions .assertThrows (IllegalArgumentException .class , writer ::testPerformFormatSpecificChecks );
113
+ actual = Assertions .assertThrows (IllegalArgumentException .class , writer ::testPerformFormatSpecificChecks );
119
114
Assertions .assertEquals ("Schema must be provided" , actual .getMessage ());
120
115
121
116
// schema equality
@@ -144,6 +139,7 @@ void testPerformFormatSpecificChecks_Format(FormatOptions formatOptions) {
144
139
145
140
TestWriter writer = new TestWriter ();
146
141
writer .setBigQuery (bigQuery );
142
+ writer .setMarshaller (new GsonJsonObjectMarshaller <>());
147
143
148
144
writer .setWriteChannelConfig (WriteChannelConfiguration .newBuilder (TABLE_ID ).setAutodetect (true ).setFormatOptions (formatOptions ).build ());
149
145
IllegalArgumentException actual = Assertions .assertThrows (IllegalArgumentException .class , writer ::testPerformFormatSpecificChecks );
@@ -164,9 +160,6 @@ static Stream<FormatOptions> invalidFormats() {
164
160
}
165
161
166
162
private static final class TestWriter extends BigQueryJsonItemWriter <PersonDto > {
167
- public void testInitializeProperties (List <PersonDto > items ) {
168
- doInitializeProperties (items );
169
- }
170
163
171
164
public List <byte []> testConvert (List <PersonDto > items ) {
172
165
return convertObjectsToByteArrays (items );
0 commit comments