I was working on an API that was provided to me to learn groovy scripting for asserting a JSON response.
The API is
https://api.nasa.gov/neo/rest/v1/feed?start_date=2019-06-10&end_date=2019-06-16&api_key= *************
You can get the key from here: https://api.nasa.gov/index.html#apply-for-an-api-key
This is a small part of the json response:
"element_count": 69,
"near_earth_objects": {
"2019-05-10": [
{
"links": {"self": "http://www.neowsapp.com/rest/v1/neo/3842596?api_key=******"},
"id": "3842596",
"neo_reference_id": "3842596",
"name": "(2019 KM2)",
"nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3842596",
"absolute_magnitude_h": 25.762,
"estimated_diameter": {
"kilometers": {
"estimated_diameter_min": 0.0187134981,
"estimated_diameter_max": 0.0418446538
},
"meters": {
"estimated_diameter_min": 18.7134980647,
"estimated_diameter_max": 41.8446537694
},
"miles": {
"estimated_diameter_min": 0.011628025,
"estimated_diameter_max": 0.0260010544
},
"feet": {
"estimated_diameter_min": 61.3959929905,
"estimated_diameter_max": 137.2856138729
}
},
"is_potentially_hazardous_asteroid": false,
"close_approach_data": [ {
"close_approach_date": "2019-05-10",
"close_approach_date_full": "2019-May-10 19:38",
"epoch_date_close_approach": 1557517080000,
"relative_velocity": {
"kilometers_per_second": "2.5687395339",
"kilometers_per_hour": "9247.4623220529",
"miles_per_hour": "5746.0134633889"
},
"miss_distance": {
"astronomical": "0.0830780649",
"lunar": "32.3173672461",
"kilometers": "12428301.552761763",
"miles": "7722588.4836998094"
},
"orbiting_body": "Earth"
}],
"is_sentry_object": false
},
How can you assert that is_potentially_hazardous_asteroid is false when astronomical miss_distance is greater than 0.05 from the above API for each object(in this case each meteor)
I know how to parse the response using JsonSlurper but I can't figure out how to just compare within each object. Sorry for the poor wording as I am new to groovy and json.
I tried the following but still no luck
import groovy.json.JsonSlurper
def ResponseMessage = messageExchange.response.responseContent
def obj = new JsonSlurper().parseText(ResponseMessage)
obj.near_earth_objects.each{
date,objects->objects.each{
Object neo = it
neo.close_approach_data.each{
if((it.miss_distance.astronomical as Double)>0.05){
assert (neo.is_potentially_hazardous_asteroid == false)
}
}
}
}
error that I got:
assert (neo.is_potentially_hazardous_asteroid == false) | | | | true
false [absolute_magnitude_h:21.1, close_approach_data:
[[close_approach_date:2019-05-10, close_approach_date_full:2019-May-10
16:45, epoch_date_close_approach:1557506700000, miss_distance:
[astronomical:0.2259243576, kilometers:33797802.678078312,
lunar:87.8845751064, miles:21000980.7557402256], orbiting_body:Earth,
relative_velocity:[kilometers_per_hour:61066.4765518526,
kilometers_per_second:16.9629101533, miles_per_hour:37944.3337218998]]],
estimated_diameter:[feet:[estimated_diameter_max:1174.9652706022,
estimated_diameter_min:525.4604432536], kilometers:
[estimated_diameter_max:0.358129403, estimated_diameter_min:0.160160338],
meters:[estimated_diameter_max:358.1294030194,
estimated_diameter_min:160.1603379786], miles:
[estimated_diameter_max:0.2225312253,
estimated_diameter_min:0.0995189894]], id:3258077,
is_potentially_hazardous_asteroid:true, is_sentry_object:false, links:
[self:http://www.neowsapp.com/rest/v1/neo/3258077?api_key=****], name:
(2004 UU1), nasa_jpl_url:http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3258077,
neo_reference_id:3258077]
I should mention that I am doing this on soapui by adding a script assertion
Your code works as far as I can see...
I tweaked your script a bit, and got the same (valid afaict) result...
obj.near_earth_objects.each { date, objects ->
objects.each { neo ->
if (neo.close_approach_data.find { it.miss_distance.astronomical.toDouble() > 0.05 }) {
assert !neo.is_potentially_hazardous_asteroid
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.