Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
JHttpUserScenarioStep.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.invoker.scenario;
2 
3 import com.griddynamics.jagger.invoker.v2.JHttpEndpoint;
4 import com.griddynamics.jagger.invoker.v2.JHttpQuery;
5 import com.griddynamics.jagger.invoker.v2.JHttpResponse;
6 
7 import java.util.concurrent.TimeUnit;
8 import java.util.function.BiConsumer;
9 import java.util.function.Function;
10 
16 public class JHttpUserScenarioStep {
17  private int stepNumber;
18  private final String stepId; // mandatory parameter. required for metrics saving
19  private JHttpEndpoint endpoint;
20  private JHttpQuery query;
21  private JHttpResponse response;
22  private final long waitAfterExecutionInSeconds;
23  private final String stepDisplayName; // should be equal to stepId if not defined
24  private final BiConsumer<JHttpUserScenarioStep, JHttpUserScenarioStep> previousAndCurrentStepConsumer;
25  private final BiConsumer<JHttpUserScenarioStep, JHttpScenarioGlobalContext> previousStepAndContextConsumer;
26  private final Function<JHttpResponse, Boolean> responseFunction;
27 
32  public void preProcessGlobalContext(JHttpUserScenarioStep previousStep, JHttpScenarioGlobalContext scenarioContext) {
33  if (previousStepAndContextConsumer != null)
34  previousStepAndContextConsumer.accept(previousStep, scenarioContext);
35  }
36 
41  public void preProcess(JHttpUserScenarioStep previousStep) {
42  if (previousAndCurrentStepConsumer != null) {
43  previousAndCurrentStepConsumer.accept(previousStep, this);
44  }
45  }
46 
51  public Boolean postProcess(JHttpResponse response) {
52  this.response = response;
53  if (responseFunction != null)
54  return responseFunction.apply(JHttpResponse.copyOf(response));
55  return true;
56  }
57 
58  public void waitAfterExecution() {
59  if (waitAfterExecutionInSeconds > 0) {
60  try {
61  TimeUnit.SECONDS.sleep(waitAfterExecutionInSeconds);
62  } catch (InterruptedException e) {
63  throw new RuntimeException("Error occurred while waiting after execution", e);
64  }
65  }
66  }
67 
68  private JHttpUserScenarioStep(Builder builder) {
69  this.stepId = builder.stepId;
70  this.endpoint = builder.endpoint;
71  this.query = builder.query;
72  this.waitAfterExecutionInSeconds = builder.waitAfterExecutionInSeconds;
73  this.stepDisplayName = (builder.stepDisplayName == null) ? builder.stepId : builder.stepDisplayName;
74  this.previousAndCurrentStepConsumer = builder.previousAndCurrentStepConsumer;
75  this.responseFunction = builder.responseFunction;
76  this.previousStepAndContextConsumer = builder.previousStepAndContextConsumer;
77  }
78 
79  public static Builder builder(String id, JHttpEndpoint endpoint) {
80  return new Builder(id, endpoint);
81  }
82 
86  public static Builder builder(String id) {
87  return new Builder(id, null);
88  }
89 
90  public static class Builder {
91  private final String stepId;
92  private final JHttpEndpoint endpoint;
93  private JHttpQuery query;
94  private long waitAfterExecutionInSeconds;
95  private String stepDisplayName;
96  private BiConsumer<JHttpUserScenarioStep, JHttpUserScenarioStep> previousAndCurrentStepConsumer;
97  private BiConsumer<JHttpUserScenarioStep, JHttpScenarioGlobalContext> previousStepAndContextConsumer;
98  private Function<JHttpResponse, Boolean> responseFunction;
99 
100  private Builder(String stepId, JHttpEndpoint endpoint) {
101  this.stepId = stepId;
102  this.endpoint = endpoint;
103  }
104 
105  public Builder withQuery(JHttpQuery query) {
106  this.query = query;
107  return this;
108  }
109 
110  public Builder withWaitAfterExecutionInSeconds(long waitAfterExecutionInSeconds) {
111  this.waitAfterExecutionInSeconds = waitAfterExecutionInSeconds;
112  return this;
113  }
114 
115  public Builder withDisplayName(String displayName) {
116  this.stepDisplayName = displayName;
117  return this;
118  }
119 
120  public Builder withPreProcessGlobalContextFunction(BiConsumer<JHttpUserScenarioStep, JHttpScenarioGlobalContext> previousStepAndContextConsumer) {
121  this.previousStepAndContextConsumer = previousStepAndContextConsumer;
122  return this;
123  }
124 
125  public Builder withPreProcessFunction(BiConsumer<JHttpUserScenarioStep, JHttpUserScenarioStep> previousAndCurrentStepConsumer) {
126  this.previousAndCurrentStepConsumer = previousAndCurrentStepConsumer;
127  return this;
128  }
129 
130  public Builder withPostProcessFunction(Function<JHttpResponse, Boolean> responseFunction) {
131  this.responseFunction = responseFunction;
132  return this;
133  }
134 
136  return new JHttpUserScenarioStep(this);
137  }
138  }
139 
141  return waitAfterExecutionInSeconds;
142  }
143 
145  return endpoint;
146  }
147 
151  public void setEndpoint(JHttpEndpoint endpoint) {
152  this.endpoint = endpoint;
153  }
154 
155  public JHttpQuery getQuery() {
156  return query;
157  }
158 
162  public void setQuery(JHttpQuery query) {
163  this.query = query;
164  }
165 
166  public JHttpResponse getResponse() {
167  return JHttpResponse.copyOf(response);
168  }
169 
170  public String getStepDisplayName() {
171  return stepDisplayName;
172  }
173 
174  public String getStepId() {
175  return stepId;
176  }
177 
178  public int getStepNumber() {
179  return stepNumber;
180  }
181 
182  public void setStepNumber(int stepNumber) {
183  this.stepNumber = stepNumber;
184  }
185 }