package com.coding4people.mosquitoreport.api.controllers;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Test;
import org.mockito.Mock;

import com.coding4people.mosquitoreport.api.WithServer;
import com.coding4people.mosquitoreport.api.controllers.SignUpController.SignUpInput;
import com.coding4people.mosquitoreport.api.models.Email;
import com.coding4people.mosquitoreport.api.repositories.EmailRepository;
import com.coding4people.mosquitoreport.api.repositories.UserRepository;

public class SignUpControllerTest extends WithServer {
    @Mock
    EmailRepository emailRepository;
    @Mock
    UserRepository userRepository;

    @Override
    protected ResourceConfig configure() {
        return super.configure().register(SignUpController.class);
    }

    @Test
    public void testSignUpEmailAlreadyExists() {
        SignUpInput input = new SignUpInput();
        input.setEmail("test@test.org");
        input.setPassword("123456");
        input.setFirstname("new firstname");
        input.setLastname("new lastname");

        when(emailRepository.load(input.getEmail())).thenReturn(new Email());

        Response response = target().path("/signup/email").request().post(Entity.json(input));

        assertEquals(400, response.getStatus());

        verify(emailRepository).load(eq(input.getEmail()));
    }
}

@@ -100,16 +100,104 @@
 quals;%0D%0A
+import static org.junit.Assert.assertNotNull;%0D%0Aimport static org.mockito.Matchers.any;%0D%0A
 import s
@@ -228,16 +228,16 @@
 rs.eq;%0D%0A
-
 import s
@@ -689,24 +689,82 @@
 els.Email;%0D%0A
+import com.coding4people.mosquitoreport.api.models.User;%0D%0A
 import com.c
@@ -1696,32 +1696,32 @@
 etStatus());%0D%0A%0D%0A
-
         verify(e
@@ -1771,12 +1771,1045 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    @Test%0D%0A    public void testSignUp() %7B%0D%0A        SignUpInput input = new SignUpInput();%0D%0A        input.setEmail(%22test@test.org%22);%0D%0A        input.setPassword(%22123456%22);%0D%0A        input.setFirstname(%22firstname%22);%0D%0A        input.setLastname(%22lastname%22);%0D%0A%0D%0A        when(emailRepository.load(input.getEmail())).thenReturn(null);%0D%0A%0D%0A        Response response = target().path(%22/signup/email%22).request().post(Entity.json(input));%0D%0A%0D%0A        assertEquals(200, response.getStatus());%0D%0A        assertEquals(%22application/json;charset=UTF-8%22, response.getHeaderString(%22Content-type%22));%0D%0A%0D%0A        verify(emailRepository).load(eq(input.getEmail()));%0D%0A        verify(emailRepository).save(any(Email.class));%0D%0A        verify(userRepository).save(any(User.class));%0D%0A        %0D%0A        User user = response.readEntity(User.class);%0D%0A        assertNotNull(user.getGuid());%0D%0A        assertEquals(%22test@test.org%22, user.getEmail());%0D%0A        assertEquals(%22firstname%22, user.getFirstname());%0D%0A        assertEquals(%22lastname%22, user.getLastname());%0D%0A    %7D%0D
 %0A%7D%0D%0A

Test sign up success