lundi 27 avril 2020

How to write test case for controller that uses Authentication as function argument in spring-boot

I am completely new to spring boot and trying to write Unit test cases but stuck completely and not able to understand how Authentication works.

Controller class

@GetMapping(value = "/getAllConceptsNames")
    public ConceptCO getAllConceptsNames(Authentication authentication) {
        return conceptDelegate
                .getAllConceptNames(PrincipalUtil.getConcepts(authentication));
    }

PrincipalUtil.java

public final class PrincipalUtil {

    public static Set<String> getConcepts(final Authentication authentication) {
        UserPrincipal user =
                (UserPrincipal) authentication.getPrincipal();
        Set<String> allConcepts = new HashSet<String>();
        user.getUserPrincipalAttributes().forEach(role -> allConcepts.addAll(role.getConcepts()));
        return allConcepts;
    }

UserPrincipalAttributes .java

public class UserPrincipalAttributes {
    Set<String> columns;
    Set<String> concepts;
    String role;
    // getter & setters
}

UserPrincipal.java

public class UserPrincipal implements AuthenticatedPrincipal {
    private String name;
    private Set<UserPrincipalAttributes> userPrincipalAttributes;
    private String token;
    // getter & setters
}

Aucun commentaire:

Enregistrer un commentaire