Repository: authorjapps/zerocode
Pull request #301 (2019-08-22 02:52:11)

ISS-00 - Protocol mapping, http build


Patch:
diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArrayIsEmptyAsserter.java b/core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArrayIsEmptyAsserterImpl.java
similarity index 90%
rename from core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArrayIsEmptyAsserter.java
rename to core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArrayIsEmptyAsserterImpl.java
index ae3b738c..dc081425 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArrayIsEmptyAsserter.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArrayIsEmptyAsserterImpl.java
@@ -8,10 +8,10 @@ import org.jsmart.zerocode.core.engine.assertion.JsonAsserter;
 import static org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher.aMatchingMessage;
 import static org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher.aNotMatchingMessage;
 
-public class ArrayIsEmptyAsserter implements JsonAsserter {
+public class ArrayIsEmptyAsserterImpl implements JsonAsserter {
     private final String path;
 
-    public ArrayIsEmptyAsserter(String path) {
+    public ArrayIsEmptyAsserterImpl(String path) {
         this.path = path;
     }
 
diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArraySizeAsserter.java b/core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArraySizeAsserterImpl.java
similarity index 95%
rename from core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArraySizeAsserter.java
rename to core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArraySizeAsserterImpl.java
index e8b4d808..eef6c1a7 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArraySizeAsserter.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/engine/assertion/array/ArraySizeAsserterImpl.java
@@ -12,18 +12,18 @@ import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASS
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_LESSER_THAN;
 import static org.jsmart.zerocode.core.engine.tokens.ZeroCodeAssertionTokens.ASSERT_VALUE_NOT_EQUAL_TO_NUMBER;
 
-public class ArraySizeAsserter implements JsonAsserter {
+public class ArraySizeAsserterImpl implements JsonAsserter {
     private final String path;
     private final int expectedSize;
     private final String expectedSizeExpression;
 
-    public ArraySizeAsserter(String path, int size) {
+    public ArraySizeAsserterImpl(String path, int size) {
         this.path = path;
         expectedSize = size;
         expectedSizeExpression = null;
     }
 
-    public ArraySizeAsserter(String path, String expression) {
+    public ArraySizeAsserterImpl(String path, String expression) {
         this.path = path;
         expectedSizeExpression = expression;
         expectedSize = -1;
diff --git a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java
index a6ced310..0bb98170 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/engine/preprocessor/ZeroCodeAssertionsProcessorImpl.java
@@ -21,8 +21,8 @@ import net.minidev.json.JSONArray;
 import org.apache.commons.lang.text.StrSubstitutor;
 import org.jsmart.zerocode.core.engine.assertion.FieldAssertionMatcher;
 import org.jsmart.zerocode.core.engine.assertion.JsonAsserter;
-import org.jsmart.zerocode.core.engine.assertion.array.ArrayIsEmptyAsserter;
-import org.jsmart.zerocode.core.engine.assertion.array.ArraySizeAsserter;
+import org.jsmart.zerocode.core.engine.assertion.array.ArrayIsEmptyAsserterImpl;
+import org.jsmart.zerocode.core.engine.assertion.array.ArraySizeAsserterImpl;
 import org.jsmart.zerocode.core.engine.assertion.field.FieldContainsStringAsserter;
 import org.jsmart.zerocode.core.engine.assertion.field.FieldContainsStringIgnoreCaseAsserter;
 import org.jsmart.zerocode.core.engine.assertion.field.FieldHasDateAfterValueAsserter;
@@ -189,13 +189,13 @@ public class ZeroCodeAssertionsProcessorImpl implements ZeroCodeAssertionsProces
                 } else if (ASSERT_VALUE_NULL.equals(value) || ASSERT_VALUE_IS_NULL.equals(value)) {
                     asserter = new FieldIsNullAsserter(path);
                 } else if (ASSERT_VALUE_EMPTY_ARRAY.equals(value)) {
-                    asserter = new ArrayIsEmptyAsserter(path);
+                    asserter = new ArrayIsEmptyAsserterImpl(path);
                 } else if (path.endsWith(ASSERT_PATH_SIZE)) {
                     path = path.substring(0, path.length() - ASSERT_PATH_SIZE.length());
                     if (value instanceof Number) {
-                        asserter = new ArraySizeAsserter(path, ((Integer) value).intValue());
+                        asserter = new ArraySizeAsserterImpl(path, ((Integer) value).intValue());
                     } else if (value instanceof String) {
-                        asserter = new ArraySizeAsserter(path, (String) value);
+                        asserter = new ArraySizeAsserterImpl(path, (String) value);
                     } else {
                         throw new RuntimeException(format("Oops! Unsupported value for .SIZE: %s", value));
                     }
diff --git a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeMultiStepsScenarioRunnerImpl.java b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeMultiStepsScenarioRunnerImpl.java
index ab09fde8..9055f2b6 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeMultiStepsScenarioRunnerImpl.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeMultiStepsScenarioRunnerImpl.java
@@ -21,6 +21,7 @@ import org.jsmart.zerocode.core.engine.preprocessor.ZeroCodeExternalFileProcesso
 import org.jsmart.zerocode.core.engine.preprocessor.ZeroCodeAssertionsProcessor;
 import org.jsmart.zerocode.core.engine.preprocessor.ZeroCodeParameterizedProcessor;
 import org.jsmart.zerocode.core.logbuilder.ZerocodeCorrelationshipLogger;
+import org.jsmart.zerocode.core.utils.ApiTypeUtils;
 import org.junit.runner.Description;
 import org.junit.runner.notification.RunNotifier;
 import org.slf4j.Logger;
@@ -58,6 +59,9 @@ public class ZeroCodeMultiStepsScenarioRunnerImpl implements ZeroCodeMultiStepsS
     @Inject
     private CsvParser csvParser;
 
+    @Inject
+    private ApiTypeUtils apiTypeUtils;
+
     @Inject(optional = true)
     @Named("web.application.endpoint.host")
     private String host;
@@ -390,6 +394,7 @@ public class ZeroCodeMultiStepsScenarioRunnerImpl implements ZeroCodeMultiStepsS
                         .method(operationName)
                         .request(prettyPrintJson(resolvedRequestJson));
 
+                url = apiTypeUtils.getQualifiedJavaApi(url);
                 executionResult = apiExecutor.executeJavaOperation(url, operationName, resolvedRequestJson);
                 break;
 
diff --git a/core/src/main/java/org/jsmart/zerocode/core/utils/ApiTypeUtils.java b/core/src/main/java/org/jsmart/zerocode/core/utils/ApiTypeUtils.java
index 68aae652..123e012e 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/utils/ApiTypeUtils.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/utils/ApiTypeUtils.java
@@ -1,11 +1,31 @@
 package org.jsmart.zerocode.core.utils;
 
+import com.google.inject.Inject;
+import com.google.inject.name.Named;
+import java.util.Arrays;
+import java.util.List;
 import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import static org.apache.commons.lang.StringUtils.isEmpty;
 import static org.jsmart.zerocode.core.constants.ZerocodeConstants.KAFKA;
 
 public class ApiTypeUtils {
+    private static final Logger LOGGER = LoggerFactory.getLogger(ApiTypeUtils.class);
+
+    public static final String JAVA_API_PROTOCOL_MAPPINGS = "java.api.protocol.mappings";
+
+    @Inject(optional = true)
+    @Named(JAVA_API_PROTOCOL_MAPPINGS)
+    private String javaApiProtoMappings;
+
+    public ApiTypeUtils() {
+    }
+
+    public ApiTypeUtils(String javaApiProtoMappings) {
+        this.javaApiProtoMappings = javaApiProtoMappings;
+    }
 
     public static ApiType apiType(String serviceName, String methodName) {
         ApiType apiType;
@@ -13,6 +33,9 @@ public class ApiTypeUtils {
         if (StringUtils.isEmpty(serviceName) || isEmpty(methodName)) {
             apiType = ApiType.NONE;
 
+        } else if (serviceName.contains("://") && !serviceName.startsWith("http")) {
+            apiType = ApiType.JAVA_CALL;
+
         } else if (serviceName != null && serviceName.contains("/")) {
             apiType = ApiType.REST_CALL;
 
@@ -27,4 +50,29 @@ public class ApiTypeUtils {
         return apiType;
     }
 
+    public String getQualifiedJavaApi(String url) {
+        if (!url.contains("://")){
+            return url;
+        }
+        return findMapping(javaApiProtoMappings, url);
+    }
+
+    private String findMapping(String javaApiProtoMappings, String url) {
+        LOGGER.info("Locating protocol service mapping for - '{}'", url);
+
+        if (isEmpty(javaApiProtoMappings)) {
+            LOGGER.error("Protocol mapping was null or empty. Please create the mappings first and then rerun");
+            throw new RuntimeException("\nProtocol mapping was null or empty.");
+        }
+        List<String> mappingList = Arrays.asList(javaApiProtoMappings.split(","));
+        String foundMapping = mappingList.stream()
+                .filter(thisMapping -> thisMapping.startsWith(url))
+                .findFirst()
+                .orElseThrow(() -> new RuntimeException("\nurl '" + url + "' Not found"));
+
+        String qualifiedClazz = foundMapping.split("\\|")[1];
+        LOGGER.info("Found protocol mapping for - '{} -> {}'", url, qualifiedClazz);
+
+        return qualifiedClazz;
+    }
 }


Test patch:
diff --git a/core/src/test/java/org/jsmart/zerocode/core/utils/ApiTypeUtilsTest.java b/core/src/test/java/org/jsmart/zerocode/core/utils/ApiTypeUtilsTest.java
new file mode 100644
index 00000000..6efbd98a
--- /dev/null
+++ b/core/src/test/java/org/jsmart/zerocode/core/utils/ApiTypeUtilsTest.java
@@ -0,0 +1,49 @@
+package org.jsmart.zerocode.core.utils;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+public class ApiTypeUtilsTest {
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    ApiTypeUtils apiTypeUtils;
+
+    @Test
+    public void testJavaApiProtoMappings_service1() {
+        String mappings = "foo://v1/s1|bar.service1";
+        apiTypeUtils = new ApiTypeUtils(mappings);
+        String qualifiedClass = apiTypeUtils.getQualifiedJavaApi("foo://v1/s1");
+        assertThat(qualifiedClass, is("bar.service1"));
+    }
+
+    @Test
+    public void testJavaApiProtoMappings_notFound() {
+        String mappings = "xyz|mno";
+        apiTypeUtils = new ApiTypeUtils(mappings);
+        expectedException.expectMessage("url 'foo://v1/s1' Not found");
+        String qualifiedClass = apiTypeUtils.getQualifiedJavaApi("foo://v1/s1");
+    }
+
+    @Test
+    public void testJavaApiProtoMappings_emptyMappings() {
+        String mappings = "";
+        apiTypeUtils = new ApiTypeUtils(mappings);
+        expectedException.expectMessage("Protocol mapping was null or empty.");
+        String qualifiedClass = apiTypeUtils.getQualifiedJavaApi("foo://v1/s1");
+    }
+
+    @Test
+    public void testJavaApiProtoMappings_nullMappings() {
+        String mappings = "";
+        apiTypeUtils = new ApiTypeUtils(mappings);
+        expectedException.expectMessage("Protocol mapping was null or empty.");
+        String qualifiedClass = apiTypeUtils.getQualifiedJavaApi("foo://v1/s1");
+    }
+
+}
\ No newline at end of file
diff --git a/http-testing/pom.xml b/http-testing/pom.xml
index 20ea3ae4..8ce2fc93 100644
--- a/http-testing/pom.xml
+++ b/http-testing/pom.xml
@@ -57,8 +57,11 @@
                 <version>2.19.1</version>
                 <configuration>
                     <includes>
-                        <!-- <include>org.jsmart.zerocode.testhelp.tests.HelloWorldGitHubSuite</include> -->
                         <include>org.jsmart.zerocode.zerocodejavaexec.pojo.OrderTest</include>
+                        <include>org.jsmart.zerocode.testhelp.tests.HelloWorldCherryPickSuite</include>
+                        <include>org.jsmart.zerocode.testhelp.tests.helloworldjavaexec.HelloWorldJavaApiAsProtocolTest</include>
+                        <include>org.jsmart.zerocode.testhelp.tests.helloworldarrayelementmatching.HelloWorldArrayElementPickerTest</include>
+                        <include>org.jsmart.zerocode.testhelp.tests.helloworldimplicitdelay.JustHelloImplicitDelayTimeOutTest</include>
                     </includes>
                 </configuration>
             </plugin>
diff --git a/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/HelloWorldSelectedGitHubSuite.java b/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/HelloWorldCherryPickSuite.java
similarity index 93%
rename from http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/HelloWorldSelectedGitHubSuite.java
rename to http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/HelloWorldCherryPickSuite.java
index f49db28c..e0315a2e 100644
--- a/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/HelloWorldSelectedGitHubSuite.java
+++ b/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/HelloWorldCherryPickSuite.java
@@ -15,5 +15,5 @@ import org.junit.runner.RunWith;
         @JsonTestCase("no_server/no_server_call_simple.json"),
         @JsonTestCase("no_server/no_server_call_multi.json"),
 })
-public class HelloWorldSelectedGitHubSuite {
+public class HelloWorldCherryPickSuite {
 }
diff --git a/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/helloworldjavaexec/HelloWorldJavaApiAsProtocolTest.java b/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/helloworldjavaexec/HelloWorldJavaApiAsProtocolTest.java
new file mode 100644
index 00000000..d1f8a838
--- /dev/null
+++ b/http-testing/src/test/java/org/jsmart/zerocode/testhelp/tests/helloworldjavaexec/HelloWorldJavaApiAsProtocolTest.java
@@ -0,0 +1,20 @@
+package org.jsmart.zerocode.testhelp.tests.helloworldjavaexec;
+
+import org.jsmart.zerocode.core.domain.JsonTestCase;
+import org.jsmart.zerocode.core.domain.TargetEnv;
+import org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@TargetEnv("myapp_proto.properties")
+@RunWith(ZeroCodeUnitRunner.class)
+public class HelloWorldJavaApiAsProtocolTest {
+
+    @Test
+    @JsonTestCase("helloworldjavaexec/hello_world_protocol_method.json")
+    public void testJava_protocol() throws Exception {
+
+    }
+
+
+}
diff --git a/http-testing/src/test/java/org/jsmart/zerocode/zerocodejavaexec/DbSqlExecutorTest.java b/http-testing/src/test/java/org/jsmart/zerocode/zerocodejavaexec/DbSqlExecutorTest.java
index f208e37a..1d8a3333 100644
--- a/http-testing/src/test/java/org/jsmart/zerocode/zerocodejavaexec/DbSqlExecutorTest.java
+++ b/http-testing/src/test/java/org/jsmart/zerocode/zerocodejavaexec/DbSqlExecutorTest.java
@@ -33,7 +33,5 @@ public class DbSqlExecutorTest {
         String json = objectMapper.writeValueAsString(resultMap);
 
         assertThat(json, is("{\"results\":[{\"id\":1,\"name\":\"Elon Musk\"},{\"id\":2,\"name\":\"Jeff Bezos\"}]}"));
-        System.out.println("json: " + json);
-
     }
 }
\ No newline at end of file
diff --git a/http-testing/src/test/resources/helloworldjavaexec/hello_world_protocol_method.json b/http-testing/src/test/resources/helloworldjavaexec/hello_world_protocol_method.json
new file mode 100644
index 00000000..890f2f86
--- /dev/null
+++ b/http-testing/src/test/resources/helloworldjavaexec/hello_world_protocol_method.json
@@ -0,0 +1,44 @@
+{
+    "scenarioName": "Java method return as JSON assertions",
+    "steps": [
+        {
+            "name": "execute_java_method",
+            "url": "pg://v1/sql-executor",
+            "operation": "fetchDbCustomers",
+            "request": "select id, name from customers",
+            "assertions": {
+                "results": [
+                    {
+                        "id": 1,
+                        "name": "Elon Musk"
+                    },
+                    {
+                        "id": 2,
+                        "name": "Jeff Bezos"
+                    }
+                ]
+
+            }
+        },
+        {
+            "name": "execute_gremlin_ql",
+            "url": "bar2://v1/gremlin-exe",
+            "operation": "fetchDbCustomers",
+            "request": "select id, name from customers",
+            "assertions": {
+                "results": [
+                    {
+                        "id": 1,
+                        "name": "Elon Musk"
+                    },
+                    {
+                        "id": 2,
+                        "name": "Jeff Bezos"
+                    }
+                ]
+
+            }
+        }
+
+    ]
+}
diff --git a/http-testing/src/test/resources/localhost_app.properties b/http-testing/src/test/resources/localhost_app.properties
index 18d13fa4..6bc2722c 100644
--- a/http-testing/src/test/resources/localhost_app.properties
+++ b/http-testing/src/test/resources/localhost_app.properties
@@ -5,4 +5,4 @@ web.application.endpoint.context=
 # To make the test PASS, update this value to 3000(3 sec) or anything higher than 2000.
 # Where 2000 milli-sec(2sec) is the delay from the Web(WireMock) server in responding
 # to the http request.
-http.max.timeout.milliseconds=1000
\ No newline at end of file
+http.max.timeout.milliseconds=2500
\ No newline at end of file
diff --git a/http-testing/src/test/resources/myapp_proto.properties b/http-testing/src/test/resources/myapp_proto.properties
new file mode 100644
index 00000000..ad982186
--- /dev/null
+++ b/http-testing/src/test/resources/myapp_proto.properties
@@ -0,0 +1,6 @@
+db.conn.user.name=localappuser
+db.conn.password=pass00rd
+java.api.protocol.mappings=pg://v1/sql-executor|org.jsmart.zerocode.zerocodejavaexec.DbSqlExecutor,\
+  bar://v1/gremlin-exe|graph.ql.Exec,\
+  bar1://v1/gremlin-exe|graph.ql.Exec,\
+  bar2://v1/gremlin-exe|org.jsmart.zerocode.zerocodejavaexec.DbSqlExecutor
\ No newline at end of file