Repository: authorjapps/zerocode
Pull request #109 (2018-07-25 22:27:01)

ISS-99 queryParams as key-value map 


Patch:
diff --git a/src/main/java/org/jsmart/zerocode/core/httpclient/ssl/SslTrustHttpClient.java b/src/main/java/org/jsmart/zerocode/core/httpclient/ssl/SslTrustHttpClient.java
index 989d1a13..27c4bbba 100644
--- a/src/main/java/org/jsmart/zerocode/core/httpclient/ssl/SslTrustHttpClient.java
+++ b/src/main/java/org/jsmart/zerocode/core/httpclient/ssl/SslTrustHttpClient.java
@@ -38,7 +38,7 @@ public class SslTrustHttpClient extends BasicHttpClient{
      */
     @Override
     public CloseableHttpClient createHttpClient() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
-        LOGGER.info("###Used SSL Enabled Http Client for both Http and Https connections");
+        LOGGER.info("###Used SSL Enabled Http Client for http/https/TLS connections");
 
         SSLContext sslContext = new SSLContextBuilder()
                 .loadTrustMaterial(null, (certificate, authType) -> true).build();
diff --git a/src/main/java/org/jsmart/zerocode/core/httpclient/utils/UrlQueryParamsUtils.java b/src/main/java/org/jsmart/zerocode/core/httpclient/utils/UrlQueryParamsUtils.java
index 46356e66..9c193799 100644
--- a/src/main/java/org/jsmart/zerocode/core/httpclient/utils/UrlQueryParamsUtils.java
+++ b/src/main/java/org/jsmart/zerocode/core/httpclient/utils/UrlQueryParamsUtils.java
@@ -1,29 +1,33 @@
 package org.jsmart.zerocode.core.httpclient.utils;
 
-import org.jsmart.zerocode.core.utils.HelperJsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import java.util.Map;
 
 import static java.lang.String.format;
 
 public class UrlQueryParamsUtils {
+    private static final Logger LOGGER = LoggerFactory.getLogger(UrlQueryParamsUtils.class);
 
     public static String setQueryParams(String httpUrl, Map<String, Object> queryParams) {
         String qualifiedQueryParams = createQualifiedQueryParams(queryParams);
         httpUrl = httpUrl + qualifiedQueryParams;
+
+        LOGGER.info("### Effective url is : " + httpUrl);
         return httpUrl;
     }
 
-    private static String createQualifiedQueryParams(Object queryParams) {
-        String qualifiedQueryParam = "?";
-        Map queryParamsMap = HelperJsonUtils.readHeadersAsMap(queryParams);
+    protected static String createQualifiedQueryParams(Map<String, Object> queryParamsMap) {
+        String qualifiedQueryParam = "";
         for (Object key : queryParamsMap.keySet()) {
-            if ("?".equals(qualifiedQueryParam)) {
-                qualifiedQueryParam = qualifiedQueryParam + format("%s=%s", key, queryParamsMap.get(key));
+            if ("".equals(qualifiedQueryParam)) {
+                qualifiedQueryParam = "?" + format("%s=%s", key, queryParamsMap.get(key));
             } else {
                 qualifiedQueryParam = qualifiedQueryParam + format("&%s=%s", key, queryParamsMap.get(key));
             }
         }
+        LOGGER.info("### qualifiedQueryParams : " + qualifiedQueryParam);
         return qualifiedQueryParam;
     }
 
diff --git a/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java b/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java
index 27d580b1..d637ffc9 100644
--- a/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java
+++ b/src/main/java/org/jsmart/zerocode/core/runner/ZeroCodeUnitRunner.java
@@ -34,8 +34,8 @@ public class ZeroCodeUnitRunner extends BlockJUnit4ClassRunner {
     protected boolean testRunCompleted;
     private ZeroCodeMultiStepsScenarioRunner zeroCodeMultiStepsScenarioRunner;
     private final Class<?> testClass;
-    Injector injector;
-    SmartUtils smartUtils;
+    private Injector injector;
+    private SmartUtils smartUtils;
 
     private HostProperties hostProperties;
     private String host;


Test patch:
diff --git a/src/test/java/org/jsmart/zerocode/core/httpclient/utils/UrlQueryParamsUtilsTest.java b/src/test/java/org/jsmart/zerocode/core/httpclient/utils/UrlQueryParamsUtilsTest.java
new file mode 100644
index 00000000..b8273174
--- /dev/null
+++ b/src/test/java/org/jsmart/zerocode/core/httpclient/utils/UrlQueryParamsUtilsTest.java
@@ -0,0 +1,33 @@
+package org.jsmart.zerocode.core.httpclient.utils;
+
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+
+public class UrlQueryParamsUtilsTest {
+
+    @Test
+    public void testNotNull_queryParams() {
+        Map<String, Object> queryParamsMap = new HashMap<>();
+        queryParamsMap.put("q1", "value1");
+        queryParamsMap.put("q2", "value2");
+
+        String qualifiedQueryParams = UrlQueryParamsUtils.createQualifiedQueryParams(queryParamsMap);
+
+        assertThat(qualifiedQueryParams, is("?q1=value1&q2=value2"));
+    }
+
+    @Test
+    public void testNull_queryParams() {
+        Map<String, Object> queryParamsMap = new HashMap<>();
+        String qualifiedQueryParams = UrlQueryParamsUtils.createQualifiedQueryParams(queryParamsMap);
+
+        assertThat(qualifiedQueryParams, is(""));
+
+    }
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/jsmart/zerocode/core/verification/queryparams/QueryParamVerifyTest.java b/src/test/java/org/jsmart/zerocode/core/verification/queryparams/QueryParamVerifyTest.java
new file mode 100644
index 00000000..5a32dc3b
--- /dev/null
+++ b/src/test/java/org/jsmart/zerocode/core/verification/queryparams/QueryParamVerifyTest.java
@@ -0,0 +1,27 @@
+package org.jsmart.zerocode.core.verification.queryparams;
+
+import org.jsmart.zerocode.core.domain.HostProperties;
+import org.jsmart.zerocode.core.domain.JsonTestCase;
+import org.jsmart.zerocode.core.tests.customrunner.TestOnlyZeroCodeUnitRunner;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@HostProperties(host="http://localhost", port=9998, context = "")
+@RunWith(TestOnlyZeroCodeUnitRunner.class)
+public class QueryParamVerifyTest {
+    
+    /**
+     * Mock end points are in test/resources: simulators/test_purpose_end_points.json
+     */
+
+    @Test
+    @JsonTestCase("query_params/request_with_query_paramas_map_test.json")
+    public void testQueryParamsAsMap() throws Exception {
+    
+    }
+
+}
+
+
+
diff --git a/src/test/resources/query_params/request_with_query_paramas_map_test.json b/src/test/resources/query_params/request_with_query_paramas_map_test.json
new file mode 100644
index 00000000..9285d329
--- /dev/null
+++ b/src/test/resources/query_params/request_with_query_paramas_map_test.json
@@ -0,0 +1,58 @@
+{
+    "scenarioName": "request with query params as map test",
+    "steps": [
+        {
+            "name": "find_match",
+            "url": "/api/v1/search/persons",
+            "operation": "GET",
+            "request": {
+                "queryParams": {
+                    "lang": "Amazing",
+                    "city": "Lon"
+                }
+            },
+            "assertions": {
+                "status": 200,
+                "body": {
+                    "exactMatches": true,
+                    "name": "Mr Bean",
+                    "lang": "Amazing",
+                    "city": "Lon"
+                }
+            }
+        },
+        {
+            "name": "find_match_introduce_error",
+            "url": "/api/v1/search/persons?lang=Amazing&city=Lon",
+            "operation": "GET",
+            "request": {
+                "queryParams": {
+                    "lang": "Amazing",
+                    "city": "Lon"
+                }
+            },
+            "assertions": {
+                "status": 404,
+                "body": {
+                    "errorId" : "$CONTAINS.STRING:could.not.find.end.point:/api/v1/search/persons?lang=Amazing&city=Lon?lang=Amazing&city=Lon"
+                }
+            }
+        },
+        {
+            "name": "find_match_same",
+            "url": "/api/v1/search/persons?lang=Amazing&city=Lon",
+            "operation": "GET",
+            "request": {
+            },
+            "assertions": {
+                "status": 200,
+                "body" : {
+                    "exactMatches" : true,
+                    "name" : "Mr Bean",
+                    "lang" : "Amazing",
+                    "city" : "Lon"
+                }
+            }
+        }
+    ]
+}
diff --git a/src/test/resources/simulators/test_purpose_end_points.json b/src/test/resources/simulators/test_purpose_end_points.json
index 785e335c..51af93e6 100644
--- a/src/test/resources/simulators/test_purpose_end_points.json
+++ b/src/test/resources/simulators/test_purpose_end_points.json
@@ -85,6 +85,21 @@
                     "zeroKey": "zeroValue"
                 }
             }
+        },
+        {
+            "name": "request with query params",
+            "operation": "GET",
+            "url": "/api/v1/search/persons?lang=Amazing&city=Lon",
+            "response": {
+                "status": 200,
+                "body": {
+                    "exactMatches": true,
+                    "name": "Mr Bean",
+                    "lang": "Amazing",
+                    "city": "Lon"
+                }
+            }
         }
+
     ]
 }
\ No newline at end of file