Log failed tesg case and continue with loop - java

I am reading the test data from excel sheet and iterating the record.
On every iteration I am asserting the response in restassured.
Here is the code:
if (rbac == true && isallowed == var)
System.out.println("Test case is pass because rbac status and allowed status are matched");
else if (rbac == false && isallowed == false) {
assertEquals(403, response.statusCode());
System.out.println("Test case is pass because rbac status and allowed status are matched (false)");
}
else if (rbac == false && isallowed == true)
Assert.fail("Test case fail because rbac status is false but allowed status is true.");
else
Assert.fail("Test case fail");
Now I want
Pass and failure test cases record will get logged in report.
Test will continue if any record during test case will get failed until last record.

Related

RemoteMediator with read capability

I'm now trying to call paging data from the database using remotemediator.
It does not insert into the ROOM if the item on the page queried from the network is already read.
The problem is that if all the items on the page queried from the network were already read, there is no trigger to query the next page because pagingState.firstItemOrNull() == null and pagingState.lastItemOrNull() == null.
Is there a way to requery with loadkey +1 if all the query results have been read?
How can I solve this problem?
switch (loadType) {
case REFRESH:
loadKey = initialKey;
keysFuture = keysDao.getRemoteKeyFuture(-1);
break;
case PREPEND:
if(pagingState.firstItemOrNull() == null){
return Futures.immediateFuture(new MediatorResult.Success(false));
}
keysFuture = keysDao.getRemoteKeyFuture(pagingState.firstItemOrNull().getId());
break;
case APPEND:
if(pagingState.lastItemOrNull() == null){
return Futures.immediateFuture(new MediatorResult.Success(false));
}
keysFuture = keysDao.getRemoteKeyFuture(pagingState.lastItemOrNull().getId());
break;
}

Android Firebase Firestore real time update with pagination [duplicate]

I am working with Firestore right now and have a little bit of a problem with pagination.
Basically, I have a collection (assume 10 items) where each item has some data and a timestamp.
Now, I am fetching the first 3 items like this:
Firestore.firestore()
.collection("collectionPath")
.order(by: "timestamp", descending: true)
.limit(to: 3)
.addSnapshotListener(snapshotListener())
Inside my snapshot listener, I save the last document from the snapshot, in order to use that as a starting point for my next page.
So, at some time I will request the next page of items like this:
Firestore.firestore()
.collection("collectionPath")
.order(by: "timestamp", descending: true)
.start(afterDocument: lastDocument)
.limit(to: 3)
.addSnapshotListener(snapshotListener2()) // Note that this is a new snapshot listener, I don't know how I could reuse the first one
Now I have the items from index 0 to index 5 (in total 6) in my frontend. Neat!
If the document at index 4 now updates its timestamp to the newest timestamp of the whole collection, things start to go down.
Remember that the timestamp determines its position on account of the order clause!
What I expected to happen was, that after the changes are applied, I still show 6 items (and still ordered by their timestamps)
What happened was, that after the changes are applied, I have only 5 items remaining, since the item that got pushed out of the first snapshot is not added to the second snapshot automatically.
Am I missing something about Pagination with Firestore?
EDIT: As requested, I post some more code here:
This is my function to return a snapshot listener. Well, and the two methods I use to request the first page and then the second page I posted already above
private func snapshotListener() -> FIRQuerySnapshotBlock {
let index = self.index
return { querySnapshot, error in
guard let snap = querySnapshot, error == nil else {
log.error(error)
return
}
// Save the last doc, so we can later use pagination to retrieve further chats
if snap.count == self.limit {
self.lastDoc = snap.documents.last
} else {
self.lastDoc = nil
}
let offset = index * self.limit
snap.documentChanges.forEach() { diff in
switch diff.type {
case .added:
log.debug("added chat at index: \(diff.newIndex), offset: \(offset)")
self.tVHandler.dataManager.insert(item: Chat(dictionary: diff.document.data() as NSDictionary), at: IndexPath(row: Int(diff.newIndex) + offset, section: 0), in: nil)
case .removed:
log.debug("deleted chat at index: \(diff.oldIndex), offset: \(offset)")
self.tVHandler.dataManager.remove(itemAt: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), in: nil)
case .modified:
if diff.oldIndex == diff.newIndex {
log.debug("updated chat at index: \(diff.oldIndex), offset: \(offset)")
self.tVHandler.dataManager.update(item: Chat(dictionary: diff.document.data() as NSDictionary), at: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), in: nil)
} else {
log.debug("moved chat at index: \(diff.oldIndex), offset: \(offset) to index: \(diff.newIndex), offset: \(offset)")
self.tVHandler.dataManager.move(item: Chat(dictionary: diff.document.data() as NSDictionary), from: IndexPath(row: Int(diff.oldIndex) + offset, section: 0), to: IndexPath(row: Int(diff.newIndex) + offset, section: 0), in: nil)
}
}
}
self.tableView?.reloadData()
}
}
So again, I am asking if I can have one snapshot listener that listens for changes in more than one page I requested from Firestore
Well, I contacted the guys over at Firebase Google Group for help, and they were able to tell me that my use case is not yet supported.
Thanks to Kato Richardson for attending to my problem!
For anyone interested in the details, see this thread
I came across the same use case today and I have successfully implemented a working solution in Objective C client. Below is the algorithm if anyone wants to apply in their program and I will really appreciate if google-cloud-firestore team can put my solution on their page.
Use Case: A feature to allow paginating a long list of recent chats along with the option to attach real time listeners to update the list to have chat with most recent message on top.
Solution: This can be made possible by using pagination logic like we do for other long lists and attaching real time listener with limit set to 1:
Step 1: On page load fetch the chats using pagination query as below:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self fetchChats];
}
-(void)fetchChats {
__weak typeof(self) weakSelf = self;
FIRQuery *paginateChatsQuery = [[[self.db collectionWithPath:MAGConstCollectionNameChats]queryOrderedByField:MAGConstFieldNameTimestamp descending:YES]queryLimitedTo:MAGConstPageLimit];
if(self.arrChats.count > 0){
FIRDocumentSnapshot *lastChatDocument = self.arrChats.lastObject;
paginateChatsQuery = [paginateChatsQuery queryStartingAfterDocument:lastChatDocument];
}
[paginateChatsQuery getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot, NSError * _Nullable error) {
if (snapshot == nil) {
NSLog(#"Error fetching documents: %#", error);
return;
}
///2. Observe chat updates if not attached
if(weakSelf.chatObserverState == ChatObserverStateNotAttached) {
weakSelf.chatObserverState = ChatObserverStateAttaching;
[weakSelf observeChats];
}
if(snapshot.documents.count < MAGConstPageLimit) {
weakSelf.noMoreData = YES;
}
else {
weakSelf.noMoreData = NO;
}
[weakSelf.arrChats addObjectsFromArray:snapshot.documents];
[weakSelf.tblVuChatsList reloadData];
}];
}
Step 2: On success callback of "fetchAlerts" method attach the observer for real time updates only once with limit set to 1.
-(void)observeChats {
__weak typeof(self) weakSelf = self;
self.chatsListener = [[[[self.db collectionWithPath:MAGConstCollectionNameChats]queryOrderedByField:MAGConstFieldNameTimestamp descending:YES]queryLimitedTo:1]addSnapshotListener:^(FIRQuerySnapshot * _Nullable snapshot, NSError * _Nullable error) {
if (snapshot == nil) {
NSLog(#"Error fetching documents: %#", error);
return;
}
if(weakSelf.chatObserverState == ChatObserverStateAttaching) {
weakSelf.chatObserverState = ChatObserverStateAttached;
}
for (FIRDocumentChange *diff in snapshot.documentChanges) {
if (diff.type == FIRDocumentChangeTypeAdded) {
///New chat added
NSLog(#"Added chat: %#", diff.document.data);
FIRDocumentSnapshot *chatDoc = diff.document;
[weakSelf handleChatUpdates:chatDoc];
}
else if (diff.type == FIRDocumentChangeTypeModified) {
NSLog(#"Modified chat: %#", diff.document.data);
FIRDocumentSnapshot *chatDoc = diff.document;
[weakSelf handleChatUpdates:chatDoc];
}
else if (diff.type == FIRDocumentChangeTypeRemoved) {
NSLog(#"Removed chat: %#", diff.document.data);
}
}
}];
}
Step 3. On listener callback check for document changes and handle only FIRDocumentChangeTypeAdded and FIRDocumentChangeTypeModified events and ignore the FIRDocumentChangeTypeRemoved event. We are doing this by calling "handleChatUpdates" method for both FIRDocumentChangeTypeAdded and FIRDocumentChangeTypeModified event in which we are first trying to find the matching chat document from local list and if it exist we are removing it from the list and then we are adding the new document received from listener callback and adding it to the beginning of the list.
-(void)handleChatUpdates:(FIRDocumentSnapshot *)chatDoc {
NSInteger chatIndex = [self getIndexOfMatchingChatDoc:chatDoc];
if(chatIndex != NSNotFound) {
///Remove this object
[self.arrChats removeObjectAtIndex:chatIndex];
}
///Insert this chat object at the beginning of the array
[self.arrChats insertObject:chatDoc atIndex:0];
///Refresh the tableview
[self.tblVuChatsList reloadData];
}
-(NSInteger)getIndexOfMatchingChatDoc:(FIRDocumentSnapshot *)chatDoc {
NSInteger chatIndex = 0;
for (FIRDocumentSnapshot *chatDocument in self.arrChats) {
if([chatDocument.documentID isEqualToString:chatDoc.documentID]) {
return chatIndex;
}
chatIndex++;
}
return NSNotFound;
}
Step 4. Reload the tableview to see the changes.
my solution is to create 1 maintainer query - listener to observe on those removed item from first query, and we will update it every time there's new message coming.
To make pagination with snapshot listener first we have to create reference point document from the collection.After that we are listening to collection based on that reference point document.
Let's you have a collection called messages and timestamp called createdAt with each document in that collection.
//get messages
getMessages(){
//first we will fetch the very last/latest document.
//to hold listeners
listnerArray=[];
const very_last_document= await this.afs.collectons('messages')
.ref
.limit(1)
.orderBy('createdAt','desc')
.get({ source: 'server' });
//if very_last.document.empty property become true,which means there is no messages
//present till now ,we can go with a query without having a limit
//else we have to apply the limit
if (!very_last_document.empty) {
const start = very_last_document.docs[very_last_document.docs.length - 1].data().createdAt;
//listner for new messages
//all new message will be registered on this listener
const listner_1 = this.afs.collectons('messages')
.ref
.orderBy('createdAt','desc')
.endAt(start) <== this will make sure the query will fetch up to 'start' point(including 'start' point document)
.onSnapshot(messages => {
for (const message of messages .docChanges()) {
if (message .type === "added")
//do the job...
if (message.type === "modified")
//do the job...
if (message.type === "removed")
//do the job ....
}
},
err => {
//on error
})
//old message will be registered on this listener
const listner_2 = this.afs.collectons('messages')
.ref
.orderBy('createdAt','desc')
.limit(20)
.startAfter(start) <== this will make sure the query will fetch after the 'start' point
.onSnapshot(messages => {
for (const message of messages .docChanges()) {
if (message .type === "added")
//do the job...
if (message.type === "modified")
//do the job...
if (message.type === "removed")
//do the job ....
}
this.listenerArray.push(listner_1, listner_2);
},
err => {
//on error
})
} else {
//no document found!
//very_last_document.empty = true
const listner_1 = this.afs.collectons('messages')
.ref
.orderBy('createdAt','desc')
.onSnapshot(messages => {
for (const message of messages .docChanges()) {
if (message .type === "added")
//do the job...
if (message.type === "modified")
//do the job...
if (message.type === "removed")
//do the job ....
}
},
err => {
//on error
})
this.listenerArray.push(listner_1);
}
}
//to load more messages
LoadMoreMessage(){
//Assuming messages array holding the the message we have fetched
//getting the last element from the array messages.
//that will be the starting point of our next batch
const endAt = this.messages[this.messages.length-1].createdAt
const listner_2 = this.getService
.collections('messages')
.ref
.limit(20)
.orderBy('createdAt', "asc") <== should be in 'asc' order
.endBefore(endAt) <== Getting the 20 documnents (the limit we have applied) from the point 'endAt';
.onSnapshot(messages => {
if (messages.empty && this.messages.length)
this.messages[this.messages.length - 1].hasMore = false;
for (const message of messages.docChanges()) {
if (message.type === "added")
//do the job...
if (message.type === "modified")
//do the job
if (message.type === "removed")
//do the job
}
},
err => {
//on error
})
this.listenerArray.push(listner_2)
}

Best practice of handling multiple http status error code in java

What’s the best practice of handling multiple http status error code, For example, I want to handle all 4xx and 5xx status code, except 404.
I’ve my current implementation like below : (I’ve place only sample code)
if ( HTTP.STATUS == 500 || HTTP.STATUS == 502 || HTTP.STATUS == 503 || HTTP.STATUS == 400
|| HTTP.STATUS == 401 || HTTP.STATUS == 402 || HTTP.STATUS == 403) {
String status = “Failed to send”;
}
I know this is very ugly code, can anyone suggest best way of handling the error code between 4xx and 5xx except 404 using java program
This may not be a good solution for HTTP status codes, but with regards to matching many possible values, you can use a switch statement without break to stack them all:
String status = null;
switch (HTTP.STATUS) {
case 400:
case 401:
case 402:
case 403:
case 500:
case 502:
case 503:
status = “Failed to send”;
break;
default:
status = "All ok!";
}
As you are able to switch on Enums in Java, define an Enum for the range of possible codes e.g.
public enum HttpStatusCodeRange {
SUCCESS_RANGE, CLIENT_ERROR_RANGE, SERVER_ERROR_RANGE, UNKNOWN; }
Then create a utility to calculate the Enum for the particular code you get e.g.
public static HttpStatusCodeRange getRange(int code) {
if (code >= 200 && code < 300) {
return HttpStatusCodeRange.SUCCESS_RANGE;
}
if (code >= 400 && code < 500) {
return HttpStatusCodeRange.CLIENT_ERROR_RANGE;
}
if (code >= 500 && code < 600) {
return HttpStatusCodeRange.SERVER_ERROR_RANGE;
}
return HttpStatusCodeRange.UNKNOWN;
}
Then you can write fairly neat code to process your HTTP response codes e.g.
HttpStatusCodeRange range = HttpStatusCodeRangeUtil.getRange(response.getStatus());
switch (range) {
case SUCCESS_RANGE :
handleSuccess();
break;
case CLIENT_ERROR_RANGE :
handleClientError();
break;
case SERVER_ERROR_RANGE :
handleServerError();
break;
case UNKNOWN :
handleUnexpectedError();
break;
default :
handleUnknownError();
break;
}
It's better to send particular messages, for different HTTP status responses.
Look here:
http://racksburg.com/choosing-an-http-status-code/
You can also make some generic method like this:
public class GenericExceptionMapper implements ExceptionMapper<Throwable> {
#Override
public Response toResponse(Throwable ex) {
ErrorMessage errorMessage = new ErrorMessage();
setHttpStatus(ex, errorMessage);
errorMessage.setCode(AppConstants.GENERIC_APP_ERROR_CODE);
errorMessage.setMessage(ex.getMessage());
StringWriter errorStackTrace = new StringWriter();
ex.printStackTrace(new PrintWriter(errorStackTrace));
errorMessage.setDeveloperMessage(errorStackTrace.toString());
errorMessage.setLink(AppConstants.BLOG_POST_URL);
return Response.status(errorMessage.getStatus())
.entity(errorMessage)
.type(MediaType.APPLICATION_JSON)
.build();
}
private void setHttpStatus(Throwable ex, ErrorMessage errorMessage) {
if(ex instanceof WebApplicationException ) {
errorMessage.setStatus(((WebApplicationException)ex).getResponse().getStatus());
} else {
errorMessage.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); //defaults to internal server error 500
}
}
}
Something like so perhaps:
if (status == 404) {
// handle 404
} else {
String sstatus = '' + status;
switch (sstatus.charAt(0)) {
case '4':
// handle 4xx
break
case '5':
...

How to check if call was ended by the user or automatically?

I would like to know if there is a way to ckeck if incoming call was ended by the user (dropped) or ended automatically because there wasn't a response (missed call). In other words: I want to do some action the moment when there appears a missed call (when the call is ended automatically).
Do Something like that when missed call is appeared. In my code i open activity when missed call appeared.
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.v("idle state", "CALL_STATE_IDLE");
// CALL_STATE_IDLE ( OpenMissedCallScreen )
if (ring == true && callReceived == false && CheckMissCall.isRunning== false) {
flag = true;
if (prefs.getBoolean("main_state", true))
{
Intent inter = new Intent(c, MissCall.class);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK ( OpenReceivedCallScreen )
callReceived = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
ring = true;
// CALL_STATE_RINGING ( openIncomingCallScreen )
break;

String and EditText function issues in Android

i'm trying to trigger a conditional by checking the user input in an EditText field. when i print the String from the EditText to logcat, i can see the data change, but the String functions that check against the values always return false.
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
this code always displays "Missing Driver ID!". i have tried these other conditionals, with no success:
(((EditText)findViewById(R.id.drv_in)).getText().toString().isEmpty()) //does not compile, says cannot find symbol, but the function is in the Android documentation
(((EditText)findViewById(R.id.drv_in)).getText().toString().length() < 1) //returns false, even for strings of length > 1
i can confirm that the data is, indeed, no null by looking at logcat and seeing my data show up in the logs. what's wrong with the conditional?
it doesn't fail if you insert no data in the first transmit. if the first transmit fails, all subsequent transmissions fail, regardless of whether you change the data or not. furthermore, if it passes the first transmission, it will pass all subsequent transmissions.
additionally, there are other conditionals, posted in the full code below, which also evaluate only on the first click of the button.
transmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//data validation
///////////////////////
boolean valid = true;
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
if(custSpn.getSelectedItemPosition() == 0) {
TX_FAIL_TEXT = "Missing Customer Selection!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(prdSpn.getSelectedItemPosition() == 0) {
TX_FAIL_TEXT = "Missing Product Selection!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("CAR") ||
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("AUTO") ||
((Cursor)prdSpn.getItemAtPosition(prdSpn.getSelectedItemPosition())).getString(prdSpn.getSelectedItemPosition()).contains("TRUCK")
) {
//must have make, license# and 1vin
if(((EditText)findViewById(R.id.make_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing Make/Model!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((EditText)findViewById(R.id.tag_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing Tag Number!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
if(((EditText)findViewById(R.id.vin1_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin2_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin3_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin4_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin5_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin6_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin7_in)).getText().toString().equals("") ||
((EditText)findViewById(R.id.vin8_in)).getText().toString().equals("")
) {
TX_FAIL_TEXT = "Vehicle Entry:\n Missing VIN Number!";
showDialog(DIALOG_FAIL);
TX_FAIL_TEXT = "Transmission Failed!"; //reset the dialog fail text to default
valid = false;
}
}
//Log.e("smsDRVERR",((EditText)smsActivity.this.findViewById(R.id.drv_in)).getText().toString());
//begin transmission
///////////////////////
if(valid) {
showDialog(DIALOG_TX_PROGRESS);
Thread t = new Thread(txRunnable);
t.start();
} else {
//do things if needed
}
}
I'd post this as a comment, but it'd be too long...
I don't think the problem is what you think it is. However, I can't say what the problem is, because you haven't been clear about how you're detecting success and/or failure.
Let's start by clarifying the diagnostic code, to remove any possible ambiguities. I'd suggest you change this:
if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {
TX_FAIL_TEXT = "Missing Driver ID!";
}
Log.e("SMSDRVERR", ((EditText)findViewById(R.id.drv_in)).getText().toString());
to:
final String drv = (EditText)findViewById(R.id.drv_in)).getText().toString();
if(drv.equals("") {
TX_FAIL_TEXT = "Missing Driver ID!";
Log.e("SMSDRVERR", "Missing ID " + drv);
}
else {
Log.e("SMSDRVERR", "Found ID" + drv);
}
This will eliminate any possible ambiguity in the log about whether the text really was missing. (It also makes for more readable code.)
the problem was actually with the Dialog objects. the conditional is fine. at the beginning of the onClick method, i added a call to:
removeDialog(DIALOG_FAIL);
this forces Android to rebuild the Dialog the next time it is called.
EDIT: for future reference, there is a more elegant way to do this using onPrepareDialog(), but this solution was easier for me.

Categories