Skip to content

Commit 3f311ec

Browse files
committed
Clean-up Unit tests
Signed-off-by: Gopal S Akshintala <[email protected]>
1 parent e1f6ecf commit 3f311ec

File tree

10 files changed

+156
-246
lines changed

10 files changed

+156
-246
lines changed

src/integrationTest/java/com/salesforce/revoman/input/json/JsonReaderUtilsTest.java renamed to src/integrationTest/java/com/salesforce/revoman/input/json/JsonPojoUtils2Test.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,37 @@
88
package com.salesforce.revoman.input.json;
99

1010
import static com.google.common.truth.Truth.assertThat;
11+
import static com.salesforce.revoman.input.FileUtils.readFileInResourcesToString;
1112
import static com.salesforce.revoman.integration.core.pq.adapters.ConnectInputRepWithGraphAdapter.adapter;
1213
import static org.junit.jupiter.api.Assertions.assertThrows;
1314

1415
import com.salesforce.revoman.integration.core.pq.connect.request.PlaceQuoteInputRepresentation;
1516
import com.salesforce.revoman.integration.core.pq.connect.request.PricingPreferenceEnum;
1617
import com.squareup.moshi.JsonDataException;
1718
import java.util.List;
19+
import org.json.JSONException;
1820
import org.junit.jupiter.api.DisplayName;
1921
import org.junit.jupiter.api.Test;
22+
import org.skyscreamer.jsonassert.JSONAssert;
23+
import org.skyscreamer.jsonassert.JSONCompareMode;
2024

21-
class JsonReaderUtilsTest {
25+
class JsonPojoUtils2Test {
26+
27+
@Test
28+
@DisplayName("toJson: PQ Payload JSON -> PlaceQuoteInputRep -> PQ Payload JSON")
29+
void pqInputRepToPQPayloadJson() throws JSONException {
30+
final var pqAdapter = adapter(PlaceQuoteInputRepresentation.class);
31+
final var pqPayloadJsonFilePath = "json/pq-payload.json";
32+
final var pqInputRep =
33+
JsonPojoUtils.<PlaceQuoteInputRepresentation>jsonFileToPojo(
34+
PlaceQuoteInputRepresentation.class, pqPayloadJsonFilePath, List.of(pqAdapter));
35+
assertThat(pqInputRep).isNotNull();
36+
final var pqPayloadJsonStr =
37+
JsonPojoUtils.pojoToJson(
38+
PlaceQuoteInputRepresentation.class, pqInputRep, List.of(pqAdapter));
39+
final var expectedPQPayload = readFileInResourcesToString(pqPayloadJsonFilePath);
40+
JSONAssert.assertEquals(expectedPQPayload, pqPayloadJsonStr, JSONCompareMode.STRICT);
41+
}
2242

2343
@Test
2444
@DisplayName("fromJson: PQ payload JSON --> PlaceQuoteInputRep")

src/integrationTest/java/com/salesforce/revoman/input/json/JsonWriterUtilsTest.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main/kotlin/com/salesforce/revoman/input/json/JsonPojoUtils.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ inline fun <reified PojoT : Any> jsonToPojo(
6464
): PojoT? =
6565
jsonToPojo(PojoT::class.java, jsonStr, customAdapters, customAdaptersWithType, typesToIgnore)
6666

67+
/**
68+
* Generate a JSON string from a POJO object.
69+
*
70+
* @param pojoType The type of the POJO object.
71+
* @param pojo The POJO object to be converted to JSON.
72+
* @param customAdapters A list of custom adapters for Moshi to use during the conversion.
73+
* @param customAdaptersWithType A map of custom adapters with their respective types.
74+
* @param typesToIgnore A set of classes to ignore during the conversion.
75+
* @param indent An optional string for pretty-printing the JSON output.
76+
* @param <PojoT> The type of the POJO object.
77+
* @return A JSON string or null if the input is null.
78+
*/
6779
@JvmOverloads
6880
fun <PojoT : Any> pojoToJson(
6981
pojoType: Type,

src/test/java/com/salesforce/revoman/input/json/JsonPojoUtilsTest.java

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,97 @@
88
package com.salesforce.revoman.input.json;
99

1010
import 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;
1222
import java.util.Date;
1323
import java.util.List;
24+
import java.util.Map;
25+
import org.json.JSONException;
1426
import org.junit.jupiter.api.DisplayName;
1527
import org.junit.jupiter.api.Test;
28+
import org.skyscreamer.jsonassert.JSONAssert;
29+
import org.skyscreamer.jsonassert.JSONCompareMode;
1630

1731
class 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

src/test/java/com/salesforce/revoman/input/json/SObjectGraphRequestMarshallerTest.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/test/java/com/salesforce/revoman/input/json/pojo/Entity.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/test/java/com/salesforce/revoman/input/json/pojo/SObjectGraphRequest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package com.salesforce.revoman.input.json.pojo;
99

1010
import java.util.List;
11+
import java.util.Map;
1112

1213
public class SObjectGraphRequest {
1314

@@ -26,4 +27,34 @@ public String getGraphId() {
2627
public List<SObjectWithReferenceRequest> getRecords() {
2728
return this.records;
2829
}
30+
31+
public static class Entity {
32+
private final Map<String, Object> fields;
33+
34+
public Entity(Map<String, Object> fields) {
35+
this.fields = fields;
36+
}
37+
38+
public Map<String, Object> getFields() {
39+
return fields;
40+
}
41+
}
42+
43+
public static class SObjectWithReferenceRequest {
44+
private final String referenceId;
45+
private final Entity record;
46+
47+
public SObjectWithReferenceRequest(String referenceId, Entity record) {
48+
this.referenceId = referenceId;
49+
this.record = record;
50+
}
51+
52+
public String getReferenceId() {
53+
return this.referenceId;
54+
}
55+
56+
public Entity getRecord() {
57+
return this.record;
58+
}
59+
}
2960
}

src/test/java/com/salesforce/revoman/input/json/pojo/SObjectWithReferenceRequest.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)