X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=vid-automation%2Fsrc%2Fmain%2Fjava%2Fvid%2Fautomation%2Ftest%2Fservices%2FSimulatorApi.java;h=6e15b7b523723d0fe59cf5d330e927ec40d7ae51;hb=e8d29ab5e3775472f59a4997fa3a5df9319de306;hp=0874496241c1d3e493bc42e8d5648663015c7589;hpb=f4dff328c0386c8901b5841943b11f0c13b3169f;p=vid.git diff --git a/vid-automation/src/main/java/vid/automation/test/services/SimulatorApi.java b/vid-automation/src/main/java/vid/automation/test/services/SimulatorApi.java index 087449624..6e15b7b52 100644 --- a/vid-automation/src/main/java/vid/automation/test/services/SimulatorApi.java +++ b/vid-automation/src/main/java/vid/automation/test/services/SimulatorApi.java @@ -1,8 +1,27 @@ 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; @@ -14,25 +33,6 @@ import org.slf4j.LoggerFactory; 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.GenericType; -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.Function; -import java.util.function.UnaryOperator; -import java.util.stream.Collectors; - -import static org.testng.Assert.assertEquals; -import static vid.automation.test.services.DropTestApiField.dropFieldCloudOwnerFromString; -import static vid.automation.test.services.DropTestApiField.dropTestApiFieldFromString; - public class SimulatorApi { public enum RegistrationStrategy { @@ -47,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 values; + } + + public static class HttpRequest { + public StringWrapper path; + public List headers; + } + + public static class RecordedRequests { + public String path; + public Map> headers; + + public RecordedRequests(String path, Map> headers) { + this.path = path; + this.headers = headers; + } + + public RecordedRequests() { + } } private static final URI uri; //uri for registration @@ -63,7 +82,7 @@ public class SimulatorApi { ImmutableList.of(dropTestApiFieldFromString(), dropFieldCloudOwnerFromString()); static { - String host = System.getProperty("VID_HOST", "127.0.0.1" ); + 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(); @@ -120,7 +139,7 @@ public class SimulatorApi { public static void registerExpectationFromPresetsCollections(Collection> presets, RegistrationStrategy registrationStrategy) { registerExpectationFromPresets(presets.stream() .flatMap(Collection::stream) - .collect(Collectors.toList()), registrationStrategy); + .collect(toList()), registrationStrategy); } public static void registerExpectationFromPresets(Collection presets, RegistrationStrategy registrationStrategy) { @@ -130,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); } } @@ -147,12 +166,27 @@ public class SimulatorApi { The key of the map is a path, and the value is counter */ public static Map retrieveRecordedRequestsPathCounter() { - Response response = client.target(uri).path("retrieveRecordedRequests").request().get(); - List httpRequests = response.readEntity(new GenericType>(){}); + List httpRequests = retrieveRecordedHttpRequests(); return httpRequests.stream().map(x->x.path.value).collect( Collectors.groupingBy(Function.identity(), Collectors.counting())); } + private static List retrieveRecordedHttpRequests() { + Response response = client.target(uri).path("retrieveRecordedRequests").request().get(); + return response.readEntity(new GenericType>(){}); + } + + public static List retrieveRecordedRequests() { + List 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();