I've been trying to make groovy script that parses jdbc and REST response, put the results in a model and then compare them. I am following this answer: Dynamically compare Rest XML/JSON response and JDBC using groovy array in SoapUI, but not with much success. My jdbc response is below:
<Results>
<ResultSet fetchSize="128">
<Row rowNumber="1">
<ID>BCE448A4DFB94C6892D957DB8275D2AC</ID>
<NAME>SevDealRecord</NAME>
<AMOUNT/>
<CREATIONDATE>2012-06-20 11:31:48.0</CREATIONDATE>
<MODIFICATIONDATE>2012-06-20 15:20:02.0</MODIFICATIONDATE>
<CURRENCY>EUR</CURRENCY>
<REFERENCEDATE>2012-06-20 00:00:00.0</REFERENCEDATE>
<STATUSCODE>DPE_2</STATUSCODE>
<STATUSDESCRIPTION>2 - Preliminary evaluation in progress (Direct PE)</STATUSDESCRIPTION>
<ASSIGNEDTOUSERIQID>E506565555A6486FBA8FCC431F4E979E</ASSIGNEDTOUSERIQID>
<ASSIGNTOUSERDISPLAYNAME>NMISO</ASSIGNTOUSERDISPLAYNAME>
<WORKFLOWID>140AE208F9334FB9946BFEAF5C89CE18</WORKFLOWID>
<WORKFLOWNAME>1 - Direct Private Equity</WORKFLOWNAME>
</Row>
<Row rowNumber="2">
<ID>D4DBB1B906A04DE49AB1FF3EE4653180</ID>
<NAME>T28678</NAME>
<AMOUNT/>
<CREATIONDATE>2012-06-21 13:45:36.0</CREATIONDATE>
<MODIFICATIONDATE>2012-06-21 13:46:03.0</MODIFICATIONDATE>
<CURRENCY>EUR</CURRENCY>
<REFERENCEDATE>2012-06-21 00:00:00.0</REFERENCEDATE>
<STATUSCODE>DRAFT</STATUSCODE>
<STATUSDESCRIPTION>Draft{F}Brouillon</STATUSDESCRIPTION>
<ASSIGNEDTOUSERIQID>E506565555A6486FBA8FCC431F4E979E</ASSIGNEDTOUSERIQID>
<ASSIGNTOUSERDISPLAYNAME>NMISO</ASSIGNTOUSERDISPLAYNAME>
<WORKFLOWID/>
<WORKFLOWNAME/>
</Row>
And here is REST response:
[{
"id": "12CF6F8DA3B148D98D63A428EC7F8D7B",
"name": "アコム株式会社",
"amount1": null,
"creationDate": null,
"modificationDate": "2019-01-14T16:28:21.027+00:00",
"currency": "USD",
"referenceDate": "2019-01-04T00:00:00+00:00",
"status": {
"code": "DRAFT",
"description": "Draft"
},
"assignedToUser": {
"id": "E506565555A6486FBA8FCC431F4E979E",
"displayName": "NMISO"
},
"assignedToGroup": null,
"workflow": null
}, {
"id": "AA4F19E5C8B34222865EFED293D52146",
"name": "Lürssen",
"amount1": null,
"creationDate": null,
"modificationDate": "2019-01-14T16:28:20.963+00:00",
"currency": "USD",
"referenceDate": "2019-01-04T00:00:00+00:00",
"status": {
"code": "DRAFT",
"description": "Draft"
},
"assignedToUser": {
"id": "E506565555A6486FBA8FCC431F4E979E",
"displayName": "NMISO"
},
"assignedToGroup": null,
"workflow": null
},
What I tried:
#groovy.transform.Canonical
class Model {
def id
def name
def amount1
def creationDate
def modificationDate
def currency
def referenceDate
def statusCode
def statusDescription
def assignedToUserIqid
def assignedToUserDisplayName
def assignedToGroup
def workflowId
def workflowName
// this will accept jdbc row
def buildJdbcData(row) {
row.with {
id = ID
name = NAME
amount1 = AMOUNT
creationDate = CREATIONDATE
modificationDate = MODIFICATIONDATE
currency = CURRENCY
referenceDate = REFERENCEDATE
statusCode = STATUSCODE
statusDescription = STATUSDESCRIPTION
assignedToUserDisplayName = ASSIGNTOUSERDISPLAYNAME
assignedToGroup = ASSIGNTOUSERDISPLAYNAME
workflowId = WORKFLOWID
workflowName = WORKFLOWNAME
}
}
def buildJsonData(slurp){
id = slurp.id
name = slurp.name
amount1 = slurp.amount1
creationDate = slurp.creationDate
modificationDate = slurp.modificationDate
currency = slurp.currency
referenceDate = slurp.referenceDate
statusCode = slurp.status.code
statusDescription = slurp.status.description
assignedToUserIqid = slurp.assignedToUser.id
assignedToUserDisplayName = slurp.assignedToUser.displayName
assignedToGroup = slurp.assignedToGroup
workflowId = slurp.workflow
}
}
def jdbcResponse = context.expand('${JDBC_DealList#ResponseAsXml}')
def results = new XmlSlurper().parseText(jdbcResponse)
def jdbcDataObjects = []
results.ResultSet.Row.each { row ->
jdbcDataObjects.add(new Model().buildJdbcData(row)) //Objects not added properly to the model
}
log.info jdbcDataObjects
def jsonResponse = testRunner.testCase.testSteps["Deals"].testRequest.response.contentAsString
def jsonObjects = new JsonSlurper().parseText(jsonResponse)
log.info jsonObjects
def jsonDataObjects = [] jsonDataObjects.add(new Model().buildJsonData(jsonObjects))
Now, the log.info jdbcDataObjects is giving me the WORKFLOWNAME elements from jdbc response. And log.info jsonObjects is giving me the whole JSON model, and I am not sure how to add all the elements to the above defined Model? Some help would be much appreciated.
In previous project ,we did this but for Soap not REST.but I believe, you can follow same approach.
1.we put query in Execel -ESMQuery1
2.Execel had 2 columns. First node to compare,second DB value to compare.
Eg- //soap/xmlnode1 ESMQuery1(UserName)
Here UserName is column name.
3.You simply need to create loop for all nodes mentioned and resolve ESMQuery(UserName).
Instead of xmlpath you can use Json path.
Thanks.
Related
So what trying to do is get some values I get from an API, pass onto an array to THEN pass it through another API and then add up the data it gets.
I already made this fetching it from 1 api, but I don't know how to fetch all the values from one api, pass it into an array, and fetch each value for each item.
let req1 = `https://inventory.roblox.com/v1/users/${imageURL}/assets/collectibles?sortOrder=Asc&limit=100`
let req2 = `https://users.roblox.com/v1/users/${imageURL}`
function getItems() {
return axios.get(req1)
}
function getUser() {
return axios.get(req2)
}
await Promise.all([getItems(), getUser(),getValue()])
.then(function(response){
let rap = response[0].data.data
let items = response[0].data.data.assetId
let itemarray = 0
let value = response[2].data.items[items array goes here][4]
let sum = 0
for(let i = 0; i < rap.length; i++) {sum += response[0].data.data[i].recentAveragePrice}
this is what I have so far. Any help will be appreciated. Thanks.
Edit:
Heres what req1 spits out:
{
"previousPageCursor": null,
"nextPageCursor": "49034821991_1_a887f8ff3a6b076aafd71c91a5a4a4fb",
"data": [
{
"userAssetId": 23726188,
"serialNumber": null,
"assetId": 7636350,
"name": "Supa Fly Cap",
"recentAveragePrice": 23535,
"originalPrice": null,
"assetStock": null,
"buildersClubMembershipType": 0
},
{
"userAssetId": 1329942875,
"serialNumber": 13717,
"assetId": 113325603,
"name": "Recycled Cardboard Shades",
"recentAveragePrice": 5703,
"originalPrice": 25,
"assetStock": 15000,
"buildersClubMembershipType": 0
},
```
req2 doesnt apply for this part of the code :p
I'm following the tutorials https://quarkus.io/guides/rest-data-panache and https://quarkus.io/guides/mongodb-panache to implement a simple MongoDB entity and resource with Quarkus Panache MongoDB.
Here's what I have so far:
#MongoEntity(collection = "guests")
class GuestEntity(
var id: ObjectId? = null,
var name: String? = null
)
#ApplicationScoped
class GuestRepository: PanacheMongoRepository<GuestEntity>
interface GuestResource: PanacheMongoRepositoryResource<GuestRepository, GuestEntity, ObjectId>
When running this, I can create a document by calling
POST localhost:8080/guest
Content-Type: application/json
{
"name": "Foo"
}
The response contains the created entity
{
"id": {
"timestamp": 1618306409,
"date": 1618306409000
},
"name": "Foo"
}
Notice, how the id field is an object whereas I would like it to be a string.
It turns out that the application was using quarkus-resteasy instead of quarkus-resteasy-jackson.
Once the proper dependency was in place, everything worked as expected
To serialize the id field as a String, apply the following annotation to the id field:
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import io.quarkus.mongodb.panache.jackson.ObjectIdSerializer
#MongoEntity(collection = "guests")
class GuestEntity(
// important: apply the annotation to the field
#field:JsonSerialize(using = ObjectIdSerializer::class)
var id: ObjectId? = null,
var name: String
)
Now the response is
{
"id": "607567590ced4472ce95be23",
"name": "Foo"
}
I have a MongoDB entity that has a list of tags:
#Document
#TypeAlias("project")
data class Project(#Id var id: String?,
val name: String,
val tags: MutableList<Tag>)
I would like to look for Project's containing a subset of the tags.
For example given these proyects:
projectService.save(Project(null, "someProject11",
mutableListOf(Tag("area","IT"), Tag("department","Architecture"))))
projectService.save(Project(null, "someProject12",
mutableListOf(Tag("area","IT"), Tag("department","HR"))))
If I define my repository as follows:
interface ProjectRepository : ReactiveCrudRepository<Project, String> {
fun findByTags(tags : List<Tag>): Flux<Project>
}
Looking for a subset doesn't work, e.g: projects with Tag("area","IT") should return two results but actually returns 0. The underlying mongo query is:
{"find": "project", "filter": {"tags": [{"key": "area", "value": "IT"}]}
It only works passing the complete content of the list listOf(Tag("area","IT"), Tag("department","Architecture")):
{"find": "project", "filter": {"tags": [{"key": "area", "value": "IT"},{"key": "department", "value": "Architecture"}]}
How can I query for entities containing a subset of the list?
Solved using Criteria and the $elemMatch operator:
interface CustomProjectRepository {
fun findByTags(tags: List<Tag>): Flux<Project>
}
class CustomProjectRepositoryImpl(private val mongoTemplate: ReactiveMongoTemplate) : CustomProjectRepository {
override fun findByTags(tags: List<Tag>): Flux<Project> {
val query = Query()
val criterias = mutableListOf<Criteria>()
tags.forEach {
criterias.add(Criteria.where("tags").elemMatch(Criteria.where("key").`is`(it.key).and("value").`is`(it.value)))
}
query.addCriteria(Criteria().andOperator(* criterias.toTypedArray()))
return mongoTemplate.find(query, Project::class.java)
}
}
interface ProjectRepository : ReactiveCrudRepository<Project, String>, CustomProjectRepository {
fun findByClientIdAndId(clientId: String, id: String): Mono<Project>
fun findByClientId(clientId: String): Flux<Project>
}
I work with scala play and I use WS to make get a response from an URL.
My JSON example :
[
{
"object": "001",
"object-description": "MODEL",
"criterion": "TW3",
"criterion-description": "MODELE X07"
},
{
"object": "002",
"object-description": "TYPE",
"criterion": "STANDA",
"criterion-description": "STANDARD TYPE"
}, ...
I want to get only "criterion" field where "object" equal "002". So, in this example the value "STANDA".
A Test:
ws.url(
url)
.get()
.map { response =>
Right((response.json \ "object="002"" \\ "criterion").map(_.as[String]))
}
How I can do that ?
Thanks for your help.
Your can transform the whole response into scala classes using automated formatters and then operate on those.
case class Data(`object`: String, criterion: String)
implicit val dataRead = Json.reads[Data]
response.json.as[List[Data]]
.filter(_.`object` == "002")
.map(_.criterion)
I am getting an Error when running a script in the Postman Tests tab, When trying to check that a property is not null.
My JSON response:
{
"statusMessage": "Success",
"timeStamp": "2018-01-23 05:13:16.7",
"numberOfRecords": 7,
"parties": [
{
"shippingAddress": null,
"shippingDetails": null,
"paExpirationDate": "",
"historyDate": "01/22/2018",
"renewal": {
"renewalRx": "N",
"priorRxNumber": "",
"priorSB": "",
"priorFillNumber": ""
},
"noOfRefillingRemaining": "1",
"ndc": "00074-3799-06",
"rxId": "7004942",
"fillingNumber": "0"
},
{
"shippingAddress": {
"addressLine1": "2150 St",
"addressLine2": "Home Line 2",
"city": "Bronx",
"state": "NY",
"zipCode": "10453",
"addressSeqNumber": "1",
"medFacilityIndicator": "N"
}
}
]
}
My postman script is:
var jsonData = JSON.parse(responseBody);
var parties = jsonData.parties;
parties.forEach(function(data){
if(data.shippingAddress!==null && data.shippingAddress.addressLine1 !== null ){
postman.setEnvironmentVariable("addressLine1",data.shippingAddress.addressLine1);
}
I am getting the following error:
"Error running tests for results: TypeError: Cannot read property 'addressLine1' of null"
You could try this, I changed your code slightly but this would work:
var parties = pm.response.json().parties
for(i = 0; i < parties.length; i++) {
if(parties[i].shippingAddress !== null) {
pm.environment.set("addressLine1", parties[i].shippingAddress.addressLine1)
}
}
I tested this locally with the Schema that you provided and that it wrote 2150 St to my environment file.
The schema you posted doesn't seem to be a complete one, I think that the parties array has a shippingAddress property which is either null or it is an object containing the shippingAddress details - I might be wrong but I can't get my head around the data that you posted.
I don't think what you're searching on the in the if statement is correct and it wouldn't work the way you have it because if the first condition is null (like in your response data) it wouldn't ever meet the second condition because the object wouldn't be there and that shippingAddress.addressLine1 reference would always show that error.
Or you could use your code like this:
var jsonData = JSON.parse(responseBody)
var parties = jsonData.parties
parties.forEach(function(data) {
if(data.shippingAddress !== null) {
postman.setEnvironmentVariable("addressLine1",data.shippingAddress.addressLine1)
}
})