Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit tests for UserServiceV1 #168

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
Expand All @@ -39,6 +40,8 @@
*/
@Path(UserService.SERVICE_RELATIVE_PATH)
public class UserServiceV1 implements UserService {
@Inject
private UserDao _userDao;

/**
* {@inheritDoc}
Expand All @@ -56,7 +59,7 @@ public Response getAllUsers() {
ResponseBuilder responseBuilder = Response.ok();
try {
List<User> users;
List<com.intuit.tank.project.User> findAll = new UserDao().findAll();
List<com.intuit.tank.project.User> findAll = _userDao.findAll();
users = findAll.stream().map(UserServiceUtil::userToTransferObject).collect(Collectors.toList());
UserContainer container = new UserContainer(users);
responseBuilder.entity(container);
Expand All @@ -75,7 +78,7 @@ public Response getAllUsers() {
public Response getUserByUsername(String username) {
ResponseBuilder responseBuilder = Response.ok();
try {
com.intuit.tank.project.User user = new UserDao().findByUserName(username);
com.intuit.tank.project.User user = _userDao.findByUserName(username);
if (user != null) {
responseBuilder.entity(UserServiceUtil.userToTransferObject(user));
} else {
Expand All @@ -93,7 +96,7 @@ public Response getUserByUsername(String username) {
public Response getUserByEmail(String email) {
ResponseBuilder responseBuilder = Response.ok();
try {
com.intuit.tank.project.User user = new UserDao().findByEmail(email);
com.intuit.tank.project.User user = _userDao.findByEmail(email);
if (user != null) {
responseBuilder.entity(UserServiceUtil.userToTransferObject(user));
} else {
Expand All @@ -114,7 +117,7 @@ public Response getUserByEmail(String email) {
public Response authenticate(UserCredentials credentials) {
ResponseBuilder responseBuilder = Response.ok();
try {
com.intuit.tank.project.User user = new UserDao().authenticate(credentials.getName(),
com.intuit.tank.project.User user = _userDao.authenticate(credentials.getName(),
credentials.getPass());
if (user != null) {
responseBuilder.entity(UserServiceUtil.userToTransferObject(user));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
package com.intuit.tank.service.impl.v1.user;

import com.intuit.tank.api.model.v1.user.UserCredentials;
import com.intuit.tank.dao.UserDao;
import com.intuit.tank.project.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class UserServiceV1Test {
@Mock
private UserDao _userDaoMock;

@Mock
private UserCredentials _userCredentialsMock;

@InjectMocks
private UserServiceV1 _sut;

private AutoCloseable closeable;


private List<User> _listUsersStub;
private User _userStub;

@BeforeEach
public void SetUp() {
closeable = MockitoAnnotations.openMocks(this);

_userStub = new User();
_listUsersStub = new ArrayList<>();
_listUsersStub.add(_userStub);
_listUsersStub.add(_userStub);
}

@AfterEach
void closeService() throws Exception {
closeable.close();
}

//region getAllUsers

@Test
public void GetAllUsers_Given_Existing_Users_Calls_User_Data_Accessor() {
// Arrange
when(_userDaoMock.findAll()).thenReturn(_listUsersStub);

// Act
_sut.getAllUsers();

// Assert
verify(_userDaoMock, times(1)).findAll();
}

@Test
public void GetAllUsers_Given_Existing_Users_Gets_Ok_Status_Code() {
// Arrange
when(_userDaoMock.findAll()).thenReturn(_listUsersStub);

// Act
Response response = _sut.getAllUsers();

// Assert
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
public void GetAllUsers_Given_Exception_Throws_WebApplicationException() {
// Arrange
doThrow(new WebApplicationException()).when(_userDaoMock).findAll();

// Act + Assert
assertThrows(WebApplicationException.class, () -> {
_sut.getAllUsers();
});
}

//endregion

//region getUserByUsername

@Test
public void GetUserByUsername_Given_Existing_User_Calls_User_Data_Accessor() {
// Arrange
when(_userDaoMock.findByUserName(any(String.class))).thenReturn(_userStub);

// Act
_sut.getUserByUsername("TestName");

// Assert
verify(_userDaoMock, times(1)).findByUserName(any(String.class));
}

@Test
public void GetUserByUsername_Given_Existing_User_Gets_Ok_Status_Code() {
// Arrange
when(_userDaoMock.findByUserName(any(String.class))).thenReturn(_userStub);

// Act
Response response = _sut.getUserByUsername("TestName");

// Assert
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
public void GetUserByUsername_Given_Not_Existing_Sets_Not_Found_Status() {
// Arrange
when(_userDaoMock.findByUserName(any(String.class))).thenReturn(null);

// Act
Response response = _sut.getUserByUsername("TestName");

// Assert
assertNotNull(response);
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
}

@Test
public void GetUserByUsername_Given_Exception_Throws_WebApplicationException() {
// Arrange
doThrow(new WebApplicationException()).when(_userDaoMock).findByUserName(any(String.class));

// Act + Assert
assertThrows(WebApplicationException.class, () -> {
_sut.getUserByUsername("Name");
});
}

//endregion

//region getUserByEmail

@Test
public void GetUserByEmail_Given_Existing_User_Calls_User_Data_Accessor() {
// Arrange
when(_userDaoMock.findByEmail(any(String.class))).thenReturn(_userStub);

// Act
_sut.getUserByEmail("TestEmail");

// Assert
verify(_userDaoMock, times(1)).findByEmail(any(String.class));
}

@Test
public void GetUserByEmail_Given_Existing_User_Gets_Ok_Status_Code() {
// Arrange
when(_userDaoMock.findByEmail(any(String.class))).thenReturn(_userStub);

// Act
Response response = _sut.getUserByEmail("TestEmail");

// Assert
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
public void GetUserByEmail_Given_Not_Existing_Sets_Not_Found_Status() {
// Arrange
when(_userDaoMock.findByEmail(any(String.class))).thenReturn(null);

// Act
Response response = _sut.getUserByEmail("TestEmail");

// Assert
assertNotNull(response);
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
}

@Test
public void GetUserByEmail_Given_Exception_Throws_WebApplicationException() {
// Arrange
doThrow(new WebApplicationException()).when(_userDaoMock).findByEmail(any(String.class));

// Act + Assert
assertThrows(WebApplicationException.class, () -> {
_sut.getUserByEmail("Name");
});
}

//endregion

//region authenticate

@Test
public void Authenticate_Given_Existing_Credentials_Calls_User_Data_Accessor() {
// Arrange
when(_userCredentialsMock.getName()).thenReturn("TestName");
when(_userCredentialsMock.getPass()).thenReturn("TestPass");
when(_userDaoMock.authenticate(any(String.class), any(String.class))).thenReturn(_userStub);

// Act
_sut.authenticate(_userCredentialsMock);

// Assert
verify(_userDaoMock, times(1)).authenticate(any(String.class), any(String.class));
}

@Test
public void Authenticate_Given_Existing_Credentials_Gets_Ok_Status_Code() {
// Arrange
when(_userCredentialsMock.getName()).thenReturn("TestName");
when(_userCredentialsMock.getPass()).thenReturn("TestPass");
when(_userDaoMock.authenticate(any(String.class), any(String.class))).thenReturn(_userStub);

// Act
Response response = _sut.authenticate(_userCredentialsMock);

// Assert
assertNotNull(response);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
}

@Test
public void Authenticate_Given_Not_Credentials_Sets_Unauthorized_Status() {
// Arrange
when(_userDaoMock.authenticate(any(String.class), any(String.class))).thenReturn(null);

// Act
Response response = _sut.authenticate(_userCredentialsMock);

// Assert
assertNotNull(response);
assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
}

@Test
public void Authenticate_Given_Exception_Throws_WebApplicationException() {
// Arrange
when(_userCredentialsMock.getName()).thenReturn("TestName");
when(_userCredentialsMock.getPass()).thenReturn("TestPass");
doThrow(new WebApplicationException()).when(_userDaoMock).authenticate(any(String.class), any(String.class));

// Act + Assert
assertThrows(WebApplicationException.class, () -> {
_sut.authenticate(_userCredentialsMock);
});
}

//endregion
}