package org.camunda.bpm.qa.upgrade.authorization;

import static org.junit.Assert.assertEquals;

import java.util.Collections;

import org.camunda.bpm.engine.AuthorizationService;
import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.authorization.Groups;
import org.camunda.bpm.engine.authorization.Permissions;
import org.camunda.bpm.engine.authorization.Resources;
import org.camunda.bpm.engine.impl.db.sql.DbSqlSessionFactory;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class AuthorizationTest {

  protected AuthorizationService authorizationService;

  protected IdentityService identityService;

  protected ProcessEngineConfiguration processEngineConfiguration;

  @Rule
  public ProcessEngineRule rule = new ProcessEngineRule();

  @Before
  public void init() {
    authorizationService = rule.getAuthorizationService();
    identityService = rule.getIdentityService();
    processEngineConfiguration = rule.getProcessEngineConfiguration();
  }
  
  @Test
  public void testDefaultAuthorizationQueryForCamundaAdminOnUpgrade() {

    processEngineConfiguration.setAuthorizationEnabled(true);
    
    assertEquals(1, authorizationService.createAuthorizationQuery()
      .resourceType(Resources.TENANT)
      .groupIdIn(Groups.CAMUNDA_ADMIN)
      .hasPermission(Permissions.ALL).count());

    assertEquals(1, authorizationService.createAuthorizationQuery()
      .resourceType(Resources.TENANT_MEMBERSHIP)
      .groupIdIn(Groups.CAMUNDA_ADMIN)
      .hasPermission(Permissions.ALL).count());
    
    assertEquals(1, authorizationService.createAuthorizationQuery()
      .resourceType(Resources.BATCH)
      .groupIdIn(Groups.CAMUNDA_ADMIN)
      .hasPermission(Permissions.ALL).count());

  }

  @Test
  public void testDefaultAuthorizationForCamundaAdminOnUpgrade() {

    // The below test cases are skipped for H2 as there is a bug in H2 version 1.3 (Query does not return the expected output)
    // This H2 exclusion check will be removed as part of CAM-6044, when the H2 database is upgraded to the version 1.4 (Bug was fixed)
    if (DbSqlSessionFactory.H2.equals(processEngineConfiguration.getDatabaseType())) {
      return;
    }
    
    processEngineConfiguration.setAuthorizationEnabled(true);
    assertEquals(true,authorizationService.isUserAuthorized(null, Collections.singletonList(Groups.CAMUNDA_ADMIN), Permissions.ALL, Resources.TENANT));
    assertEquals(true,authorizationService.isUserAuthorized(null, Collections.singletonList(Groups.CAMUNDA_ADMIN), Permissions.ALL, Resources.TENANT_MEMBERSHIP));
    assertEquals(true,authorizationService.isUserAuthorized(null, Collections.singletonList(Groups.CAMUNDA_ADMIN), Permissions.ALL, Resources.BATCH));  
  }
}

@@ -555,24 +555,48 @@
 EngineRule;%0A
+import org.junit.After;%0A
 import org.j
@@ -608,16 +608,16 @@
 Before;%0A
-
 import o
@@ -859,16 +859,66 @@
 ation;%0A%0A
+  protected boolean defaultAuthorizationEnabled;%0A%0A
   @Rule%0A
@@ -1193,15 +1193,238 @@
 ();%0A
+    defaultAuthorizationEnabled = processEngineConfiguration.isAuthorizationEnabled();%0A  %7D%0A%0A  @After%0A  public void restoreAuthorization() %7B%0A    processEngineConfiguration.setAuthorizationEnabled(defaultAuthorizationEnabled);%0A
   %7D%0A
-  
 %0A
+
   @T
@@ -1562,21 +1562,17 @@
 (true);%0A
-    
 %0A
+
     asse
@@ -1961,21 +1961,17 @@
 unt());%0A
-    
 %0A
+
     asse
@@ -2610,13 +2610,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -2980,24 +2980,24 @@
 MBERSHIP));%0A
+
     assertEq
@@ -3134,17 +3134,15 @@
 BATCH));
-  
 %0A  %7D%0A%7D%0A

disable authorization after test