PolicyScopeDictionaryController { @RequestMapping(value = {"/ps_dictionary/save_psGroupPolicyScope"}, method = {RequestMethod.POST}) public ModelAndView savePSGroupScopeDictionary(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()); GroupPolicyScopeList gpdata = null; GroupPolicyScope groupData = null; boolean duplicateGroupFlag = false; if (fromAPI) { gpdata = mapper.readValue(root.get(dictionaryFields).toString(), GroupPolicyScopeList.class); try { groupData = mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class); } catch (Exception e) { groupData = new GroupPolicyScope(); groupData.setResource(root.get(dictionaryFields).get("resource").toString().replace("\"", "")); groupData.setClosedloop(root.get(dictionaryFields).get("closedloop").toString().replace("\"", "")); groupData.setService(root.get(dictionaryFields).get("service").toString().replace("\"", "")); groupData.setType(root.get(dictionaryFields).get("type").toString().replace("\"", "")); LOGGER.error(e); } } else { gpdata = mapper.readValue(root.get("groupPolicyScopeListData").toString(), GroupPolicyScopeList.class); try { groupData = mapper.readValue(root.get(groupPolicyScopeListData1).toString(), GroupPolicyScope.class); } catch (Exception e) { LOGGER.error(e); groupData = new GroupPolicyScope(); groupData.setResource( root.get(groupPolicyScopeListData1).get("resource").toString().replace("\"", "")); groupData.setClosedloop( root.get(groupPolicyScopeListData1).get("closedloop").toString().replace("\"", "")); groupData.setService( root.get(groupPolicyScopeListData1).get("service").toString().replace("\"", "")); groupData.setType(root.get(groupPolicyScopeListData1).get("type").toString().replace("\"", "")); } } if (!gpdata.getGroupName().startsWith(policyScope)) { String name = "PolicyScope_" + gpdata.getGroupName(); gpdata.setGroupName(name); } ArrayList<String> valueList = new ArrayList<>(); String resourceValue = groupData.getResource(); String typeValue = groupData.getType(); String serviceValue = groupData.getService(); String closedLoopValue = groupData.getClosedloop(); valueList.add("resource=" + resourceValue); valueList.add("service=" + serviceValue); valueList.add("type=" + typeValue); valueList.add("closedLoopControlName=" + closedLoopValue); String list = StringUtils.replaceEach(valueList.toString(), new String[] {"[", "]", " "}, new String[] {"", "", ""}); gpdata.setGroupList(list); List<Object> duplicateData = commonClassDao.checkDuplicateEntry(gpdata.getGroupName(), "name", GroupPolicyScopeList.class); if (duplicateData.isEmpty()) { duplicateData = commonClassDao.checkDuplicateEntry(gpdata.getGroupList(), "groupList", GroupPolicyScopeList.class); if (duplicateData.isEmpty()) { duplicateGroupFlag = true; } } boolean duplicateflag = false; if (!duplicateData.isEmpty()) { GroupPolicyScopeList data = (GroupPolicyScopeList) duplicateData.get(0); if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) { gpdata.setId(data.getId()); } else if ((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) || (request.getParameter(operation) == null && (data.getId() != gpdata.getId()))) { duplicateflag = true; } } String responseString = null; if (!duplicateflag && !duplicateGroupFlag) { if (gpdata.getId() == 0) { commonClassDao.save(gpdata); } else { commonClassDao.update(gpdata); } responseString = mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class)); } else if (duplicateGroupFlag) { responseString = "DuplicateGroup"; } else { responseString = duplicateResponseString; } if (fromAPI) { return utils.getResultForApi(responseString); } else { utils.setResponseData(response, groupPolicyScopeDatas, responseString); } } catch (Exception e) { utils.setErrorResponseData(response, e); } return null; } PolicyScopeDictionaryController(); @Autowired  PolicyScopeDictionaryController(CommonClassDao commonClassDao); void setCommonClassDao(CommonClassDao commonClassDao); @RequestMapping( value = {"/get_GroupPolicyScopeDataByName"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getGroupPolicyScopeEntityDataByName(HttpServletResponse response); @RequestMapping( value = {"/get_GroupPolicyScopeData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getGroupPolicyScopeEntityData(HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/save_psGroupPolicyScope"}, method = {RequestMethod.POST}) ModelAndView savePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/remove_GroupPolicyScope"}, method = {RequestMethod.POST}) void removePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping( value = {"/get_PSClosedLoopDataByName"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPSClosedLoopEntityDataByName(HttpServletResponse response); @RequestMapping( value = {"/get_PSClosedLoopData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPSClosedLoopEntityData(HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/save_psClosedLoop"}, method = {RequestMethod.POST}) ModelAndView savePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/remove_PSClosedLoop"}, method = {RequestMethod.POST}) void removePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping( value = {"/get_PSServiceDataByName"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPSServiceEntityDataByName(HttpServletResponse response); @RequestMapping( value = {"/get_PSServiceData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPSServiceEntityData(HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/save_psService"}, method = {RequestMethod.POST}) ModelAndView savePSServiceDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/remove_PSService"}, method = {RequestMethod.POST}) void removePSServiceDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping( value = {"/get_PSTypeDataByName"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPSTypeEntityDataByName(HttpServletResponse response); @RequestMapping( value = {"/get_PSTypeData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPSTypeEntityData(HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/save_psType"}, method = {RequestMethod.POST}) ModelAndView savePSTypeDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/remove_PSType"}, method = {RequestMethod.POST}) void removePSTypeDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping( value = {"/get_PSResourceDataByName"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPSResourceEntityDataByName(HttpServletResponse response); @RequestMapping( value = {"/get_PSResourceData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getPSResourceEntityData(HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/save_psResource"}, method = {RequestMethod.POST}) ModelAndView savePSResourceDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/ps_dictionary/remove_PSResource"}, method = {RequestMethod.POST}) void removePSResourceDictionary(HttpServletRequest request, HttpServletResponse response);  }
@Test public void testSavePSGroupScopeDictionary() { jsonString = "{\"groupPolicyScopeListData\":{\"description\":\"test\",\"groupName\":\"Test\"},\"groupPolicyScopeListData1\":{\"closedloop\":\"SampleClosedLoop\",\"resource\":\"SampleResource\",\"service\":\"SampleService\",\"type\":\"SampleType\"},\"userid\":\"demo\"}"; try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) { when(request.getReader()).thenReturn(br); controller.savePSGroupScopeDictionary(request, response); assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("groupPolicyScopeListDatas")); } catch (Exception e) { logger.error("Exception" + e); } }
@Test public void testUpdatePSGroupScopeDictionary() { jsonString = "{\"groupPolicyScopeListData\":{\"id\":1,\"description\":\"test\",\"groupName\":" + "\"Test\"},\"groupPolicyScopeListData1\":{\"closedloop\":\"SampleClosedLoop\",\"resource\":" + "\"SampleResource\",\"service\":\"SampleService\",\"type\":\"SampleType\"},\"userid\":\"demo\"}"; try (BufferedReader br = new BufferedReader(new StringReader(jsonString))) { when(request.getReader()).thenReturn(br); controller.savePSGroupScopeDictionary(request, response); assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("groupPolicyScopeListDatas")); } catch (Exception e) { logger.error("Exception" + e); } }