package ru.job4j.layoutPresentation;

import org.junit.Assert;
import org.junit.Test;
import ru.job4j.layoutPersistent.DBStore;
import ru.job4j.layoutPersistent.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static org.mockito.Mockito.*;

public class CRUDServletTest {

    @Test
    public void createUser() throws ServletException, IOException {
        UserCreateServlet userCreate = new UserCreateServlet();
        HttpServletRequest request = mock(HttpServletRequest.class);
        HttpServletResponse response = mock(HttpServletResponse.class);

        when(request.getParameter("name")).thenReturn("test");
        when(request.getParameter("login")).thenReturn("test");
        when(request.getParameter("password")).thenReturn("test");
        when(request.getParameter("email")).thenReturn("test@test.te");
        when(request.getParameter("role")).thenReturn("user");

        userCreate.doPost(request, response);
        DBStore db = DBStore.getInstance();
        User user = db.findById(db.getId());

        Assert.assertEquals(user.getLogin(), "test");

        verify(request, atLeast(1)).getParameter("name");
        verify(request, atLeast(1)).getParameter("login");
        verify(request, atLeast(1)).getParameter("password");
        verify(request, atLeast(1)).getParameter("email");
        verify(request, atLeast(1)).getParameter("role");
    }

    @Test
    public void updateTest() throws ServletException, IOException {
        UserUpdateServlet userUpdate = new UserUpdateServlet();
        HttpServletRequest request = mock(HttpServletRequest.class);
        HttpServletResponse response = mock(HttpServletResponse.class);
        DBStore db = DBStore.getInstance();
        this.createUser();
        when(request.getParameter("id")).thenReturn(String.valueOf(db.getId()));
        when(request.getParameter("name")).thenReturn("newTest");
        when(request.getParameter("login")).thenReturn("newTest");
        when(request.getParameter("password")).thenReturn("newPassword");
        when(request.getParameter("email")).thenReturn("newTest@test.te");
        when(request.getParameter("role")).thenReturn("user");
        userUpdate.doPost(request,response);
        User user = db.findById(db.getId());
        Assert.assertTrue(user.getLogin().equals("newTest") &
                user.getPassword().equals("newPassword"));
        verify(request, atLeast(1)).getParameter("id");
        verify(request, atLeast(1)).getParameter("name");
        verify(request, atLeast(1)).getParameter("login");
        verify(request, atLeast(1)).getParameter("password");
        verify(request, atLeast(1)).getParameter("email");
        verify(request, atLeast(1)).getParameter("role");
    }
}
@@ -2848,10 +2848,665 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void deleteTest() throws ServletException, IOException %7B%0A        UserDeleteServlet deleteUser = new UserDeleteServlet();%0A        HttpServletRequest request = mock(HttpServletRequest.class);%0A        HttpServletResponse response = mock(HttpServletResponse.class);%0A        this.createUser();%0A        DBStore db = DBStore.getInstance();%0A        int currentId = db.getId();%0A        when(request.getParameter(%22id%22)).thenReturn(String.valueOf(currentId));%0A        deleteUser.doGet(request, response);%0A        User user = db.findById(currentId);%0A        Assert.assertNull(user);%0A        verify(request, atLeast(1)).getParameter(%22id%22);%0A    %7D
 %0A%7D

update file for chapter_006_9.7.1