88package com .salesforce .revoman .input .json ;
99
1010import static com .google .common .truth .Truth .assertThat ;
11-
11+ import static com .salesforce .revoman .input .FileUtils .readFileInResourcesToString ;
12+
13+ import com .salesforce .revoman .input .json .adapters .CompositeGraphResponse ;
14+ import com .salesforce .revoman .input .json .adapters .CompositeGraphResponse .Graph .ErrorGraph ;
15+ import com .salesforce .revoman .input .json .adapters .CompositeGraphResponse .Graph .SuccessGraph ;
16+ import com .salesforce .revoman .input .json .adapters .SObjectGraphRequestMarshaller ;
17+ import com .salesforce .revoman .input .json .pojo .SObjectGraphRequest ;
18+ import com .salesforce .revoman .input .json .pojo .SObjectGraphRequest .Entity ;
19+ import com .salesforce .revoman .input .json .pojo .SObjectGraphRequest .SObjectWithReferenceRequest ;
20+ import java .text .ParseException ;
21+ import java .text .SimpleDateFormat ;
1222import java .util .Date ;
1323import java .util .List ;
24+ import java .util .Map ;
25+ import org .json .JSONException ;
1426import org .junit .jupiter .api .DisplayName ;
1527import org .junit .jupiter .api .Test ;
28+ import org .skyscreamer .jsonassert .JSONAssert ;
29+ import org .skyscreamer .jsonassert .JSONCompareMode ;
1630
1731class JsonPojoUtilsTest {
1832
33+ @ Test
34+ @ DisplayName ("DiMorphic CompositeGraph Success Response --> POJO --> JSON" )
35+ void compositeGraphSuccessResponseMarshallUnmarshall () throws JSONException {
36+ final var graphSuccessResponseJsonStr =
37+ readFileInResourcesToString ("composite/graph/resp/graph-success-response.json" );
38+ final var successGraphResponse =
39+ JsonPojoUtils .<CompositeGraphResponse >jsonToPojo (
40+ CompositeGraphResponse .class ,
41+ graphSuccessResponseJsonStr ,
42+ List .of (CompositeGraphResponse .ADAPTER ));
43+ assertThat (successGraphResponse .getGraphs ().get (0 )).isInstanceOf (SuccessGraph .class );
44+ final var successGraphResponseUnmarshalled =
45+ JsonPojoUtils .pojoToJson (
46+ CompositeGraphResponse .class ,
47+ successGraphResponse ,
48+ List .of (CompositeGraphResponse .ADAPTER ));
49+ JSONAssert .assertEquals (
50+ graphSuccessResponseJsonStr , successGraphResponseUnmarshalled , JSONCompareMode .STRICT );
51+ }
52+
53+ @ Test
54+ @ DisplayName ("DiMorphic CompositeGraph Error Response --> POJO --> JSON" )
55+ void compositeGraphErrorResponseMarshallUnmarshall () throws JSONException {
56+ final var graphErrorResponseJsonStr =
57+ readFileInResourcesToString ("composite/graph/resp/graph-error-response.json" );
58+ final var errorGraphResponse =
59+ JsonPojoUtils .<CompositeGraphResponse >jsonToPojo (
60+ CompositeGraphResponse .class ,
61+ graphErrorResponseJsonStr ,
62+ List .of (CompositeGraphResponse .ADAPTER ));
63+ assertThat (errorGraphResponse .getGraphs ().get (0 )).isInstanceOf (ErrorGraph .class );
64+ final var errorGraphResponseUnmarshalled =
65+ JsonPojoUtils .pojoToJson (
66+ CompositeGraphResponse .class ,
67+ errorGraphResponse ,
68+ List .of (CompositeGraphResponse .ADAPTER ));
69+ JSONAssert .assertEquals (
70+ graphErrorResponseJsonStr , errorGraphResponseUnmarshalled , JSONCompareMode .STRICT );
71+ }
72+
73+ @ DisplayName ("toJson: SObjectGraphRequest POJO --> PQ Payload JSON" )
74+ @ Test
75+ void sObjectGraphMarshallToPQPayload () throws JSONException {
76+ final var pqTestInputRepMarshaller =
77+ SObjectGraphRequestMarshaller .adapter (
78+ Map .of ("pricingPref" , "skip" , "configurationInput" , "skip" ));
79+ final var pqPayloadJsonStr =
80+ JsonPojoUtils .pojoToJson (
81+ SObjectGraphRequest .class ,
82+ prepareSObjectGraphReqPojo (),
83+ List .of (pqTestInputRepMarshaller ));
84+ final var expectedPQPayload = readFileInResourcesToString ("json/pq-graph-req.json" );
85+ JSONAssert .assertEquals (expectedPQPayload , pqPayloadJsonStr , JSONCompareMode .STRICT );
86+ }
87+
88+ static SObjectGraphRequest prepareSObjectGraphReqPojo () {
89+ return new SObjectGraphRequest (
90+ "pq-update-quote" ,
91+ List .of (
92+ new SObjectWithReferenceRequest (
93+ "refQuote" ,
94+ new Entity (
95+ Map .of (
96+ "attributes" ,
97+ Map .of ("type" , "Quote" , "method" , "PATCH" , "id" , "quoteId" ),
98+ "Name" ,
99+ "Overfullstack" )))));
100+ }
101+
19102 @ Test
20103 @ DisplayName ("json file To Pojo" )
21104 void jsonFileToPojo () {
@@ -29,19 +112,22 @@ void jsonFileToPojo() {
29112 @ Test
30113 @ DisplayName ("json with Epoch Date To Pojo" )
31114 void jsonWithEpochDateToPojo () {
115+ final var epochDate = 1604216172747L ;
32116 final var beanWithDate =
33- JsonPojoUtils .<BeanWithDate >jsonToPojo (BeanWithDate .class , "{\" date\" : 1604216172813 }" );
117+ JsonPojoUtils .<BeanWithDate >jsonToPojo (BeanWithDate .class , "{\" date\" : " + epochDate + " }" );
34118 assertThat (beanWithDate ).isNotNull ();
35- assertThat (beanWithDate .date ). isNotNull ( );
119+ assertThat (beanWithDate .date . toInstant (). toEpochMilli ()). isEqualTo ( epochDate );
36120 }
37121
38122 @ Test
39123 @ DisplayName ("json with ISO Date To Pojo" )
40- void jsonWithISODateToPojo () {
124+ void jsonWithISODateToPojo () throws ParseException {
125+ final var date = "2015-09-01" ;
41126 final var beanWithDate =
42- JsonPojoUtils .<BeanWithDate >jsonToPojo (BeanWithDate .class , "{\" date\" : \" 2015-09-01 \" }" );
127+ JsonPojoUtils .<BeanWithDate >jsonToPojo (BeanWithDate .class , "{\" date\" : \" " + date + " \" }" );
43128 assertThat (beanWithDate ).isNotNull ();
44- assertThat (beanWithDate .date ).isNotNull ();
129+ final var formatter = new SimpleDateFormat ("yyyy-MM-dd" );
130+ assertThat (beanWithDate .date ).isEqualTo (formatter .parse (date ));
45131 }
46132
47133 @ Test
0 commit comments