Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
JHttpUserScenarioInvoker.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.invoker.scenario;
2 
3 import com.griddynamics.jagger.invoker.InvocationException;
4 import com.griddynamics.jagger.invoker.Invoker;
5 import com.griddynamics.jagger.invoker.v2.JHttpResponse;
6 import com.griddynamics.jagger.invoker.v2.SpringBasedHttpClient;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 
10 import java.util.ArrayList;
11 import java.util.Base64;
12 import java.util.List;
13 import java.util.stream.Collectors;
14 
15 import static com.google.common.collect.Lists.newArrayList;
16 
22 public class JHttpUserScenarioInvoker implements Invoker<Void, JHttpUserScenarioInvocationResult, JHttpUserScenario> {
23 
24  private final SpringBasedHttpClient httpClient = new SpringBasedHttpClient();
25 
26  private static Logger log = LoggerFactory.getLogger(JHttpUserScenarioInvoker.class);
27 
28  @Override
29  public JHttpUserScenarioInvocationResult invoke(Void nothing, JHttpUserScenario scenario) throws InvocationException {
30  Boolean scenarioSucceeded = true;
31  JHttpUserScenarioStep previousStep = null;
32  List<JHttpUserScenarioStepInvocationResult> stepInvocationResults = new ArrayList<>();
33 
34  JHttpScenarioGlobalContext localScenarioContext = scenario.getScenarioGlobalContext().copy();
35 
36  for (JHttpUserScenarioStep userScenarioStep : scenario.getUserScenarioSteps()) {
37 
38  // Pre process step: internal setup. Can be later overridden by the user
39  // Basic auth
40  if (localScenarioContext.getPassword() != null && localScenarioContext.getUserName() != null) {
41  String value = Base64.getEncoder().encodeToString((localScenarioContext.getUserName() + ":" + localScenarioContext.getPassword()).getBytes());
42  userScenarioStep.getQuery().header("Authorization", "Basic " + value);
43  }
44 
45  // Pre process step: user actions executed before request
46  userScenarioStep.preProcessGlobalContext(previousStep, localScenarioContext);
47  userScenarioStep.preProcess(previousStep);
48 
49  // check endpoint for null
50  if (localScenarioContext.getGlobalEndpoint() == null && userScenarioStep.getEndpoint() == null)
51  throw new IllegalArgumentException("Endpoint must not be null! Please, set global endpoint or set endpoint for every step.");
52  // use global endpoint for step if step has none
53  if (userScenarioStep.getEndpoint() == null)
54  userScenarioStep.setEndpoint(localScenarioContext.getGlobalEndpoint());
55  // copy global headers and cookies to current step if query is present
56  if (localScenarioContext.getGlobalHeaders() != null && userScenarioStep.getQuery() != null) {
57  localScenarioContext.getGlobalHeaders().forEach((header, values) -> {
58  if (userScenarioStep.getQuery().getHeaders() != null && userScenarioStep.getQuery().getHeaders().containsKey(header)) {
59  List<String> newValues = newArrayList(userScenarioStep.getQuery().getHeaders().get(header));
60  newValues.addAll(values);
61  userScenarioStep.getQuery().getHeaders().put(header, newValues.stream().distinct().collect(
62  Collectors.toList()));
63  } else {
64  userScenarioStep.getQuery().header(header, values);
65  }
66  });
67  }
68 
69  log.info("Step {}: {}", userScenarioStep.getStepNumber(), userScenarioStep.getStepId());
70  log.info("Endpoint: {}", userScenarioStep.getEndpoint());
71  log.info("Query: {}", userScenarioStep.getQuery());
72 
73  // Request execution step
74  long requestStartTime = System.nanoTime();
75  JHttpResponse response = httpClient.execute(userScenarioStep.getEndpoint(), userScenarioStep.getQuery());
76  Double requestTimeInMilliseconds = (System.nanoTime() - requestStartTime) / 1_000_000.0;
77 
78  //TODO: JFG-1121
79  log.info("Response: {}", response);
80 
81  // Wait after execution if needed
82  userScenarioStep.waitAfterExecution();
83 
84  // Post process step: executed after request. If returned false, scenario invocation stops.
85  Boolean succeeded = userScenarioStep.postProcess(response);
86  stepInvocationResults.add(new JHttpUserScenarioStepInvocationResult(userScenarioStep.getStepId(), userScenarioStep.getStepDisplayName(),
87  requestTimeInMilliseconds, succeeded));
88  previousStep = userScenarioStep;
89 
90  if (!succeeded) {
91  scenarioSucceeded = false;
92  log.error("Step {} '{}' post process returned false! Stopping scenario (next steps won't be executed).",
93  userScenarioStep.getStepId(), userScenarioStep.getStepDisplayName());
94  break;
95  }
96  }
97 
98  return new JHttpUserScenarioInvocationResult(stepInvocationResults, scenario.getScenarioId(), scenario.getScenarioDisplayName(), scenarioSucceeded);
99  }
100 }