|
10 | 10 | import static com.salesforce.revoman.input.config.HookConfig.post; |
11 | 11 | import static com.salesforce.revoman.input.config.ResponseConfig.unmarshallResponse; |
12 | 12 | import static com.salesforce.revoman.input.config.StepPick.PostTxnStepPick.afterStepContainingURIPathOfAny; |
| 13 | +import static com.salesforce.revoman.input.config.StepPick.PostTxnStepPick.afterStepEndingWithURIPathOfAny; |
| 14 | +import static com.salesforce.revoman.output.report.StepReport.containsHeader; |
13 | 15 | import static org.junit.jupiter.api.Assertions.assertTrue; |
14 | 16 |
|
15 | 17 | import com.salesforce.revoman.input.config.HookConfig; |
16 | 18 | import com.salesforce.revoman.input.config.ResponseConfig; |
17 | 19 | import com.salesforce.revoman.input.json.adapters.salesforce.CompositeGraphResponse; |
18 | 20 | import com.salesforce.revoman.input.json.adapters.salesforce.CompositeGraphResponse.Graph.ErrorGraph; |
| 21 | +import com.salesforce.revoman.input.json.adapters.salesforce.CompositeResponse; |
19 | 22 | import com.salesforce.revoman.output.report.StepReport; |
20 | 23 | import java.util.List; |
21 | | -import kotlin.collections.CollectionsKt; |
22 | 24 |
|
23 | 25 | public class CoreUtils { |
24 | 26 | private CoreUtils() {} |
25 | 27 |
|
26 | 28 | public static final String COMPOSITE_GRAPH_URI_PATH = "composite/graph"; |
27 | 29 |
|
28 | | - public static final HookConfig ASSERT_COMPOSITE_GRAPH_RESPONSE_SUCCESS = |
29 | | - post( |
30 | | - afterStepContainingURIPathOfAny(COMPOSITE_GRAPH_URI_PATH), |
31 | | - (stepReport, ignore) -> assertCompositeGraphResponseSuccess(stepReport)); |
32 | | - |
| 30 | + /** Reusable Response config for CompositeGraphResponse to enhance debugging experience */ |
33 | 31 | public static ResponseConfig unmarshallCompositeGraphResponse() { |
34 | 32 | return unmarshallResponse( |
35 | 33 | afterStepContainingURIPathOfAny(COMPOSITE_GRAPH_URI_PATH), |
36 | 34 | CompositeGraphResponse.class, |
37 | 35 | CompositeGraphResponse.ADAPTER); |
38 | 36 | } |
39 | 37 |
|
| 38 | + /** |
| 39 | + * Reusable ReVoman Post-Step hooks that can be added to your configuration to assert |
| 40 | + * CompositeGraphResponse(success/error). For expected failure response, this expects you to add |
| 41 | + * `expectToFail=true` header to your request in Postman If you have to use a different header, |
| 42 | + * compose your own hook using {@link #assertCompositeGraphResponseSuccess(StepReport, String)} |
| 43 | + */ |
| 44 | + public static final HookConfig ASSERT_COMPOSITE_GRAPH_RESPONSE_SUCCESS = |
| 45 | + post( |
| 46 | + afterStepContainingURIPathOfAny(COMPOSITE_GRAPH_URI_PATH), |
| 47 | + (stepReport, ignore) -> assertCompositeGraphResponseSuccess(stepReport)); |
| 48 | + |
| 49 | + public static final String EXPECT_TO_FAIL_HEADER = "expectToFail"; |
| 50 | + |
| 51 | + private static final String UNSUCCESSFUL_COMPOSITE_GRAPH_RESPONSE_ERROR_MSG = |
| 52 | + """ |
| 53 | + Unsuccessful Composite Graph response for graphId: %s |
| 54 | + { |
| 55 | + first errorCode: %s |
| 56 | + first errorMessage: %s |
| 57 | + } |
| 58 | + StepReport: |
| 59 | + %s"""; |
| 60 | + |
40 | 61 | public static void assertCompositeGraphResponseSuccess(StepReport stepReport) { |
| 62 | + assertCompositeGraphResponseSuccess(stepReport, EXPECT_TO_FAIL_HEADER); |
| 63 | + } |
| 64 | + |
| 65 | + static void assertCompositeGraphResponseSuccess( |
| 66 | + StepReport stepReport, String expectToFailHeader) { |
41 | 67 | final var responseTxnInfo = stepReport.responseInfo.get(); |
42 | | - final var graphResp = |
| 68 | + final var graphsResp = |
43 | 69 | responseTxnInfo |
44 | 70 | .<CompositeGraphResponse>getTypedTxnObj( |
45 | 71 | CompositeGraphResponse.class, List.of(CompositeGraphResponse.ADAPTER)) |
46 | | - .getGraphs() |
47 | | - .getFirst(); |
| 72 | + .getGraphs(); |
| 73 | + for (var graphResp : graphsResp) { |
| 74 | + assertTrue( |
| 75 | + graphResp.isSuccessful() || containsHeader(stepReport.requestInfo, expectToFailHeader), |
| 76 | + () -> { |
| 77 | + final var firstErrorResponseBody = ((ErrorGraph) graphResp).firstErrorResponseBody(); |
| 78 | + return String.format( |
| 79 | + UNSUCCESSFUL_COMPOSITE_GRAPH_RESPONSE_ERROR_MSG, |
| 80 | + graphResp.getGraphId(), |
| 81 | + firstErrorResponseBody.getErrorCode(), |
| 82 | + firstErrorResponseBody.getMessage(), |
| 83 | + stepReport); |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public static final String COMPOSITE_URI_PATH = "composite"; |
| 89 | + |
| 90 | + /** Reusable Response config for CompositeGraphResponse to enhance debugging experience */ |
| 91 | + public static ResponseConfig unmarshallCompositeResponse() { |
| 92 | + return unmarshallResponse( |
| 93 | + afterStepEndingWithURIPathOfAny(COMPOSITE_URI_PATH), |
| 94 | + CompositeResponse.class, |
| 95 | + CompositeResponse.ADAPTER); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Reusable ReVoman Post-Step hooks that can be added to your configuration to assert |
| 100 | + * CompositeResponse(success/error). For expected failure response, this expects you to add |
| 101 | + * `expectToFail=true` header to your request in Postman If you have to use a different header, |
| 102 | + * compose your own hook using {@link #assertCompositeResponseSuccess(StepReport, String)} |
| 103 | + */ |
| 104 | + public static final HookConfig ASSERT_COMPOSITE_RESPONSE_SUCCESS = |
| 105 | + post( |
| 106 | + afterStepEndingWithURIPathOfAny(COMPOSITE_URI_PATH), |
| 107 | + (stepReport, ignore) -> assertCompositeResponseSuccess(stepReport)); |
| 108 | + |
| 109 | + private static final String UNSUCCESSFUL_COMPOSITE_RESPONSE_ERROR_MSG = |
| 110 | + """ |
| 111 | + Unsuccessful Composite response: |
| 112 | + { |
| 113 | + first errorCode: %s |
| 114 | + first errorMessage: %s |
| 115 | + } |
| 116 | + StepReport: |
| 117 | + %s"""; |
| 118 | + |
| 119 | + static void assertCompositeResponseSuccess(StepReport stepReport) { |
| 120 | + assertCompositeResponseSuccess(stepReport, EXPECT_TO_FAIL_HEADER); |
| 121 | + } |
| 122 | + |
| 123 | + static void assertCompositeResponseSuccess(StepReport stepReport, String expectToFailHeader) { |
| 124 | + final var responseTxnInfo = stepReport.responseInfo.get(); |
| 125 | + final var compositeResp = |
| 126 | + responseTxnInfo.<CompositeResponse>getTypedTxnObj( |
| 127 | + CompositeResponse.class, List.of(CompositeResponse.ADAPTER)); |
48 | 128 | assertTrue( |
49 | | - graphResp.isSuccessful(), |
| 129 | + compositeResp.isSuccessful() || containsHeader(stepReport.requestInfo, expectToFailHeader), |
50 | 130 | () -> { |
51 | | - final var firstErrorResponse = |
52 | | - CollectionsKt.first(((ErrorGraph) graphResp).errorResponses()); |
53 | | - final var firstErrorResponseBody = ((ErrorGraph) graphResp).firstErrorResponseBody(); |
| 131 | + final var firstErrorResponseBody = compositeResp.firstErrorResponseBody(); |
54 | 132 | return String.format( |
55 | | - "Unsuccessful Composite Graph response%n{%n first error ReferenceId: %s%n first errorCode: %s%n first errorMessage: %s%n}%n%s", |
56 | | - firstErrorResponse.getReferenceId(), |
| 133 | + UNSUCCESSFUL_COMPOSITE_RESPONSE_ERROR_MSG, |
57 | 134 | firstErrorResponseBody.getErrorCode(), |
58 | 135 | firstErrorResponseBody.getMessage(), |
59 | 136 | stepReport); |
|
0 commit comments