Gatling uri params replacement - scala

I have a feeder file that contains following json:
{
"method": "POST",
"uri": "/v2/mydata/{timestamp}",
"payload": [
{
"productId": "abcd",
"timezoneOffset": 420
}
]
}
Following is the Gatling test code that fetch uri,method and payload.
val allRecommendationRequestRecords = jsonFile(scenarioAllRecommendationRequests).records
val allRecommendationPostRequest = allRecommendationRequestRecords.filter(record => record("method").toString.toUpperCase == "POST")
val allRecommendationGetRequest = allRecommendationRequestRecords.filter(record => record("method").toString.toUpperCase == "GET")
val totalPostRequestCount = allRecommendationPostRequest.length
val totalGetRequestCount = allRecommendationGetRequest.length
/**
* Triggers
*/
val triggerForPostRequests = feed(allRecommendationPostRequest.circular)
.exec(http(simulationTitle).post( "${uri}").body(StringBody("${payload.jsonStringify()}".stripMargin)).check(status.is(200)))
val triggerForGetRequests = feed(allRecommendationGetRequest.circular)
.exec(http(simulationTitle).get("${uri}").check(status.is(200)))
val scenarioForPostRequests = scenario(simulationTitle + " post requests").exec(triggerForPostRequests)
val scenarioForGetRequests = scenario(simulationTitle+ " get requests").exec(triggerForGetRequests)
var httpProtocol = Recommendation.httpProtocol.shareConnections
/**
* setup scenario using weight*
*/
Now I want to replace uri {timestamp} with a randomUUID().toString

Related

Wrong PUT method gets triggered in Akka Http using scala

In my APIendpoint class, I have 2 PUT methods lets say updateA and UpdateB but when I'm trying to hit UpdateB using swagger it resolves to a UpdateA everytime. I dunno what I'm doing wrong because the code seems ok to me. Any help is appreciated.
#Api(value = "/connectroutes", produces = "application/json", consumes = "application/json",
authorizations = Array(new Authorization(value = "")))
#Produces(Array(MediaType.APPLICATION_JSON))
def routes: Route = {
pathPrefix("sampleroute") {
authenticateBasic(realm = "sample realm", authenticator) { authenticationResult =>
updateA ~
updateB
}
}
#PUT
#Path("{name}")
#Produces(Array(MediaType.APPLICATION_JSON))
#Operation(summary = "sample", description = "UPDATE A",
parameters = Array(new Parameter(name = "name", in = ParameterIn.PATH, description = "updateA name", required = true)),
requestBody = new RequestBody(content = Array(new Content(schema = new Schema(implementation = classOf[A]), mediaType = MediaType.APPLICATION_JSON))),
)
def updateA: Route = {
path(Segment) { name =>
put {
entity(as[A]) { a: A => {
complete(updateAMethod(name, a))
}
}
}
}
}
#PUT
#Path("updateb")
#Produces(Array(MediaType.APPLICATION_JSON))
#Authorization("basicAuth")
#Operation(summary = "Sample", description = "Update B",
requestBody = new RequestBody(content = Array(new Content(schema = new Schema(implementation = classOf[String]), mediaType = MediaType.APPLICATION_JSON))),
)
def updateB: Route = {
path("updateb" / Segment) { namespace =>
put {
entity(as[String]) { updatedval: String => {
complete(updatedVal))
}
}
}
}
}

Synchronizing clients and server

I'm making a distributed sorting system, where each machine (client) has a specific range of values (partition) to store. I don't have any problem sending and receiving data, but the synchronization is my problem. Before retrieving the wanted partition from the server, it must first arrive to the server from one of the other clients.
I've tried with delay and various other RPC calls to try to try to synchronize the clients and server, but the clients run out of synchronization, and the program breaks. I think I have to incorporate future or promise, but I'm not sure how.
Every client has a unique ID (from 0 and up), and will store partitions according to that number. One line of data looks like this: AsfAGHM5om 00000000000000000000000000000000 0000222200002222000022220000222200002222000000001111
EDIT: updated code for example
Client
val numberOfClients = 3
var sentPartitionsCounter = 0
while(sentPartitionsCounter < numberOfClients) {
if(sentPartitionsCounter != id){
sendUnwantedPartition("src/main/scala/testFile."+sentPartitionsCounter.toString) //"partition."+client.sentPartitionsCounter
sentPartitionsCounter += 1
}
client.getWantedPartition()
}
def sendUnwantedPartition(filename: String): Unit = {
val dataList = Source.fromFile(filename).getLines.toList
val dataSeq = for {
dataLine <- dataList
dataValues = dataLine.split(" ", 2)
} yield (Data(key = dataValues(0), value = dataValues(1)))
val partitionID = filename takeRight 1
val request = Dataset(data = dataSeq, partitionID = partitionID.toInt)
val response = blockingStub.getUnwantedPartitions(request)
print("sent partitions")
}
def getWantedPartition(): Unit = {
val request = ID(id = id)
val response = blockingStub.sendWantedPartitions(request)
val wantedPartitions = new File("clientFiles/wantedPartitions"+id.toString+".txt")
val printWriter: PrintWriter = new PrintWriter(new FileWriter(wantedPartitions, true));
if(!wantedPartitions.exists()){
wantedPartitions.createNewFile()
}
for {
data <- response.data
} yield (printWriter.append(data.key + " " + data.value + "\n"))
printWriter.close();
}
Server
override def getUnwantedPartitions(req: Dataset) = {
val filename = "serverPartitions/partition"+req.partitionID+".txt"
val partition = new File(filename)
val printWriter: PrintWriter = new PrintWriter(new FileWriter(partition, true));
if(!partition.exists()){
partition.createNewFile()
}
for {
data <- req.data
} yield (printWriter.append(data.key + " " + data.value + "\n"))
printWriter.close();
Future.successful(Text("Got unwanted data"))
}
override def sendWantedPartitions(req: ID) = {
val filename = "serverPartitions/partition"+req.id+".txt"
try {
val dataList = Source.fromFile(filename).getLines.toList
val dataSeq = for {
dataLine <- dataList
dataValues = dataLine.split(" ", 2)
} yield (Data(key = dataValues(0), value = dataValues(1)))
val reply = Dataset(data = dataSeq, partitionID = req.id)
new File(filename).delete()
Future.successful(reply)
} catch {
// Partition hasn't been received yet
case e: FileNotFoundException => Future.successful(Dataset())
}
}
Proto
syntax = "proto3";
package protoPackage;
service DistrSorting {
rpc getUnwantedPartitions(Dataset) returns (Text) {}
rpc sendWantedPartitions(ID) returns (Dataset) {}
}
message ID {
int32 id = 1;
}
message Text {
string text = 1;
}
message Dataset {
repeated Data data = 1;
int32 partitionID = 2;
}
message Data {
string key = 1;
string value = 2;
}

Play Framework - Respond with JSON after uploading a file (multipartFormData)

I am using this code to upload an image on server , that i get from this link play upload
def upload = Action(parse.multipartFormData) { request =>
request.body
.file("picture")
.map { picture =>
val dataParts = request.body.dataParts;
val filename = Paths.get(picture.filename).getFileName
val fileSize = picture.fileSize
val contentType = picture.contentType
val picturePaths =
picture.ref.copyTo(
Paths.get(
s"/opt/docker/images/$filename"
),
replace = true
)
if (dataParts.get("firstPoint") == None) {
val pointlocation = new Point_LocationModel(
dataParts.get("step").get(0),
dataParts.get("backgroundTargetName").get(0),
dataParts.get("x").get(0),
dataParts.get("y").get(0),
dataParts.get("z").get(0),
dataParts.get("rotation").get(0),
dataParts.get("note").get(0),
dataParts.get("tag").get(0),
dataParts.get("work_session_id").get(0),
(picturePaths).toString
)
point_LocationRepository.create(pointlocation).map { data =>
Created(Json.toJson(data._2))
}
} else {
val jValuefirstPoint =
Json.parse(dataParts.get("firstPoint").get(0)).as[PointModel]
val jValuesecondPoint =
Json.parse(dataParts.get("secondPoint").get(0)).as[PointModel]
val pointlocation = new Point_LocationModel(
dataParts.get("step").get(0),
dataParts.get("backgroundTargetName").get(0),
Some(jValuefirstPoint),
Some(jValuesecondPoint),
dataParts.get("rotation").get(0),
dataParts.get("note").get(0),
dataParts.get("tag").get(0),
dataParts.get("work_session_id").get(0),
(picturePaths).toString
)
point_LocationRepository.create(pointlocation).map { data =>
logger.info(s"repoResponse: ${data}");
Created(Json.toJson(data._2))
}
}
Ok(s"picturePaths ${picturePaths}")
}
.getOrElse(Ok("Invalid Format"))
}
This code works very well, but on the response I want to get the response from the repository. How can i await for the response of the repository to return this?
Can you give me any idea how can i do it?
Thanks in advance.
If we simplify your code to the essential bits, you have:
def upload = Action(parse.multipartFormData) { request =>
request.body
.file("picture")
.map { picture =>
if (someConditionA) {
someBusinessLogicA().map { data =>
Created(Json.toJson(data._2))
}
} else {
someBusinessLogicB().map { data =>
Created(Json.toJson(data._2))
}
}
Ok(s"picturePaths ${picturePaths}")
}
.getOrElse(Ok("Invalid Format"))
}
There are 2 problems:
the return of your if/else is swallowed by the Ok(s"picturePaths ${picturePaths}")
assuming your business logic returns Future[_] you need to "propagate" the Future everywhere
Something like this should work:
def upload = Action.async(parse.multipartFormData) { request =>
request.body
.file("picture")
.map { picture =>
if (someConditionA) {
someBusinessLogicA().map { data =>
Created(Json.toJson(data._2))
}
} else {
someBusinessLogicB().map { data =>
Created(Json.toJson(data._2))
}
}
}
.getOrElse(Future.successful(Ok("Invalid Format")))
}
Notice the Action.async and the Future in the getOrElse.

How to generate swagger document for akka http web socket route?

How to generate swagger document for akka http web socket route ?
I have able to write akka Http get,put,post,delete. for example
#Path("/postPing")
#ApiOperation(value = "Find a ping", notes = "Returns a pong",
httpMethod = "POST",response = classOf[String])
#ApiImplicitParams(Array(
new ApiImplicitParam(name = "data", value = "\"data\" to sum", required = true,
dataType = "string", paramType = "query"),
new ApiImplicitParam(name = "file", required = true,
dataType = "file", paramType = "query")
))
#ApiResponses(Array(
new ApiResponse(code = 404, message = "websocket not found"),
new ApiResponse(code = 200, message = "websocket found"),
new ApiResponse(code = 400, message = "Invalid websocket supplied")))
def postRoute = path("postPing") {
complete("post pong")
}
But I need for Akka web socket
def webSocketRoute: Route = path("websocket") {
handleWebSocketMessages(broadcast)
}
def broadcast: Flow[Message, Message, Any] = {
Flow[Message].mapConcat {
case tm: TextMessage =>
TextMessage(tm.textStream) :: Nil
}
}
For example
To connect with websocket server
/connect ws://echo.websocket.org/websocket
To send data to websocket server
/send Hello\ world
Thanks in Advance

Http Get request is not working

I am trying to get xml data from apache ranger using it's rest api. Here is my code
val httpclient = new DefaultHttpClient()
val auth=new AuthScope(host,AuthScope.ANY_PORT)
val credentials=new UsernamePasswordCredentials(username, password)
httpclient.getCredentialsProvider()
.setCredentials(auth, credentials)
val httpget = new HttpGet("http://localhost:6080/service/public/api/repository/1")
httpget.setHeader("Accept", "application/xml")
val response = httpclient.execute(httpget)
val entity = response.getEntity
if (entity != null) {
val in = new BufferedReader(new InputStreamReader(entity.getContent()))
var line = in.readLine()
var response = new StringBuffer()
while (line != null) {
response.append(line)
line = in.readLine()
}
in.close()
println(response.toString())
}
If i hit this url from browser it shows result fine. But in case of code it returns html.
Any help?
Try this code
val httpclient = new DefaultHttpClient()
val httpget = new HttpGet("url")
httpget.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("user", "pass"),
"UTF-8", false))
httpget.setHeader("Accept", "application/json")
val response = httpclient.execute(httpget)
EntityUtils.toString(response.getEntity())
setting authentication in the request.
Thanks