package de.osiam.client;

import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.osiam.client.OsiamUserService;
import org.osiam.client.exception.NoResultException;
import org.osiam.client.exception.UnauthorizedException;
import org.osiam.client.query.QueryBuilder;
import org.osiam.client.query.QueryResult;
import org.osiam.resources.scim.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

import java.util.List;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/context.xml")
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
        DbUnitTestExecutionListener.class})
@DatabaseSetup("/database_seed.xml")
public class UserServiceIT extends AbstractIntegrationTestBase {


    private UUID validUUID = null;

    private OsiamUserService service;
    private User deserializedUser;
    private QueryResult<User> queryResult;

    @Before
    public void setUp() throws Exception {
        service = new OsiamUserService.Builder(endpointAddress).build();
    }

    @Test
    public void get_a_valid_user() throws Exception {
        givenAValidUserUUID();
        whenUserIsDeserialized();
        assertEquals(validUUID.toString(), deserializedUser.getId());
    }



    @Test
    public void name_is_deserialized_correctly() throws Exception {
        givenAValidUserUUID();
        whenUserIsDeserialized();

        Name name = deserializedUser.getName();

        assertEquals("Jensen", name.getFamilyName());
        assertEquals("Ms. Barbara J Jensen III", name.getFormatted());
        assertEquals("Barbara", name.getGivenName());
        assertEquals(null, name.getHonorificPrefix());
        assertEquals(null, name.getHonorificSuffix());
        assertEquals(null, name.getMiddleName());
    }

    @Test
    public void emails_are_deserialized_correctly() throws Exception {
        givenAValidUserUUID();
        whenUserIsDeserialized();

        List<MultiValuedAttribute> emails = deserializedUser.getEmails();
        assertEquals(1, emails.size());
        MultiValuedAttribute email = emails.get(0);

        assertEquals("bjensen@example.com", email.getValue().toString());
        assertEquals("work", email.getType());
    }

    @Test
    public void photos_are_deserialized_correctly() throws Exception {
        givenAValidUserUUID();
        whenUserIsDeserialized();

        List<MultiValuedAttribute> photos = deserializedUser.getPhotos();
        assertEquals(1, photos.size());
        MultiValuedAttribute photo = photos.get(0);

        assertEquals("http://example.com/barbara.jpg", photo.getValue().toString());
        assertEquals("photo", photo.getType());
    }

    @Test
    public void ims_are_deserialized_correctly() throws Exception {
        givenAValidUserUUID();
        whenUserIsDeserialized();

        List<MultiValuedAttribute> ims = deserializedUser.getIms();
        assertEquals(1, ims.size());
        MultiValuedAttribute im = ims.get(0);

        assertEquals("barbara", im.getValue().toString());
        assertEquals("xmpp", im.getType());
    }

    @Test
    public void external_id_is_deserialized_correctly() throws Exception {
        givenAValidUserUUID();
        whenUserIsDeserialized();

    }

    @Test
    public void ensure_basic_values_are_deserialized_correctly() throws Exception {
        givenAValidUserUUID();
        whenUserIsDeserialized();

        assertEquals("bjensen", deserializedUser.getExternalId());
        assertEquals("BarbaraJ.", deserializedUser.getDisplayName());
        assertEquals("de", deserializedUser.getLocale());
        assertEquals("Barbara", deserializedUser.getNickName());
        assertEquals("UTC", deserializedUser.getTimezone());
        assertEquals("Dr.", deserializedUser.getTitle());
        assertEquals("bjensen", deserializedUser.getUserName());
        assertEquals("user", deserializedUser.getUserType());
    }

    @Test
    public void password_is_not_transferred() throws Exception {
        givenAValidUserUUID();
        whenUserIsDeserialized();
        assertEquals(null, deserializedUser.getPassword());
    }

    @Test(expected = NoResultException.class)
    public void get_an_invalid_user_raises_exception() throws Exception {
        service.getUserByUUID(UUID.fromString(INVALID_UUID), accessToken);
    }

    @Test(expected = UnauthorizedException.class)
    public void provide_an_invalid_access_token_raises_exception() throws Exception {
        givenAValidUserUUID();
        given_an_invalid_access_token();

        whenUserIsDeserialized();
        fail();
    }

    private void whenUserIsDeserialized() {
        deserializedUser = service.getUserByUUID(validUUID, accessToken);
    }

    private void givenAValidUserUUID() throws Exception {
        validUUID = UUID.fromString(VALID_USER_UUID);
    }
}
@@ -1374,50 +1374,8 @@
 er;%0A
-    private QueryResult%3CUser%3E queryResult;
 %0A%0A  
@@ -1511,216 +1511,8 @@
  %7D%0A%0A
-    @Test%0A    public void get_a_valid_user() throws Exception %7B%0A        givenAValidUserUUID();%0A        whenUserIsDeserialized();%0A        assertEquals(validUUID.toString(), deserializedUser.getId());%0A    %7D%0A%0A%0A%0A
     
@@ -2518,2072 +2518,337 @@
 oid 
-photos_are_deserialized_correctly() throws Exception %7B%0A        givenAValidUserUUID();%0A        whenUserIsDeserialized();%0A%0A        List%3CMultiValuedAttribute%3E photos = deserializedUser.getPhotos();%0A        assertEquals(1, photos.size());%0A        MultiValuedAttribute photo = photos.get(0);%0A%0A        assertEquals(%22http://example.com/barbara.jpg%22, photo.getValue().toString());%0A        assertEquals(%22photo%22, photo.getType());%0A    %7D%0A%0A    @Test%0A    public void ims_are_deserialized_correctly() throws Exception %7B%0A        givenAValidUserUUID();%0A        whenUserIsDeserialized();%0A%0A        List%3CMultiValuedAttribute%3E ims = deserializedUser.getIms();%0A        assertEquals(1, ims.size());%0A        MultiValuedAttribute im = ims.get(0);%0A%0A        assertEquals(%22barbara%22, im.getValue().toString());%0A        assertEquals(%22xmpp%22, im.getType());%0A    %7D%0A%0A    @Test%0A    public void external_id_is_deserialized_correctly() throws Exception %7B%0A        givenAValidUserUUID();%0A        whenUserIsDeserialized();%0A%0A    %7D%0A%0A    @Test%0A    public void ensure_basic_values_are_deserialized_correctly() throws Exception %7B%0A        givenAValidUserUUID();%0A        whenUserIsDeserialized();%0A%0A        assertEquals(%22bjensen%22, deserializedUser.getExternalId());%0A        assertEquals(%22BarbaraJ.%22, deserializedUser.getDisplayName());%0A        assertEquals(%22de%22, deserializedUser.getLocale());%0A        assertEquals(%22Barbara%22, deserializedUser.getNickName());%0A        assertEquals(%22UTC%22, deserializedUser.getTimezone());%0A        assertEquals(%22Dr.%22, deserializedUser.getTitle());%0A        assertEquals(%22bjensen%22, deserializedUser.getUserName());%0A        assertEquals(%22user%22, deserializedUser.getUserType());%0A    %7D%0A%0A    @Test%0A    public void password_is_not_transferred() throws Exception %7B%0A        givenAValidUserUUID();%0A        whenUserIsDeserialized();%0A        assertEquals(null, deserializedUser.getPassword());%0A    %7D%0A%0A    @Test(expected = NoResultException.class)%0A    public void get_an_invalid_user_raises_exception() throws Exception %7B%0A        service.getUserByUUID(UUID.fromString(INVALID_UUID), accessToken
+external_id_is_deserialized_correctly() throws Exception %7B%0A        givenAValidUserUUID();%0A        whenUserIsDeserialized();%0A%0A    %7D%0A%0A    @Test%0A    public void password_is_not_transferred() throws Exception %7B%0A        givenAValidUserUUID();%0A        whenUserIsDeserialized();%0A        assertEquals(null, deserializedUser.getPassword()
 );%0A 

make UserServiceIt smaller for faster Testing