0

I have following post mapping.

@PostMapping(value = BULK_UPDATE)
@ApiOperation(value = "Bulk Update of Markets by pairs of Market Key and Tier Quantity Id", tags = "Bulk", code = 200)
@ApiImplicitParams({
        @ApiImplicitParam(name = "MarketTierQuantityId", value = "List of Market Key and Tier Quantity Id pairs",
                paramType = "body", allowMultiple = true, dataType = "MarketTierQuantityId", required = true) })
@ApiResponses({
        @ApiResponse(code = 200, message = "Bulk update successful", response = MarketStatus.class, responseContainer = "List") })
@ResponseStatus(org.springframework.http.HttpStatus.OK)
public ResponseEntity<StreamingResponseBody> bulkUpdate(
        @RequestParam(name = IGNORE_SYNC_PAUSE_FAILURE, required = false, defaultValue = "false")
        @ApiParam(name = IGNORE_SYNC_PAUSE_FAILURE, value = "Ignore failure of the jobs pause command") boolean ignoreJobsPauseFailure,
        @RequestBody @ApiParam(name = "MarketTierQuantityId", value = "List of Market Key and Tier Quantity Id pairs", required = true) List<MarketTierQuantityId> marketTierQuantities,
        @RequestParam(name = MOVE_TO_PREAUTH_FLAG, required = false, defaultValue = "true")
        @ApiParam(name = MOVE_TO_PREAUTH_FLAG, value = "Move new units to Preauth for the markets with active waitlists") boolean moveToPreauth) throws BusinessException {
    String requestId = getRequestId();
    boolean jobsPaused = pauseJobs(ignoreJobsPauseFailure);

    return LoggingStopWatch.wrap(() -> {
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON)
                .body(outputStream -> process(new SyncBulkProcessorHelper(outputStream),
                        marketTierQuantities, jobsPaused, requestId, moveToPreauth, LoggingStopWatch.create(LOGGER, "Bulk Update")));
    });
}

and i have written the following test.

@RunWith(SpringRunner.class)
@WebMvcTest(BulkUpdateController.class)
@ContextConfiguration(classes = { BulkUpdateController.class, SharedExecutor.class })
@ActiveProfiles("dev")
public class BulkUpdateControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private BulkStatusService bulkStatusService;

    @MockBean
    private BulkMarketService bulkMarketService;

    @MockBean
    private HttpService httpService;

    @MockBean
    private RestClient restClient;

    @MockBean
    private BulkProcessorHelper helper;


    @Test
    public void test() throws Exception {
        String request = TestHelper.getSerializedRequest(getBulkUpdateRequest(), MarketTierQuantityId.class);
        mockMvc.perform(post("/bulkupdate").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
                .content(request)).andExpect(status().is4xxClientError());
    }


    public MarketTierQuantityId getBulkUpdateRequest() {
        MarketTierQuantityId market = new MarketTierQuantityId();
        market.setMarketKey("00601|PR|COBROKE|POSTALCODE|FULL");
        market.setTierQuantityId("10");
        return market;
    }

Getting the following error, have tried every possible way to resolve it but doesnt help.

Request failed. Error response: {\"responseStatus\":{\"errorCode\":\"BadRequest\",\"message\":\"JSON parse error: Cannot deserialize instance of java.util.ArrayList out of START_OBJECT token\",\"stackTrace\":\"BusinessException(JSON parse error:

P.S -> new to JUnits and mocks

Nilamber Singh
  • 804
  • 1
  • 14
  • 33
  • The method expects a JSON array (a List), you're sending a JSON object (a MarketTierQuantityId). – JB Nizet Nov 08 '18 at 20:07
  • @JBNizet how can i change the test class to send JSONArray, content() in post request doesnt accept anything other than String. Apologies if the doubt seems too silly!! – Nilamber Singh Nov 08 '18 at 20:09
  • Well, instead of sending a MarketTierQuantityId serialized to JSON, send a List serialized to JSON. – JB Nizet Nov 08 '18 at 20:10
  • @JBNizet : Doesn't help, i tried doing this. **List list = new ArrayList<>(); list.add(getBulkUpdateRequest()); String request = TestHelper.getSerializedRequest(list, MarketTierQuantityId.class);** Got below error: **java.lang.IllegalArgumentException: Can not set java.lang.String field com.move.aws.eai.inventory.bulk.model.MarketTierQuantityId.marketKey to java.util.ArrayList** – Nilamber Singh Nov 08 '18 at 20:14
  • We have no idea of what TestHelper.getSerializedRequest() does if you dont post its code. It looks wrong just by the signature of it: you shouldn't have to pass a Class in order to serialize something to JSON. Post the relevant code. But really, you should read it first, and read the documentation of the classes and methods that it uses. – JB Nizet Nov 08 '18 at 20:17
  • @JBNizet: I'm sure i will do it, but stuck at this point, i want to wuicly resolve the issue. Here is the code snippet for getSerializedRequest(). If u can figure out something. `public static String getSerializedRequest(final Object request, final Type type) { return new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create().toJson(request, type); }` – Nilamber Singh Nov 08 '18 at 20:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183321/discussion-between-nilamber-singh-and-jb-nizet). – Nilamber Singh Nov 08 '18 at 20:26
  • It would have been quicker to read the javadoc than to ask a question: https://static.javadoc.io/com.google.code.gson/gson/2.8.5/com/google/gson/Gson.html#toJson-java.lang.Object-java.lang.reflect.Type-, https://github.com/google/gson/blob/master/UserGuide.md#collections-examples – JB Nizet Nov 08 '18 at 20:28

0 Answers0