SafePolicyController { @RequestMapping(value = {"/sp_dictionary/save_riskType"}, method = {RequestMethod.POST}) public ModelAndView saveRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException { DictionaryUtils utils = getDictionaryUtilsInstance(); try { boolean fromAPI = utils.isRequestFromAPI(request); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); RiskType riskTypeData; String userId = null; if (fromAPI) { riskTypeData = mapper.readValue(root.get("dictionaryFields").toString(), RiskType.class); userId = "API"; } else { riskTypeData = mapper.readValue(root.get("riskTypeDictionaryData").toString(), RiskType.class); userId = root.get("userid").textValue(); } UserInfo userInfo = utils.getUserInfo(userId); List<Object> duplicateData = commonClassDao.checkDuplicateEntry(riskTypeData.getName(), "name", RiskType.class); boolean duplicateflag = false; if (!duplicateData.isEmpty()) { RiskType data = (RiskType) duplicateData.get(0); if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) { riskTypeData.setId(data.getId()); } else if ((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) || (request.getParameter(operation) == null && (data.getId() != riskTypeData.getId()))) { duplicateflag = true; } } String responseString = null; if (!duplicateflag) { riskTypeData.setUserModifiedBy(userInfo); if (riskTypeData.getId() == 0) { riskTypeData.setUserCreatedBy(userInfo); commonClassDao.save(riskTypeData); } else { riskTypeData.setModifiedDate(new Date()); commonClassDao.update(riskTypeData); } responseString = mapper.writeValueAsString(commonClassDao.getData(RiskType.class)); } else { responseString = duplicateResponseString; } if (fromAPI) { return utils.getResultForApi(responseString); } else { utils.setResponseData(response, riskTypeDatas, responseString); } } catch (Exception e) { utils.setErrorResponseData(response, e); } return null; } @Autowired  SafePolicyController(CommonClassDao commonClassDao);  SafePolicyController(); void setCommonClassDao(CommonClassDao commonClassDao); @RequestMapping( value = {"/get_RiskTypeDataByName"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getRiskTypeDictionaryByNameEntityData(HttpServletResponse response); @RequestMapping( value = {"/get_RiskTypeData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getRiskTypeDictionaryEntityData(HttpServletResponse response); @RequestMapping(value = {"/sp_dictionary/save_riskType"}, method = {RequestMethod.POST}) ModelAndView saveRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/sp_dictionary/remove_riskType"}, method = {RequestMethod.POST}) void removeRiskTypeDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping( value = {"/get_SafePolicyWarningDataByName"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getSafePolicyWarningEntityDataByName(HttpServletResponse response); @RequestMapping( value = {"/get_SafePolicyWarningData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getSafePolicyWarningeEntityData(HttpServletResponse response); @RequestMapping(value = {"/sp_dictionary/save_safePolicyWarning"}, method = {RequestMethod.POST}) ModelAndView saveSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/sp_dictionary/remove_SafePolicyWarning"}, method = {RequestMethod.POST}) void removeSafePolicyWarningDictionary(HttpServletRequest request, HttpServletResponse response);  }
@Test public void testSaveRiskTypeDictionary() { jsonString = "{\"userid\":\"demo\",\"riskTypeDictionaryData\":{\"id\":1,\"description\":\"test\",\"name\":\"Test\"}}"; try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) { when(request.getReader()).thenReturn(br); controller.saveRiskTypeDictionary(request, response); assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas")); } catch (Exception e) { logger.error("Exception" + e); } }
@Test public void testUpdateRiskTypeDictionary() { jsonString = "{\"userid\":\"demo\",\"riskTypeDictionaryData\":{\"description\":\"test\",\"name\":\"Test\"}}"; try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) { when(request.getReader()).thenReturn(br); controller.saveRiskTypeDictionary(request, response); assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("riskTypeDictionaryDatas")); } catch (Exception e) { logger.error("Exception" + e); } }