package fi.nls.oskari.control.admin;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.HashSet;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.oskari.permissions.PermissionService;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import fi.nls.oskari.control.ActionDeniedException;
import fi.nls.oskari.control.ActionParameters;
import fi.nls.oskari.domain.Role;
import fi.nls.oskari.service.OskariComponentManager;
import fi.nls.oskari.service.ServiceException;
import fi.nls.oskari.service.UserService;
import fi.nls.test.control.JSONActionRouteTest;
import fi.nls.test.util.ResourceHelper;

@RunWith(PowerMockRunner.class)
@PrepareForTest({UserService.class, OskariComponentManager.class})
public class LayerAdminMetadataHandlerTest extends JSONActionRouteTest {
    
    final private static LayerAdminMetadataHandler handler = new LayerAdminMetadataHandler();
    
    @BeforeClass
    public static void setup() throws ServiceException {
        
        setupUserServiceMock();
        setupPermissionsServiceMock();
        handler.init();
    }
    private static void setupUserServiceMock() throws ServiceException {
        
        UserService userService = mock(UserService.class);
        PowerMockito.mockStatic(UserService.class);
        when(UserService.getInstance()).thenReturn(userService);
        
        Role role1 = new Role();
        role1.setId(1L);
        role1.setName("Guest");
        Role role2 = new Role();
        role2.setId(2L);
        role2.setName("User");
        Role role3 = new Role();
        role3.setId(3L);
        role3.setName("Admin");
        
        Role[] mockRoles = new Role[]{role1,role2,role3};
        
        doReturn(mockRoles).when(userService).getRoles();
        doReturn(Role.getAdminRole()).when(userService).getRoleByName("Admin");

    }
    private static void setupPermissionsServiceMock() throws ServiceException {
        PermissionService permissionService = mock(PermissionService.class);
        PowerMockito.mockStatic(OskariComponentManager.class);
        when(OskariComponentManager.getComponentOfType(PermissionService.class)).thenReturn(permissionService);
        
        doReturn(new HashSet<String>()).when(permissionService).getAdditionalPermissions();
        when(permissionService.getPermissionName(anyString(),anyString())).thenAnswer(invocation -> { return (String) invocation.getArgument(0); });
    }
    
    @Test
    public void testGetWithAdmin() throws Exception {
   
        final ActionParameters params = createActionParams(getAdminUser());
        handler.handleAction(params);

        verifyResponseContent(ResourceHelper.readJSONResource("LayerAdminMetadataHandlerTests-expected.json", this));
    }
    
    
    @Test(expected = ActionDeniedException.class)
    public void testGetWithGuest() throws Exception {

        final ActionParameters params = createActionParams(getGuestUser());
        handler.handleAction(params);
    }
    
    @Test(expected = ActionDeniedException.class)
    public void testGetWithLoggedInNonAdminUser() throws Exception {

        final ActionParameters params = createActionParams(getLoggedInUser());
        handler.handleAction(params);
    }
}

@@ -123,125 +123,178 @@
 ito.
-doReturn;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;%0A%0Aimport java.util.HashSet;%0A
+*;%0A%0Aimport java.util.HashSet;%0A%0Aimport fi.nls.oskari.service.DummyUserService;%0Aimport fi.nls.oskari.util.PropertyUtil;%0Aimport org.junit.After;%0Aimport org.junit.AfterClass;
 %0Aimp
@@ -1009,27 +1009,8 @@
 st(%7B
-UserService.class, 
 Oska
@@ -1260,39 +1260,32 @@
  setup() throws 
-Service
 Exception %7B%0A    
@@ -1292,867 +1292,274 @@
     
-%0A        setupUserServiceMock();%0A        setupPermissionsServiceMock();%0A        handler.init();%0A    %7D%0A    private static void setupUserServiceMock() throws ServiceException %7B%0A        %0A        UserService 
+PropertyUtil.addProperty(%22oskari.
 user
-S
+.s
 ervice
- = mock(UserService.class);%0A        PowerMockito.mockStatic(UserService.class);%0A        when(UserService.getInstance()).thenReturn(userService);%0A        %0A        Role role1 = new Role();%0A        role1.setId(1L);%0A        role1.setName(%22Guest%22);%0A        Role role2 = new Role();%0A        role2.setId(2L
+%22, DummyUserService.class.getCanonicalName(), true);%0A        setupPermissionsServiceMock();%0A        handler.init(
 );%0A    
+%7D%0A
     
-role2.setName(%22User%22);%0A        Role role3 = new Role();%0A        role3.setId(3L);%0A        role3.setName(%22Admin%22);%0A        %0A        Role%5B%5D mockRoles = new Role%5B%5D%7Brole1,role2,role3%7D;%0A        %0A        doReturn(mockRoles).when(userService).getRoles();%0A        doReturn(Role.getAdminRole()).when(userService).getRoleByName(%22Admin%22
+@AfterClass%0A    public static void tearDown() %7B%0A        PropertyUtil.clearProperties(
 );%0A
-%0A
     %7D%0A
+%0A
     
@@ -1611,32 +1611,8 @@
 ck()
- throws ServiceException
  %7B%0A 
@@ -1967,24 +1967,74 @@
 ();%0A        
+doAnswer(invocation -%3E invocation.getArgument(0)).
 when(permiss
@@ -2043,16 +2043,17 @@
 nService
+)
 .getPerm
@@ -2091,82 +2091,8 @@
 g())
-).thenAnswer(invocation -%3E %7B return (String) invocation.getArgument(0); %7D)
 ;%0A  
@@ -2164,20 +2164,16 @@
 ption %7B%0A
-   %0A
         
@@ -2278,17 +2278,16 @@
 arams);%0A
-%0A
         
@@ -2508,33 +2508,32 @@
 ows Exception %7B%0A
-%0A
         final Ac
@@ -2760,17 +2760,16 @@
 ption %7B%0A
-%0A
         

Remove custom mock and use DummyUserService