FeatureParser { public static List<Feature> parse(String json) { List<Feature> features = null; try (InputStreamReader is = new InputStreamReader(new FileInputStream(json),"UTF-8")) { features = new ObjectMapper().readValue(is, new TypeReference<List<Feature>>() { }); Iterator<Feature> it = features.iterator(); while (it.hasNext()) { Feature feature = it.next(); if (!feature.isCucumberFeature()) { log.warning("json:" + json + " is NOT a Cucumber feature result file and will be ignored"); it.remove(); }else{ feature.initScenarios(); feature.processSteps(); } } } catch (FileNotFoundException e) { log.log(Level.WARNING, "Could not find json file:" + json); } catch (JsonMappingException e) { log.log(Level.WARNING, "Could not map json file:" + json); } catch (JsonParseException e) { log.log(Level.WARNING, "Could not parse json file:" + json); } catch (IOException e) { log.log(Level.WARNING, "Could not read json file:" + json); } return features; }  static List<Feature> parse(String json); static List<Feature> parse(List<String> paths); static List<Feature> parse(String... paths); static List<Feature> findAndParse(String basePath);  }
@Test public void shouldParseFeature() throws IOException { String path = FileUtil.findJsonFile("target/test-classes/json-output/" + onePassingOneFailing); List<Feature> features = FeatureParser.parse(path); assertThat(features).isNotNull().hasSize(1).contains(FeatureBuilder.instance().name("One passing scenario, one failing scenario").id("one-passing-scenario,-one-failing-scenario").build()); }
@Test public void shouldParseFeatureUsingResourcePath() throws IOException { URL featureFile = getClass().getResource("/json-output/" + onePassingOneFailing); String path = FileUtil.findJsonFile(featureFile.getPath()); List<Feature> features = FeatureParser.parse(path); assertThat(features).isNotNull().hasSize(1).contains(FeatureBuilder.instance().name("One passing scenario, one failing scenario").id("one-passing-scenario,-one-failing-scenario").build()); }
@Test public void shouldParseFeatureUsingLeadingSlash() throws IOException { String path = FileUtil.findJsonFile("/target/test-classes/json-output/" + onePassingOneFailing); List<Feature> features = FeatureParser.parse(path); assertThat(features).isNotNull().hasSize(1).contains(FeatureBuilder.instance().name("One passing scenario, one failing scenario").id("one-passing-scenario,-one-failing-scenario").build()); }
@Test public void shouldParseFeaturesInDir() throws IOException { List<String> paths = FileUtil.findJsonFiles("target/test-classes/json-output/parser"); assertThat(paths).hasSize(6); List<Feature> features = FeatureParser.parse(paths); assertThat(features).hasSize(4).contains(FeatureBuilder.instance().name("An embed data directly feature").id("an-embed-data-directly-feature").build()); }
@Test public void shouldParseFeatureWithDocstring() throws IOException { List<Feature> features = FeatureParser.parse("target/test-classes/json-output/parser/feature_with_docstring.json"); assertThat(features).hasSize(1).contains(FeatureBuilder.instance().name("An embed data directly feature").id("an-embed-data-directly-feature").build()); List<Scenario> scenarios = features.get(0).getScenarios(); assertNotNull(scenarios); for (Scenario scenario : scenarios) { if(scenario.getName().equals("scenario 1")){ Step step = scenario.getStepByName("I embed data directly"); assertThat(step).isNotNull(); assertThat(step.getDocString()).isNotNull(); assertThat(step.getDocString().getValue()).isEqualTo("A paragraph in an open block."); assertThat(step.getDocString().getContentType()).isEqualTo(""); step = scenario.getStepByName("I embed code directly"); assertThat(step).isNotNull(); assertThat(step.getDocString()).isNotNull(); assertThat(step.getDocString().getValue()).isEqualTo("puts('Hello world')"); assertThat(step.getDocString().getContentType()).isEqualTo("ruby"); } } }
@Test public void shouldParseFeatureWithOutput() throws IOException { List<Feature> features = FeatureParser.parse("target/test-classes/json-output/parser/feature_with_output.json"); assertThat(features).hasSize(1); List<Scenario> scenarios = features.get(0).getScenarios(); assertNotNull(scenarios); Scenario scenario = features.get(0).getScenarioByName("Show the current version of sdkman"); assertThat(scenario).isNotNull(); Step step = scenario.getStepByName("I see \"SDKMAN x.y.z\""); assertThat(step).isNotNull(); assertThat(step.getOutput()).isNotNull().hasSize(1); assertThat(step.getOutput().get(0).getValue().replaceAll("\n","")).isEqualTo("Output: broadcast messageSDKMAN x.y.z"); }
@Test public void shouldParseFeatureWithAttachments() throws IOException { List<Feature> features = FeatureParser.parse("target/test-classes/json-output/attachments.json"); assertThat(features).hasSize(1); List<Scenario> scenarios = features.get(0).getScenarios(); assertNotNull(scenarios); Scenario scenario = features.get(0).getScenarioByName("Multiple attachments"); assertThat(scenario).isNotNull(); Step step = scenario.getStepByName("a Step that has multiple attachments"); assertThat(step).isNotNull(); List<Embedding> embeddings = step.getEmbeddings(); assertThat(embeddings).isNotNull().hasSize(3); assertThat(embeddings.get(0)) .isNotNull() .hasFieldOrPropertyWithValue("data", "First attachment") .hasFieldOrPropertyWithValue("mimeType", "text/plain") .hasFieldOrPropertyWithValue("name", null); assertThat(embeddings.get(1)) .isNotNull() .hasFieldOrPropertyWithValue("data", "U2Vjb25kIEF0dGFjaG1lbnQ\u003d") .hasFieldOrPropertyWithValue("mimeType", "text/plain") .hasFieldOrPropertyWithValue("name", "Second"); assertThat(embeddings.get(2)) .isNotNull() .hasFieldOrPropertyWithValue("data", "Third attachment") .hasFieldOrPropertyWithValue("mimeType", "text/plain") .hasFieldOrPropertyWithValue("name", null); }
@Test public void shouldParseFeatureInAbsolutePath() throws IOException { String absolutePath = Paths.get("").toAbsolutePath() + "/target/test-classes/json-output/" + onePassingOneFailing; String path = FileUtil.findJsonFile(absolutePath); assertThat(path).isNotNull(); List<Feature> features = FeatureParser.parse(path); assertThat(features).isNotNull().hasSize(1).contains(FeatureBuilder.instance().name("One passing scenario, one failing scenario").id("one-passing-scenario,-one-failing-scenario").build()); }
@Test public void shouldNotLoadNonExistingFeature() { List<Feature> features = FeatureParser.parse("/dev"); assertNull(features); }
@Test public void shouldNotFindFeatures() { List<Feature> features = FeatureParser.parse(FileUtil.findJsonFile("/target/invalid/")); assertNull(features); }
@Test public void shouldNotParseInvalidJsonFile() { List<Feature> features = FeatureParser.parse(FileUtil.findJsonFile("json-output/invalid_json.json")); assertNull(features); }
@Test public void shouldNotReadInvalidJsonFile() { String filePath = FileUtil.findJsonFile("features/outline.feature"); List<Feature> features = FeatureParser.parse(filePath); assertNull(features); }
@Test public void shouldParseFeatureWithCommentsInScenariosExamples() { List<Feature> features = FeatureParser.parse(getClass().getResource("/json-output/feature_with_comments_in_examples.json").getPath()); assertThat(features).isNotNull().hasSize(1); }