How do I Mock Rest Template for Post method

Viewed 68

I want one JSONObject response after passing URI through RESTTemplate

Test case is passing but the code coverage is still 0%

I have to return accountDetails object in JSON format

how do we Pass URI which takes account ID and given response entity in JSON Format this is what I have to figure out.

Test method:

void scheduleOnDemand() throws Exception {

        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setId(1);
        accountDTO.setTimeZone("Asia/Kolkata");
        accountDTO.setPlatformType("AZURE");
        accountDTO.setEnvironmentName("test");
        accountDTO.setName("azureAccount");
        accountDTO.setNextScheduleDate("2021-09-13");
        accountDTO.setEnvironmentId(1);

        HashMap<String, Object> accountDetails = new HashMap<>();

        accountDetails.put("account_Id", "1");
        accountDetails.put("TimeZone", "Asia/Kolkata");
        accountDetails.put("AgentStatus", "Initiated");
        accountDetails.put("Account_Platform", "AZURE");
        accountDetails.put("Schedule_Time", "13:30:50.000");
        accountDetails.put("Environment_Name", "test");
        accountDetails.put("Account_Name", "azureAccount");
        accountDetails.put("History_Id", "109");
        accountDetails.put("Schedule_Date", "2021-09-13");
        accountDetails.put("Environment_Id", "1");


    Mockito.when(restTemplate.postForEntity("configure/accountDetails?Account_Id=1",null, JSONObject.class))
                .thenReturn(new ResponseEntity((accountDetails), HttpStatus.OK)); 

    }

Actual Method:

@Override
public JSONObject scheduleOnDemand(String accountId) throws Exception {

    JSONObject object = null;
 
    // PlatformHistoryDetails phistory = null;
    HashMap<String, Object> accountDetails = new HashMap<>();
    accountDetails = utilService.getAccountDetails(Integer.parseInt(accountId));
    if (((String) accountDetails.get("scantype")).equalsIgnoreCase("infra")||((String) accountDetails.get("scantype")).equalsIgnoreCase("all")) {
        URI postUri = UriComponentsBuilder.fromPath("/").pathSegment("api/scheduleOnDemand")
                .queryParam("requestId", MDC.get("requestId")).queryParam("service", "scan")
                .queryParam("Account_Id", accountId).build().toUri();

        PlatformHistoryDetails phistory = modelMapper.map(apiClient.postOperation(postUri, Object.class),
                PlatformHistoryDetails.class); 
        phistory.getHistory().setUser("admin");
        object = utilService.processOneAccount(phistory);
    } else {
        throw new Exception("Account is not of type INFRA or ALL but of type " + accountDetails.get("scantype"));
    }
    return object;   
}

accountDetails Implementation:

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public HashMap<String, Object> getAccountDetails(int accountId) {
    URI getUri = UriComponentsBuilder.fromPath("/").pathSegment("configure/accountDetails")
            .queryParam("Account_Id", accountId).build().toUri();

    HashMap<String, Object> account = (LinkedHashMap) apiClient.getAccountDetails(getUri, Object.class);
   
    return account;
}
0 Answers
Related