-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathGiven.java
More file actions
103 lines (85 loc) · 2.86 KB
/
Given.java
File metadata and controls
103 lines (85 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.datadog.api;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/** Describes given action. */
public class Given {
public String step;
public String tag;
public String operationId;
/** Defines how to populate operation parameters. */
public static class Parameter {
public String name;
@JsonInclude(Include.NON_NULL)
public String source;
@JsonInclude(Include.NON_NULL)
public String value;
@JsonInclude(Include.NON_NULL)
public String origin;
public <T> T resolve(Class<T> clazz, Object context, ObjectMapper mapper, String version) {
try {
if (value != null) {
if (clazz == File.class) {
// trim leading and trailing quotes from the value variable
String filePath =
"src/test/resources/com/datadog/api/client/"
+ version
+ "/api/"
+ value.replaceAll("^\"|\"$", "");
return (T) new File(filePath);
}
return World.fromJSON(mapper, clazz, World.templated(value, context));
}
if (source != null) {
return (T) World.lookup(context, source);
}
} catch (java.lang.IllegalAccessException
| java.lang.NoSuchFieldException
| com.fasterxml.jackson.core.JsonProcessingException e) {
throw new RuntimeException(e);
}
return null;
}
}
@JsonInclude(Include.NON_NULL)
public List<Parameter> parameters;
@JsonInclude(Include.NON_NULL)
public String source;
public String key;
public Map<String, Parameter> getRequestParameters() {
Map<String, Parameter> requestParams = new LinkedHashMap<>();
if (parameters != null) {
for (Parameter p : parameters) {
requestParams.put(World.toPropertyName(p.name), p);
}
}
return requestParams;
}
/** Load given definition from a configuration file. */
public static List<Given> load(File file) throws IOException {
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<Given>> typeRef = new TypeReference<List<Given>>() {};
return mapper.readValue(file, typeRef);
}
public static String toOperationName(String string) {
if (string == null || string.length() == 0) {
return string;
}
char c[] = string.toCharArray();
c[0] = Character.toLowerCase(c[0]);
return new String(c);
}
public String getAPIName() {
return Pattern.compile("[- ]").matcher(tag).replaceAll("");
}
public String getOperationName() {
return toOperationName(operationId);
}
}