/*
 * Copyright 2012 Splunk, Inc.
 *
 * 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.
 */

package com.splunk;

import java.util.Arrays;

import org.junit.Test;

public class RoleTest extends SDKTestCase {
    @Test
    public void testSymmetricAccessors() {
        Role role = service.getRoles().create(createTemporaryName());
        try {
            role.setCapabilities(new String[] { "admin_all_objects" });
            role.setDefaultApp("search");
            role.setImportedRoles(new String[] { "user" });
            role.setSearchDiskQuota(9000);
            role.setSearchFilter("*");
            role.setSearchIndexesAllowed(new String[] { "*" });
            role.setSearchIndexesDefault(new String[] { "main" });
            role.setSearchJobsQuota(49);
            
            role.setRealTimeSearchJobsQuota(99);
            role.setSearchTimeWindow(99);
            
            role.update();
            role.refresh();
            
            assertEquals(new String[] { "admin_all_objects" }, role.getCapabilities());
            assertEquals("search", role.getDefaultApp());
            assertEquals(new String[] { "user" }, role.getImportedRoles());
            assertEquals(9000, role.getSearchDiskQuota());
            assertEquals("*", role.getSearchFilter());
            assertEquals(new String[] { "*" }, role.getSearchIndexesAllowed());
            assertEquals(new String[] { "main" }, role.getSearchIndexesDefault());
            assertEquals(49, role.getSearchJobsQuota());
            
            assertEquals(99, role.getRealTimeSearchJobsQuota());
            assertEquals(99, role.getSearchTimeWindow());
        }
        finally {
            role.remove();
        }
    }
    
    @Test
    public void testArrayPromotingSetters() {
        Role role = service.getRoles().create(createTemporaryName());
        try {
            role.setCapabilities("admin_all_objects");
            role.setImportedRoles("user");
            role.setSearchIndexesAllowed("*");
            role.setSearchIndexesDefault("main");
            
            role.update();
            role.refresh();
            
            assertEquals(new String[] { "admin_all_objects" }, role.getCapabilities());
            assertEquals(new String[] { "user" }, role.getImportedRoles());
            assertEquals(new String[] { "*" }, role.getSearchIndexesAllowed());
            assertEquals(new String[] { "main" }, role.getSearchIndexesDefault());
        }
        finally {
            role.remove();
        }
    }
    
    @Test
    public void testUnpairedImportedGetters() {
        Role role = service.getRoles().create(createTemporaryName());
        try {
            role.setImportedRoles("user");
            
            role.update();
            role.refresh();
            
            /*
             * NOTE: These expected values probably come from the "user" role.
             *       If the default values of the "user" role change in the
             *       future, this test should be rewritten to lookup the values
             *       from the "user" role to determine the expected values here.
             */
            assertTrue(Arrays.asList(role.getImportedCapabilities()).contains(
                    "change_own_password"));
            assertEquals(6, role.getImportedRealTimeSearchJobsQuota());
            assertEquals(100, role.getImportedSearchDiskQuota());
            assertEquals(null, role.getImportedSearchFilter());
            assertEquals(new String[] { "*" }, role.getImportedIndexesAllowed());
            assertEquals(new String[] { "main" }, role.getImportedIndexesDefault());
            assertEquals(3, role.getImportedSearchJobsQuota());
            assertEquals(-1, role.getImportedSearchTimeWindow());
        }
        finally {
            role.remove();
        }
    }
    
    // === Utility ===
    
    private void assertEquals(String[] a1, String[] a2) {
        assertEquals(Arrays.asList(a1), Arrays.asList(a2));
    }
}

@@ -635,16 +635,39 @@
 .Arrays;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -1247,16 +1247,29 @@
 %7B %22main%22
+, %22_internal%22
  %7D);%0A   
@@ -1923,75 +1923,252 @@
     
-assertEquals(new String%5B%5D %7B %22main%22 %7D, role.getSearchIndexesDefault(
+List%3CString%3E defaultIndexes = Arrays.asList(role.getSearchIndexesDefault());%0A            assertEquals(2, defaultIndexes.size());%0A            assertTrue(defaultIndexes.contains(%22main%22));%0A            assertTrue(defaultIndexes.contains(%22_internal%22
 ));%0A

Test get/set searchIndexesDefault on Role with multiple values.