CreateOptimizationController extends RestrictedBaseController { @RequestMapping( value = {"/oof_dictionary/set_ModelData"}, method = {org.springframework.web.bind.annotation.RequestMethod.POST}) public void setModelData(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException { modelList = new ArrayList<>(); dirDependencyList = new ArrayList<>(); classMap = new LinkedHashMap<>(); List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request); boolean zip = false; boolean yml = false; String errorMsg = ""; for (FileItem item : items) { if (item.getName().endsWith(".zip") || item.getName().endsWith(".xmi") || item.getName().endsWith(".yml")) { this.newModel = new OptimizationModels(); try { File file = new File(item.getName()); OutputStream outputStream = new FileOutputStream(file); IOUtils.copy(item.getInputStream(), outputStream); outputStream.close(); this.newFile = file.toString(); this.newModel.setModelName(this.newFile.split("-v")[0]); if (this.newFile.contains("-v")) { if (item.getName().endsWith(".zip")) { this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", "")); zip = true; } else if (item.getName().endsWith(".yml")) { this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", "")); yml = true; } else { this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", "")); } } } catch (Exception e) { LOGGER.error("Upload error : ", e); errorMsg = "Upload error:" + e.getMessage(); } } } if (!errorMsg.isEmpty()) { response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); response.setContentType(PolicyUtils.APPLICATION_JSON); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); response.getWriter().write(new JSONObject().put("errorMsg", errorMsg).toString()); return; } List<File> fileList = new ArrayList<>(); MsModelUtils modelUtil = new MsModelUtils(); this.directory = MODEL; if (zip) { extractFolder(this.newFile); fileList = listModelFiles(this.directory); } else if (yml) { modelUtil.parseTosca(this.newFile); } else { File file = new File(this.newFile); fileList.add(file); } String modelType; if (!yml) { modelType = "xmi"; classMap = new LinkedHashMap<>(); for (File file : fileList) { if (!file.isDirectory() && file.getName().endsWith(".xmi")) { retrieveDependency(file.toString()); } } modelList = createList(); msController.cleanUp(this.newFile); msController.cleanUp(directory); } else { modelType = "yml"; modelList.add(this.newModel.getModelName()); String className = this.newModel.getModelName(); MsAttributeObject optimizationAttributes = new MsAttributeObject(); optimizationAttributes.setClassName(className); LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>(); returnAttributeList.put(className, modelUtil.getAttributeString()); optimizationAttributes.setAttribute(returnAttributeList); optimizationAttributes.setSubClass(modelUtil.getRetmap()); optimizationAttributes.setMatchingSet(modelUtil.getMatchableValues()); LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>(); returnReferenceList.put(className, modelUtil.getReferenceAttributes()); optimizationAttributes.setRefAttribute(returnReferenceList); if (!"".equals(modelUtil.getListConstraints())) { LinkedHashMap<String, String> enumList = new LinkedHashMap<>(); String[] listArray = modelUtil.getListConstraints().split("#"); for (String str : listArray) { String[] strArr = str.split("="); if (strArr.length > 1) { enumList.put(strArr[0], strArr[1]); } } optimizationAttributes.setEnumType(enumList); } classMap = new LinkedHashMap<>(); classMap.put(className, optimizationAttributes); } response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); response.setContentType(PolicyUtils.APPLICATION_JSON); request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING); ObjectMapper mapper = new ObjectMapper(); JSONObject json = new JSONObject(); json.put("classListDatas", modelList); json.put("modelDatas", mapper.writeValueAsString(classMap)); json.put("modelType", modelType); json.put("dataOrderInfo", modelUtil.getDataOrderInfo()); response.getWriter().write(json.toString()); } @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 testSetModelData() { logger.debug("testSetModelData: enter"); CreateOptimizationController controller = new CreateOptimizationController(); HttpServletRequest request = createMock(HttpServletRequest.class); MockHttpServletResponse response = new MockHttpServletResponse(); expect(request.getContentType()) .andReturn("multipart/form-data; boundary=----WebKitFormBoundaryWcRUaIbC8kXgjr3p"); expect(request.getMethod()).andReturn("post"); expect(request.getHeader("Content-length")).andReturn("7809"); expect(request.getContentLength()).andReturn(7809); try { String fileName = ""; try { ClassLoader classLoader = getClass().getClassLoader(); fileName = new File(classLoader.getResource("schedulerPolicies-v1707.xmi").getFile()).getAbsolutePath(); } catch (Exception e1) { logger.error("Exception Occured while loading file" + e1); } expect(request.getInputStream()).andReturn(new MockServletInputStream(fileName)); expect(request.getCharacterEncoding()).andReturn("UTF-8"); expect(request.getContentLength()).andReturn(1024); replay(request); controller.setModelData(request, response); } catch (Exception e) { logger.error("testSetModelData" + e); e.printStackTrace(); } logger.debug("testSetModelData: exit"); }