[R9] Unit test classes for JWT Use Case classes

This commit is contained in:
Mirna Gama 2024-01-12 15:13:46 -03:00 committed by Mirna Gama
parent 57a4dd52d7
commit 60c2336579
2 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,51 @@
package com.mirna.hospitalmanagementapi.unit.application.usecase.jwt;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import com.mirna.hospitalmanagementapi.HospitalManagementApiApplication;
import com.mirna.hospitalmanagementapi.application.usecase.auth.jwt.CreateJWTUseCase;
import com.mirna.hospitalmanagementapi.domain.dtos.auth.UserDTO;
import com.mirna.hospitalmanagementapi.domain.entities.auth.User;
/**
*
* @author Mirna Gama
* @version 1.0
*/
@SpringBootTest(classes = HospitalManagementApiApplication.class)
@TestInstance(Lifecycle.PER_CLASS)
@ActiveProfiles("test")
public class CreateJWTUseCaseTest {
@Autowired
private CreateJWTUseCase createJWT;
@Value("${api.security.token.secret}")
private String secret;
/**
* Should execute create jwt method and return token successfully
*
*/
@Test
@DisplayName("Should execute create jwt method and return token successfully")
public void testCreateJWT() throws Exception {
User user = new User(new UserDTO("test", "password"));
String token = createJWT.execute(user);
assertNotNull(token);
}
}

View File

@ -0,0 +1,58 @@
package com.mirna.hospitalmanagementapi.unit.application.usecase.jwt;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.mirna.hospitalmanagementapi.HospitalManagementApiApplication;
import com.mirna.hospitalmanagementapi.application.usecase.auth.jwt.GetJWTSubjectUseCase;
/**
*
* @author Mirna Gama
* @version 1.0
*/
@SpringBootTest(classes = HospitalManagementApiApplication.class)
@TestInstance(Lifecycle.PER_CLASS)
@ActiveProfiles("test")
public class GetJWTSubjectUseCaseTest {
@Autowired
GetJWTSubjectUseCase getJWTSubject;
@Value("${api.security.token.secret}")
private String secret;
String testToken;
@BeforeAll
public void init() {
testToken = JWT.create().withIssuer("Hospital-Management-API")
.withSubject("test")
.withClaim("id", 1L)
.sign(Algorithm.HMAC256(secret));
}
/**
* Should execute verify method and get jwt subject successfully
*
*/
@Test
@DisplayName("Should execute verify method and get jwt subject successfully")
public void testGetSubject() throws Exception {
String subject = getJWTSubject.execute(testToken);
assertEquals("test", subject);
}
}