diff options
author | 2018-08-12 14:51:28 +0300 | |
---|---|---|
committer | 2018-08-12 15:02:57 +0300 | |
commit | 4a4dcc5185f8ba5a28c7f9fef509f32c0c2389e6 (patch) | |
tree | 23e55ee7e1ad9b91bcc3ef1dbe1fb7b183f8b2b6 /vid-automation/src/main/java/vid/automation/test/services/SimulatorApi.java | |
parent | 661a24fd57de02869a9771761e0fcba7eb77d121 (diff) |
vid-automation selenium tests
Change-Id: I6c1b0a0cf3bbfa4314c81f0cc72507db805ec632
Issue-ID: VID-281
Signed-off-by: Sonsino, Ofir (os0695) <os0695@intl.att.com>
Diffstat (limited to 'vid-automation/src/main/java/vid/automation/test/services/SimulatorApi.java')
-rw-r--r-- | vid-automation/src/main/java/vid/automation/test/services/SimulatorApi.java | 126 |
1 files changed, 90 insertions, 36 deletions
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 95370fc8..74cda799 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,11 +1,15 @@ 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.apache.commons.io.IOUtils; -import org.apache.log4j.Logger; import org.glassfish.jersey.client.ClientProperties; import org.glassfish.jersey.uri.internal.JerseyUriBuilder; +import org.opencomp.simulator.presetGenerator.presets.BasePresets.BasePreset; +import org.opencomp.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; @@ -13,86 +17,136 @@ 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.io.IOException; -import java.io.InputStream; 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 { - private static Logger logger = Logger.getLogger(SimulatorApi.class.getName()); - public enum RegistrationStrategy { APPEND, CLEAR_THEN_SET } - private static final URI uri; + 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("VID_PORT", "8080")); + 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 void registerExpectation(String expectationFilename) { - registerExpectation(expectationFilename, ImmutableMap.<String, Object>of(), RegistrationStrategy.APPEND); + 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(String expectationTemplateFilename, ImmutableMap<String, Object> templateParams) { - registerExpectation(expectationTemplateFilename, templateParams, RegistrationStrategy.APPEND); - } - - public static void registerExpectation(String... expectationTemplateFilenames) { - registerExpectation(expectationTemplateFilenames, ImmutableMap.of()); + public static void registerExpectation(RegistrationStrategy strategy, String... expectationTemplateFilenames) { + registerExpectation(expectationTemplateFilenames, ImmutableMap.of(), strategy); } - public static void registerExpectation(String[] expectationTemplateFilenames, ImmutableMap<String, Object> templateParams) { + 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); + registerExpectation(expectationTemplateFilename, templateParams, RegistrationStrategy.APPEND); } } public static void registerExpectation(String expectationTemplateFilename, ImmutableMap<String, Object> templateParams, RegistrationStrategy registrationStrategy) { - try { - final InputStream resource = SimulatorApi.class.getClassLoader().getResourceAsStream("registration_to_simulator/" + expectationTemplateFilename); - if (resource == null) throw new RuntimeException("template file not found: " + "/registration_to_simulator/" + expectationTemplateFilename); - String content = IOUtils.toString(resource, "UTF-8"); + String content = ReadFile.loadResourceAsString("registration_to_simulator/" + expectationTemplateFilename); - for (Map.Entry<String, Object> templateParam : templateParams.entrySet()) { - content = content.replaceAll(templateParam.getKey(), templateParam.getValue().toString()); - } + for (Map.Entry<String, Object> templateParam : templateParams.entrySet()) { + content = content.replaceAll(templateParam.getKey(), templateParam.getValue().toString()); + } - registerToSimulatorAndAssertSuccess(content, registrationStrategy); + registerToSimulatorAndAssertSuccess(content, registrationStrategy); - } catch (IOException e) { - logger.error("couldn't read " + expectationTemplateFilename, e); -// throw new RuntimeException("couldn't read " + expectationTemplateFilename, e); + } + + 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(String content, RegistrationStrategy registrationStrategy) { - WebTarget webTarget = client.target(uri).path("registerToVidSimulator"); - Response response; + private static void registerToSimulatorAndAssertSuccess(Object content, RegistrationStrategy registrationStrategy) { if (registrationStrategy == RegistrationStrategy.CLEAR_THEN_SET) { - response = webTarget.request().delete(); - assertEquals(response.getStatus(), HttpStatus.OK.value()); + 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); + } } - response = webTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(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 = client.target(uri).path("registerToVidSimulator"); + WebTarget webTarget = createSimulatorRegistrationWebTarget(); webTarget.request().delete(); } } |