1616
1717package org .springframework .batch .extensions .bigquery .unit .writer ;
1818
19- import com .fasterxml .jackson .core .JsonFactory ;
20- import com .fasterxml .jackson .databind .ObjectWriter ;
2119import com .google .cloud .bigquery .BigQuery ;
2220import com .google .cloud .bigquery .Field ;
2321import com .google .cloud .bigquery .FormatOptions ;
3634import org .springframework .batch .extensions .bigquery .common .TestConstants ;
3735import org .springframework .batch .extensions .bigquery .unit .base .AbstractBigQueryTest ;
3836import 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 ;
4040
4141import java .lang .invoke .MethodHandles ;
4242import java .util .List ;
@@ -47,34 +47,15 @@ class BigQueryJsonItemWriterTest extends AbstractBigQueryTest {
4747 private static final TableId TABLE_ID = TableId .of ("1" , "2" );
4848
4949 @ 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 {
7051 BigQueryJsonItemWriter <PersonDto > reader = new BigQueryJsonItemWriter <>();
7152 MethodHandles .Lookup handle = MethodHandles .privateLookupIn (BigQueryJsonItemWriter .class , MethodHandles .lookup ());
72- Converter <PersonDto , String > expected = source -> null ;
53+ JsonObjectMarshaller <PersonDto > expected = new JacksonJsonObjectMarshaller <>() ;
7354
74- reader .setRowMapper (expected );
55+ reader .setMarshaller (expected );
7556
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 )
7859 .get (reader );
7960
8061 Assertions .assertEquals (expected , actual );
@@ -83,14 +64,23 @@ void testSetRowMapper() throws IllegalAccessException, NoSuchFieldException {
8364 @ Test
8465 void testConvertObjectsToByteArrays () {
8566 TestWriter writer = new TestWriter ();
67+ writer .setMarshaller (new JacksonJsonObjectMarshaller <>());
8668
8769 // Empty
8870 Assertions .assertTrue (writer .testConvert (List .of ()).isEmpty ());
8971
9072 // Not empty
91- writer .setRowMapper (Record ::toString );
73+ writer .setMarshaller (Record ::toString );
9274 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+
9484 Assertions .assertEquals (expected .size (), actual .size ());
9585
9686 for (int i = 0 ; i < actual .size (); i ++) {
@@ -112,10 +102,15 @@ void testPerformFormatSpecificChecks() {
112102 BigQuery bigQuery = prepareMockedBigQuery ();
113103 Mockito .when (bigQuery .getTable (Mockito .any (TableId .class ))).thenReturn (table );
114104
105+ // marshaller
106+ IllegalArgumentException actual = Assertions .assertThrows (IllegalArgumentException .class , writer ::testPerformFormatSpecificChecks );
107+ Assertions .assertEquals ("Marshaller is mandatory" , actual .getMessage ());
108+
115109 // schema
110+ writer .setMarshaller (new JacksonJsonObjectMarshaller <>());
116111 writer .setBigQuery (bigQuery );
117112 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 );
119114 Assertions .assertEquals ("Schema must be provided" , actual .getMessage ());
120115
121116 // schema equality
@@ -144,6 +139,7 @@ void testPerformFormatSpecificChecks_Format(FormatOptions formatOptions) {
144139
145140 TestWriter writer = new TestWriter ();
146141 writer .setBigQuery (bigQuery );
142+ writer .setMarshaller (new GsonJsonObjectMarshaller <>());
147143
148144 writer .setWriteChannelConfig (WriteChannelConfiguration .newBuilder (TABLE_ID ).setAutodetect (true ).setFormatOptions (formatOptions ).build ());
149145 IllegalArgumentException actual = Assertions .assertThrows (IllegalArgumentException .class , writer ::testPerformFormatSpecificChecks );
@@ -164,9 +160,6 @@ static Stream<FormatOptions> invalidFormats() {
164160 }
165161
166162 private static final class TestWriter extends BigQueryJsonItemWriter <PersonDto > {
167- public void testInitializeProperties (List <PersonDto > items ) {
168- doInitializeProperties (items );
169- }
170163
171164 public List <byte []> testConvert (List <PersonDto > items ) {
172165 return convertObjectsToByteArrays (items );
0 commit comments