I am trying to pass the session to a function in Gatling as shown in .body():
exec(http("ED/CTS/SGM")
.post(IDS_BASE_URL + edEndpoint)
.body(StringBody(createBodyStringForStudentAssignmentSetup(session)))
.headers(getHeaderMap)
.check(status().is(201),
jsonPath("$.assignments[0].refId").saveAs("assignmentRefId")));
The function being called is shown here:
public class CreateAssignmentDataSetup {
public static Body.WithString createBodyStringForStudentAssignmentSetup(Session session)
{
String title;
String assignmentName = "PerformanceTest_AssignmentApi_";
title = assignmentName.concat(LocalDate.now().toString());
String studentsList = session.getString("listOfStudents");
....
....
return StringBody(body);
}
Is it possible to pass "session" values like this to "createBodyStringForStudentAssignmetnSetup()" like this, since using an exec(session-> block (shown below) will not allow requests to be triggered for some reason:
exec(session -> {
});
Thanks
StringBody takes a Function<Session, String> so you want to write:
public static String createBodyStringForStudentAssignmentSetup(Session session)
{
String title;
String assignmentName = "PerformanceTest_AssignmentApi_";
title = assignmentName.concat(LocalDate.now().toString());
String studentsList = session.getString("listOfStudents");
....
....
return body;
}
.body(StringBody(CreateAssignmentDataSetup::createBodyStringForStudentAssignmentSetup))
Related
I have a Java hashmap, to which multiple variables were added(ex.):
class client {
public static int clientIdentifier;
public static String clientName;
public static String clientFamilyname;
public static String clientBirthdate;
}
Inside the main method:
client newclient = new client();
HashMap<Integer, String> clients = new HashMap<>();
clients.put(newclient.clientIdentifier,
newclient.clientName +
newclient.clientFamilyname +
newclient.clientBirthdate
Where new client.clientIdentifier is the key and newClient.clientName/Familyname/Birthdate are already defined. They belong to an object newclient, it belonging to the class client
Is there any way to get only the value clientName from that hashmap, to compare later?
Looking for something like:
String requiredName = clients.get(scannedID, newclient.clientName);
(I know that it is totally wrong, but just to have an idea)
Thank you very much!
You need to make all the fields in Client non-static first! You'd want each instance of Client to have a different ID, name, and birthday, right?
Instead of using a HashMap<Integer, String>, use a HashMap<Integer, Client>:
Client newclient = new Client();
HashMap<Integer, Client> clients = new HashMap<>();
clients.put(newclient.clientIdentifier, newclient);
Now you can just do:
String requiredName = clients.get(scannedID).clientName;
But really though, the fields in the class Client should all be private and should be accessed via getters and setters. You also should not be able to reset the client's clientIdentifier as that is the map's key.
Client should look like this:
class Client {
private int clientIdentifier;
private String clientName;
private String clientFamilyname;
private String clientBirthdate;
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public String getClientFamilyname() {
return clientFamilyname;
}
public void setClientFamilyname(String clientFamilyname) {
this.clientFamilyname = clientFamilyname;
}
public String getClientBirthdate() {
return clientBirthdate;
}
public void setClientBirthdate(String clientBirthdate) {
this.clientBirthdate = clientBirthdate;
}
public int getClientIdentifier() {
return clientIdentifier;
}
public Client(int clientIdentifier, String clientName, String clientFamilyname, String clientBirthdate) {
this.clientIdentifier = clientIdentifier;
this.clientName = clientName;
this.clientFamilyname = clientFamilyname;
this.clientBirthdate = clientBirthdate;
}
}
Your hash map code will now look like:
Client newclient = new Client();
HashMap<Integer, Client> clients = new HashMap<>();
clients.put(newclient.getClientIdentifier(), newclient);
String requiredName = clients.get(scannedID).getClientName();
I would put the value in the hashmap something like this:
map.put(clientID,name+"+"+familyname+"+"+bithdate);
This way when I get the value from Map using the key, I would get a string like John+Wood+1994.
Now I can easily get the name by using substring.
Does this help ?
Why does Integer field couldn't get the value that i sent. If i use Integer parameter by itself (like sending an ID) in WebMethod works fine.
Also the String field can get the value that i sent without any problem.
Here is the web service:
#WebService(serviceName = "testWS")
public class testWS {
#WebMethod(operationName = "Hello")
public Integer Hello(#WebParam(name = "testData") TEST testData) {
return testData.getINTPROP();
}
}
TEST type definition:
#XmlAccessorType(XmlAccessType.FIELD)
public class TEST {
private Integer INTPROP;//making public also didn't work
public Integer getINTPROP() {
return INTPROP;
}
public void setINTPROP(Integer INTPROP) {
this.INTPROP = INTPROP;
}
private String STRINGPROP;
public String getSTRINGPROP() {
return STRINGPROP;
}
public void setSTRINGPROP(String STRINGPROP) {
this.STRINGPROP = STRINGPROP;
}
}
C# consumer application:
...
test f = new test();
f.INTPROP = 123;
f.STRINGPROP = "abcdef";
var ff = ws.Hello(f);//returns 0
I hope i could explained my problem. thanks.
Try to change the setter to String.
public void setINTPROP(String INTPROP) {
this.INTPROPString = INTPROP;
Then change the String to an integrer with another function.
Return it as an integer, this should solve the problem.
Here is another aproach:
Add this function. Java should be able to handle this.
If the Variable you are passing is a integer, then the function you have will be used. If it is a String, Then this function will be called:
public String setINTPROP(String INTPROP) {
this.INTPROP = Integer.valueOf(INTPROP);
Ok. Marking field with #XmlSchemaType solved my problem.
#XmlSchemaType(name = "string")
public Integer INTPROP
I want to pass list of Object as the input to the web service.
I came to know we cannot achieve this using the built in Web Service task in SSIS. So I tried calling it through script task which uses C# Code.
I am able to call a Java Web Service(SOAP) through script Task.
I am able to test by passing simple parameters like string to the web service method.
Now I want to pass list of objects as parameter to the Web service method.
For testing purpose first I tried passing a object. The class in the c# client is as below
[Serializable]
public class Person
{
public string _PersonName;
public string _PersonNumber;
public string _Password;
public bool _isTrue;
public List<string> _configs;
public Person()
{
}
public Person(string PersonName, string PersonNumber, string Password, bool val)
{
_PersonName = PersonName;
_PersonNumber = PersonNumber;
_Password = Password;
_isTrue = val;
// _configs = config;
}
}
The corresponding proxy client class is as below
public partial class person {
private string _PersonNameField;
private string _PersonNumberField;
private string _PasswordField;
private bool _isTrueField;
private string[] _configsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string _PersonName {
get {
return this._PersonNameField;
}
set {
this._PersonNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string _PersonNumber {
get {
return this._PersonNumberField;
}
set {
this._PersonNumberField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string _Password {
get {
return this._PasswordField;
}
set {
this._PasswordField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public bool _isTrue {
get {
return this._isTrueField;
}
set {
this._isTrueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("_configs", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public string[] _configs {
get {
return this._configsField;
}
set {
this._configsField = value;
}
}
}
The method in the proxy class is below
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace="http://sample.xyz.abc.ext/", ResponseNamespace="http://sample.xyz.abc.ext/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string createPerson([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] person arg0) {
object[] results = this.Invoke("createJigBoard", new object[] {
arg0});
return ((string)(results[0]));
}
I am calling the method in the client as below
ServiceReference.TestService per = new ServiceReference.TestService();
var testList=new List<string>();
Person personOne = new Person("Manoj", "123456761", "Administrator", true,testList);
NetworkCredential myCred = new NetworkCredential("person", "person");
CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri("http://pcblr********:80/*******/servlet/TestService"), "Basic", myCred);
StringWriter textWriter = new StringWriter();
XmlSerializer xmlSer = new XmlSerializer(typeof(Person));
xmlSer.Serialize(textWriter, personOne);
textWriter.Close();
per.createPerson(personOne);
I am getting the error
Argument 1: cannot convert from 'Client.Person' to 'Proxyclass.person' ******\ScriptMain.cs
The error message is correct. The service expects a ProxyClass.person but you are sending a Client.Person.
Instead of this line:
Person personOne = new Person("Manoj", "123456761", "Administrator", true,testList);
you should create a ProxyClass.person-object and map the parameters manually or use AutoMapper or similar.
You also need to change serialization from
XmlSerializer xmlSer = new XmlSerializer(typeof(Person));
to
XmlSerializer xmlSer = new XmlSerializer(typeof(ProxyClass.person));
Im try to insert data into Database using ArrayList.there is a Erro msg.
That is my Custmer.class method. this is what i got from when i going to pass ArrayList into another class.
incompatible types: ArrayList<String> cannot be converted to ArrayList<Inquiries>
I want to know how to do this using correct Using OOP concept
public void passingMsg(ArrayList<Inquiries> arrlist){
try {
System.out.println("Method "+arrlist);
String sq = "INSERT INTO Inquiries (name,mail,tp,msg)VALUES(?,?,?)";
PreparedStatement pr = con.prepareStatement(sq);
for(int i=0;i<arrlist.size();i++){
pr.setString(1,arrlist.get(i).getName());
pr.setString(2,arrlist.get(i).getMail());
pr.setString(3,arrlist.get(i).getTp());
pr.setString(4,arrlist.get(i).getMsg());
}
pr.executeQuery();//executeBatch();
} catch (SQLException ex) {
}
}
and this is how i get values from user
String name = txtName.getText();
String mail = txtEmail.getText();
String tp = txtTp.getText();
String msg = txtMsg.getText();
ArrayList<String> arrInq = new ArrayList<String>();
arrInq.add(name);
arrInq.add(mail);
arrInq.add(tp);
arrInq.add(msg);
Custmer c =new Custmer();
if( c.passingMsg(arrInq)){
try {
JOptionPane.showMessageDialog(this, "Successs!!");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Unsuccesss!!");
e.printStackTrace();
}
}
and this is my Inquiries.class :
public class Inquiries {
private String name;
private String mail;
private String tp;
private String msg;
public Inquiries(String name,String mail,String tp,String msg){
this.name = name;
this.mail = mail;
this.tp = tp;
this.msg = msg;
}
//
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getTp() {
return tp;
}
public void setTp(String tp) {
this.tp = tp;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
Can Some one please explain whats wrong with this. please ?
Reason For Error
This was simply telling you that your types were incompatible for the operation you were trying to perform. In your passingMsg() method, you have its header as: public void passingMsg(ArrayList<Inquiries> arrlist). However, inside your "how i get values from user" area, which I will now refer to as "2nd Snippet", you have your method call declared as: if( c.passingMsg(arrInq)). This means that you are implying that your parameter being passed, arrInq in this case, is of the type ArrayList<Inquiries>, but it's not. It's being initialized in your 2nd Snippet as: ArrayList<String> arrInq = new ArrayList<String>();
Simple Fix
I take no responsibility for this code; use at your own risk. To fix this, you would want to change that entire 2nd Snippet to something similar to the following:
String name = txtName.getText();
String mail = txtEmail.getText();
String tp = txtTp.getText();
String msg = txtMsg.getText();
ArrayList<Inquiries> arrInq = new ArrayList<Inquiries>();
arrInq.add(new Inquiries(name, mail, tp, msg));
Custmer c = new Custmer();
try {
c.passingMsg(arrInq);
JOptionPane.showMessageDialog(this, "Successs!!");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Unsuccesss!!");
e.printStackTrace();
}
You would also want to change the method header to either return a boolean, or fix it up a little bit to actually throw the exception. Such as:
public void passingMsg(ArrayList<Inquiries> arrlist) {
System.out.println("Method " + arrlist);
String sq = "INSERT INTO Inquiries(name,mail,tp,msg) VALUES(?,?,?)";
PreparedStatement pr = con.prepareStatement(sq);
for (Inquiries inquiries : arrlist) {
pr.setString(1, inquiries.getName());
pr.setString(2, inquiries.getMail());
pr.setString(3, inquiries.getTp());
pr.setString(4, inquiries.getMsg());
}
pr.executeQuery();//executeBatch();
}
Let's talk in O-O-P way.
Here Inquiries is your model, model is nothing but simple class that has instance members and public methods to get and set value of model's instance variable.
Generally we put all database related operations code in their respective models.
e.g. I have model "Model" which typically maps to database table say it as "TableModel" ,I would do something like this:
public class Model{
private int id;
private String attr;
//other properties of the model
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
//other getters and setters
//here we write methods to performs database operations
public void save(){
//use "this" to get properties of object
//logic to save to this object in database table TableModel as record
}
public void delete(int id){
//logic to delete this object i.e. from database table TableModel
}
public Model get(int id){
//retrieve record from table TableModel with this id
}
//other methods to get data from database.
}
Now question is how I can use this in some another class. Let's say I have list of Model objects and I wish to insert them in to database.I will do it something like this:
public class AnotherClass{
public void someMethod(){
//create list of models objects e.g. get them from user interface
ArrayList<Model> models=new ArrayList<>();
for(int i=0;i<3;i++){
Model model=new Model();
model.setId(i);
model.setAttr("attr"+i);
models.add(model);
}
SomeOtherClass obj=new SomeOtherClass();
obj.insert(models);
}
}
public class SomeOtherClass{
//other code above.....
//my method that inserts each Model object in database
//Note: this is sample method , you should do it in optimized way
// e.g. batch insert
public void insert(ArrayList<Model> models){
for(Model myModel:models){
myModel.save();
}
}
//other code below.....
}
You are using the wrong type parameter for the ArrayList. Instead of ArrayList<String> you need ArrayList<Inquiries>. To fix the problem, you should remove this code ...
ArrayList<String> arrInq = new ArrayList<String>();
arrInq.add(name);
arrInq.add(mail);
arrInq.add(tp);
arrInq.add(msg);
... and replace it with this code:
ArrayList<Inquiries> arrInq = new ArrayList<Inquiries>();
arrInq.add(new Inquiries(name, mail, tp, msg));
For example, I have to create a webservice with a details below:
Webservice name is WS1
Method name is initiateBatchProcess (String Status, int BatchID)
I have tried the following with one parameter, but how do I do it with two parameters and return it in the response of webservice/soap
public class WS1
{
int status;
#WebMethod(operationName="status")
public int status(int status) {
return status;
}
}
You simply add another parameter. The use of the #WebParam is optional, keep it if you want or ditch it.
#WebMethod(operationName="initBatch")
public void initiateBatchProcess(#WebParam(name = "Status") String Status,
#WebParam(name = "Batch") int BatchID) {
//do stuff
}
LE:
So, if you want to send back more than one thing, the best solution i can think of is encapsulating those things into a single object.
#WebMethod(operationName="initBatch")
public RezultSet initiateBatchProcess(String status, int batchID) {
//do stuff
ResultSet result = new ResultSet();
result.setStatus(status);
result.setBatchId(batchID);
return result;
/*
*Or you can do something like
*return new ResultSet(status, batchID);
*/
}
And ResultSet is just a simple bean with 2 members.
public class ResultSet {
private String status;
private int batchID;
// getters, setters, constructors
}