Data Extraction from JMeter SampleResult Response - java

In the snippet shown below data and data1 are set from different JMeter SampleResult Responses. The challenge which I am facing is during the processing of value1, I require the data from value which is present in another class.
The value is coming from the response of JMeter SampleResult(say 1), whereas the data1 is coming from the response of JMeter SampleResult(say 2).
I am also using a validate file for BeanShell assertions which only processes the response of JMeter SampleResult 2 for validation purposes.
How can I get the data from value to use it for further calculations of value1?
Class C is an abstract class
class A extends C {
#Override
public String processValue() {
****Some code written here****
value = getValue();
****Calculation of result done here****
return result;
}
#Override
public void setData(Object data) {
this.data=(typecast)data;
}
private String getValue() {
****logic written here****
return value;
}
}
value1 requires value from Class A for it's processing
class B extends C {
#Override
public String processValue() {
****Some code written here****
return value1;
}
#Override
public void setData(Object data1) {
this.data1=(typecast)data1;
}
}
data1 and data are typecasted into different types

In JMeter you can put different objects in JMeterVariables as put:
JMeterVariables vars = JMeterContextService.getContext().getVariables();
vars.putObject("data1", data1);
vars.putObject("data", data);
and get:
vars.getObject("data1");
vars.getObject("data");

Related

What is the best way to send a collection of objects over the Event Bus in Vertx?

I have a handler that serves HTTP requests at a given endpoint. The handler messages a verticle via the event bus that makes some external paginated REST calls, aggregates the results, and returns the results back to the handler. The result of the paginated REST calls is represented as a List of custom objects. If I just try to send the List itself, Vertx throws an exception complaining that it can't find a codec for java.util.ArrayList.
I'm trying to find the "best" -- meaning the easiest, most efficient, and most readable/maintainable -- way in Vertx to send a list of these objects back across the event bus to my handler. These are the options I know of and have tried so far, are there better ways to achieve this?
Serialize list to JSON and store in a JsonObject. This requires an explicit serialization/deserialization on either end which seems unnecessary:
// Verticle
List<CustomObject> result = method();
JsonObject data = new JsonObject();
data.put("result", Json.encode(result));
msg.reply(data);
// Handler
String serializedList = body.getString("result");
List<CustomObject> list = objectMapper.readValue(serializedList, new TypeReference<List<CustomObject>>(){});
Define a message codec for ArrayList<CustomObject>. In theory I believe this would work, but all the examples I've seen online for message codecs are always about creating a codec for a single object, and I'm not entirely sure if this would work for collections.
Is there a simpler method that fits my use case that I'm unaware of? Thanks!
Sorry for a lengthy example, but here you go:
public class EventBusHolder {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.eventBus().registerDefaultCodec(Holder.class, new HolderCodec());
vertx.deployVerticle(new SomeVerticle(), (r) -> {
vertx.eventBus().send("custom", new Holder(new CustomObject("a")));
});
}
}
class HolderCodec implements MessageCodec<Holder, Holder> {
#Override
public void encodeToWire(Buffer buffer, Holder holder) {
}
#Override
public Holder decodeFromWire(int pos, Buffer buffer) {
return null;
}
#Override
public Holder transform(Holder holder) {
return holder;
}
#Override
public String name() {
return "HolderCodec";
}
#Override
public byte systemCodecID() {
return -1;
}
}
class SomeVerticle extends AbstractVerticle {
#Override
public void start() {
vertx.eventBus().consumer("custom", (msg) -> {
System.out.println(msg.body());
});
}
}
class CustomObject {
public String name;
public CustomObject(String name) {
this.name = name;
}
#Override
public String toString() {
return "CustomObject{" +
"name='" + name + '\'' +
'}';
}
}
final class Holder {
#Override
public String toString() {
return "Holder{" +
"data=" + data +
'}';
}
private final List<CustomObject> data;
public Holder(final CustomObject... data) {
this.data = Arrays.asList(data);
}
public List<CustomObject> getData() {
return data;
}
}
Take note that encodeToWire and decodeFromWire are not implemented. They aren't invoked for local messages.
Having this Holder object is an easy way to get around type erasure on the JVM.

How to map an arraylist of hashmap to anothoer hashmap arraylist

I have a Switch that contains 13 case, each case executes a different sql request. I got the result in an ArrayList<HashMap<String, String>>. This result is supposed to be displayed with angular , for now i'm using this this.respTest = JSON.stringify(response); so it displays a list of "key":"value" .
My problem is since each request gets me different database fields and values ,so I want to merge some fields .
I created this class :
public class DataCollect {
private String type ;
private String entity ;
private String modPar ;
private String dateModif ;
private String numVersion ;
public DataCollect(String type, String entity, String modPar, String dateModif, String numVersion) {
this.type = type;
this.entity = entity;
this.modPar = modPar;
this.dateModif = dateModif;
this.numVersion = numVersion;
}
public DataCollect() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getEntity() {
return entity;
}
public void setEntity(String entity) {
this.entity = entity;
}
public String getModPar() {
return modPar;
}
public void setModPar(String modPar) {
this.modPar = modPar;
}
public String getDateModif() {
return dateModif;
}
public void setDateModif(String dateModif) {
this.dateModif = dateModif;
}
public String getNumVersion() {
return numVersion;
}
public void setNumVersion(String numVersion) {
this.numVersion = numVersion;
} }
In this class I'm supposed to affect the fields' names to the variables that I created and as a return an arraylist of hashmap with the data I extracted from the data base.
I mean I used to return for example "field-name":"value" , I want to return "type":"value","entity":"value" ..etc
I'm using springboot for the backend and angular 5 for the front.
Any help would be appreciated.
What you essentially want is a way to map keys in [each of] your hashmap to the corresponding member variable in the "DataCollect" POJO.
If there is a one to one mapping between the key present and corresponding member variable, you can expose a public constructor in "DataCollect" that takes in the hash map and constructs the corresponding object.
public DataCollect(Map<String, String> result) {
this.type = result.get("type");
this.entity = result.get("db_entity_key");
...
}
If there is no one on one mapping, you'd have to create a factory class, which takes your Map as an input and some context, and returns you the constructed DataCollect object.
Once you have the constructor or the factory class, you only need to iterate over your results list and do the needful to convert each Map into the DataCollect object.
Your controller should automatically serialise the DataCollect objects to corresponding JSON, or you can even use Jackson's ObjectMapper to achieve the same.

Sync methods using rxjava

I have these following scenarios -
Say I have 2 async callback(imagine I am calling 2 diff apis) methods callback1(Data d1) and callback2(Data d2), based on d1 and d2 (i.e when both the callback methods are being called)I have to call a method say setupUI(), How to efficiently do that using RxJava
There are two viewpagers v1 and v2 which needs to be synced, i.e on v1 pagechange v2 will also change its current page (indices will be the same) and vice-versa, using Rxjava
Try putting subjects in the callbacks to convert them into rx streams.
You can then zip the two subjects up and subscribe to the zipped observable to get the result of both callbacks at the same time
Example: lets make two subjects
PublishSubject<Data> subject1 = PublishSubject.create();
PublishSubject<Data> subject2 = PublishSubject.create();
We can use these to convert our callbacks into something we can subscribe to like this:
public void callback1(Data d1) {
subject1.onNext(d1);
}
public void callback2(Data d2) {
subject2.onNext(d2);
}
Now we can get the output when they both emit something like this:
class DataDto {
Data data1;
Data data2;
DataDto(Data data1, Data data2) {
this.data1 = data1;
this.data2 = data2;
}
}
public void main() {
Observable.zip(subject1, subject2, new BiFunction<Data, Data, DataDto>() {
#Override
public DataDto apply(#NonNull Data data1, #NonNull Data data2) throws Exception {
return new DataDto(data1, data2);
}
}).subscribe(new Consumer<DataDto>() {
#Override
public void accept(#NonNull DataDto dataDto) throws Exception {
//do something
}
});
}
What zip does is that it waits until both streams have emitted, then emits that as one item.
Here we made a DataDto which contains both data1 and data2
Hope this helps

Write RequestBody for Restful method

I'm a new member in Restful API, I'm writing a GET method:
#RequestMapping(method = RequestMethod.GET, value = "/resourcerecords", produces={"application/json", "application/xml"})
public #ResponseBody Object getRRs(#RequestBody RRRequest requestBody){
// do something
}
The RRRequest class:
public class RRRequest{
private RRREC reqObject;
// getter and setter
}
The RRREC class:
public class RRREC{
protected String infraAddr;
protected RRINFRATYPE infraType;
// getter and setter
}
And the RRINFRATYPE class:
public enum RRINFRATYPE {
V_6_ADDRESS("V6ADDRESS"),
OBJECT("OBJECT"),
ZONE("ZONE"),
V_4_REVERSEZONE("V4REVERSEZONE"),
V_6_REVERSEZONE("V6REVERSEZONE"),
NODE("NODE"),
ALL("ALL");
private final String value;
RRINFRATYPE(String v) {
value = v;
}
public String value() {
return value;
}
public static RRINFRATYPE fromValue(String v) {
for (RRINFRATYPE c: RRINFRATYPE.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
Then, I sent a request GET with RequestBody ( I use Fiddler Web Debugger)
"reqObject" : {
"infraAddr" : "192.168.88.4",
"infraType": {
"value": "OBJECT"
}
}
I get 400 Bad Request. If I change to
"reqObject" : {
"infraAddr" : "192.168.88.4",
"InfraType": {
"value": "OBJECT"
}
}
I can debug.
However, The reqObject only receive infraAddr with "192.168.88.4", the InfraType is null.
Who can explain to me, why I must be use "InfraType" instead of "infraType" and how to send value for InfraType.
The first one is when your api in GET method you still cant send body of request to server, try to change it to POST.
Because you use ENUM in your Object so you should define a converter like Converting JSON to Enum type with #RequestBody
But in this case, I think the fastest way is change infraType to String and use switch case with String on server side.
public class RRREC{
protected String infraAddr;
protected String infraType;
// getter and setter
}
Your JSON will be:
{
"reqObject" : {
"infraAddr" : "192.168.88.4",
"infraType": "OBJECT"
}
}

json request enum

We have RestWS where need to pass request in JSON format. This request contains different type of values such as String, List, enum etc.
We figured out how need to pass String and List (see below) but not sure how to pass enum in JSON request object.
Sample JSON Request for List and String in request:
{"firstparam":["195","196"],"secondparam":"test"}
First param is List and second param is String. Similarly we need to know how we can pass enum (also in the above request).
Sample enum class:
#XmlType(name = "Type")
#XmlEnum
public enum Type {
#XmlEnumValue("New")
NEW("New"),
#XmlEnumValue("Delete")
DELETE("Delete"),
#XmlEnumValue("Process")
PROCESS("Process");
private final String value;
WorkingStatusType(String v) {
value = v;
}
public String value() {
return value;
}
public static WorkingStatusType fromValue(String v) {
for (WorkingStatusType c: WorkingStatusType.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
This Google JSON style guide might help you.

Categories