package org.oregami.service;

import io.dropwizard.testing.junit.DropwizardAppRule;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.mockito.Mockito;
import org.oregami.data.UserDao;
import org.oregami.dropwizard.OregamiApplication;
import org.oregami.dropwizard.OregamiConfiguration;
import org.oregami.entities.user.User;
import org.oregami.util.MailHelper;

public class TestUserService {

    @ClassRule
    public static final DropwizardAppRule<OregamiConfiguration> RULE =
            new DropwizardAppRule<OregamiConfiguration>(OregamiApplication.class, "src/test/resources/oregami.yml");

    private UserServiceImpl userService;

    @Before
    public void setup() {
        userService = new UserServiceImpl();
        UserDao userRepository = Mockito.mock(UserDao.class);
        userService.setUserDao(userRepository);
    }

    @After
    public void tearDown() {
        userService = null;
    }

    @Test
    public void testRegisterInvalidUser() {
        User user = new User();

        ServiceResult<User> result = userService.register(user);
        Assert.assertEquals(false, result.wasSuccessful());
        Assert.assertEquals(true, result.hasErrors());
        Assert.assertTrue(result.containsError(new ServiceError(new ServiceErrorContext("user.email"), ServiceErrorMessage.USER_EMAIL_EMPTY)));
        Assert.assertTrue(result.containsError(new ServiceError(new ServiceErrorContext("user.password"), ServiceErrorMessage.USER_PASSWORD_EMPTY)));
        Assert.assertTrue(result.containsError(new ServiceError(new ServiceErrorContext("user.username"), ServiceErrorMessage.USER_USERNAME_EMPTY)));
        Assert.assertTrue(result.containsError(new ServiceError(new ServiceErrorContext("user.username"), ServiceErrorMessage.USER_USERNAME_TOO_SHORT)));
        Assert.assertEquals(4,  result.getErrors().size());
        
    }

    @Test
    public void testRegisterUser() {
        User user = new User();
        user.setEmail("email@example.com");
        user.setUsername("username@example.com");
        user.setPassword("password");

        userService.setMailhelper(Mockito.mock(MailHelper.class));
        
        ServiceResult<User> result = userService.register(user);
        Assert.assertEquals(true, result.wasSuccessful());
        Assert.assertEquals(false, result.hasErrors());

        //        Assert.assertNotNull(user.getRegistrationTime());
    }
    
    

}

@@ -2042,25 +2042,28 @@
 il(%22
-email@example.com
+testuser@oregami.org
 %22);%0A
@@ -2092,28 +2092,16 @@
 me(%22
+test
 user
-name@example.com
 %22);%0A
@@ -2126,16 +2126,20 @@
 ssword(%22
+test
 password
@@ -2206,32 +2206,24 @@
 er.class));%0A
-        
 %0A        Ser

test update