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
|
package vid.automation.test.services;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.glassfish.jersey.client.ClientProperties;
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.springframework.http.HttpStatus;
import vid.automation.test.utils.ReadFile;
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.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.UnaryOperator;
import static org.testng.Assert.assertEquals;
import static vid.automation.test.services.DropTestApiField.dropTestApiFieldFromString;
public class SimulatorApi {
public enum RegistrationStrategy {
APPEND, CLEAR_THEN_SET
}
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());
static {
String 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();
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(content, registrationStrategy);
}
public static void registerExpectationFromPreset(BasePreset preset, RegistrationStrategy registrationStrategy) {
RegistrationRequest content = preset.generateScenario();
registerToSimulatorAndAssertSuccess(content, registrationStrategy);
}
public static void registerExpectationFromPresets(Collection<BasePreset> presets, RegistrationStrategy registrationStrategy) {
if (registrationStrategy == RegistrationStrategy.CLEAR_THEN_SET) {
clearRegistrations();
}
presets.forEach(
preset-> {
try {registerToSimulatorAndAssertSuccess(preset.generateScenario());}
catch (RuntimeException e) {
throw new RuntimeException("Failed to register preset "+preset.getClass().getName(), e);
}
}
);
}
private static void registerToSimulatorAndAssertSuccess(Object content, RegistrationStrategy registrationStrategy) {
if (registrationStrategy == RegistrationStrategy.CLEAR_THEN_SET) {
clearRegistrations();
}
registerToSimulatorAndAssertSuccess(content);
}
private static void registerToSimulatorAndAssertSuccess(Object content) {
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() {
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();
}
}
|