use logging interceptor in SDC client
[vid.git] / vid-automation / src / main / java / vid / automation / test / services / SimulatorApi.java
index 65ef367..6e15b7b 100644 (file)
@@ -1,5 +1,6 @@
 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;
@@ -46,12 +47,31 @@ public class SimulatorApi {
     com.fasterxml.jackson.databind.JsonMappingException: Can not find a (Map) Key deserializer for type
      [simple type, class org.mockserver.model.NottableString]
     */
-    public static class Path {
+    public static class StringWrapper {
         public String value;
     }
 
-    public static class HttpRequest{
-        public Path path;
+    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
@@ -62,7 +82,7 @@ public class SimulatorApi {
             ImmutableList.of(dropTestApiFieldFromString(), dropFieldCloudOwnerFromString());
 
     static {
-        String host = System.getProperty("VID_HOST", "10.0.0.10" );
+        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();
@@ -116,6 +136,12 @@ public class SimulatorApi {
         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();
@@ -123,7 +149,7 @@ public class SimulatorApi {
         presets.forEach(
                 preset-> {
                     try {registerToSimulatorAndAssertSuccess(preset.getClass().getCanonicalName(), preset.generateScenario());}
-                    catch (RuntimeException e) {
+                    catch (Throwable e) {
                         throw new RuntimeException("Failed to register preset "+preset.getClass().getName(), e);
                     }
                 }
@@ -140,12 +166,27 @@ public class SimulatorApi {
         The key of the map is a path, and the value is counter
      */
     public static Map<String, Long> retrieveRecordedRequestsPathCounter() {
-        Response response = client.target(uri).path("retrieveRecordedRequests").request().get();
-        List<HttpRequest> httpRequests =  response.readEntity(new GenericType<List<HttpRequest>>(){});
+        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();