package fi.nls.oskari.control.admin;

import fi.nls.oskari.control.ActionDeniedException;
import fi.nls.oskari.control.ActionHandler;
import fi.nls.oskari.domain.Role;
import fi.nls.oskari.service.UserService;
import fi.nls.test.control.JSONActionRouteTest;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.reflect.Whitebox;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.fail;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

/**
 * Simple user-credentials check for handlers listed in getAdminHandlers().
 */
@PrepareForTest(value = {UserService.class})
@RunWith(Parameterized.class)
public class UserCredentialsTest extends JSONActionRouteTest {

    private ActionHandler handler = null;

    @BeforeClass
    public static void setup() {

        UserService userService = mock(UserService.class);
        Role role = new Role();
        role.setName("Admin");
        doReturn(role).when(userService).getRoleByName(role.getName());
        Whitebox.setInternalState(UserService.class, "instance", userService);
    }

    @Parameterized.Parameters
    public static Collection getAdminHandlers() {
        // testing the same thing for all these admin routes
        return Arrays.asList(new Object[][]{
                {CacheHandler.class},
                {ManageRolesHandler.class},
                {SystemViewsHandler.class},
                {UsersHandler.class}
        });
    }

    public UserCredentialsTest(Class<ActionHandler> clazz) {
        try {
            handler = clazz.newInstance();
            handler.init();
        } catch (Exception ignored) {}
    }

    @Test(expected = ActionDeniedException.class)
    public void testWithGuest() throws Exception {
        handler.handleAction(createActionParams());
        fail("ActionDeniedException should have been thrown. ActionHandler: " + handler.getClass().getName());
    }

    @Test(expected = ActionDeniedException.class)
    public void testWithUser() throws Exception {
        handler.handleAction(createActionParams(getLoggedInUser()));
        fail("ActionDeniedException should have been thrown. ActionHandler: " + handler.getClass().getName());
    }
    @Test
    public void testWithAdmin() throws Exception {
        System.out.println("Parameterized ActionHandler is : " + handler.getClass().getName());
        handler.handleAction(createActionParams(getAdminUser()));
        // no exception thrown so we are happy
    }
}

@@ -190,16 +190,21 @@
 service.
+Dummy
 UserServ
@@ -208,16 +208,98 @@
 ervice;%0A
+import fi.nls.oskari.service.UserService;%0Aimport fi.nls.oskari.util.PropertyUtil;%0A
 import f
@@ -334,24 +334,53 @@
 nRouteTest;%0A
+import org.junit.AfterClass;%0A
 import org.j
@@ -864,53 +864,8 @@
  */%0A
-@PrepareForTest(value = %7BUserService.class%7D)%0A
 @Run
@@ -1049,50 +1049,88 @@
 p() 
-%7B%0A%0A        UserService 
+throws Exception %7B%0A        PropertyUtil.addProperty(%22oskari.
 user
-S
+.s
 ervice
- = mock(
+%22, Dummy
 User
@@ -1146,222 +1146,131 @@
 lass
-);%0A        Role role = new Role(
+.getCanonicalName(), true
 );%0A    
+%7D%0A
     
-role.setName(%22Admin%22);%0A        doReturn(role).when(userService).getRoleByName(role.getName());%0A        Whitebox.setInternalState(UserService.class, %22instance%22, userService
+@AfterClass%0A    public static void tearDown() %7B%0A        PropertyUtil.clearProperties(
 );%0A 
@@ -1579,32 +1579,32 @@
 Handler.class%7D,%0A
-
                 
@@ -1623,16 +1623,67 @@
 r.class%7D
+,%0A                %7BLayerAdminMetadataHandler.class%7D
 %0A       

Use DummyUserService instead of mock