ImportService { public void doImportMicroServicePut(HttpServletRequest request, HttpServletResponse response) { String importServiceCreation = request.getParameter("importService"); String fileName = request.getParameter("fileName"); String version = request.getParameter("version"); String serviceName = request.getParameter("serviceName"); if (serviceName == null || serviceName.isEmpty() || !serviceName.matches(REGEX)) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader(errorMsg, MISSING); response.addHeader(operation, importHeader); response.addHeader(service, invalidServiceName); return; } String description = request.getParameter("description"); Map<String, String> successMap = new HashMap<>(); if (("BRMSPARAM").equals(importServiceCreation)) { StringBuilder builder = new StringBuilder(); int ch; try { ServletInputStream stream = request.getInputStream(); while ((ch = stream.read()) != -1) { builder.append((char) ch); } } catch (IOException e) { logger.error(e); PolicyLogger.error(errorMessage); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(errorMsg, MISSING); response.addHeader(operation, importHeader); response.addHeader(service, serviceName); } CreateBRMSRuleTemplate brmsRuleTemplate = new CreateBRMSRuleTemplate(); successMap = brmsRuleTemplate.addRule(builder.toString(), serviceName, description, "API"); } else if (("MICROSERVICE").equals(importServiceCreation)) { CreateNewMicroServiceModel newMS = null; String randomID = UUID.randomUUID().toString(); String type = ".xmi"; if (fileName != null) { File extracDir = new File(extractDir); if (!extracDir.exists()) { extracDir.mkdirs(); } if (fileName.contains(".xmi") || fileName.contains(".yml")) { if (fileName.contains(".yml")) { type = ".yml"; } String xmi = null; java.util.Scanner scanner; try { scanner = new java.util.Scanner(request.getInputStream()); scanner.useDelimiter("\\A"); xmi = scanner.hasNext() ? scanner.next() : ""; scanner.close(); } catch (IOException e1) { logger.error(e1); PolicyLogger.error(errorMessage); return; } PolicyLogger.info("Request from API to import new Service"); try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(extractDir + File.separator + randomID + type), "utf-8"))) { writer.write(xmi); } catch (IOException e) { logger.error(e); PolicyLogger.error(errorMessage); return; } } else { InputStream inputStream = null; try (FileOutputStream outputStream = new FileOutputStream(extractDir + File.separator + randomID + ".zip")) { inputStream = request.getInputStream(); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } catch (IOException e) { PolicyLogger.error("Error in reading in Zip File from API call" + e); return; } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { PolicyLogger.error("Exception Occured while closing the input/output stream" + e); } } } newMS = new CreateNewMicroServiceModel(fileName, serviceName, "API", version, randomID); successMap = newMS.addValuesToNewModel(type); if (successMap.containsKey(successMessage)) { successMap.clear(); successMap = newMS.saveImportService(); } } } else if (("OPTIMIZATION").equals(importServiceCreation)) { CreateNewOptimizationModel newOOF = null; String randomID = UUID.randomUUID().toString(); if (fileName != null) { File extracDir = new File(extractDir); if (!extracDir.exists()) { extracDir.mkdirs(); } String type = ".yml"; String yml = null; try (java.util.Scanner scanner = new java.util.Scanner(request.getInputStream());) { scanner.useDelimiter("\\A"); yml = scanner.hasNext() ? scanner.next() : ""; } catch (IOException e1) { logger.error(e1); PolicyLogger.error(errorMessage); return; } PolicyLogger.info("Request from API to import new Optimization Service Model"); try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(extractDir + File.separator + randomID + type), "utf-8"))) { writer.write(yml); } catch (IOException e) { logger.error(e); PolicyLogger.error(errorMessage); return; } newOOF = new CreateNewOptimizationModel(fileName, serviceName, "API Import Service", version, randomID); successMap = newOOF.addValuesToNewModel(); if (successMap.containsKey(successMessage)) { successMap.clear(); successMap = newOOF.saveImportService(); } } } if (successMap.containsKey(successMessage)) { response.setStatus(HttpServletResponse.SC_OK); response.addHeader("successMapKey", successMessage); response.addHeader(operation, importHeader); response.addHeader(service, serviceName); } else if (successMap.containsKey("DBError")) { if (successMap.get("DBError").contains("EXISTS")) { response.setStatus(HttpServletResponse.SC_CONFLICT); response.addHeader(service, serviceName); response.addHeader(errorMsg, "modelExistsDB"); } else { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(errorMsg, "importDB"); } response.addHeader(operation, importHeader); response.addHeader(service, serviceName); } else if (successMap.get(errorMsg).contains("MISSING")) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(errorMsg, MISSING); response.addHeader(operation, importHeader); response.addHeader(service, serviceName); } else if (successMap.get(errorMsg).contains("VALIDATION")) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.addHeader(errorMsg, "validation"); response.addHeader(operation, importHeader); response.addHeader(service, serviceName); } }  void doImportMicroServicePut(HttpServletRequest request, HttpServletResponse response);  }
@Test public void testNegativeCases() { ImportService service = new ImportService(); HttpServletRequest request = new MockHttpServletRequest(); HttpServletResponse response = new MockHttpServletResponse(); service.doImportMicroServicePut(request, response); assertEquals("missing", response.getHeader("error")); }
@Test public void testImportBRMS() { ImportService service = new ImportService(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setupAddParameter("serviceName", "serviceName"); request.setupAddParameter("importService", "BRMSPARAM"); request.setBodyContent("foo"); HttpServletResponse response = new MockHttpServletResponse(); assertThatCode(() -> service.doImportMicroServicePut(request, response)).doesNotThrowAnyException(); }
@Test public void testImportMS() { ImportService service = new ImportService(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setupAddParameter("serviceName", "serviceName"); request.setupAddParameter("importService", "MICROSERVICE"); request.setupAddParameter("fileName", "fileName"); request.setBodyContent("foo"); HttpServletResponse response = new MockHttpServletResponse(); assertThatThrownBy(() -> service.doImportMicroServicePut(request, response)).isInstanceOf(Exception.class); }
@Test public void testImportOpt() { ImportService service = new ImportService(); MockHttpServletRequest request = new MockHttpServletRequest(); request.setupAddParameter("serviceName", "serviceName"); request.setupAddParameter("importService", "OPTIMIZATION"); request.setupAddParameter("fileName", "fileName"); request.setBodyContent("foo"); HttpServletResponse response = new MockHttpServletResponse(); assertThatThrownBy(() -> service.doImportMicroServicePut(request, response)).isInstanceOf(Exception.class); }