This is login screen from my spring mvc web app.i need to catch every possible error conditon and store to database.like bug detecting system.
for example if user click login button without filling form the error condition nothing provided should save on error table.
and if it not provide password it should save password not provided.
@RequestMapping(value = { "/do-login" }, method = RequestMethod.POST)
public String login(@ModelAttribute(value = "user") User user) {
process(user);
return "success";
}
public void process(User user) {
List<User> users = userService.findAll();
for (User u : users) {
if (!u.getUserName().equals(user.getUserName())) {
IssueDetail detail = new IssueDetail();
detail.setSummary("user name");
detail.setDescription("wrong user name entered");
issueDetailService.save(detail);
} else if (!u.getPassword().equals(user.getPassword())) {
IssueDetail detail = new IssueDetail();
detail.setSummary("password");
detail.setDescription("wrong password entered");
issueDetailService.save(detail);
} else if (!u.getUserType().equalsIgnoreCase(user.getUserType())) {
IssueDetail detail = new IssueDetail();
detail.setSummary("user type");
detail.setDescription("wrong user type entered");
issueDetailService.save(detail);
}
}
}
i tried the above code.... when the above looping run, if user not entering any of the field the loop is saving any of the if statement 3 times in database.
is there any alternate way to do it.
TEST CASES
Username password type
-------------------------
N N N =NULL
Y N N = WRONG PASS & TYPE
Y Y N = WRONG TYPE.
N Y Y = WRONG USER.
N N Y = WRONG PASSWORD & TYPE.
Try below line of code,inside process method in place of your for loop.
List<User> users = userService.findAll();
boolean isUserNameExist = false;
boolean isPasswordExist = false;
boolean isUserTypeExist = false;
for (User u : users) {
if (u.getUserName().equals(user.getUserName())) {
isUserNameExist = true;
break;
}
}
for (User u : users) {
if (u.getPassword().equals(user.getPassword())) {
isPasswordExist =true;
break;
}
}
for (User u : users) {
if (u.getUserType().equalsIgnoreCase(user.getUserType())) {
isUserTypeExist = true;
break;
}
}
IssueDetail detail = new IssueDetail();
if(!isUserNameExist){
detail.setSummary("user name");
detail.setDescription("wrong user name entered");
}
if(!isPasswordExist){
detail.setSummary(detail.getSummary()+" password");
detail.setDescription(detail.getDescription()+" wrong password entered");
}
if(!isUserTypeExist){
detail.setSummary(detail.getSummary() +" user type");
detail.setDescription(detail.getDescription() +" wrong user type entered");
}
issueDetailService.save(detail);