package net.davidvoid.thor.lightning.service.security;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import net.davidvoid.thor.lightning.entity.User;
import net.davidvoid.thor.lightning.exception.AuthenticationException;

import org.junit.Test;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

public class JwtAuthenticationServiceTest {

    @Test
    public void Authenticate_GivenInvalidToken_WillThrow() {
        JwtAuthenticationService service = new JwtAuthenticationService();
        
        try {
            service.authenticate("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ");
            fail("should throw");
        } catch (AuthenticationException e) {}
    }
    @Test
    public void Authenticate_GivenValid_WillSuccess() {
        JwtAuthenticationService service = new JwtAuthenticationService();
        User user = new User();
        user.setName("david");
        user.setId(1);

        String jwtToken = service.getToken(user);
        Authentication auth = service.authenticate(jwtToken);
        assertEquals("david", auth.getName());
        assertEquals("david", auth.getPrincipal());
        assertEquals("", auth.getCredentials());
        assertEquals(1, auth.getAuthorities().size());
        GrantedAuthority role = (GrantedAuthority) auth.getAuthorities().toArray()[0];
        assertEquals("USER", role.toString());
        assertEquals(true, auth.isAuthenticated());
    }

}

@@ -125,20 +125,207 @@
 .Assert.
-fail
+assertTrue;%0Aimport static org.junit.Assert.fail;%0A%0Aimport io.jsonwebtoken.Claims;%0Aimport io.jsonwebtoken.Jws;%0Aimport io.jsonwebtoken.Jwts;%0Aimport net.davidvoid.thor.lightning.config.RootConfig
 ;%0Aimport
@@ -462,16 +462,112 @@
 t.Test;%0A
+import org.junit.runner.RunWith;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0A
 import o
@@ -678,208 +678,1716 @@
 ty;%0A
-%0Apublic class JwtAuthenticationServiceTest %7B%0A%0A    @Test%0A    public void Authenticate_GivenInvalidToken_WillThrow() %7B%0A        JwtAuthenticationService service = new JwtAuthenticationService();%0A        
+import org.springframework.test.context.ActiveProfiles;%0Aimport org.springframework.test.context.ContextConfiguration;%0Aimport org.springframework.test.context.junit4.SpringJUnit4ClassRunner;%0A%0Aimport java.io.BufferedReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStreamReader;%0Aimport java.util.Date;%0A%0A@RunWith(SpringJUnit4ClassRunner.class)%0A@ActiveProfiles(%22test%22)%0A@ContextConfiguration(classes = %7B RootConfig.class %7D)%0Apublic class JwtAuthenticationServiceTest %7B%0A%0A    @Autowired%0A    JwtAuthenticationService service = new JwtAuthenticationService();%0A%0A    @Test%0A    public void getToken_willReturnAValidToken_WichContainsSubjectAndUidAndIssueAtFields() %7B%0A        User user = new User();%0A        user.setName(%22david%22);%0A        user.setPassword(%221234%22);%0A        user.setId(123);%0A%0A        String token = service.getToken(user);%0A        String key = load_key(%22config/key.hmac%22);%0A%0A        Jws%3CClaims%3E claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token);%0A        assertEquals(%22david%22, claims.getBody().getSubject());%0A        assertEquals(123, claims.getBody().get(%22uid%22));%0A        // the issue time should be no more than 5 seconds before now%0A        assertTrue((new Date().getTime() - claims.getBody().getIssuedAt().getTime()) %3C 5000);%0A    %7D%0A%0A    private String load_key(String filename) %7B%0A        ClassLoader loader = this.getClass().getClassLoader();%0A%0A        try (BufferedReader reader = new BufferedReader(new InputStreamReader(loader.getResourceAsStream(filename)))) %7B%0A            return reader.readLine();%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(%22failed to load key%22);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void Authenticate_GivenInvalidToken_WillThrow() %7B
 %0A   
@@ -2667,24 +2667,25 @@
 e) %7B%7D%0A    %7D%0A
+%0A
     @Test%0A  
@@ -2742,83 +2742,8 @@
 ) %7B%0A
-        JwtAuthenticationService service = new JwtAuthenticationService();%0A
     

add test for token generation and use spring framework while test