@Test public void invalidInput() throws Exception { String notDashboardJson = "{ \"user\": \"dmiller\" }"; String userId = "ann-acm"; Dashboard dashboard = new Dashboard(); dashboard.setDashboardConfig("UPDATE TEST"); AcmUser user = new AcmUser(); user.setUserId(userId); DashboardDto dashboardDto = new DashboardDto(); dashboardDto.setDashboardConfig("UPDATE TEST"); Capture<DashboardDto> savedDashboardDto = new Capture<>(); Capture<Dashboard> publishedDashboard = new Capture<>(); List<String> retList = new ArrayList<>(); retList.add(DashboardConstants.DASHBOARD_MODULE_NAME); expect(mockDashboardPropertyReader.getModuleNameList()).andReturn(retList); expect(mockDashboardService.getUserByUserId(userId)).andReturn(user); expect(mockDashboardService.getDashboardConfigForUserAndModuleName(user, DashboardConstants.DASHBOARD_MODULE_NAME)) .andReturn(dashboard); expect(mockDashboardService.setDashboardConfigForUserAndModule(eq(user), capture(savedDashboardDto), eq(DashboardConstants.DASHBOARD_MODULE_NAME))).andThrow(new RuntimeException()); mockDashboardEventPublisher.publishDashboardEvent(capture(publishedDashboard), eq(mockAuthentication), eq(false), eq(false)); expect(mockAuthentication.getName()).andReturn("ann-acm").atLeastOnce(); replayAll(); mockMvc.perform( post("/api/latest/plugin/dashboard/set") .accept(MediaType.parseMediaType("application/json;charset=UTF-8")) .contentType(MediaType.APPLICATION_JSON) .principal(mockAuthentication) .content(notDashboardJson)) .andExpect(status().is4xxClientError()) .andExpect(content().contentType(MediaType.TEXT_PLAIN)); verifyAll(); }
@RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public DashboardDto setDashboardConfig( @RequestBody DashboardDto updateDashboardDto, Authentication authentication, HttpSession session) throws AcmObjectNotFoundException, AcmUserActionFailedException, AcmDashboardException { String userId = authentication.getName(); AcmUser user = dashboardService.getUserByUserId(userId); List<String> modules = dashboardPropertyReader.getModuleNameList(); String moduleName; if (updateDashboardDto.getModule() != null) { moduleName = updateDashboardDto.getModule().trim(); } else { moduleName = DashboardConstants.DASHBOARD_MODULE_NAME; } if (!modules.contains(moduleName)) { throw new AcmDashboardException("Module name:" + moduleName + "does not exist"); } log.info("Updating dashboard configuration for user: [{}] '", userId); Dashboard d = null; try { d = dashboardService.getDashboardConfigForUserAndModuleName(user, moduleName); int retval = dashboardService.setDashboardConfigForUserAndModule(user, updateDashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } else { getEventPublisher().publishDashboardEvent(d, authentication, false, true); updateDashboardDto.setUpdated(true); return updateDashboardDto; } } catch (Exception e) { getEventPublisher().publishDashboardEvent(d, authentication, false, false); throw new AcmUserActionFailedException("update", "dashboard", null, e.getMessage(), e); } }
SetDashboardConfigAPIController { @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public DashboardDto setDashboardConfig( @RequestBody DashboardDto updateDashboardDto, Authentication authentication, HttpSession session) throws AcmObjectNotFoundException, AcmUserActionFailedException, AcmDashboardException { String userId = authentication.getName(); AcmUser user = dashboardService.getUserByUserId(userId); List<String> modules = dashboardPropertyReader.getModuleNameList(); String moduleName; if (updateDashboardDto.getModule() != null) { moduleName = updateDashboardDto.getModule().trim(); } else { moduleName = DashboardConstants.DASHBOARD_MODULE_NAME; } if (!modules.contains(moduleName)) { throw new AcmDashboardException("Module name:" + moduleName + "does not exist"); } log.info("Updating dashboard configuration for user: [{}] '", userId); Dashboard d = null; try { d = dashboardService.getDashboardConfigForUserAndModuleName(user, moduleName); int retval = dashboardService.setDashboardConfigForUserAndModule(user, updateDashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } else { getEventPublisher().publishDashboardEvent(d, authentication, false, true); updateDashboardDto.setUpdated(true); return updateDashboardDto; } } catch (Exception e) { getEventPublisher().publishDashboardEvent(d, authentication, false, false); throw new AcmUserActionFailedException("update", "dashboard", null, e.getMessage(), e); } } }
SetDashboardConfigAPIController { @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public DashboardDto setDashboardConfig( @RequestBody DashboardDto updateDashboardDto, Authentication authentication, HttpSession session) throws AcmObjectNotFoundException, AcmUserActionFailedException, AcmDashboardException { String userId = authentication.getName(); AcmUser user = dashboardService.getUserByUserId(userId); List<String> modules = dashboardPropertyReader.getModuleNameList(); String moduleName; if (updateDashboardDto.getModule() != null) { moduleName = updateDashboardDto.getModule().trim(); } else { moduleName = DashboardConstants.DASHBOARD_MODULE_NAME; } if (!modules.contains(moduleName)) { throw new AcmDashboardException("Module name:" + moduleName + "does not exist"); } log.info("Updating dashboard configuration for user: [{}] '", userId); Dashboard d = null; try { d = dashboardService.getDashboardConfigForUserAndModuleName(user, moduleName); int retval = dashboardService.setDashboardConfigForUserAndModule(user, updateDashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } else { getEventPublisher().publishDashboardEvent(d, authentication, false, true); updateDashboardDto.setUpdated(true); return updateDashboardDto; } } catch (Exception e) { getEventPublisher().publishDashboardEvent(d, authentication, false, false); throw new AcmUserActionFailedException("update", "dashboard", null, e.getMessage(), e); } }  }
SetDashboardConfigAPIController { @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public DashboardDto setDashboardConfig( @RequestBody DashboardDto updateDashboardDto, Authentication authentication, HttpSession session) throws AcmObjectNotFoundException, AcmUserActionFailedException, AcmDashboardException { String userId = authentication.getName(); AcmUser user = dashboardService.getUserByUserId(userId); List<String> modules = dashboardPropertyReader.getModuleNameList(); String moduleName; if (updateDashboardDto.getModule() != null) { moduleName = updateDashboardDto.getModule().trim(); } else { moduleName = DashboardConstants.DASHBOARD_MODULE_NAME; } if (!modules.contains(moduleName)) { throw new AcmDashboardException("Module name:" + moduleName + "does not exist"); } log.info("Updating dashboard configuration for user: [{}] '", userId); Dashboard d = null; try { d = dashboardService.getDashboardConfigForUserAndModuleName(user, moduleName); int retval = dashboardService.setDashboardConfigForUserAndModule(user, updateDashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } else { getEventPublisher().publishDashboardEvent(d, authentication, false, true); updateDashboardDto.setUpdated(true); return updateDashboardDto; } } catch (Exception e) { getEventPublisher().publishDashboardEvent(d, authentication, false, false); throw new AcmUserActionFailedException("update", "dashboard", null, e.getMessage(), e); } }  @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody DashboardDto setDashboardConfig(
            @RequestBody DashboardDto updateDashboardDto,
            Authentication authentication,
            HttpSession session); DashboardEventPublisher getEventPublisher(); void setEventPublisher(DashboardEventPublisher eventPublisher); DashboardPropertyReader getDashboardPropertyReader(); void setDashboardPropertyReader(DashboardPropertyReader dashboardPropertyReader); DashboardService getDashboardService(); void setDashboardService(DashboardService dashboardService); }
SetDashboardConfigAPIController { @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody public DashboardDto setDashboardConfig( @RequestBody DashboardDto updateDashboardDto, Authentication authentication, HttpSession session) throws AcmObjectNotFoundException, AcmUserActionFailedException, AcmDashboardException { String userId = authentication.getName(); AcmUser user = dashboardService.getUserByUserId(userId); List<String> modules = dashboardPropertyReader.getModuleNameList(); String moduleName; if (updateDashboardDto.getModule() != null) { moduleName = updateDashboardDto.getModule().trim(); } else { moduleName = DashboardConstants.DASHBOARD_MODULE_NAME; } if (!modules.contains(moduleName)) { throw new AcmDashboardException("Module name:" + moduleName + "does not exist"); } log.info("Updating dashboard configuration for user: [{}] '", userId); Dashboard d = null; try { d = dashboardService.getDashboardConfigForUserAndModuleName(user, moduleName); int retval = dashboardService.setDashboardConfigForUserAndModule(user, updateDashboardDto, moduleName); if (retval != 1) { log.error("Unable to update dashboard config because dashboard for user: [{}] is not found", userId); throw new AcmObjectNotFoundException("dashboard", null, "Object not found", null); } else { getEventPublisher().publishDashboardEvent(d, authentication, false, true); updateDashboardDto.setUpdated(true); return updateDashboardDto; } } catch (Exception e) { getEventPublisher().publishDashboardEvent(d, authentication, false, false); throw new AcmUserActionFailedException("update", "dashboard", null, e.getMessage(), e); } }  @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody DashboardDto setDashboardConfig(
            @RequestBody DashboardDto updateDashboardDto,
            Authentication authentication,
            HttpSession session); DashboardEventPublisher getEventPublisher(); void setEventPublisher(DashboardEventPublisher eventPublisher); DashboardPropertyReader getDashboardPropertyReader(); void setDashboardPropertyReader(DashboardPropertyReader dashboardPropertyReader); DashboardService getDashboardService(); void setDashboardService(DashboardService dashboardService);  }