1 package vid.automation.test.services;
3 import com.google.common.collect.ImmutableMap;
4 import org.apache.commons.io.IOUtils;
5 import org.apache.log4j.Logger;
6 import org.glassfish.jersey.client.ClientProperties;
7 import org.glassfish.jersey.uri.internal.JerseyUriBuilder;
8 import org.springframework.http.HttpStatus;
10 import javax.ws.rs.client.Client;
11 import javax.ws.rs.client.ClientBuilder;
12 import javax.ws.rs.client.Entity;
13 import javax.ws.rs.client.WebTarget;
14 import javax.ws.rs.core.MediaType;
15 import javax.ws.rs.core.Response;
16 import java.io.IOException;
17 import java.io.InputStream;
21 import static org.testng.Assert.assertEquals;
23 public class SimulatorApi {
25 private static Logger logger = Logger.getLogger(SimulatorApi.class.getName());
27 public enum RegistrationStrategy {
28 APPEND, CLEAR_THEN_SET
31 private static final URI uri;
32 private static final Client client;
35 String host = System.getProperty("VID_HOST", "127.0.0.1" );
36 Integer port = Integer.valueOf(System.getProperty("VID_PORT", "8080"));
37 uri = new JerseyUriBuilder().host(host).port(port).scheme("http").path("vidSimulator").build();
38 client = ClientBuilder.newClient();
39 client.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
42 public static void registerExpectation(String expectationFilename) {
43 registerExpectation(expectationFilename, ImmutableMap.<String, Object>of(), RegistrationStrategy.APPEND);
46 public static void registerExpectation(String expectationFilename, RegistrationStrategy registrationStrategy) {
47 registerExpectation(expectationFilename, ImmutableMap.<String, Object>of(), registrationStrategy);
50 public static void registerExpectation(String expectationTemplateFilename, ImmutableMap<String, Object> templateParams) {
51 registerExpectation(expectationTemplateFilename, templateParams, RegistrationStrategy.APPEND);
54 public static void registerExpectation(String... expectationTemplateFilenames) {
55 registerExpectation(expectationTemplateFilenames, ImmutableMap.of());
58 public static void registerExpectation(String[] expectationTemplateFilenames, ImmutableMap<String, Object> templateParams) {
59 for (String expectationTemplateFilename: expectationTemplateFilenames) {
60 registerExpectation(expectationTemplateFilename, templateParams);
64 public static void registerExpectation(String expectationTemplateFilename, ImmutableMap<String, Object> templateParams, RegistrationStrategy registrationStrategy) {
67 final InputStream resource = SimulatorApi.class.getClassLoader().getResourceAsStream("registration_to_simulator/" + expectationTemplateFilename);
68 if (resource == null) throw new RuntimeException("template file not found: " + "/registration_to_simulator/" + expectationTemplateFilename);
69 String content = IOUtils.toString(resource, "UTF-8");
71 for (Map.Entry<String, Object> templateParam : templateParams.entrySet()) {
72 content = content.replaceAll(templateParam.getKey(), templateParam.getValue().toString());
75 registerToSimulatorAndAssertSuccess(content, registrationStrategy);
77 } catch (IOException e) {
78 logger.error("couldn't read " + expectationTemplateFilename, e);
79 // throw new RuntimeException("couldn't read " + expectationTemplateFilename, e);
83 private static void registerToSimulatorAndAssertSuccess(String content, RegistrationStrategy registrationStrategy) {
84 WebTarget webTarget = client.target(uri).path("registerToVidSimulator");
86 if (registrationStrategy == RegistrationStrategy.CLEAR_THEN_SET) {
87 response = webTarget.request().delete();
88 assertEquals(response.getStatus(), HttpStatus.OK.value());
90 response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(content));
91 assertEquals(response.getStatus(), HttpStatus.OK.value());
94 public static void clearAll() {
95 WebTarget webTarget = client.target(uri).path("registerToVidSimulator");
96 webTarget.request().delete();