@@ -23,6 +23,7 @@ import africa.absa.testing.scapi.rest.response.action.types.ExtractJsonResponseA
23
23
import africa .absa .testing .scapi .utils .cache .RuntimeCache
24
24
import africa .absa .testing .scapi .utils .validation .ContentValidator
25
25
import africa .absa .testing .scapi .{AssertionException , UndefinedResponseActionTypeException }
26
+ import com .jayway .jsonpath .{Configuration , JsonPath }
26
27
import spray .json ._
27
28
28
29
import scala .util .Try
@@ -45,6 +46,7 @@ object ExtractJsonResponseAction extends ResponseActions {
45
46
val action = fromString(responseAction.name.toLowerCase).getOrElse(None )
46
47
action match {
47
48
case StringFromList => validateStringFromList(responseAction)
49
+ case StringFromJsonPath => validateStringFromJsonPath(responseAction)
48
50
case _ => throw UndefinedResponseActionTypeException (responseAction.name)
49
51
}
50
52
}
@@ -61,13 +63,21 @@ object ExtractJsonResponseAction extends ResponseActions {
61
63
62
64
val action = fromString(responseAction.name.toLowerCase).getOrElse(None )
63
65
action match {
64
- case StringFromList =>
66
+ case StringFromList | StringFromJsonPath =>
65
67
val cacheKey = responseAction.params(" cacheKey" )
66
- val listIndex = responseAction.params(" listIndex" ).toInt
67
- val jsonKey = responseAction.params(" jsonKey" )
68
68
val cacheLevel = responseAction.params(" cacheLevel" )
69
69
70
- stringFromList(response, cacheKey, listIndex, jsonKey, cacheLevel)
70
+ action match {
71
+ case StringFromList =>
72
+ val jsonKey = responseAction.params(" jsonKey" )
73
+ val listIndex = responseAction.params(" listIndex" ).toInt
74
+ stringFromList(response, cacheKey, listIndex, jsonKey, cacheLevel)
75
+
76
+ case StringFromJsonPath =>
77
+ val jsonPath = responseAction.params(" jsonPath" )
78
+ stringFromJsonPath(response, jsonPath, cacheKey, cacheLevel)
79
+ }
80
+
71
81
case _ => throw UndefinedResponseActionTypeException (s " Unsupported assertion[group: extract]: ${responseAction.name}" )
72
82
}
73
83
}
@@ -137,4 +147,45 @@ object ExtractJsonResponseAction extends ResponseActions {
137
147
ContentValidator .validateIntegerString(listIndex, s " ExtractJson. $StringFromList.listIndex " )
138
148
}
139
149
150
+ /**
151
+ * This method validates the parameters of the StringFromJsonPath type of response action.
152
+ *
153
+ * @param assertion The ResponseAction instance containing the response action details.
154
+ */
155
+ private def validateStringFromJsonPath (assertion : ResponseAction ): Unit = {
156
+ val cacheKey = assertion.params.getOrElse(" cacheKey" , throw new IllegalArgumentException (s " Missing required 'cacheKey' parameter for extract $StringFromJsonPath logic " ))
157
+ val jsonPath = assertion.params.getOrElse(" jsonPath" , throw new IllegalArgumentException (s " Missing required 'jsonPath' parameter for extract $StringFromJsonPath logic " ))
158
+ val cacheLevel = assertion.params.getOrElse(" cacheLevel" , throw new IllegalArgumentException (s " Missing required 'cacheLevel' parameter for extract $StringFromJsonPath logic " ))
159
+
160
+ ContentValidator .validateNonEmptyString(jsonPath, s " ExtractJson. $StringFromJsonPath.jsonPath " )
161
+ ContentValidator .validateNonEmptyString(cacheKey, s " ExtractJson. $StringFromJsonPath.cacheKey " )
162
+ ContentValidator .validateNonEmptyString(cacheLevel, s " ExtractJson. $StringFromJsonPath.cacheLevel " )
163
+ }
164
+
165
+ /**
166
+ * This method extracts a string from a JSON array response at a given json path
167
+ * and stores it in a runtime cache with a given key and expiration level.
168
+ *
169
+ * @param response The Response instance containing the JSON body.
170
+ * @param jsonPath The json path in the JSON from which to extract the string.
171
+ * @param cacheKey The key to use when storing the extracted string in the runtime cache.
172
+ * @param runtimeCacheLevel The expiration level to use when storing the extracted string in the runtime cache.
173
+ * @return A Try[Unit] indicating whether the string extraction and caching operation was successful or not.
174
+ */
175
+ private def stringFromJsonPath (response : Response , jsonPath : String , cacheKey : String , runtimeCacheLevel : String ): Try [Unit ] = Try {
176
+ val configuration = Configuration .defaultConfiguration().addOptions(com.jayway.jsonpath.Option .SUPPRESS_EXCEPTIONS )
177
+ val json = JsonPath .using(configuration).parse(response.body)
178
+ val extractedValueOpt = Option (json.read[Any ](jsonPath))
179
+
180
+ extractedValueOpt match {
181
+ case Some (extractedValue) =>
182
+ RuntimeCache .put(key = cacheKey, value = extractedValue.toString, RuntimeCache .determineLevel(runtimeCacheLevel))
183
+ Logger .debug(s " Extracted string ' ${extractedValue.toString}' from json path ' $jsonPath' and stored it in runtime cache with key ' $cacheKey' and expiration level ' $runtimeCacheLevel'. " )
184
+
185
+ case None =>
186
+ val errMsg = s " Expected JSON path ' $jsonPath' does not exist in the response body "
187
+ Logger .error(errMsg)
188
+ throw AssertionException (errMsg)
189
+ }
190
+ }
140
191
}
0 commit comments