/**
 * Copyright 2013 DuraSpace, 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 org.fcrepo.auth.roles.common.integration;

import static javax.ws.rs.core.Response.Status.CREATED;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static javax.ws.rs.core.Response.Status.OK;
import static org.junit.Assert.assertEquals;

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

import org.apache.http.client.ClientProtocolException;
import org.fcrepo.auth.roles.common.AccessRolesProvider;
import org.junit.Test;

/**
 * @author Gregory Jansen
 * @author Scott Prater
 */
public class AccessRolesIT extends AbstractCommonRolesIT {

    @Override
    protected List<RolesPepTestObjectBean> getTestObjs() {
        return test_objs;
    }

    /**
     * Test method for
     * {@link org.fcrepo.auth.roles.common.AccessRoles#get(java.util.List)}.
     *
     * @throws IOException
     * @throws ClientProtocolException
     */
    @Test
    public void testGetEmptyRoles() throws ClientProtocolException, IOException {
        assertEquals("Can get empty role list from object with no roles",
                NO_CONTENT.getStatusCode(), canGetRoles(null, "testcommonobj1",
                        false));
    }

    /**
     * Test method for
     * {@link org.fcrepo.auth.roles.common.AccessRoles#get(java.util.List)}.
     *
     * @throws IOException
     * @throws ClientProtocolException
     */
    @Test
    public void testCRUDRoles() throws ClientProtocolException, IOException {

        // add the roles
        assertEquals(CREATED.getStatusCode(), postRoles("testcommonobj1",
                test_json_roles));

        // Get the roles
        assertEquals("Can get role list from object with roles", OK
                .getStatusCode(),
                canGetRoles(null, "testcommonobj1", false));

        assertEquals("result must equal test data", t_roles,
                getRoles("testcommonobj1"));

        // delete the roles
        assertEquals("Can delete role list from object with roles", NO_CONTENT
                .getStatusCode(), canDeleteRoles(null, "testcommonobj1", false));

        // verify that roles are gone
        assertEquals("Can get empty role list from object with no roles",
                NO_CONTENT.getStatusCode(), canGetRoles(null, "testcommonobj1",
                        false));

    }

    /**
     * Test method for
     * {@link org.fcrepo.auth.roles.common.AccessRoles#get(java.util.List, java.util.List)}
     * .
     *
     * @throws IOException
     * @throws ClientProtocolException
     */
    @Test
    public void testGetEffectiveRoles() throws ClientProtocolException,
    IOException {
        // verify that default roles are returned
        // Get the roles
        assertEquals("Can get effective role list from object", OK
                .getStatusCode(), canGetEffectiveRoles(null, "testcommonobj1",
                        false));

        assertEquals("result must equal test data",
                AccessRolesProvider.DEFAULT_ACCESS_ROLES,
                getEffectiveRoles("testcommonobj1"));

        // post some roles on parent
        assertEquals(CREATED.getStatusCode(), postRoles("testcommonobj1",
                test_json_roles));

        // see that parent roles are effective for child
        assertEquals(
                "Can get effective role list from child object with inherited roles",
                OK
                .getStatusCode(), canGetEffectiveRoles(null,
                        "testcommonobj1/testchildobj1", false));

        assertEquals("result must equal test data", t_roles,
                getEffectiveRoles("testcommonobj1/testchildobj1"));


        // post different acl with fewer roles on the child
        assertEquals("Can post admin ACL on child object", CREATED
                .getStatusCode(), postRoles("testcommonobj1/testchildobj1",
                        admin_json_role));

        // see that only child roles are effective for child
        assertEquals(
                "Can get effective role list from child object with own role",
                OK.getStatusCode(), canGetEffectiveRoles(null,
                        "testcommonobj1/testchildobj1", false));

        assertEquals("result must equal test data", admin_role,
                getEffectiveRoles("testcommonobj1/testchildobj1"));

    }
}

@@ -1567,32 +1567,35 @@
 ssertEquals(%22Can
+not
  get empty role 
@@ -1616,32 +1616,33 @@
 ct with no roles
+!
 %22,%0A             
@@ -2207,24 +2207,27 @@
 tEquals(%22Can
+not
  get role li
@@ -2251,16 +2251,17 @@
 th roles
+!
 %22, OK%0A  
@@ -2372,34 +2372,38 @@
 sertEquals(%22
-r
+R
 esult 
-mus
+does no
 t equal test
@@ -2399,32 +2399,33 @@
  equal test data
+!
 %22, t_roles,%0A    
@@ -2519,16 +2519,19 @@
 als(%22Can
+not
  delete 
@@ -2562,18 +2562,35 @@
 th roles
+!
 %22,
+%0A               
  NO_CONT
@@ -2735,24 +2735,27 @@
 tEquals(%22Can
+not
  get empty r
@@ -2784,24 +2784,25 @@
 ith no roles
+!
 %22,%0A         
@@ -3322,24 +3322,27 @@
 tEquals(%22Can
+not
  get effecti
@@ -3365,16 +3365,17 @@
 m object
+!
 %22, OK%0A  
@@ -3503,34 +3503,38 @@
 sertEquals(%22
-r
+R
 esult 
-mus
+does no
 t equal test
@@ -3538,16 +3538,17 @@
 est data
+!
 %22,%0A     
@@ -3893,32 +3893,35 @@
             %22Can
+not
  get effective r
@@ -3967,16 +3967,17 @@
 ed roles
+!
 %22,%0A     
@@ -4135,34 +4135,38 @@
 sertEquals(%22
-r
+R
 esult 
-mus
+does no
 t equal test
@@ -4170,16 +4170,17 @@
 est data
+!
 %22, t_rol
@@ -4338,16 +4338,19 @@
 als(%22Can
+not
  post ad
@@ -4372,16 +4372,17 @@
 d object
+!
 %22, CREAT
@@ -4607,16 +4607,19 @@
     %22Can
+not
  get eff
@@ -4666,16 +4666,17 @@
 own role
+!
 %22,%0A     
@@ -4825,18 +4825,22 @@
 ls(%22
-r
+R
 esult 
-mus
+does no
 t eq
@@ -4852,16 +4852,17 @@
 est data
+!
 %22, admin

Fix assertEquals() messages to indicate error.