CreateOptimizationController extends RestrictedBaseController { @RequestMapping( value = {"/policyController/getOptimizationTemplateData.htm"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST}) public ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response) throws IOException { CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); String value = root.get("policyData").toString().replaceAll("^\"|\"$", ""); String servicename = value.split("-v")[0]; String version = null; if (value.contains("-v")) { version = value.split("-v")[1]; } OptimizationModels returnModel = getAttributeObject(servicename, version); MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value); JSONObject jsonHdDefaultObj = null; if (returnHeaderDefauls != null) { jsonHdDefaultObj = new JSONObject(); jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName()); jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard()); jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel()); jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType()); jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority()); } String headDefautlsData = ""; if (jsonHdDefaultObj != null) { headDefautlsData = jsonHdDefaultObj.toString(); LOGGER.info("returnHeaderDefauls headDefautlsData: " + headDefautlsData); } else { headDefautlsData = "null"; } Set<String> allkeys = null; if (returnModel.getSubattributes() != null && !returnModel.getSubattributes().isEmpty()) { getAllKeys(new JSONObject(returnModel.getSubattributes())); allkeys = allManyTrueKeys; allManyTrueKeys = new HashSet<>(); LOGGER.info("allkeys : " + allkeys); } String dataOrderInfo = returnModel.getDataOrderInfo(); if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) { dataOrderInfo = "\"" + dataOrderInfo + "\""; } String nameOfTrueKeys = ""; if (allkeys != null) { nameOfTrueKeys = allkeys.toString(); } String jsonModel = createOptimizationJson(returnModel); JSONObject jsonObject = new JSONObject(jsonModel); JSONObject finalJsonObject = null; if (allkeys != null) { Iterator<String> iter = allkeys.iterator(); while (iter.hasNext()) { finalJsonObject = controller.convertToArrayElement(jsonObject, iter.next()); } } if (finalJsonObject != null) { LOGGER.info(finalJsonObject.toString()); jsonModel = finalJsonObject.toString(); } Set<String> manyTrueProperties = controller.getManyTrueProperties(returnModel.getRefattributes()); JSONObject jsonObj = new JSONObject(jsonModel); for (String s : manyTrueProperties) { LOGGER.info(s); finalJsonObject = controller.convertToArrayElement(jsonObj, s.trim()); } if (finalJsonObject != null) { LOGGER.info(finalJsonObject.toString()); jsonModel = finalJsonObject.toString(); } response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); response.setContentType(PolicyUtils.APPLICATION_JSON); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); List<Object> list = new ArrayList<>(); String responseString = mapper.writeValueAsString(returnModel); JSONObject json = null; if ("".equals(nameOfTrueKeys)) { json = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}"); } else { json = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}"); } list.add(json); response.getWriter().write(list.toString()); return null; } @Autowired private  CreateOptimizationController(CommonClassDao commonClassDao);  CreateOptimizationController(); static CommonClassDao getCommonClassDao(); static void setCommonClassDao(CommonClassDao commonClassDao); PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root); @RequestMapping( value = {"/policyController/getOptimizationTemplateData.htm"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST}) ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response); @RequestMapping( value = {"/policyController/getModelServiceVersionData.htm"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST}) ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response); void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity); @RequestMapping( value = {"/oof_dictionary/set_ModelData"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST}) void setModelData(HttpServletRequest request, HttpServletResponse response); Map<String, String> getAttributesListRefMap(); Map<String, LinkedList<String>> getArrayTextList(); static final String DATATYPE; static final String PROPERTIES; static final String TYPE; static final String STRING; static final String INTEGER; static final String LIST; static final String DEFAULT; static final String REQUIRED; static final String MATCHABLE; static final String MANYFALSE; static final String MODEL; static final String MANY; static final String MODELNAME; }
@Test public void testGetOptimizationTemplateData() { logger.debug("testGetOptimizationTemplateData: enter"); CreateOptimizationController controller = new CreateOptimizationController(); MockHttpServletResponse response = new MockHttpServletResponse(); String modelJson = "{\"policyData\":\"testPolicyBody\"}"; try { CreateOptimizationController.setCommonClassDao(commonClassDao); BufferedReader br = new BufferedReader(new StringReader(modelJson)); when(request.getReader()).thenReturn(br); List<Object> optimizationModelsData = new ArrayList<Object>(); OptimizationModels testData = new OptimizationModels(); testData.setVersion("1707.4.1.2-Junit"); optimizationModelsData.add(testData); when(commonClassDao.getDataById(OptimizationModels.class, "modelName", "testPolicyBody")) .thenReturn(optimizationModelsData); controller.getOptimizationTemplateData(request, response); assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("optimizationModelData")); logger.debug("response: " + response.getContentAsString()); } catch (Exception e) { logger.error("testGetOptimizationTemplateData", e); } logger.debug("testGetOptimizationTemplateData: exit"); }