Repeated query parameter in spring-cloud-contract - spring-cloud-contract

Is it possible to specify repeated query parameters through the groovy syntax in spring cloud contract 1.2.5.RELEASE?
I would like to create a contract that generates a get request to /path?type=1&type=2.
I tried the following in the contract test groovy file
urlPath('/path') {
queryParameters {
parameter type: [1, 2]
}
}
the generated RestTest contains the wrong type unfortunately:
.queryParam("type","[1, 2]")
instead of
.queryParam("type", 1, 2)

Related

How to manually set the Firestore document ID saving a new document calling Firestore API?

I am finding the following problem trying to save a new document into Firestore database by calling the related POST API and manually setting the document ID.
I am using Python but I suppose that the problem is not related to the language.
I try to explain what I have done and what is not working. My first attempt (that works but automatically set the document ID on Firestore) was:
First of all, I created this JSON document that will be the payload of my API:
# Convert the record to a dictionary
doc = {
'fields': {
'surname': {'stringValue':record[2]},
'firstName': {'stringValue':record[1]},
'socialSecurityCode': {'stringValue':codici_fiscali_list_as_string},
'city': {'stringValue':record[4]},
'personalPhone': {'stringValue':record[5]},
'personalPhone2': {'stringValue':record[6]},
'personalEmail': {'stringValue':emails_list_as_string},
'pazPres': {'stringValue':record[7]},
'pazNotes': {'stringValue':record[8]},
'pazMemo': {'stringValue':record[9]},
'isArchived': {'booleanValue':isArchived},
'isMigrated': {'booleanValue':True},
#'decomposition_keyword_search_list':{'arrayValue':{'values':decomposition_keyword_search_list}}
"decomposition_keyword_search_list":{
"arrayValue":{
"values":[
]
}
}
}
}
then I perform the API call by these lines:
api_endpoint = 'https://firestore.googleapis.com/v1/projects/MY-PROJECT-NAME/databases/(default)/documents/test/'
response = requests.post(api_endpoint, json=doc)
It works fine and it put the expected document into my test collection. But in this way, the ID was automatically generated by Firestore. For some reason, I have to use the content of a variable as ID (my ID must be the value of my record[0] that is a unique string)
So I tried to change the previous API endpoint in the following way:
api_endpoint = 'https://firestore.googleapis.com/v1/projects/MY-PROJECT-NAME/databases/(default)/documents/test/'+ record[0]
I expected that it creates a document using the record[0] as a document ID but it seems that I am wrong since I am obtaining the following error message:
Error saving document: {
"error": {
"code": 400,
"message": "Document parent name \"projects/MY-PROJECT-NAME/databases/(default)/documents/test\" lacks \"/\" at index 71.",
"status": "INVALID_ARGUMENT"
}
}
So, what is wrong? What am I missing? How can correctly manually set the ID of the document that I am creating calling the previous API?
Take a look at the documentation for creating documents. If you want to specify a document ID, it says you should pass that as a query parameter called documentId:
The client-assigned document ID to use for this document.
Optional. If not specified, an ID will be assigned by the service.

'deserializing' JSON to sqlalchemy model

I'm storing some RESTful api calls into a relational database using sqlalchemy, and I'm looking for a way to 'deserialize' or pythonify some or all of the incoming fields, for instance I might have a json object like
{
'id': 1,
'created_at': '2021-05-27T03:22:38Z',
'count': '3'
}
and I would like a way to "automatically" deserialize the data, similar to how djangorestframework serializers work where fields like "created_at" could be defined as datetime fields, and you could optionally cast 'count' as an integer, and run something like
...setup
# get the json from before as a dict
item = client.get_item()
# somehow serialize here
session = Session()
item_model = Item(**item_data[0])
session.add(item_model)
session.commit()
https://www.django-rest-framework.org/api-guide/serializers/
You have multiple well knowns module to perform serialization (independant of any framework):
Marshmallow: https://marshmallow.readthedocs.io/en/stable/
Typesystem (by the creator of DRF): https://github.com/encode/typesystem
You can also do your own serializer, based on DRF serializer code, if your use case is simple, or just perform a lookup of your dict fields that perform validation/transformation.

Spring cloud contract: how to verify an array list (Kotlin based project)

I would like to write a groovy contract to verify an array list with string values.
Lets say I have an object:
data class MyDataObject(val messageList: List<String>)
my contract is the following:
package contracts
import org.springframework.cloud.contract.spec.Contract
Contract.make {
name("retrieve_list_of_objects")
description("""
given:
you want to have a list of MyObjects
when:
you get the list
then:
you have the list
""")
request {
method 'GET'
url '/10/my-objects'
headers {
contentType(applicationJson())
}
}
response {
status 200
body(
[
messageList: ["23412341324"]
]
)
headers {
contentType(applicationJson())
}
} }
the problem is that created test is translated to:
assertThatJson(parsedJson).array("['messageList']").contains("23412341324").value();
and that results in:
com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['messageList'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
The question is: how can I write my contract to create the following test:
assertThatJson(parsedJson).array("['messageList']").contains("23412341324");
I ran your snippet in my project and it generated a test that looks like this (I don't know why my test generating looks different than yours)
MockMvcRequestSpecification request = given()
.header("Accept", "application/json")
.body("{\"messageList\":[\"23412341324\"]}");
If I am reading your question right, you want the body to be a list of MyObjects, and not just one.
I think the problem is that you need to surround MyObject with one more set of square brackets, if indeed you want this to verify a list of MyObjects.
body(
[[
messageList: ["23412341324"]
]]
)
In General
Use SQUARE BRACKETS to make objects (yes i know in JSON square brackets are for arrays, its weird, i didn't invent it)
You can surround field names with quotes or without, they both seem to work.
body([
stringField1: value(regex(".*")),
stringField2: value(regex(alphaNumeric()),
innerObject1: [
innerStringField1: "Hardcoded1",
innerIntegerField1: anyInteger()
]
])
Wait? How do I make JSON lists then if square brackets are for objects?
Double square brackets. Seriously.
body(
[[
stringFieldOfObjectInList: regex(".*")
]]
)

How to get the table name in AWS dynamodb trigger function?

I am new with AWS and working on creating a lambda function on Python. The function will get the dynamodb table stream and write to a file in s3. Here the name of the file should be the name of the table.
Can someone please tell me how to get the table name if the trigger that is invoking the lambda function?
Thanks for help.
Since you mentioned you are new to AWS, I am going to answer descriptively.
I am assuming that you have set 'Stream enabled' setting for your DynamoDB table to 'Yes', and have set up this as an event source to your lambda function.
This is how I got the table name from the stream that invoked my lambda function -
def lambda_handler(event, context):
print(json.dumps(event, indent=2)) # Shows what's in the event object
for record in event['Records']:
ddbARN = record['eventSourceARN']
ddbTable = ddbARN.split(':')[5].split('/')[1]
print("DynamoDB table name: " + ddbTable)
return 'Successfully processed records.'
Basically, the event object that contains all the information about a particular DynamoDB stream that was responsible for that particular lambda function invoke, contains a parameter eventSourceARN. This eventSourceARN is the ARN (Amazon Resource Number) that uniquely identifies your DynamoDB table from which the event occurred.
This is a sample value for eventSourceARN -
arn:aws:dynamodb:us-east-1:111111111111:table/test/stream/2020-10-10T08:18:22.385
Notice the bold text above - test; this is the table name you are looking for.
In the line ddbTable = ddbARN.split(':')[5].split('/')[1] above, I have tried to split the entire ARN by ':' first, and then by '/' in order to get the value test. Once you have this value, you can call S3 APIs to write to a file in S3 with the same name.
Hope this helps.
Please note that eventSourceArn is not always provided. From my testing today, I didn't see eventSourceArn presented in record. You can also refer to the links:
Issue: https://github.com/aws/aws-sdk-js/issues/2226
API: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_Record.html
One way to do it will be via pattern matching in Scala using regex:
val ddbArnRegex: Regex = """arn:aws:dynamodb:(.+):(.+):table/(.+)/stream/(.+)""".r
def parseTableName(ddbARN: String): Option[String] = {
if (null == ddbARN) None
ddbARN match {
case ddbArnRegex(_, _, table, _) => Some(table)
case _ => None
}
}

what is #params in Iron:router

with meteor's IronRouter, I'm trying to use the this.params object elsewhere, but confused as to what it is. It seems to be a zero length array, that is actually an object with named methods after the path components.
# coffee
#route 'magnets',
path: '/magnets/lesson/:lessonCname'
data: ->
if #ready()
debugger;
console.log("route.params", #params)
with this code, in the debug console I will get:
this.params
[]
this.params.lessonCname
"despite-magnets-01"
typeof(this.params)
"object"
this.params.length
0
this.ready()
but in passing the params object to a server method, the methods (ie "lessonCname") disappear.
If my understanding is correct, then the near-term question is what is the best way to retrieve/convert these methods to {property:value} so they can be serialized and passed to server calls?
There are two easy ways of solving your problem, you can either set a global variable from within the data scope (but this is considered bad practice, at least IMO) or you can use the "data" function, which returns the data context for the current template:
data: ->
window._globalscopedata = #params.whatever #setting global variable
return someCollection.findOne #returns data context
_id: #params.whatever
when proccessing this route I will have the whatever param available in _globalscoredata and my document available in the template context.
Take a look at the source code for retrieving the parameters from a path. params is an array, but may have named properties. To iterate over everything, you can use the for in loop:
for(var x in myArray){
// Do something.
}
In this way, you can copy over everything to a new object (there may be a simpler way to create a copy).
The params property attached to a RouteController is an object with the following properties :
hash : the value of the URL hash.
query : an object consisting of key/value pairs representing the query string.
a list of URL fragments with their name and actual value.
Let's take an example, for this route definition :
// using iron:router#1.0.0-pre2 new route definition
Router.route("/posts/:slug");
And this URL typed in the browser address bar : /posts/first-post#comments?lang=en
We can use the console to find out precisely what params will actually contain :
> Router.current().params
Which will display this result :
Object {
hash: "comments",
slug: "first-post",
query: {
lang: "en"
}
}
Here slug is already a property of the params object whose value is "first-post", this is not a method.
If you want to extract from params these URL fragments as an object of key/value pairs, you can use underscore omit :
// getting rid of the hash and the query string
var parameters=_.omit(this.params,["hash","query"]);

Resources