21
21
import static java .util .Collections .unmodifiableMap ;
22
22
import static java .util .Optional .ofNullable ;
23
23
24
- import com .chutneytesting .ExecutionConfiguration ;
25
24
import com .chutneytesting .action .spi .ActionExecutionResult ;
26
25
import com .chutneytesting .action .spi .injectable .Target ;
27
26
import com .chutneytesting .engine .domain .environment .TargetImpl ;
28
27
import com .chutneytesting .engine .domain .execution .RxBus ;
29
28
import com .chutneytesting .engine .domain .execution .ScenarioExecution ;
30
29
import com .chutneytesting .engine .domain .execution .StepDefinition ;
31
30
import com .chutneytesting .engine .domain .execution .engine .StepExecutor ;
32
- import com .chutneytesting .engine .domain .execution .engine .evaluation .EvaluationException ;
33
31
import com .chutneytesting .engine .domain .execution .engine .evaluation .StepDataEvaluator ;
34
32
import com .chutneytesting .engine .domain .execution .engine .scenario .ScenarioContext ;
35
- import com .chutneytesting .engine .domain .execution .engine .scenario .ScenarioContextImpl ;
36
33
import com .chutneytesting .engine .domain .execution .event .BeginStepExecutionEvent ;
37
34
import com .chutneytesting .engine .domain .execution .event .EndStepExecutionEvent ;
38
35
import com .chutneytesting .engine .domain .execution .event .PauseStepExecutionEvent ;
39
36
import com .chutneytesting .engine .domain .execution .report .Status ;
40
37
import com .chutneytesting .engine .domain .execution .report .StepExecutionReport ;
41
38
import com .chutneytesting .engine .domain .execution .strategies .StepStrategyDefinition ;
42
39
import com .chutneytesting .tools .Try ;
43
- import com .fasterxml .jackson .core .JsonProcessingException ;
44
- import com .fasterxml .jackson .databind .ObjectMapper ;
45
40
import com .google .common .collect .Lists ;
46
- import com .google .common .collect .Maps ;
47
41
import java .time .Duration ;
48
42
import java .time .Instant ;
49
43
import java .util .Collections ;
50
44
import java .util .HashMap ;
51
- import java .util .LinkedHashMap ;
52
45
import java .util .List ;
53
46
import java .util .Map ;
54
47
import java .util .Optional ;
@@ -70,7 +63,6 @@ public class Step {
70
63
private Target target ;
71
64
private final StepExecutor executor ;
72
65
private final StepDataEvaluator dataEvaluator ;
73
- private static final ObjectMapper objectMapper = ExecutionConfiguration .reportObjectMapper ();
74
66
private StepContext stepContext ;
75
67
76
68
public Step (StepDataEvaluator dataEvaluator , StepDefinition definition , StepExecutor executor , List <Step > steps ) {
@@ -80,7 +72,7 @@ public Step(StepDataEvaluator dataEvaluator, StepDefinition definition, StepExec
80
72
this .executor = executor ;
81
73
this .steps = steps ;
82
74
this .state = new StepState (definition .name );
83
- this .stepContext = new StepContext (objectMapper );
75
+ this .stepContext = new StepContext ();
84
76
}
85
77
86
78
public static Step nonExecutable (StepDefinition definition ) {
@@ -114,7 +106,7 @@ public Status execute(ScenarioExecution scenarioExecution, ScenarioContext scena
114
106
target = dataEvaluator .evaluateTarget (target , evaluationContext );
115
107
resolveName (evaluationContext );
116
108
Try
117
- .exec (() -> this .stepContext = new StepContext (scenarioContext , localContext , evaluatedInputs , objectMapper ))
109
+ .exec (() -> this .stepContext = new StepContext (scenarioContext , localContext , evaluatedInputs ))
118
110
.ifSuccess (stepContextExecuted -> {
119
111
executor .execute (scenarioExecution , target , this );
120
112
if (Status .SUCCESS .equals (this .state .status ())) {
@@ -157,10 +149,6 @@ public String name() {
157
149
return this .state .name ();
158
150
}
159
151
160
- public ObjectMapper getObjectMapper () {
161
- return objectMapper ;
162
- }
163
-
164
152
public void resolveName (Map <String , Object > context ) {
165
153
this .state .setName (dataEvaluator .silentEvaluateString (state .name (), context ));
166
154
}
@@ -349,120 +337,15 @@ public Map<String, Object> getStepOutputs() {
349
337
}
350
338
351
339
public Map <String , Object > getStepContextInputSnapshot () {
352
- return this .stepContext .stepContextSnapshot .getInputsSnapshot ();
340
+ return this .stepContext .getStepContextSnapshot () .getInputsSnapshot ();
353
341
}
354
342
355
343
public Map <String , Object > getStepContextOutputSnapshot () {
356
- return this .stepContext .stepContextSnapshot .getOutputsSnapshot ();
344
+ return this .stepContext .getStepContextSnapshot () .getOutputsSnapshot ();
357
345
}
358
346
359
347
public void removeStepExecution () {
360
348
this .steps .clear ();
361
349
}
362
350
363
-
364
- private static class StepContext {
365
-
366
- private final ScenarioContext scenarioContext ;
367
- private final Map <String , Object > localContext ;
368
- private final Map <String , Object > evaluatedInputs ;
369
- private final Map <String , Object > stepOutputs ;
370
- private StepContextSnapshot stepContextSnapshot ;
371
-
372
- private StepContext (ObjectMapper objectMapper ) {
373
- this (new ScenarioContextImpl (), new LinkedHashMap <>(), new LinkedHashMap <>(), objectMapper );
374
- }
375
-
376
- private StepContext (ScenarioContext scenarioContext , Map <String , Object > localContext , Map <String , Object > evaluatedInputs , ObjectMapper objectMapper ) throws EvaluationException {
377
- this (scenarioContext , localContext , evaluatedInputs , new LinkedHashMap <>(), objectMapper );
378
- }
379
-
380
- private StepContext (ScenarioContext scenarioContext , Map <String , Object > localContext , Map <String , Object > evaluatedInputs , Map <String , Object > stepOutputs , ObjectMapper objectMapper ) {
381
- this .scenarioContext = scenarioContext ;
382
- this .localContext = localContext ;
383
- this .evaluatedInputs = evaluatedInputs ;
384
- this .stepOutputs = stepOutputs ;
385
- this .stepContextSnapshot = new StepContextSnapshot (objectMapper );
386
- }
387
-
388
- private StepContext copySnapshotsInputOutput () {
389
- this .stepContextSnapshot = new StepContextSnapshot (evaluatedInputs , stepOutputs , objectMapper );
390
- return this ;
391
- }
392
-
393
- private Map <String , Object > evaluationContext () {
394
- final Map <String , Object > allResults = Maps .newLinkedHashMap (scenarioContext );
395
- allResults .putAll (localContext );
396
- allResults .putAll (stepOutputs );
397
- return allResults ;
398
- }
399
-
400
- private ScenarioContext getScenarioContext () {
401
- return scenarioContext ;
402
- }
403
-
404
- private Map <String , Object > getEvaluatedInputs () {
405
- return ofNullable (evaluatedInputs ).orElse (emptyMap ());
406
- }
407
-
408
- private void addStepOutputs (Map <String , Object > stepOutputs ) {
409
- if (stepOutputs != null ) {
410
- this .stepOutputs .putAll (stepOutputs );
411
- }
412
- }
413
-
414
- private void addScenarioContext (Map <String , Object > context ) {
415
- if (context != null ) {
416
- this .scenarioContext .putAll (context );
417
- }
418
- }
419
-
420
- private Map <String , Object > getStepOutputs () {
421
- return ofNullable (stepOutputs ).orElse (emptyMap ());
422
- }
423
-
424
- private StepContext copy () {
425
- return new StepContext (scenarioContext .unmodifiable (), unmodifiableMap (localContext ), unmodifiableMap (evaluatedInputs ), unmodifiableMap (stepOutputs ), objectMapper ).copySnapshotsInputOutput ();
426
- }
427
- }
428
-
429
- public static class StepContextSnapshot {
430
- private final Map <String , Object > inputsSnapshot ;
431
- private final Map <String , Object > outputsSnapshot ;
432
- private final ObjectMapper objectMapper ;
433
-
434
- public StepContextSnapshot (ObjectMapper objectMapper ) {
435
- this .objectMapper = objectMapper ;
436
- this .inputsSnapshot = emptyMap ();
437
- this .outputsSnapshot = emptyMap ();
438
- }
439
-
440
- public StepContextSnapshot (Map <String , Object > inputsSnapshot , Map <String , Object > outputsSnapshot , ObjectMapper objectMapper ) {
441
- this .objectMapper = objectMapper ;
442
- this .inputsSnapshot = mapStringObjectToString (inputsSnapshot );
443
- this .outputsSnapshot = mapStringObjectToString (outputsSnapshot );
444
- }
445
-
446
- public Map <String , Object > getInputsSnapshot () {
447
- return unmodifiableMap (inputsSnapshot );
448
- }
449
-
450
- public Map <String , Object > getOutputsSnapshot () {
451
- return unmodifiableMap (outputsSnapshot );
452
- }
453
-
454
- private Map <String , Object > mapStringObjectToString (Map <String , Object > originalMap ) {
455
- Map <String , Object > stringMap = new HashMap <>();
456
- originalMap .forEach ((key , value ) -> {
457
- try {
458
- String stringObject = objectMapper .writeValueAsString (value );
459
- Object jsonObject = objectMapper .readTree (stringObject );
460
- stringMap .put (key , jsonObject );
461
- } catch (JsonProcessingException e ) {
462
- throw new RuntimeException (e );
463
- }
464
- });
465
- return stringMap ;
466
- }
467
- }
468
351
}
0 commit comments