PostmanConverter { public boolean convert(final String importFile, final String outputDir) { try { final Path pathTo = Paths.get(importFile); if (!Files.exists(pathTo)) { System.err.println("File at '" + importFile + "' does not exist; cannot import"); return false; } final String content = new String(Files.readAllBytes(pathTo), StandardCharsets.UTF_8); final List<PostmanItem> items = PostmanUtils.readPostmanJson(content); final String collectionName = pathTo.getFileName().toString() .replace(".postman_collection", "") .replace(".json", ""); final String converted = PostmanUtils.toKarateFeature(collectionName, items); final Path outputFilePath = Paths.get(outputDir, collectionName + ".feature"); Files.write(outputFilePath, converted.getBytes()); System.out.println("Converted feature file available at " + outputFilePath.toAbsolutePath().toString()); return true; } catch (IOException e) { System.err.println("An error occurred with processing the file - the task could not be completed"); return false; } }  boolean convert(final String importFile, final String outputDir);  }
@Test public void testSuccess() throws IOException { if (FileUtils.isOsWindows()) { return; } File tempSource = File.createTempFile("karate-postman-input", ".postman_collection.json"); tempSource.deleteOnExit(); Path tempOutput = Files.createTempDirectory("karate-postman-output"); tempOutput.toFile().deleteOnExit(); InputStream is = getClass().getResourceAsStream("postman-echo-single.postman_collection"); String postman = FileUtils.toString(is); Files.write(Paths.get(tempSource.toURI()), postman.getBytes()); boolean successful = new PostmanConverter().convert(tempSource.toString(), tempOutput.toString()); Assert.assertTrue(successful); is = getClass().getResourceAsStream("expected-converted.txt"); String expectedConverted = FileUtils.toString(is); Path actualOutputPath = Paths.get(tempOutput.toString(), tempSource.getName().replace(".postman_collection.json", "") + ".feature"); String converted = new String(Files.readAllBytes(actualOutputPath), StandardCharsets.UTF_8); Assert.assertTrue(converted.startsWith("Feature: karate-postman-input")); String convertedTrimmed = Arrays.stream(converted.split(System.lineSeparator())) .filter(line -> !line.startsWith("Feature:")) .collect(Collectors.joining(System.lineSeparator())); Assert.assertEquals(convertedTrimmed.trim(), expectedConverted.trim()); }
@Test public void testInvalidSourcePath() { boolean successful = new PostmanConverter().convert("does-not-exist_1234567890", "./"); Assert.assertFalse(successful); }