OptimizationDictionaryController { @RequestMapping(value = {"/oof_dictionary/save_model"}, method = {RequestMethod.POST}) public ModelAndView saveOptimizationModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException { DictionaryUtils dUtils = getDictionaryUtilsInstance(); try { this.newModel = new OptimizationModels(); boolean fromAPI = dUtils.isRequestFromAPI(request); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); OptimizationModels optimizationModels = new OptimizationModels(); String userId = null; String dataOrderInfo = null; if (root.has("dataOrderInfo")) { dataOrderInfo = root.get("dataOrderInfo").toString(); } if (root.has("modelType")) { JsonNode dataType = root.get("modelType"); String modelType = dataType.toString(); if (modelType.contains("yml")) { if (root.has(optimizationModelsDictionaryData)) { if (root.get(optimizationModelsDictionaryData).has(description)) { optimizationModels.setDescription(root.get(optimizationModelsDictionaryData) .get(description).asText().replace("\"", "")); } if (root.get(optimizationModelsDictionaryData).has(modelName)) { optimizationModels.setModelName(root.get(optimizationModelsDictionaryData).get(modelName) .asText().replace("\"", "")); this.newModel.setModelName(optimizationModels.getModelName()); } if (root.get(optimizationModelsDictionaryData).has(version)) { optimizationModels.setVersion( root.get(optimizationModelsDictionaryData).get(version).asText().replace("\"", "")); this.newModel.setVersion(optimizationModels.getVersion()); } } classMap = new LinkedHashMap<>(); JsonNode data = root.get(classMapData); ObjectMapper mapper1 = new ObjectMapper(); String data1 = data.toString().substring(1, data.toString().length() - 1); data1 = data1.replace("\\", ""); data1 = data1.replace("\"{", "{"); data1 = data1.replace("}\"", "}"); JSONObject jsonObject = new JSONObject(data1); Set<String> keys = jsonObject.keySet(); for (String key : keys) { String value = jsonObject.get(key).toString(); MsAttributeObject msAttributeObject = mapper1.readValue(value, MsAttributeObject.class); classMap.put(key, msAttributeObject); } userId = root.get("userid").textValue(); MsAttributeObject mainClass = classMap.get(this.newModel.getModelName()); this.newModel.setDependency("[]"); String value = new Gson().toJson(mainClass.getSubClass()); this.newModel.setSubattributes(value); String attributes = mainClass.getAttribute().toString().replace("{", "").replace("}", ""); int equalsIndexForAttributes = attributes.indexOf('='); String atttributesAfterFirstEquals = attributes.substring(equalsIndexForAttributes + 1, attributes.length() - 1); this.newModel.setAttributes(atttributesAfterFirstEquals); String refAttributes = mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""); int equalsIndex = refAttributes.indexOf('='); String refAttributesAfterFirstEquals = refAttributes.substring(equalsIndex + 1, refAttributes.length() - 1); this.newModel.setRefattributes(refAttributesAfterFirstEquals); this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", "")); this.newModel .setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", "")); } else { if (fromAPI) { optimizationModels = mapper.readValue(root.get(dictionaryFields).toString(), OptimizationModels.class); userId = "API"; } else { if (root.has(optimizationModelsDictionaryData)) { if (root.get(optimizationModelsDictionaryData).has(description)) { optimizationModels.setDescription(root.get(optimizationModelsDictionaryData) .get(description).asText().replace("\"", "")); } if (root.get(optimizationModelsDictionaryData).has(modelName)) { optimizationModels.setModelName(root.get(optimizationModelsDictionaryData) .get(modelName).asText().replace("\"", "")); this.newModel.setModelName(optimizationModels.getModelName()); } if (root.get(optimizationModelsDictionaryData).has(version)) { optimizationModels.setVersion(root.get(optimizationModelsDictionaryData).get(version) .asText().replace("\"", "")); this.newModel.setVersion(optimizationModels.getVersion()); } } if (root.has(classMapData)) { classMap = new LinkedHashMap<>(); JsonNode data = root.get(classMapData); ObjectMapper mapper1 = new ObjectMapper(); String data1 = data.toString().substring(1, data.toString().length() - 1); data1 = data1.replace("\\", ""); JSONObject jsonObject = new JSONObject(data1); Set<String> keys = jsonObject.keySet(); for (String key : keys) { String value = jsonObject.get(key).toString(); MsAttributeObject msAttributeObject = mapper1.readValue(value, MsAttributeObject.class); classMap.put(key, msAttributeObject); } } userId = root.get("userid").textValue(); addValuesToNewModel(classMap); } } } optimizationModels.setAttributes(this.newModel.getAttributes()); optimizationModels.setRefattributes(this.newModel.getRefattributes()); optimizationModels.setDependency(this.newModel.getDependency()); optimizationModels.setModelName(this.newModel.getModelName()); optimizationModels.setSubattributes(this.newModel.getSubattributes()); optimizationModels.setVersion(this.newModel.getVersion()); optimizationModels.setEnumValues(this.newModel.getEnumValues()); optimizationModels.setAnnotation(this.newModel.getAnnotation()); if (dataOrderInfo != null) { optimizationModels.setDataOrderInfo(dataOrderInfo); } String checkName = optimizationModels.getModelName() + ":" + optimizationModels.getVersion(); List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", OptimizationModels.class); boolean duplicateflag = false; if (duplicateData != null && !duplicateData.isEmpty()) { OptimizationModels data = (OptimizationModels) duplicateData.get(0); if (request.getParameter(operation) != null && UPDATE.equals(request.getParameter(operation))) { optimizationModels.setId(data.getId()); } else if ((request.getParameter(operation) != null && !UPDATE.equals(request.getParameter(operation))) || (request.getParameter(operation) == null && (data.getId() != optimizationModels.getId()))) { duplicateflag = true; } } UserInfo userInfo = dUtils.getUserInfo(userId); String responseString = null; if (!duplicateflag) { optimizationModels.setUserCreatedBy(userInfo); if (optimizationModels.getId() == 0) { commonClassDao.save(optimizationModels); } else { commonClassDao.update(optimizationModels); } responseString = mapper.writeValueAsString(commonClassDao.getData(OptimizationModels.class)); } else { responseString = duplicateResponseString; } if (fromAPI) { return dUtils.getResultForApi(responseString); } else { dUtils.setResponseData(response, optimizationModelsDictionaryDatas, responseString); } } catch (Exception e) { dUtils.setErrorResponseData(response, e); } return null; } OptimizationDictionaryController(); @Autowired  OptimizationDictionaryController(CommonClassDao commonClassDao); static void setCommonClassDao(CommonClassDao commonClassDao); @RequestMapping( value = {"/get_OptimizationModelsData"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getOptimizationModelsDictionaryEntityData(HttpServletResponse response); @RequestMapping( value = {"/get_OptimizationModelsDataByName"}, method = {RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE) void getOptimizationModelsDictionaryByNameEntityData(HttpServletResponse response); @RequestMapping(value = {"/oof_dictionary/save_model"}, method = {RequestMethod.POST}) ModelAndView saveOptimizationModelsDictionary(HttpServletRequest request, HttpServletResponse response); @RequestMapping(value = {"/oof_dictionary/remove_model"}, method = {RequestMethod.POST}) void removeOptimizationModelsDictionary(HttpServletRequest request, HttpServletResponse response);  }
@Test public void testSaveOptimizationModelsDictionary() { logger.info("testSaveOptimizationModelsDictionary: Entering"); MockHttpServletResponse response = new MockHttpServletResponse(); request = mock(HttpServletRequest.class); try { when(request.getReader()).thenReturn(br); controller.saveOptimizationModelsDictionary(request, response); logger.info("response.getContentAsString(): " + response.getContentAsString()); assertTrue(response.getContentAsString() != null && response.getContentAsString().contains("optimizationModelsDictionaryDatas")); } catch (Exception e) { fail("Exception: " + e); } logger.info("testSaveOptimizationModelsDictionary: exit"); }
@Test public void testSave() { OptimizationDictionaryController controller = new OptimizationDictionaryController(commonClassDao); MockHttpServletRequest req = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); req.setBodyContent("{\n\"modelType\": \"type.yml\", \"dataOrderInfo\": \"info\", \"userid\": \"id\", " + "\"optimizationModelsDictionaryData\": {\"description\": \"desc\", \"modelName\": \"name\", \"version\": \"1.0\"}, " + "\"classMap\": \"{\\\"dep\\\":\\\"{\\\"dependency\\\":\\\"depval\\\"}\\\"}\" }\n"); assertThatThrownBy(() -> controller.saveOptimizationModelsDictionary(req, response)) .isInstanceOf(NullPointerException.class); req.setBodyContent("{\n\"modelType\": \"type.xml\", \"dataOrderInfo\": \"info\", \"userid\": \"id\", " + "\"optimizationModelsDictionaryData\": {\"description\": \"desc\", \"modelName\": \"name\", \"version\": \"1.0\"}, " + "\"classMap\": \"{\\\"dep\\\": {\\\"dependency\\\":\\\"depval\\\"} }\" }\n"); assertThatCode(() -> controller.saveOptimizationModelsDictionary(req, response)).doesNotThrowAnyException(); req.setupAddParameter("apiflag", "api"); assertThatThrownBy(() -> controller.saveOptimizationModelsDictionary(req, response)) .isInstanceOf(NullPointerException.class); }