@Test(description = "Test openapi gen-service for invalid ballerina project", enabled = false) public void testIsLocationBallerinaProject() { String[] args = {"petsModule:petService", "../petstore.yml"}; OpenApiGenServiceCmd cmd = new OpenApiGenServiceCmd(printStream, tmpDir.toString()); new CommandLine(cmd).parseArgs(args); String output = ""; try { cmd.execute(); } catch (BLauncherException e) { output = e.getDetailedMessages().get(0); } Assert.assertTrue(output.contains("Ballerina service generation should be done from the project root.")); }
@Override public void execute() { outStream.println(OpenApiMesseges.EXPERIMENTAL_FEATURE); CodeGenerator generator = new CodeGenerator(); List<String> tag = new ArrayList<>(); List<String> operation = new ArrayList<>(); Filter filter = new Filter(tag, operation); tag.add("pet"); operation.add("operation"); if (helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName()); outStream.println(commandUsageInfo); return; } if (moduleArgs == null || moduleArgs.size() < 2) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_ARGS_REQUIRED); } else { if (moduleArgs.get(0).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_REQUIRED); } else if (moduleArgs.get(1).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_SERVICE_NAME_REQUIRED); } } if (argList == null) { throw LauncherUtils.createLauncherException("An OpenApi definition file is required to generate the " + "service. \nE.g: ballerina openapi gen-service " + moduleArgs.get(0) + ":" + moduleArgs.get(1) + " <OpenApiContract>"); } final Path projectRoot = ProjectDirs.findProjectRoot(Paths.get(executionPath)); if (projectRoot == null) { throw LauncherUtils.createLauncherException(GEN_SERVICE_PROJECT_ROOT); } final Path sourceDirectory = projectRoot.resolve("src"); final Path moduleDirectory = sourceDirectory.resolve(moduleArgs.get(0)); final Path resourcesDirectory = Paths.get(moduleDirectory + "/resources"); final Path testsDirectory = Paths.get(moduleDirectory + "/tests"); final File openApiFile = new File(argList.get(0)); final String openApiFilePath = openApiFile.getPath(); Path resourcePath = Paths.get(resourcesDirectory + "/" + openApiFile.getName()); Path relativeResourcePath = Paths.get("resources", openApiFile.getName()); if (Files.notExists(Paths.get(openApiFilePath))) { throw LauncherUtils.createLauncherException("Could not resolve a valid OpenApi" + " contract in " + openApiFilePath); } if (Files.notExists(moduleDirectory)) { try { Files.createDirectory(moduleDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } File moduleMd = new File(moduleDirectory + "/Module.md"); if (!moduleMd.exists()) { try { boolean createMd = true; createMd = moduleMd.createNewFile(); if (!createMd) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION); } } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(testsDirectory)) { try { Files.createDirectory(testsDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(TESTS_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (skipBind) { try { resourcePath = Paths.get(openApiFile.getCanonicalPath()); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { if (Files.notExists(resourcesDirectory)) { try { Files.createDirectory(resourcesDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(RESOURCE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(resourcePath)) { try { Files.copy(Paths.get(openApiFilePath), resourcePath); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { throw LauncherUtils.createLauncherException(DEFINITION_EXISTS + " " + resourcesDirectory); } } if (output.isEmpty()) { output = executionPath; } generator.setSrcPackage(moduleArgs.get(0)); try { generator.generateService(executionPath, resourcePath.toString(), relativeResourcePath.toString(), moduleArgs.get(1), output, filter); } catch (IOException | BallerinaOpenApiException e) { throw LauncherUtils.createLauncherException("Error occurred when generating service for openapi " + "contract at " + argList.get(0) + ". " + e.getMessage() + "."); } }
OpenApiGenServiceCmd implements BLauncherCmd { @Override public void execute() { outStream.println(OpenApiMesseges.EXPERIMENTAL_FEATURE); CodeGenerator generator = new CodeGenerator(); List<String> tag = new ArrayList<>(); List<String> operation = new ArrayList<>(); Filter filter = new Filter(tag, operation); tag.add("pet"); operation.add("operation"); if (helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName()); outStream.println(commandUsageInfo); return; } if (moduleArgs == null || moduleArgs.size() < 2) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_ARGS_REQUIRED); } else { if (moduleArgs.get(0).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_REQUIRED); } else if (moduleArgs.get(1).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_SERVICE_NAME_REQUIRED); } } if (argList == null) { throw LauncherUtils.createLauncherException("An OpenApi definition file is required to generate the " + "service. \nE.g: ballerina openapi gen-service " + moduleArgs.get(0) + ":" + moduleArgs.get(1) + " <OpenApiContract>"); } final Path projectRoot = ProjectDirs.findProjectRoot(Paths.get(executionPath)); if (projectRoot == null) { throw LauncherUtils.createLauncherException(GEN_SERVICE_PROJECT_ROOT); } final Path sourceDirectory = projectRoot.resolve("src"); final Path moduleDirectory = sourceDirectory.resolve(moduleArgs.get(0)); final Path resourcesDirectory = Paths.get(moduleDirectory + "/resources"); final Path testsDirectory = Paths.get(moduleDirectory + "/tests"); final File openApiFile = new File(argList.get(0)); final String openApiFilePath = openApiFile.getPath(); Path resourcePath = Paths.get(resourcesDirectory + "/" + openApiFile.getName()); Path relativeResourcePath = Paths.get("resources", openApiFile.getName()); if (Files.notExists(Paths.get(openApiFilePath))) { throw LauncherUtils.createLauncherException("Could not resolve a valid OpenApi" + " contract in " + openApiFilePath); } if (Files.notExists(moduleDirectory)) { try { Files.createDirectory(moduleDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } File moduleMd = new File(moduleDirectory + "/Module.md"); if (!moduleMd.exists()) { try { boolean createMd = true; createMd = moduleMd.createNewFile(); if (!createMd) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION); } } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(testsDirectory)) { try { Files.createDirectory(testsDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(TESTS_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (skipBind) { try { resourcePath = Paths.get(openApiFile.getCanonicalPath()); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { if (Files.notExists(resourcesDirectory)) { try { Files.createDirectory(resourcesDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(RESOURCE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(resourcePath)) { try { Files.copy(Paths.get(openApiFilePath), resourcePath); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { throw LauncherUtils.createLauncherException(DEFINITION_EXISTS + " " + resourcesDirectory); } } if (output.isEmpty()) { output = executionPath; } generator.setSrcPackage(moduleArgs.get(0)); try { generator.generateService(executionPath, resourcePath.toString(), relativeResourcePath.toString(), moduleArgs.get(1), output, filter); } catch (IOException | BallerinaOpenApiException e) { throw LauncherUtils.createLauncherException("Error occurred when generating service for openapi " + "contract at " + argList.get(0) + ". " + e.getMessage() + "."); } } }
OpenApiGenServiceCmd implements BLauncherCmd { @Override public void execute() { outStream.println(OpenApiMesseges.EXPERIMENTAL_FEATURE); CodeGenerator generator = new CodeGenerator(); List<String> tag = new ArrayList<>(); List<String> operation = new ArrayList<>(); Filter filter = new Filter(tag, operation); tag.add("pet"); operation.add("operation"); if (helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName()); outStream.println(commandUsageInfo); return; } if (moduleArgs == null || moduleArgs.size() < 2) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_ARGS_REQUIRED); } else { if (moduleArgs.get(0).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_REQUIRED); } else if (moduleArgs.get(1).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_SERVICE_NAME_REQUIRED); } } if (argList == null) { throw LauncherUtils.createLauncherException("An OpenApi definition file is required to generate the " + "service. \nE.g: ballerina openapi gen-service " + moduleArgs.get(0) + ":" + moduleArgs.get(1) + " <OpenApiContract>"); } final Path projectRoot = ProjectDirs.findProjectRoot(Paths.get(executionPath)); if (projectRoot == null) { throw LauncherUtils.createLauncherException(GEN_SERVICE_PROJECT_ROOT); } final Path sourceDirectory = projectRoot.resolve("src"); final Path moduleDirectory = sourceDirectory.resolve(moduleArgs.get(0)); final Path resourcesDirectory = Paths.get(moduleDirectory + "/resources"); final Path testsDirectory = Paths.get(moduleDirectory + "/tests"); final File openApiFile = new File(argList.get(0)); final String openApiFilePath = openApiFile.getPath(); Path resourcePath = Paths.get(resourcesDirectory + "/" + openApiFile.getName()); Path relativeResourcePath = Paths.get("resources", openApiFile.getName()); if (Files.notExists(Paths.get(openApiFilePath))) { throw LauncherUtils.createLauncherException("Could not resolve a valid OpenApi" + " contract in " + openApiFilePath); } if (Files.notExists(moduleDirectory)) { try { Files.createDirectory(moduleDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } File moduleMd = new File(moduleDirectory + "/Module.md"); if (!moduleMd.exists()) { try { boolean createMd = true; createMd = moduleMd.createNewFile(); if (!createMd) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION); } } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(testsDirectory)) { try { Files.createDirectory(testsDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(TESTS_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (skipBind) { try { resourcePath = Paths.get(openApiFile.getCanonicalPath()); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { if (Files.notExists(resourcesDirectory)) { try { Files.createDirectory(resourcesDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(RESOURCE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(resourcePath)) { try { Files.copy(Paths.get(openApiFilePath), resourcePath); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { throw LauncherUtils.createLauncherException(DEFINITION_EXISTS + " " + resourcesDirectory); } } if (output.isEmpty()) { output = executionPath; } generator.setSrcPackage(moduleArgs.get(0)); try { generator.generateService(executionPath, resourcePath.toString(), relativeResourcePath.toString(), moduleArgs.get(1), output, filter); } catch (IOException | BallerinaOpenApiException e) { throw LauncherUtils.createLauncherException("Error occurred when generating service for openapi " + "contract at " + argList.get(0) + ". " + e.getMessage() + "."); } } OpenApiGenServiceCmd();  OpenApiGenServiceCmd(PrintStream outStream, String executionPath); }
OpenApiGenServiceCmd implements BLauncherCmd { @Override public void execute() { outStream.println(OpenApiMesseges.EXPERIMENTAL_FEATURE); CodeGenerator generator = new CodeGenerator(); List<String> tag = new ArrayList<>(); List<String> operation = new ArrayList<>(); Filter filter = new Filter(tag, operation); tag.add("pet"); operation.add("operation"); if (helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName()); outStream.println(commandUsageInfo); return; } if (moduleArgs == null || moduleArgs.size() < 2) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_ARGS_REQUIRED); } else { if (moduleArgs.get(0).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_REQUIRED); } else if (moduleArgs.get(1).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_SERVICE_NAME_REQUIRED); } } if (argList == null) { throw LauncherUtils.createLauncherException("An OpenApi definition file is required to generate the " + "service. \nE.g: ballerina openapi gen-service " + moduleArgs.get(0) + ":" + moduleArgs.get(1) + " <OpenApiContract>"); } final Path projectRoot = ProjectDirs.findProjectRoot(Paths.get(executionPath)); if (projectRoot == null) { throw LauncherUtils.createLauncherException(GEN_SERVICE_PROJECT_ROOT); } final Path sourceDirectory = projectRoot.resolve("src"); final Path moduleDirectory = sourceDirectory.resolve(moduleArgs.get(0)); final Path resourcesDirectory = Paths.get(moduleDirectory + "/resources"); final Path testsDirectory = Paths.get(moduleDirectory + "/tests"); final File openApiFile = new File(argList.get(0)); final String openApiFilePath = openApiFile.getPath(); Path resourcePath = Paths.get(resourcesDirectory + "/" + openApiFile.getName()); Path relativeResourcePath = Paths.get("resources", openApiFile.getName()); if (Files.notExists(Paths.get(openApiFilePath))) { throw LauncherUtils.createLauncherException("Could not resolve a valid OpenApi" + " contract in " + openApiFilePath); } if (Files.notExists(moduleDirectory)) { try { Files.createDirectory(moduleDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } File moduleMd = new File(moduleDirectory + "/Module.md"); if (!moduleMd.exists()) { try { boolean createMd = true; createMd = moduleMd.createNewFile(); if (!createMd) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION); } } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(testsDirectory)) { try { Files.createDirectory(testsDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(TESTS_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (skipBind) { try { resourcePath = Paths.get(openApiFile.getCanonicalPath()); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { if (Files.notExists(resourcesDirectory)) { try { Files.createDirectory(resourcesDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(RESOURCE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(resourcePath)) { try { Files.copy(Paths.get(openApiFilePath), resourcePath); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { throw LauncherUtils.createLauncherException(DEFINITION_EXISTS + " " + resourcesDirectory); } } if (output.isEmpty()) { output = executionPath; } generator.setSrcPackage(moduleArgs.get(0)); try { generator.generateService(executionPath, resourcePath.toString(), relativeResourcePath.toString(), moduleArgs.get(1), output, filter); } catch (IOException | BallerinaOpenApiException e) { throw LauncherUtils.createLauncherException("Error occurred when generating service for openapi " + "contract at " + argList.get(0) + ". " + e.getMessage() + "."); } } OpenApiGenServiceCmd();  OpenApiGenServiceCmd(PrintStream outStream, String executionPath); @Override void execute(); @Override String getName(); @Override void printLongDesc(StringBuilder out); @Override void printUsage(StringBuilder out); @Override void setParentCmdParser(CommandLine parentCmdParser); }
OpenApiGenServiceCmd implements BLauncherCmd { @Override public void execute() { outStream.println(OpenApiMesseges.EXPERIMENTAL_FEATURE); CodeGenerator generator = new CodeGenerator(); List<String> tag = new ArrayList<>(); List<String> operation = new ArrayList<>(); Filter filter = new Filter(tag, operation); tag.add("pet"); operation.add("operation"); if (helpFlag) { String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(getName()); outStream.println(commandUsageInfo); return; } if (moduleArgs == null || moduleArgs.size() < 2) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_ARGS_REQUIRED); } else { if (moduleArgs.get(0).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_MODULE_REQUIRED); } else if (moduleArgs.get(1).trim().isEmpty()) { throw LauncherUtils.createLauncherException(GEN_SERVICE_SERVICE_NAME_REQUIRED); } } if (argList == null) { throw LauncherUtils.createLauncherException("An OpenApi definition file is required to generate the " + "service. \nE.g: ballerina openapi gen-service " + moduleArgs.get(0) + ":" + moduleArgs.get(1) + " <OpenApiContract>"); } final Path projectRoot = ProjectDirs.findProjectRoot(Paths.get(executionPath)); if (projectRoot == null) { throw LauncherUtils.createLauncherException(GEN_SERVICE_PROJECT_ROOT); } final Path sourceDirectory = projectRoot.resolve("src"); final Path moduleDirectory = sourceDirectory.resolve(moduleArgs.get(0)); final Path resourcesDirectory = Paths.get(moduleDirectory + "/resources"); final Path testsDirectory = Paths.get(moduleDirectory + "/tests"); final File openApiFile = new File(argList.get(0)); final String openApiFilePath = openApiFile.getPath(); Path resourcePath = Paths.get(resourcesDirectory + "/" + openApiFile.getName()); Path relativeResourcePath = Paths.get("resources", openApiFile.getName()); if (Files.notExists(Paths.get(openApiFilePath))) { throw LauncherUtils.createLauncherException("Could not resolve a valid OpenApi" + " contract in " + openApiFilePath); } if (Files.notExists(moduleDirectory)) { try { Files.createDirectory(moduleDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } File moduleMd = new File(moduleDirectory + "/Module.md"); if (!moduleMd.exists()) { try { boolean createMd = true; createMd = moduleMd.createNewFile(); if (!createMd) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION); } } catch (IOException e) { throw LauncherUtils.createLauncherException(MODULE_MD_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(testsDirectory)) { try { Files.createDirectory(testsDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(TESTS_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (skipBind) { try { resourcePath = Paths.get(openApiFile.getCanonicalPath()); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { if (Files.notExists(resourcesDirectory)) { try { Files.createDirectory(resourcesDirectory); } catch (IOException e) { throw LauncherUtils.createLauncherException(RESOURCE_DIRECTORY_EXCEPTION + "\n" + e.getLocalizedMessage()); } } if (Files.notExists(resourcePath)) { try { Files.copy(Paths.get(openApiFilePath), resourcePath); } catch (IOException e) { throw LauncherUtils.createLauncherException(e.getLocalizedMessage()); } } else { throw LauncherUtils.createLauncherException(DEFINITION_EXISTS + " " + resourcesDirectory); } } if (output.isEmpty()) { output = executionPath; } generator.setSrcPackage(moduleArgs.get(0)); try { generator.generateService(executionPath, resourcePath.toString(), relativeResourcePath.toString(), moduleArgs.get(1), output, filter); } catch (IOException | BallerinaOpenApiException e) { throw LauncherUtils.createLauncherException("Error occurred when generating service for openapi " + "contract at " + argList.get(0) + ". " + e.getMessage() + "."); } } OpenApiGenServiceCmd();  OpenApiGenServiceCmd(PrintStream outStream, String executionPath); @Override void execute(); @Override String getName(); @Override void printLongDesc(StringBuilder out); @Override void printUsage(StringBuilder out); @Override void setParentCmdParser(CommandLine parentCmdParser);  }