/*
 *
 * CODENVY CONFIDENTIAL
 * ________________
 *
 * [2012] - [2014] Codenvy, S.A.
 * All Rights Reserved.
 * NOTICE: All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any. The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.analytics.util;


import com.codenvy.analytics.BaseTest;
import com.codenvy.api.analytics.dto.MetricInfoDTO;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import javax.ws.rs.core.SecurityContext;
import java.security.Principal;
import java.util.Arrays;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.AssertJUnit.assertEquals;

/**
 * @author Anatoliy Bazko
 */
public class TestUtils extends BaseTest {

    private static final String SYSTEM_USER = "user@codenvy.com";
    private static final String ANY_USER    = "user@gmail.com";

    private Principal       principal;
    private SecurityContext securityContext;
    private MetricInfoDTO   metricInfoDTO;

    @BeforeMethod
    public void setUp() {
        principal = mock(Principal.class);
        securityContext = mock(SecurityContext.class);
        metricInfoDTO = mock(MetricInfoDTO.class);
    }

    @Test
    public void restrictAccessIfPrincipalIsNull() throws Exception {
        when(securityContext.getUserPrincipal()).thenReturn(null);
        when(metricInfoDTO.getRolesAllowed()).thenReturn(Arrays.asList("user"));

        assertEquals(false, Utils.isRolesAllowed(metricInfoDTO, securityContext));
    }

    @Test
    public void restrictAccessForSystemUserIfRolesAreEmpty() throws Exception {
        when(securityContext.getUserPrincipal()).thenReturn(principal);
        when(principal.getName()).thenReturn(SYSTEM_USER);

        assertEquals(false, Utils.isRolesAllowed(metricInfoDTO, securityContext));
    }

    @Test
    public void restrictAccessForUserIfRolesAreEmpty() throws Exception {
        when(securityContext.getUserPrincipal()).thenReturn(principal);
        when(principal.getName()).thenReturn(ANY_USER);

        assertEquals(false, Utils.isRolesAllowed(metricInfoDTO, securityContext));
    }

    @Test
    public void allowAccessForSystemUserIfUserRoles() throws Exception {
        when(securityContext.getUserPrincipal()).thenReturn(principal);
        when(principal.getName()).thenReturn(SYSTEM_USER);
        when(metricInfoDTO.getRolesAllowed()).thenReturn(Arrays.asList("user"));

        assertEquals(true, Utils.isRolesAllowed(metricInfoDTO, securityContext));
    }

    @Test
    public void allowAccessForUserIfAnyRoles() throws Exception {
        when(securityContext.getUserPrincipal()).thenReturn(principal);
        when(principal.getName()).thenReturn(ANY_USER);
        when(metricInfoDTO.getRolesAllowed()).thenReturn(Arrays.asList("any"));

        assertEquals(true, Utils.isRolesAllowed(metricInfoDTO, securityContext));
    }

    @Test
    public void allowAccessIfRolesSuit() throws Exception {
        when(securityContext.getUserPrincipal()).thenReturn(principal);
        when(securityContext.isUserInRole("user")).thenReturn(true);
        when(principal.getName()).thenReturn(ANY_USER);
        when(metricInfoDTO.getRolesAllowed()).thenReturn(Arrays.asList("user"));

        assertEquals(true, Utils.isRolesAllowed(metricInfoDTO, securityContext));
    }

    @Test
    public void restrictAccessIfRolesNotSuit() throws Exception {
        when(securityContext.getUserPrincipal()).thenReturn(principal);
        when(securityContext.isUserInRole("user")).thenReturn(false);
        when(principal.getName()).thenReturn(ANY_USER);
        when(metricInfoDTO.getRolesAllowed()).thenReturn(Arrays.asList("user"));

        assertEquals(false, Utils.isRolesAllowed(metricInfoDTO, securityContext));
    }

    @Test(dataProvider = "systemLoginProvider")
    public void testIsSystemUser(String login, boolean validated) throws Exception {
        assertEquals(validated, Utils.isSystemUser(login));
    }

    @Test(dataProvider = "systemLoginProvider")
    public void testIsSystemAdmin(String login, boolean validated) throws Exception {
        when(securityContext.getUserPrincipal()).thenReturn(principal);
        when(securityContext.isUserInRole("system/admin")).thenReturn(false);
        when(securityContext.isUserInRole("system/manager")).thenReturn(false);
        when(principal.getName()).thenReturn(ANY_USER);

        assertEquals(validated, Utils.isSystemUser(login, securityContext));
    }

    @DataProvider(name = "systemLoginProvider")
    public Object[][] systemLoginProvider() {
        return new Object[][]{{SYSTEM_USER, true},
                              {ANY_USER, false},
                              {"codenvy.com", false}};
    }
}

@@ -1291,19 +1291,20 @@
 ing 
-ANY
+SOME
 _USER   
  = %22
@@ -1299,17 +1299,16 @@
 _USER   
- 
 = %22user@
@@ -2471,35 +2471,36 @@
 e()).thenReturn(
-ANY
+SOME
 _USER);%0A%0A       
@@ -2859,38 +2859,41 @@
 n(Arrays.asList(
-%22user%22
+SOME_USER
 ));%0A%0A        ass
@@ -3155,35 +3155,36 @@
 e()).thenReturn(
-ANY
+SOME
 _USER);%0A        
@@ -3593,35 +3593,36 @@
 e()).thenReturn(
-ANY
+SOME
 _USER);%0A        
@@ -4039,35 +4039,36 @@
 e()).thenReturn(
-ANY
+SOME
 _USER);%0A        
@@ -4435,513 +4435,8 @@
  %7D%0A%0A
-    @Test(dataProvider = %22systemLoginProvider%22)%0A    public void testIsSystemAdmin(String login, boolean validated) throws Exception %7B%0A        when(securityContext.getUserPrincipal()).thenReturn(principal);%0A        when(securityContext.isUserInRole(%22system/admin%22)).thenReturn(false);%0A        when(securityContext.isUserInRole(%22system/manager%22)).thenReturn(false);%0A        when(principal.getName()).thenReturn(ANY_USER);%0A%0A        assertEquals(validated, Utils.isSystemUser(login, securityContext));%0A    %7D%0A%0A
     
@@ -4611,11 +4611,12 @@
    %7B
-ANY
+SOME
 _USE

Support codenvycorp realm