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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
package vid.automation.test.services;
import static java.util.stream.Collectors.toList;
import static org.testng.Assert.assertEquals;
import static vid.automation.test.services.DropTestApiField.dropFieldCloudOwnerFromString;
import static vid.automation.test.services.DropTestApiField.dropTestApiFieldFromString;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
import org.glassfish.jersey.uri.internal.JerseyUriBuilder;
import org.onap.simulator.presetGenerator.presets.BasePresets.BasePreset;
import org.onap.simulator.presetGenerator.presets.model.RegistrationRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import vid.automation.test.utils.ReadFile;
public class SimulatorApi {
public enum RegistrationStrategy {
APPEND, CLEAR_THEN_SET
}
private static final Logger logger = LoggerFactory.getLogger(SimulatorApi.class);
/*
these classes are partial representation of org.mockserver.model.HttpRequest.
We can not use HttpRequest since it contains Map with NottableString and jackson throw the following error :
com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type
[simple type, class org.mockserver.model.NottableString]
*/
public static class StringWrapper {
public String value;
}
public static class RecordedHeaders {
public StringWrapper name;
public List<StringWrapper> values;
}
public static class HttpRequest {
public StringWrapper path;
public List<RecordedHeaders> headers;
}
public static class RecordedRequests {
public String path;
public Map<String, List<String>> headers;
public RecordedRequests(String path, Map<String, List<String>> headers) {
this.path = path;
this.headers = headers;
}
public RecordedRequests() {
}
}
private static final URI uri; //uri for registration
private static final URI simulationUri; //uri for getting simulated responses
private static final Client client;
private static final List<UnaryOperator<String>> presetStringPostProccessors =
ImmutableList.of(dropTestApiFieldFromString(), dropFieldCloudOwnerFromString());
static {
String host = System.getProperty("SIM_HOST", System.getProperty("VID_HOST", "127.0.0.1"));
Integer port = Integer.valueOf(System.getProperty("SIM_PORT", System.getProperty("VID_PORT", "8080"))); //port for registration
uri = new JerseyUriBuilder().host(host).port(port).scheme("http").path("vidSimulator").build();
client = ClientBuilder.newClient();
client.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
//registering jacksonJsonProvider for avoiding exceptions like :
// org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:
// MessageBodyWriter not found for media type=application/json
JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider();
jacksonJsonProvider.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
client.register(jacksonJsonProvider);
Integer simulationPort = Integer.valueOf(System.getProperty("SIMULATION_PORT", "1080")); //port getting simulated responses
simulationUri = new JerseyUriBuilder().host(host).port(simulationPort).scheme("http").build();
}
public static URI getSimulationUri() {
return simulationUri;
}
public static void registerExpectation(String expectationFilename, RegistrationStrategy registrationStrategy) {
registerExpectation(expectationFilename, ImmutableMap.<String, Object>of(), registrationStrategy);
}
public static void registerExpectation(RegistrationStrategy strategy, String... expectationTemplateFilenames) {
registerExpectation(expectationTemplateFilenames, ImmutableMap.of(), strategy);
}
public static void registerExpectation(String[] expectationTemplateFilenames, ImmutableMap<String, Object> templateParams, RegistrationStrategy strategy) {
if (strategy.equals(RegistrationStrategy.CLEAR_THEN_SET)) {
clearRegistrations();
}
for (String expectationTemplateFilename: expectationTemplateFilenames) {
registerExpectation(expectationTemplateFilename, templateParams, RegistrationStrategy.APPEND);
}
}
public static void registerExpectation(String expectationTemplateFilename, ImmutableMap<String, Object> templateParams, RegistrationStrategy registrationStrategy) {
String content = ReadFile.loadResourceAsString("registration_to_simulator/" + expectationTemplateFilename);
for (Map.Entry<String, Object> templateParam : templateParams.entrySet()) {
content = content.replaceAll(templateParam.getKey(), templateParam.getValue().toString());
}
registerToSimulatorAndAssertSuccess(expectationTemplateFilename, content, registrationStrategy);
}
public static void registerExpectationFromPreset(BasePreset preset, RegistrationStrategy registrationStrategy) {
RegistrationRequest content = preset.generateScenario();
registerToSimulatorAndAssertSuccess(preset.getClass().getCanonicalName(), content, registrationStrategy);
}
public static void registerExpectationFromPresetsCollections(Collection<Collection<BasePreset>> presets, RegistrationStrategy registrationStrategy) {
registerExpectationFromPresets(presets.stream()
.flatMap(Collection::stream)
.collect(toList()), registrationStrategy);
}
public static void registerExpectationFromPresets(Collection<BasePreset> presets, RegistrationStrategy registrationStrategy) {
if (registrationStrategy == RegistrationStrategy.CLEAR_THEN_SET) {
clearRegistrations();
}
presets.forEach(
preset-> {
try {registerToSimulatorAndAssertSuccess(preset.getClass().getCanonicalName(), preset.generateScenario());}
catch (Throwable e) {
throw new RuntimeException("Failed to register preset "+preset.getClass().getName(), e);
}
}
);
}
// public static List<HttpRequest> retrieveRecordedRequests() {
// Response response = client.target(uri).path("retrieveRecordedRequests").request().get();
// return response.readEntity(new GenericType<List<HttpRequest>>(){});
// }
/*
This method return counter of requests that has been sent to simulator.
The key of the map is a path, and the value is counter
*/
public static Map<String, Long> retrieveRecordedRequestsPathCounter() {
List<HttpRequest> httpRequests = retrieveRecordedHttpRequests();
return httpRequests.stream().map(x->x.path.value).collect(
Collectors.groupingBy(Function.identity(), Collectors.counting()));
}
private static List<HttpRequest> retrieveRecordedHttpRequests() {
Response response = client.target(uri).path("retrieveRecordedRequests").request().get();
return response.readEntity(new GenericType<List<HttpRequest>>(){});
}
public static List<RecordedRequests> retrieveRecordedRequests() {
List<HttpRequest> rawRequests = retrieveRecordedHttpRequests();
return rawRequests.stream().map(request->new RecordedRequests(
request.path.value,
request.headers.stream().collect(
Collectors.toMap(
x->x.name.value,
x->x.values.stream().map(y->y.value).collect(toList())))
)).collect(toList());
}
private static void registerToSimulatorAndAssertSuccess(String name, Object content, RegistrationStrategy registrationStrategy) {
if (registrationStrategy == RegistrationStrategy.CLEAR_THEN_SET) {
clearRegistrations();
}
registerToSimulatorAndAssertSuccess(name, content);
}
private static void registerToSimulatorAndAssertSuccess(String name, Object content) {
logger.info("Setting {}", name);
content = postProccessContent(content);
Response response = createSimulatorRegistrationWebTarget().request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(content));
assertEquals(response.getStatus(), HttpStatus.OK.value());
}
private static Object postProccessContent(Object content) {
if (content instanceof String) {
for (UnaryOperator<String> presetStringPostProccessor : presetStringPostProccessors) {
content = presetStringPostProccessor.apply((String) content);
}
}
return content;
}
public static void clearExpectations() {
clearRegistrations();
}
private static void clearRegistrations() {
logger.info("Clearing Registrations");
Response response = createSimulatorRegistrationWebTarget().request().delete();
assertEquals(response.getStatus(), HttpStatus.OK.value());
}
private static WebTarget createSimulatorRegistrationWebTarget() {
return client.target(uri).path("registerToVidSimulator");
}
public static void clearAll() {
WebTarget webTarget = createSimulatorRegistrationWebTarget();
webTarget.request().delete();
}
}
|