/*
 * #[license]
 * Smartsheet Java SDK
 * %%
 * Copyright (C) 2014 - 2015 Smartsheet
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * %[license]
 */

import com.smartsheet.api.AuthorizationException;
import com.smartsheet.api.ResourceNotFoundException;
import com.smartsheet.api.Smartsheet;
import com.smartsheet.api.SmartsheetException;
import com.smartsheet.api.models.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

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

public class GroupResourcesIT extends ITResourcesImpl{
    Smartsheet smartsheet;
    Long groupId;
    long groupMemberId;
    PagedResult<Group> groups;

    @Before
    public void setUp() throws Exception {
        smartsheet = createAuthentication();
    }

    @Test
    public void testGroupMethods() throws SmartsheetException, IOException {
        testCreateGroup();
        testListGroups();
        testGetGroupById();
        testUpdateGroup();
        testAddMembersToGroup();
        testRemoveMemberFromGroup();
        testDeleteGroup();
    }

    public void testCreateGroup() throws SmartsheetException, IOException {
        UserProfile user = smartsheet.userResources().getCurrentUser();

        if (user.getGroupAdmin()) {
            GroupMember member = new GroupMember.AddGroupMemberBuilder().setEmail("aditi.nioding@gmail.com").build();

            Group group = new Group.CreateGroupBuilder().setName("Test Group").setDescription("Test group").setMembers(Arrays.asList(member)).build();

            try {
                group =  smartsheet.groupResources().createGroup(group);
                assertNotNull(group.getId());
            } catch (AuthorizationException e) {
                fail("Not authorized.");
            }
        } else {
            assertTrue("User does not have rights to create a group", true);
        }

    }

    public void testListGroups() throws SmartsheetException, IOException {

        PaginationParameters parameters = new PaginationParameters.PaginationParametersBuilder().setIncludeAll(true).build();
        groups =  smartsheet.groupResources().listGroups(parameters);

        assertNotNull(groups);
        groupId = groups.getData().get(0).getId();
    }

    public void testGetGroupById() throws SmartsheetException, IOException {

        Group group =  smartsheet.groupResources().getGroup(groupId);
        assertNotNull(group);
    }

    //not executed in test due to permission issue
    public void testUpdateGroup() throws SmartsheetException, IOException {
        UserProfile user = smartsheet.userResources().getCurrentUser();

        if (user.getGroupAdmin()) {
            try {
                Group groupUpdated = new Group.UpdateGroupBuilder().setName("Renamed Group").setId(groupId).setDescription("Some description").build();
                assertNotNull(smartsheet.groupResources().updateGroup(groupUpdated));
            } catch (AuthorizationException e) {
                fail("Not authorized.");
            }
        } else {
            assertTrue("User does not have rights to update a group", true);
        }
    }

    //not executed in test due to permission issue
    public void testDeleteGroup() throws SmartsheetException, IOException {
        UserProfile user = smartsheet.userResources().getCurrentUser();

        if (user.getGroupAdmin()) {
            try {
                smartsheet.groupResources().deleteGroup(groupId);
            } catch (AuthorizationException e) {
                fail("Not authorized.");
            }
        } else {
            assertTrue("User does not have rights to delete a group", true);
        }
    }

    //not executed in test due to permission issue
    public void testAddMembersToGroup() throws SmartsheetException, IOException {
        UserProfile user = smartsheet.userResources().getCurrentUser();

        if (user.getGroupAdmin()) {
            try {
                GroupMember member = new GroupMember.AddGroupMemberBuilder().setEmail("jane.doe@smartsheet.com").build();

                List<GroupMember> addedMembers = smartsheet.groupResources().memberResources().addGroupMembers(groupId, Arrays.asList(member));
                assertTrue(addedMembers.size() > 0);
                groupMemberId = addedMembers.get(0).getId();
            } catch (AuthorizationException e) {
                fail("Not authorized.");
            }
        } else {
            assertTrue("User does not have rights to add members to a group.", true);
        }
    }

    //not executed in test due to permission issue
    public void testRemoveMemberFromGroup() throws SmartsheetException, IOException {
        UserProfile user = smartsheet.userResources().getCurrentUser();

        if (user.getGroupAdmin()) {
            try {
                smartsheet.groupResources().memberResources().deleteGroupMember(groupId, groupMemberId);
            } catch (AuthorizationException e) {
                fail("Not authorized.");
            } catch (ResourceNotFoundException e) {
                fail("Resource not found.");
            }
        } else {
            assertTrue("User does not have rights to remove members from a group.", true);
        }
    }
}

@@ -2411,103 +2411,8 @@
    %7D
- else %7B%0A            assertTrue(%22User does not have rights to create a group%22, true);%0A        %7D%0A
 %0A   
@@ -3562,102 +3562,8 @@
   %7D%0A
-        %7D else %7B%0A            assertTrue(%22User does not have rights to update a group%22, true);%0A
     
@@ -4003,102 +4003,8 @@
   %7D%0A
-        %7D else %7B%0A            assertTrue(%22User does not have rights to delete a group%22, true);%0A
     
@@ -4765,111 +4765,8 @@
   %7D%0A
-        %7D else %7B%0A            assertTrue(%22User does not have rights to add members to a group.%22, true);%0A
     
@@ -5330,24 +5330,24 @@
 t found.%22);%0A
+
             
@@ -5352,116 +5352,8 @@
   %7D%0A
-        %7D else %7B%0A            assertTrue(%22User does not have rights to remove members from a group.%22, true);%0A
     

Clean up GroupResources test