|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2022 Karate Labs Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.intuit.karate.core; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * |
| 33 | + * @author OwenK2 |
| 34 | + */ |
| 35 | +public class ScenarioOutlineResult { |
| 36 | + |
| 37 | + final private ScenarioOutline scenarioOutline; |
| 38 | + final private ScenarioRuntime runtime; |
| 39 | + |
| 40 | + public ScenarioOutlineResult(ScenarioOutline scenarioOutline, ScenarioRuntime runtime) { |
| 41 | + // NOTE: this value can be null, in which case the scenario is not from an outline |
| 42 | + this.scenarioOutline = scenarioOutline; |
| 43 | + this.runtime = runtime; |
| 44 | + } |
| 45 | + |
| 46 | + public Map<String, Object> toKarateJson() { |
| 47 | + if (scenarioOutline == null) return null; |
| 48 | + Map<String, Object> map = new HashMap(); |
| 49 | + map.put("name", scenarioOutline.getName()); |
| 50 | + map.put("description", scenarioOutline.getDescription()); |
| 51 | + map.put("line", scenarioOutline.getLine()); |
| 52 | + map.put("sectionIndex", scenarioOutline.getSection().getIndex()); |
| 53 | + map.put("exampleTableCount", scenarioOutline.getNumExampleTables()); |
| 54 | + map.put("exampleTables", scenarioOutline.getAllExampleData()); |
| 55 | + map.put("numScenariosToExecute", scenarioOutline.getNumScenarios()); |
| 56 | + |
| 57 | + // Get results of other examples in this outline |
| 58 | + List<Map<String, Object>> scenarioResults = new ArrayList(); |
| 59 | + if (runtime.featureRuntime != null && runtime.featureRuntime.result != null) { |
| 60 | + // Add all past results |
| 61 | + boolean needToAddRecent = runtime.result != null; |
| 62 | + for(ScenarioResult result : runtime.featureRuntime.result.getScenarioResults()) { |
| 63 | + if (result.getScenario().getSection().getIndex() == scenarioOutline.getSection().getIndex()) { |
| 64 | + scenarioResults.add(result.toInfoJson()); |
| 65 | + if(result.equals(runtime.result)) { |
| 66 | + needToAddRecent = false; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Add most recent result if we haven't already (and it's not null) |
| 72 | + if (needToAddRecent) { |
| 73 | + scenarioResults.add(runtime.result.toInfoJson()); |
| 74 | + } |
| 75 | + } |
| 76 | + map.put("scenarioResults", scenarioResults); |
| 77 | + map.put("numScenariosExecuted", scenarioResults.size()); |
| 78 | + |
| 79 | + return map; |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments