package org.osiam.addons.administration.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.osiam.addons.administration.model.session.GeneralSessionData;
import org.osiam.client.OsiamConnector;
import org.osiam.client.oauth.AccessToken;
import org.osiam.client.query.Query;
import org.osiam.resources.scim.SCIMSearchResult;
import org.osiam.resources.scim.UpdateUser;
import org.osiam.resources.scim.User;

@RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {

    @Mock
    OsiamConnector connector;

    @Mock
    AccessToken accessToken;

    @Spy
    @InjectMocks
    GeneralSessionData sessionData = new GeneralSessionData();

    @Spy
    @InjectMocks
    UserService toTestSpy = new UserService();

    @SuppressWarnings("unchecked")
    @Test
    public void searchUser_onlyQuery() {
        final SCIMSearchResult<User> toReturn = mock(SCIMSearchResult.class);
        doReturn(toReturn).when(toTestSpy).searchUser(anyString(), anyInt(), anyLong(), anyString(), anyBoolean(),
                anyString());

        final String query = "TestQuery";
        final SCIMSearchResult<User> result = toTestSpy.searchUser(query);

        assertSame(result, toReturn);
        verify(toTestSpy).searchUser(query, null, null, null, null, null);
    }

    @SuppressWarnings("unchecked")
    @Test
    public void searchUser_withLimit() {
        final SCIMSearchResult<User> toReturn = mock(SCIMSearchResult.class);
        doReturn(toReturn).when(toTestSpy).searchUser(anyString(), anyInt(), anyLong(), anyString(), anyBoolean(),
                anyString());

        final String query = "TestQuery";
        final Integer limit = 13;
        final Long offset = 12L;
        final SCIMSearchResult<User> result = toTestSpy.searchUser(query, limit, offset);

        assertSame(result, toReturn);
        verify(toTestSpy).searchUser(query, limit, offset, null, null, null);
    }

    @Test
    public void searchUser_advanced() {
        final String query = "testQuery";
        final Integer limit = 13;
        final Long offset = 12L;
        final String orderBy = "orderby";
        final Boolean asc = false; // desc
        final String attributes = "attributes";

        toTestSpy.searchUser(query, limit, offset, orderBy, asc, attributes);

        ArgumentCaptor<Query> cap = ArgumentCaptor.forClass(Query.class);

        verify(connector).searchUsers(cap.capture(), same(accessToken));

        Query usedQuery = cap.getValue();
        assertEquals(query, usedQuery.getFilter());
        assertTrue(limit == usedQuery.getCount());
        assertTrue(offset == usedQuery.getStartIndex());
        assertEquals(orderBy, usedQuery.getSortBy());
        assertEquals("descending", usedQuery.getSortOrder());
        assertEquals(attributes, usedQuery.getAttributes());
    }

    @Test
    public void getUser() {
        String id = "userID";

        User user = mock(User.class);
        doReturn(user).when(connector).getUser(eq(id), same(accessToken));

        User result = toTestSpy.getUser(id);
        assertEquals(user, result);
    }

    @Test
    public void updateUser() {
        UpdateUser updateUser = new UpdateUser.Builder().build();
        String id = "user ID";

        toTestSpy.updateUser(id, updateUser);
        verify(connector, times(1)).updateUser(eq(id), eq(updateUser), same(accessToken));
    }
}

@@ -129,235 +129,12 @@
 sert
-Same;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.mockito.Matchers.anyBoolean;%0Aimport static org.mockito.Matchers.anyInt;%0Aimport static org.mockito.Matchers.anyLong;%0Aimport static org.mockito.Matchers.anyString
+True
 ;%0Aim
@@ -795,58 +795,8 @@
 ry;%0A
-import org.osiam.resources.scim.SCIMSearchResult;%0A
 impo
@@ -1134,24 +1134,24 @@
 InjectMocks%0A
+
     UserServ
@@ -1190,1189 +1190,8 @@
 );%0A%0A
-    @SuppressWarnings(%22unchecked%22)%0A    @Test%0A    public void searchUser_onlyQuery() %7B%0A        final SCIMSearchResult%3CUser%3E toReturn = mock(SCIMSearchResult.class);%0A        doReturn(toReturn).when(toTestSpy).searchUser(anyString(), anyInt(), anyLong(), anyString(), anyBoolean(),%0A                anyString());%0A%0A        final String query = %22TestQuery%22;%0A        final SCIMSearchResult%3CUser%3E result = toTestSpy.searchUser(query);%0A%0A        assertSame(result, toReturn);%0A        verify(toTestSpy).searchUser(query, null, null, null, null, null);%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    @Test%0A    public void searchUser_withLimit() %7B%0A        final SCIMSearchResult%3CUser%3E toReturn = mock(SCIMSearchResult.class);%0A        doReturn(toReturn).when(toTestSpy).searchUser(anyString(), anyInt(), anyLong(), anyString(), anyBoolean(),%0A                anyString());%0A%0A        final String query = %22TestQuery%22;%0A        final Integer limit = 13;%0A        final Long offset = 12L;%0A        final SCIMSearchResult%3CUser%3E result = toTestSpy.searchUser(query, limit, offset);%0A%0A        assertSame(result, toReturn);%0A        verify(toTestSpy).searchUser(query, limit, offset, null, null, null);%0A    %7D%0A%0A
     

fix rebase failure