package utils;

import models.*;
import models.Posting;

import org.junit.Test;

import controllers.UserApp;
import static org.fest.assertions.Assertions.assertThat;

import models.enumeration.Operation;
import play.db.ebean.Model.Finder;

public class AccessControlTest extends ModelTest<Role>{
    @Test
    public void isAllowed() {
        // Given
        User admin = User.findByLoginId("admin");
        User hobi = User.findByLoginId("hobi");
        Project nforge4java = Project.findByNameAndOwner("hobi", "nForge4java");
        Project jindo = Project.findByNameAndOwner("k16wire", "Jindo");

        // When
        boolean result1 = AccessControl.isAllowed(admin, nforge4java.asResource(), Operation.UPDATE);
        boolean result2 = AccessControl.isAllowed(hobi, jindo.asResource(), Operation.UPDATE);
        boolean result3 = AccessControl.isAllowed(UserApp.anonymous, jindo.asResource(), Operation.READ);

        // Then
        assertThat(result1).isEqualTo(true);
        assertThat(result2).isEqualTo(false);
        assertThat(result3).isEqualTo(false);
    }
}

@@ -29,31 +29,8 @@
 s.*;
-%0Aimport models.Posting;
 %0A%0Aim
@@ -55,36 +55,8 @@
 t;%0A%0A
-import controllers.UserApp;%0A
 impo
@@ -149,43 +149,8 @@
 ion;
-%0Aimport play.db.ebean.Model.Finder;
 %0A%0Apu
@@ -238,16 +238,26 @@
 sAllowed
+_siteAdmin
 () %7B%0A   
@@ -316,24 +316,665 @@
 d(%22admin%22);%0A
+        Project nforge4java = Project.findByNameAndOwner(%22hobi%22, %22nForge4java%22);%0A%0A        // When%0A        boolean canUpdate = AccessControl.isAllowed(admin, nforge4java.asResource(), Operation.UPDATE);%0A        boolean canRead = AccessControl.isAllowed(admin, nforge4java.asResource(), Operation.READ);%0A        boolean canDelete = AccessControl.isAllowed(admin, nforge4java.asResource(), Operation.DELETE);%0A%0A        // Then%0A        assertThat(canRead).isEqualTo(true);%0A        assertThat(canUpdate).isEqualTo(true);%0A        assertThat(canDelete).isEqualTo(true);%0A    %7D%0A%0A    @Test%0A    public void isAllowed_projectCreator() %7B%0A        // Given%0A
         User
@@ -1086,32 +1086,33 @@
 rge4java%22);%0A
+%0A
         
 Project jind
@@ -1103,21 +1103,632 @@
     
-Project jindo
+// When%0A        boolean canUpdate = AccessControl.isAllowed(hobi, nforge4java.asResource(), Operation.UPDATE);%0A        boolean canRead = AccessControl.isAllowed(hobi, nforge4java.asResource(), Operation.READ);%0A        boolean canDelete = AccessControl.isAllowed(hobi, nforge4java.asResource(), Operation.DELETE);%0A%0A        // Then%0A        assertThat(canRead).isEqualTo(true);%0A        assertThat(canUpdate).isEqualTo(true);%0A        assertThat(canDelete).isEqualTo(true);%0A    %7D%0A%0A    @Test%0A    public void isAllowed_notAMember() %7B%0A        // Given%0A        User notMember = User.findByLoginId(%22nori%22);%0A        Project nforge4java
  = P
@@ -1758,24 +1758,27 @@
 er(%22
-k16wire%22, %22Jindo
+hobi%22, %22nForge4java
 %22);%0A
@@ -1810,23 +1810,25 @@
 boolean 
-result1
+canUpdate
  = Acces
@@ -1838,37 +1838,41 @@
 ntrol.isAllowed(
-admin
+notMember
 , nforge4java.as
@@ -1918,23 +1918,23 @@
 boolean 
-result2
+canRead
  = Acces
@@ -1956,19 +1956,30 @@
 wed(
-hobi, jindo
+notMember, nforge4java
 .asR
@@ -1991,38 +1991,36 @@
 ce(), Operation.
-UPDATE
+READ
 );%0A        boole
@@ -2022,23 +2022,25 @@
 boolean 
-result3
+canDelete
  = Acces
@@ -2062,32 +2062,30 @@
 wed(
-UserApp.anonymous, jindo
+notMember, nforge4java
 .asR
@@ -2105,20 +2105,22 @@
 eration.
-READ
+DELETE
 );%0A%0A    
@@ -2154,15 +2154,15 @@
 hat(
-result1
+canRead
 ).is
@@ -2199,15 +2199,17 @@
 hat(
-result2
+canUpdate
 ).is
@@ -2247,15 +2247,17 @@
 hat(
-result3
+canDelete
 ).is

divide case and add suitable tests