org.onap migration
[vid.git] / vid-automation / src / main / java / vid / automation / test / services / SimulatorApi.java
1 package vid.automation.test.services;
2
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;
9
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;
18 import java.net.URI;
19 import java.util.Map;
20
21 import static org.testng.Assert.assertEquals;
22
23 public class SimulatorApi {
24
25     private static Logger logger = Logger.getLogger(SimulatorApi.class.getName());
26
27     public enum RegistrationStrategy {
28         APPEND, CLEAR_THEN_SET
29     }
30
31     private static final URI uri;
32     private static final Client client;
33
34     static {
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);
40     }
41
42     public static void registerExpectation(String expectationFilename) {
43         registerExpectation(expectationFilename, ImmutableMap.<String, Object>of(), RegistrationStrategy.APPEND);
44     }
45
46     public static void registerExpectation(String expectationFilename, RegistrationStrategy registrationStrategy) {
47         registerExpectation(expectationFilename, ImmutableMap.<String, Object>of(), registrationStrategy);
48     }
49
50     public static void registerExpectation(String expectationTemplateFilename, ImmutableMap<String, Object> templateParams) {
51         registerExpectation(expectationTemplateFilename, templateParams, RegistrationStrategy.APPEND);
52     }
53
54     public static void registerExpectation(String... expectationTemplateFilenames) {
55         registerExpectation(expectationTemplateFilenames, ImmutableMap.of());
56     }
57
58     public static void registerExpectation(String[] expectationTemplateFilenames, ImmutableMap<String, Object> templateParams) {
59         for (String expectationTemplateFilename: expectationTemplateFilenames) {
60             registerExpectation(expectationTemplateFilename, templateParams);
61         }
62     }
63
64     public static void registerExpectation(String expectationTemplateFilename, ImmutableMap<String, Object> templateParams, RegistrationStrategy registrationStrategy) {
65
66         try {
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");
70
71             for (Map.Entry<String, Object> templateParam : templateParams.entrySet()) {
72                 content = content.replaceAll(templateParam.getKey(), templateParam.getValue().toString());
73             }
74
75             registerToSimulatorAndAssertSuccess(content, registrationStrategy);
76
77         } catch (IOException e) {
78             logger.error("couldn't read " + expectationTemplateFilename, e);
79 //            throw new RuntimeException("couldn't read " + expectationTemplateFilename, e);
80         }
81     }
82
83     private static void registerToSimulatorAndAssertSuccess(String content, RegistrationStrategy registrationStrategy) {
84         WebTarget webTarget = client.target(uri).path("registerToVidSimulator");
85         Response response;
86         if (registrationStrategy == RegistrationStrategy.CLEAR_THEN_SET) {
87             response = webTarget.request().delete();
88             assertEquals(response.getStatus(), HttpStatus.OK.value());
89         }
90         response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(content));
91         assertEquals(response.getStatus(), HttpStatus.OK.value());
92     }
93
94     public static void clearAll() {
95         WebTarget webTarget = client.target(uri).path("registerToVidSimulator");
96         webTarget.request().delete();
97     }
98 }