File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes how to write a JUnit test for Spring COntroller which deals with Authentication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "how to write a JUnit test for Spring COntroller which deals with Authentication" Watch "how to write a JUnit test for Spring COntroller which deals with Authentication" New topic
Author

how to write a JUnit test for Spring COntroller which deals with Authentication

ravi tej
Greenhorn

Joined: Apr 07, 2013
Posts: 1
Hi,

Below is the sample code of My Controller. I need to write a JUnit test method for the below method "getNextVersionBySessionKey."

@Scope("prototype")
@Controller
@RequestMapping("Publications")
public class PublicationController extends AbstractController {

@RequestMapping(value = "Publication/Get/NextVersion/BySessionKey/")
public @ResponseBody

ReturnStatus<String> getNextVersionBySessionKey()
throws ContentStandardSecurityException, ContentStandardException,
DaoException {
String sessionKey = (new UserInformation()).getSessionKey();
ReturnStatus<String> status = new ReturnStatus<String>(
"success");
status.setPayload(publicationDelegate
.getNextVersion(sessionKey));
return status;

}
}
}

The above method calls the "UserInformation" method to retrieve the session key. Below is the code of the class "UserInformation"

public class UserInformation {

private Authentication mAuthenticationInformation = null;

public UserInformation() {
mAuthenticationInformation = getAuthentication();
}

public String getSessionKey() throws ContentStandardSecurityException{
CoreStamdardsLoginModule.UsernamePrincipal authenticatedPrincipal = null;
try {
for (GrantedAuthority authority : mAuthenticationInformation
.getAuthorities()) {
// we are only using the first principal.
JaasGrantedAuthority jaasAuthority = (JaasGrantedAuthority) authority;
authenticatedPrincipal = (CoreStamdardsLoginModule.UsernamePrincipal) jaasAuthority
.getPrincipal();
break;
}
} catch (Exception exp) {
throw new ContentStandardSecurityException(exp.getMessage());
}
return authenticatedPrincipal.getSessionKey();
}

private Authentication getAuthentication() {

StringBuilder builder = new StringBuilder("");
Subject subject = Subject.getSubject(AccessController.getContext());
if (subject != null) {
builder.append(Subject.getSubject(AccessController.getContext())
+ " is....: " + subject);
}

Authentication auth = SecurityContextHolder.getContext()
.getAuthentication();
if (auth != null) {

builder.append("Authentication object is of type: <em>"
+ auth.getClass().getName() + "</em>");
builder.append("Authentication object as a String: <br/><br/>"
+ auth.toString());

builder.append("Authentication object holds the following granted authorities:<br /><br />");
for (GrantedAuthority authority : auth.getAuthorities()) {
builder.append(authority + "(<em>" + authority.getAuthority()
+ "</em>:" + authority.getAuthority() + "<br />");
}

builder.append("<p><b>Success! Your web filters appear to be properly configured!</b></p>");
} else {
builder.append("Authentication object is null.<br />");
builder.append("This is an error and your Spring Security application will not operate properly until corrected.<br /><br />");
}

return auth;
}
}

Could any one please guide in writing the JUnit for this controller.

Thanks in Advance.

Regards,
Ravi Teja
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to write a JUnit test for Spring COntroller which deals with Authentication
 
Similar Threads
entityManager always NULL
Spring MVC 3.0 Controller Unit testing
Passing objects across controllers in Grails
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional
Need help in understanding the basics