// Path: src/main/java/com/plexobject/rbac/security/PermissionManager.java
// public interface PermissionManager {
// 
//     void check(PermissionRequest request) throws SecurityException;
// 
// }
// 
// Path: src/main/java/com/plexobject/rbac/security/PermissionRequest.java
// public class PermissionRequest {
//     private final String domain;
//     private final String subjectName;
//     private final String operation;
//     private final String target;
//     private final Map<String, Object> subjectContext = new TreeMap<String, Object>();
// 
//     public PermissionRequest(final String domain, final String subjectName,
//             final String operation, final String target,
//             final Map<String, Object> subjectContext) {
//         this.domain = domain;
//         this.subjectName = subjectName;
//         this.operation = operation;
//         this.target = target;
//         if (subjectContext != null) {
//             this.subjectContext.putAll(subjectContext);
//         }
//     }
// 
//     public String getDomain() {
//         return domain;
//     }
// 
//     /**
//      * @return the subjectName
//      */
//     public String getSubjectName() {
//         return subjectName;
//     }
// 
//     /**
//      * @return the operation
//      */
//     public String getOperation() {
//         return operation;
//     }
// 
//     /**
//      * @return the target
//      */
//     public String getTarget() {
//         return target;
//     }
// 
//     /**
//      * @return the subjectContext
//      */
//     public Map<String, Object> getSubjectContext() {
//         return subjectContext;
//     }
// 
//     /**
//      * @see java.lang.Object#equals(Object)
//      */
//     @Override
//     public boolean equals(Object object) {
//         if (!(object instanceof PermissionRequest)) {
//             return false;
//         }
//         PermissionRequest rhs = (PermissionRequest) object;
//         return new EqualsBuilder().append(this.domain, domain).append(
//                 this.subjectName, rhs.subjectName).append(this.operation,
//                 rhs.operation).append(this.target, rhs.target).append(
//                 this.subjectContext, rhs.subjectContext).isEquals();
//     }
// 
//     /**
//      * @see java.lang.Object#hashCode()
//      */
//     @Override
//     public int hashCode() {
//         return new HashCodeBuilder(786529047, 1924536713).append(domain)
//                 .append(subjectName).append(operation).append(target)
//                 .toHashCode();
//     }
// 
//     /**
//      * @see java.lang.Object#toString()
//      */
//     @Override
//     public String toString() {
//         return new ToStringBuilder(this).append(domain).append(subjectName)
//                 .append(operation).append(target).append(subjectContext)
//                 .toString();
//     }
// 
// }
// 
// Path: src/main/java/com/plexobject/rbac/service/AuthorizationService.java
// public interface AuthorizationService {
//     Response authorize(final UriInfo ui, final String domain,
//             final String operation, final String target);
// }
// 
// Path: src/main/java/com/plexobject/rbac/utils/CurrentRequest.java
// public class CurrentRequest {
//     private static final ThreadLocal<String> domain = new ThreadLocal<String>();
//     private static final ThreadLocal<String> subjectName = new ThreadLocal<String>();
//     private static final ThreadLocal<String> ipAddress = new ThreadLocal<String>();
// 
//     public static void startRequest(final String domain, final String subjectName,
//             final String ipAddress) {
//         CurrentRequest.domain.set(domain);
//         CurrentRequest.subjectName.set(subjectName);
//         CurrentRequest.ipAddress.set(ipAddress);
//     }
// 
//     public static void setSubjectName(String subjectName) {
//         CurrentRequest.subjectName.set(subjectName);
//     }
// 
//     public static void endRequest() {
//         CurrentRequest.domain.set(null);
//         CurrentRequest.subjectName.set(null);
//         CurrentRequest.ipAddress.set(null);
//     }
// 
//     public static String getDomain() {
//         return domain.get();
//     }
// 
//     public static String getSubjectName() {
//         return subjectName.get();
//     }
// 
//     public static String getIPAddress() {
//         return ipAddress.get();
//     }
// }


import static org.junit.Assert.assertEquals;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.apache.log4j.Logger;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.plexobject.rbac.security.PermissionManager;
import com.plexobject.rbac.security.PermissionRequest;
import com.plexobject.rbac.service.AuthorizationService;
import com.plexobject.rbac.utils.CurrentRequest;
    @Before
    public void setUp() throws Exception {
        CurrentRequest.startRequest(APP_NAME, USER_NAME, "127.0.0.1");

        map = new TestMultivaluedMap();
        service = new AuthorizationServiceImpl();
        ((AuthorizationServiceImpl) service)
                .setPermissionManager(((AuthorizationServiceImpl) service)
                        .getPermissionManager());
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public final void testAuthorizeFailed() throws Exception {
        ((AuthorizationServiceImpl) service).afterPropertiesSet();
        String domain = "default";
        String operation = "op";
        String target = "xx";
        UriInfo ui = new TestUriInfo();
        Response response = service.authorize(ui, domain, operation, target);

        assertEquals(401, response.getStatus());
        assertEquals("denied", response.getEntity());
    }

    @Test
    public final void testAuthorize() throws Exception {
// Path: src/main/java/com/plexobject/rbac/security/PermissionManager.java
// public interface PermissionManager {
// 
//     void check(PermissionRequest request) throws SecurityException;
// 
// }
// 
// Path: src/main/java/com/plexobject/rbac/security/PermissionRequest.java
// public class PermissionRequest {
//     private final String domain;
//     private final String subjectName;
//     private final String operation;
//     private final String target;
//     private final Map<String, Object> subjectContext = new TreeMap<String, Object>();
// 
//     public PermissionRequest(final String domain, final String subjectName,
//             final String operation, final String target,
//             final Map<String, Object> subjectContext) {
//         this.domain = domain;
//         this.subjectName = subjectName;
//         this.operation = operation;
//         this.target = target;
//         if (subjectContext != null) {
//             this.subjectContext.putAll(subjectContext);
//         }
//     }
// 
//     public String getDomain() {
//         return domain;
//     }
// 
//     /**
//      * @return the subjectName
//      */
//     public String getSubjectName() {
//         return subjectName;
//     }
// 
//     /**
//      * @return the operation
//      */
//     public String getOperation() {
//         return operation;
//     }
// 
//     /**
//      * @return the target
//      */
//     public String getTarget() {
//         return target;
//     }
// 
//     /**
//      * @return the subjectContext
//      */
//     public Map<String, Object> getSubjectContext() {
//         return subjectContext;
//     }
// 
//     /**
//      * @see java.lang.Object#equals(Object)
//      */
//     @Override
//     public boolean equals(Object object) {
//         if (!(object instanceof PermissionRequest)) {
//             return false;
//         }
//         PermissionRequest rhs = (PermissionRequest) object;
//         return new EqualsBuilder().append(this.domain, domain).append(
//                 this.subjectName, rhs.subjectName).append(this.operation,
//                 rhs.operation).append(this.target, rhs.target).append(
//                 this.subjectContext, rhs.subjectContext).isEquals();
//     }
// 
//     /**
//      * @see java.lang.Object#hashCode()
//      */
//     @Override
//     public int hashCode() {
//         return new HashCodeBuilder(786529047, 1924536713).append(domain)
//                 .append(subjectName).append(operation).append(target)
//                 .toHashCode();
//     }
// 
//     /**
//      * @see java.lang.Object#toString()
//      */
//     @Override
//     public String toString() {
//         return new ToStringBuilder(this).append(domain).append(subjectName)
//                 .append(operation).append(target).append(subjectContext)
//                 .toString();
//     }
// 
// }
// 
// Path: src/main/java/com/plexobject/rbac/service/AuthorizationService.java
// public interface AuthorizationService {
//     Response authorize(final UriInfo ui, final String domain,
//             final String operation, final String target);
// }
// 
// Path: src/main/java/com/plexobject/rbac/utils/CurrentRequest.java
// public class CurrentRequest {
//     private static final ThreadLocal<String> domain = new ThreadLocal<String>();
//     private static final ThreadLocal<String> subjectName = new ThreadLocal<String>();
//     private static final ThreadLocal<String> ipAddress = new ThreadLocal<String>();
// 
//     public static void startRequest(final String domain, final String subjectName,
//             final String ipAddress) {
//         CurrentRequest.domain.set(domain);
//         CurrentRequest.subjectName.set(subjectName);
//         CurrentRequest.ipAddress.set(ipAddress);
//     }
// 
//     public static void setSubjectName(String subjectName) {
//         CurrentRequest.subjectName.set(subjectName);
//     }
// 
//     public static void endRequest() {
//         CurrentRequest.domain.set(null);
//         CurrentRequest.subjectName.set(null);
//         CurrentRequest.ipAddress.set(null);
//     }
// 
//     public static String getDomain() {
//         return domain.get();
//     }
// 
//     public static String getSubjectName() {
//         return subjectName.get();
//     }
// 
//     public static String getIPAddress() {
//         return ipAddress.get();
//     }
// }


// Path: src/test/java/com/plexobject/rbac/service/impl/AuthorizationServiceImplTest.java
import static org.junit.Assert.assertEquals;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.apache.log4j.Logger;
import org.easymock.EasyMock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.plexobject.rbac.security.PermissionManager;
import com.plexobject.rbac.security.PermissionRequest;
import com.plexobject.rbac.service.AuthorizationService;
import com.plexobject.rbac.utils.CurrentRequest;

    @Before
    public void setUp() throws Exception {
        CurrentRequest.startRequest(APP_NAME, USER_NAME, "127.0.0.1");

        map = new TestMultivaluedMap();
        service = new AuthorizationServiceImpl();
        ((AuthorizationServiceImpl) service)
                .setPermissionManager(((AuthorizationServiceImpl) service)
                        .getPermissionManager());
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public final void testAuthorizeFailed() throws Exception {
        ((AuthorizationServiceImpl) service).afterPropertiesSet();
        String domain = "default";
        String operation = "op";
        String target = "xx";
        UriInfo ui = new TestUriInfo();
        Response response = service.authorize(ui, domain, operation, target);

        assertEquals(401, response.getStatus());
        assertEquals("denied", response.getEntity());
    }

    @Test
    public final void testAuthorize() throws Exception {
        PermissionManager mgr = EasyMock.createMock(PermissionManager.class);