assertion is failing on contains condition in Jmeter - jmeter

using a JSR223 assertion for a JSON response where I am validating a field customerID, which contains the below list of customerID,
GSK
CLINIRX
KENDLE
Test
TestIc2
Test692
TesteDe
Test2zY
TestEjC
Testb5b
Testhke
TestcI4
Test2zY
Test7x7
TesteDe
TestU4x
Testfkv
TestHea
TestEjC
The Test<random 3 characters are generating at the run time while executing the script>, but the first 4 letter is Test is fixed.
Assertion Script:
import groovy.json.JsonSlurper;
def failureMessage = "";
def jsonResponse = null;
JsonSlurper JSON = new JsonSlurper ();
try {
jsonResponse = JSON.parseText(prev.getResponseDataAsString());
customerID = jsonResponse.customerId;
}
catch (Exception e) {
failureMessage += "Invalid JSON.\n"
}
if (customerID.contains('Test') ||customerID.contains('KENDLE') || customerID.contains('CLINIRX') || customerID.contains('GSK')){
AssertionResult.setFailure(false);
}
else {
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage(failureMessage); }
How to solve this, if the customerID contain Test<3 char> should also pass, as this is failing for few?

You can use the following regular expression to validate the customer IDs
(GSK|CLINIRX|KENDLE|Test([A-Za-z0-9]{3})?)

There is nothing wrong with your code, my expectation is that something is wrong with your customerID variable
So I would suggest trying printing it to the jmeter.log file using log shorthand for the Log4jLogger class:
customerID = jsonResponse.customerId;
log.info('Current customer ID value: ' + customerID)
Then in case of failure you can look into the log file and see why it failed. The contains() function itself shouldn't be a problem, my expectation is that either your customerID variable doesn't contain what you're looking for or it's just null.

Note: According to Java Doc, String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise. It can be directly used inside the if statement.
Now solution to your problem:
import groovy.json.JsonSlurper;
def failureMessage = "";
def jsonResponse = null;
JsonSlurper JSON = new JsonSlurper ();
try {
jsonResponse = JSON.parseText(prev.getResponseDataAsString());
customerID = jsonResponse.customerId;
}
catch (Exception e) {
failureMessage += "Invalid JSON.\n"
}
if (customerID.contains("Test") ||customerID.contains('KENDLE') || customerID.contains('CLINIRX') || customerID.contains('GSK')){
AssertionResult.setFailure(false);
}
else {
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage(failureMessage); }
Another Note: single quotes bracket a string literal; the value of the literal is the value within the quotes. When double quotes are used, any references to variables or expressions within the quotes are interpolated.

Related

how to return a specific status code in Kotlin

I've set up a route that when I get a name in my post body I will search the DB and return an ID value.
What I want to do is once there is no ID present in the DB return a 204 status code.
But should that be handled in the service or in my controller?
and
How do I return my specific status code?
#ResponseStatus(HttpStatus.OK)
#PostMapping("/ID_values/")
fun getID(
#RequestBody
name: String
): ResponseEntity<String> = ResponseEntity.ok(IDLookupService.lookupIDValue(name))
}
#Service
class EmailLookupService(
private val IDRepo: IDRepo
) : Logging {
fun lookupIDValue(name: String): String {
val IDLookupResult = IDRepo.findById(name)
return if (IDLookupResult.isPresent) {
IDLookupResult.get().ID.toString()
} else {
// return status code 204
}
}
}
First, you should omit the #ResponseStatus(HttpStatus.OK) annotation if you do not wish to always return a status code of 200. Using that annotation, it would suffice to only specify the response body as return value (i.e specify return type String and then return only result in your example), and Spring would automatically wrap that into a response entity with HTTP-status OK.
Second, you need some way to tell the caller of IDLookupService.lookupIDValue (which should probably be called on an instance of IDLookupService and not the class itself) that there was nothing found. This could be done for instance by changing the return type to String? and return null if nothing was found.
Then you can change getID to return
val result = idLookupService.lookupIDValue(name)
return if(result != null) ResponseEntity.ok(result)
else ResponseEntity("not found", HttpStatus.NO_CONTENT)
If you wish to return something different than a String in the case there was nothing found (like an error object with detailed information; in the example here it is simply the text "not found"), you can change the response type of getID to ResponseEntity<*>.

How to search exact record by asp.net web api with help of lamda expresion

I am facing a problem in searching a exact record by LINQ query method in ASP.NET Web API my controller. This is my code:
[HttpGet]
[Route("api/tblProducts/AllProductbySearch/{SearchText}")]
[ResponseType(typeof(IEnumerable<tblProduct>))]
public IHttpActionResult AllProductbySearch(string SearchText)
{
IEnumerable<tblProduct> tblProduct = db.tblProducts.Where(x=>x.PrdKeyword.Contains(SearchText)).AsEnumerable();
if (tblProduct == null)
{
return NotFound();
}
return Ok(tblProduct);
}
In this I am searching the record with value have keyword column and getting the result but problem is that it is not giving exact result for example if in database two record have keyword column value like shirt and another have Tshirt
Then if I pass shirt in SearchText or pass tshirt in SearchText it is giving both record while I want one record which exact match with SearchText. Please help me
My updated action method code is:
[HttpGet]
[Route("api/tblProducts/AllProductbySearch/{SearchText}")]
[ResponseType(typeof(IEnumerable<tblProduct>))]
public IHttpActionResult AllProductbySearch(string SearchText)
{
IEnumerable<tblProduct> tblProduct = db.tblProducts.Where(x => CheckWord(x.PrdKeyword, SearchText)).AsEnumerable();
if (tblProduct == null)
{
return NotFound();
}
return Ok(tblProduct);
}
private bool CheckWord(string source, string searchWord)
{
var punctuation = source.Where(Char.IsPunctuation).Distinct().ToArray();
var words = source.Split().Select(x => x.Trim(punctuation));
return words.Contains(searchWord, StringComparer.OrdinalIgnoreCase);
}
But is throwing the same error - http 500
EDITED 2
Added ToList() - db.tblProducts.ToList().... In this case we retrieve all data from Data Base and filter them in memory. If we don't retrieve all data before filtering .Net tries to create request to SQL with filtration and can't because there are .Net methods as CheckWord().
I think we can get required data without retrieving all table into memory, but don't know how. As variant we should write specific Stored Procedure and use it. Get all into memory is a simplest way (but not faster)
Please, look at this post Get only Whole Words from a .Contains() statement
Actually, for your case solution can be:
IEnumerable<tblProduct> tblProduct = db.tblProducts.ToList()
.Where(x => Regex.Match(x.PrdKeyword, $#"\b{SearchText}\b", RegexOptions.IgnoreCase).Success)
.AsEnumerable();
Option 2. Without regexp:
public static bool CheckWord(string source, string searchWord)
{
if (source == null)
return false;
var punctuation = source.Where(Char.IsPunctuation).Distinct().ToArray();
var words = source.Split().Select(x => x.Trim(punctuation));
return words.Contains(searchWord, StringComparer.OrdinalIgnoreCase);
}
[HttpGet]
[Route("api/tblProducts/AllProductbySearch/{SearchText}")]
[ResponseType(typeof(IEnumerable<tblProduct>))]
public IHttpActionResult AllProductbySearch(string SearchText)
{
IEnumerable<tblProduct> tblProduct = db.tblProducts.ToList()
.Where(x => CheckWord(x.PrdKeyword, SearchText)).AsEnumerable();
if (tblProduct == null)
{
return NotFound();
}
return Ok(tblProduct);
}
Sorry, I'm from phone now, there can be mistakes here. Will try it in 3-4 hour
You are making a simple mistake. You just need to use .Equals instead of .Contains.
When you use Contains .Net will check if the input string is part of the main string. Whereas Equals will check for exact match.
var mainStr = “long string with Hello World”;
var inputStr = “Hello”;
var status = mainStr.Contains(inputStr);
// Value of status is `true`
status = mainStr.Equals(inputStr);
// Value of status is `false`
So your code should look like this:
IEnumerable<tblProduct> tblProduct = db.tblProducts.Where(x=>x.PrdKeyword.Equals(SearchText)).AsEnumerable();
.Equals can also help you find exact match with or without having case-sensitive check in force. The single-parameterised method does a Case-Sensitive check whereas the other overridden methods of .Equals gives you an opportunity to ignore it.
Hope this helps!

How do I read, update and replace a "String body" field in a web test from a plugin?

My recorded web performance test has several "String body" fields and I need to modify their contents at run time from within a web test request plugin.
The "String body" field is not directly available from the various fields and subfields of the PreRequestEventArgs.
How do I read out the "String body" field into a string and, after modifying it, write it back?
To read out the "String body" field, cast the request body to a StringHttpBody which makes the string available. To write it back, create a new StringHttpBody object to contain the updated string, then write it into the request.
Using a plugin I need to modify the "String body" field of a request in a web performance test. I can access the contents using the following code:
public override void PreRequest(object sender, PreRequestEventArgs e)
{
if ( e.Request.Body == null ) { return; }
StringHttpBody httpBody = e.Request.Body as StringHttpBody;
if ( httpBody == null ) { return; }
string body = httpBody.BodyString;
string updatedBody = UpdateBody(body);
StringHttpBody newBody = new StringHttpBody();
newBody.BodyString = updatedBody;
e.Request.Body = newBody;
}

How do I read the Received Date from Outlook MSG files -without- the Outlook API?

I need to read stuff from an Outlook msg file. Currently I'm using a class from CodeProject.com project to accomplish this, since deploying VSTO and Outlook on a server is not an option.
This class gets To, From, CC, Subject, Body, and everything else I need from the msg file, except Date information (such as Received Date and Sent Date).
There is some (really, really low-level) documentation on how to get stuff out of msg files on MSDN, but it's a little beyond the scope of this project and doesn't mention dates at all.
Ideally I'd be able to have a drop-in replacement for the class I am using now (OutlookStorage.cs in the previously mentioned CodeProject) or be able to modify the existing class a bit. To modify, I would need the correct 4 character hexidecimal prop identifier for received date. For instance, Subject is listed as PR_SUBJECT = "0037" and Body is listed as PR_BOY = "1000".
If you're using OutlookStorage.cs from CodeProject, then add the following:
private const string PR_RECEIVED_DATE="007D";
private const string PR_RECEIVED_DATE_2 = "0047";
...
/// <summary>
/// Gets the date the message was received.
/// </summary>
public DateTime ReceivedDate
{
get
{
if (_dateRevieved == DateTime.MinValue)
{
string dateMess = this.GetMapiPropertyString(OutlookStorage.PR_RECEIVED_DATE);
if (String.IsNullOrEmpty(dateMess))
{
dateMess = this.GetMapiPropertyString(OutlookStorage.PR_RECEIVED_DATE_2);
}
_dateRevieved = ExtractDate(dateMess);
}
return _dateRevieved;
//return ExtractDate(dateMess);
}
}
private DateTime _dateRevieved = DateTime.MinValue;
private DateTime ExtractDate(string dateMess)
{
string matchStr = "Date:";
string[] lines = dateMess.Split(new String[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
if (line.StartsWith(matchStr))
{
string dateStr = line.Substring(matchStr.Length);
DateTime response;
if (DateTime.TryParse(dateStr, out response))
{
return response;
}
}
}
return DateTime.MinValue;
}
I think the Aspose library will do what you want, ok it a 3rd party lib so may not be what you want. There are a few vbs scripts around that get basic infomation out of msg files that could be translated.
Got a hint from this:
string fullFileName = "c:\message.msg";
DateTime dateRevieved = new DateTime();
StreamReader sr = new StreamReader(fullFileName, Encoding.Default);
string full = sr.ReadToEnd();
string date;
int iStart;
int iLast;
string caption;
//This -should- handle all manner of screwage
//The ONLY way it would not is if someone guessed the -exact- to-the-second
//time that they send the message, put it in their subject in the right format
while (true) { //not an infinite loop, I swear!
caption = "Date:";
if (full.IndexOf("Date:") > -1) { //full shortens with each date is removed
string temp = "";
iStart = full.LastIndexOf(caption);
temp = full.Remove(0, iStart + caption.Length);
full = full.Substring(0, iStart);
iLast = temp.IndexOf("\r\n");
if (iLast < 0) {
date = temp;
} else {
date = temp.Substring(0, iLast);
}
date = date.Trim();
if (date.Contains(subject) || subject.Contains(date)) {
continue; //would only happen if someone is trying to screw me
}
try {
dateRevieved = DateTime.Parse(date); //will fail if not a date
break; //if not a date breaks out of while loop
} catch {
continue; //try with a smaller subset of the msg
}
} else {
break;
}
}
This is kind of a hack compared to the ways you can get other things from msg files using something this lovely project. Still, it's stood up to everything I have thrown against it, and as noted the -only- way to fool it is to put the exact to-the-second date in the subject line in the proper format.
to combine your two posts I would suggest the following solution:
To modify, I would need the correct 4 character hexidecimal prop identifier for recieved date. For instance, Subject is listed as PR_SUBJECT = "0037" and Body is listed as PR_BOY = "1000".
Look for "007D".
Use the method you posted in your second post on the received data to eliminate the problem when the same (date) string is inside the subject.
I have to mention that this method doesn't seem to work on internal eMails: In mails I receive from colleagues, there is no substg1.0_007Dxxxx-Property.
Here, the date seems to be hidden in substg1.0_0047xxxx.
All the best!
inno

Linq to sql truncating string returned by Stored Procedure

I have asked this question before. but i was not able to get any answer. may be i wasnt very clear. let me give some more details.
I have a SP which returns a long string. here is dbml file code
[Function(Name="dbo.spX")]
public ISingleResult<spXResult> spX([Parameter(DbType="VarChar(8000)")] string str)
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), str);
return ((ISingleResult<spXResult>)(result.ReturnValue));
}
and here is spXResult class
public partial class spXResult
{
private string _XML_F52E2B61_18A1_11d1_B105_00805F49916B;
public spXResult()
{ }
[Column(Name="[XML_F52E2B61-18A1-11d1-B105-00805F49916B]",
Storage="_XML_F52E2B61_18A1_11d1_B105_00805F49916B",
DbType="NText", UpdateCheck=UpdateCheck.Never)]
public string XML_F52E2B61_18A1_11d1_B105_00805F49916B
{
get
{
return this._XML_F52E2B61_18A1_11d1_B105_00805F49916B;
}
set
{
if ((this._XML_F52E2B61_18A1_11d1_B105_00805F49916B != value))
{
this._XML_F52E2B61_18A1_11d1_B105_00805F49916B = value;
}
}
}
}
and here is my code
ISingleResult<spXResult> result = ctx.spX("1234");
string returnStr = result.First().XML_F52E2B61_18A1_11d1_B105_00805F49916B;
everything is fine, when the result is not a long string, but as soon as the sp returns a very long string, it truncates the result. i have no clue why.. can someone please help.
thanks
The only thing fishy I can spot is this - here in the declaration, you hvae:
public ISingleResult<spXResult> spX([Parameter(DbType="VarChar(8000)")] string str)
(DbType=VARCHAR(8000)) - which is ANSI (non-Unicode), but then in the column declaration you use NTEXT - first of all, that's UNICODE (2-byte per character), and why NTEXT?? Above you have VARCHAR?
[Column(Name="[XML_F52E2B61-18A1-11d1-B105-00805F49916B]",
Storage="_XML_F52E2B61_18A1_11d1_B105_00805F49916B",
DbType="NText", UpdateCheck=UpdateCheck.Never)]
That seems a bit odd.......
Can you try to make it the same in both places? E.g. VARCHAR(8000) in both cases??
Marc
LinqToSql splits the XML result set into chunks, so you need to run a loop like this:
ISingleResult<spXResult> result = ctx.spX("1234");
string xml = "";
foreach (var x in result)
xml += x.XML_F52E2B61_18A1_11d1_B105_00805F49916B;
Or using LINQ:
string xml = result.Aggregate("", (current, x) => current + x.XML_F52E2B61_18A1_11d1_B105_00805F49916B);
Just change your sp from
SELECT ...
FROM MyTable
FOR XML AUTO
to
DECLARE #ResultXML xml
SET #ResultXML =
(SELECT ...
FROM MyTable
FOR XML AUTO)
SELECT #ResultXML as [MyXML]
and recreate linq method

Resources