// Path: src/test/java/com/asolutions/MockTestCase.java
// public class MockTestCase {
// 
// 	protected Mockery context = new JUnit4Mockery();
// 
// 	@Before
// 	public void setupMockery() {
// 		context.setImposteriser(ClassImposteriser.INSTANCE);
// 	}
// 	
// 	@After
// 	public void mockeryAssertIsSatisfied(){
// 		context.assertIsSatisfied();
// 	}
// 
// 	protected void checking(Expectations expectations) {
// 		context.checking(expectations);
// 	}
// 
// }
// 
// Path: src/main/java/com/asolutions/scmsshd/authenticators/LDAPUsernameResolver.java
// public class LDAPUsernameResolver {
// 	LDAPBindingProvider provider;
// 	private String userBase;
// 	private String matchingElement;
// 	public LDAPUsernameResolver(LDAPBindingProvider provider, String userBase) {
// 		this(provider, userBase, "cn=");
// 	}
// 	public LDAPUsernameResolver(LDAPBindingProvider provider, String userBase, String matchingElement) {
// 		this.provider = provider;
// 		this.userBase = userBase;
// 		this.matchingElement = matchingElement;
// 	}
// 	
// 	public String resolveUserName(String username) throws NamingException{
// 		InitialDirContext InitialDirectoryContext = provider.getBinding();
// 		SearchControls searchCtls = new SearchControls();
// 		//Specify the search scope
// 		searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 
// 		//specify the LDAP search filter
// 		String searchFilter = "("+matchingElement+username+")";
// 
// 		//initialize counter to total the results
// 
// 		// Search for objects using the filter
// 		NamingEnumeration<SearchResult> answer = InitialDirectoryContext.search(userBase, searchFilter, searchCtls);
// 		SearchResult next = answer.next();
// 		return next.getNameInNamespace();
// 	}
// }
// 
// Path: src/main/java/com/asolutions/scmsshd/authorizors/AuthorizationLevel.java
// public enum AuthorizationLevel {
// 	AUTH_LEVEL_READ_ONLY, 
// 	AUTH_LEVEL_READ_WRITE;
// }


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
import com.asolutions.MockTestCase;
import com.asolutions.scmsshd.authenticators.LDAPUsernameResolver;
import com.asolutions.scmsshd.authorizors.AuthorizationLevel;
		final NamingEnumeration<?> mockEnum = context.mock(NamingEnumeration.class);

		checking(new Expectations(){{
			one(ldapUsernameResolver).resolveUserName(usernameToCheck);
			will(returnValue(userToCheckDN));
			
			one(mockBinding).getAttributes("cn=proj-2-git,cn=Groups,DC=ldapserver,DC=lan");
			will(returnValue(mockAttrs));
			
			one(mockAttrs).get("member");
			will(returnValue(mockAttribute));
			
			one(mockAttribute).getAll();
			will(returnValue(mockEnum));
			
			one(mockEnum).hasMoreElements();
			will(returnValue(true));
			
			one(mockEnum).nextElement();
			will(returnValue(userToCheckDN));
			
			one(ldapBinding).getBinding();
			will(returnValue(mockBinding));
		}});
		
		LDAPProjectAuthorizer auth = new LDAPProjectAuthorizer(groupBaseDN,
															   "git",
															   AuthorizationLevel.AUTH_LEVEL_READ_ONLY,
															   ldapBinding,
															   ldapUsernameResolver);
// Path: src/test/java/com/asolutions/MockTestCase.java
// public class MockTestCase {
// 
// 	protected Mockery context = new JUnit4Mockery();
// 
// 	@Before
// 	public void setupMockery() {
// 		context.setImposteriser(ClassImposteriser.INSTANCE);
// 	}
// 	
// 	@After
// 	public void mockeryAssertIsSatisfied(){
// 		context.assertIsSatisfied();
// 	}
// 
// 	protected void checking(Expectations expectations) {
// 		context.checking(expectations);
// 	}
// 
// }
// 
// Path: src/main/java/com/asolutions/scmsshd/authenticators/LDAPUsernameResolver.java
// public class LDAPUsernameResolver {
// 	LDAPBindingProvider provider;
// 	private String userBase;
// 	private String matchingElement;
// 	public LDAPUsernameResolver(LDAPBindingProvider provider, String userBase) {
// 		this(provider, userBase, "cn=");
// 	}
// 	public LDAPUsernameResolver(LDAPBindingProvider provider, String userBase, String matchingElement) {
// 		this.provider = provider;
// 		this.userBase = userBase;
// 		this.matchingElement = matchingElement;
// 	}
// 	
// 	public String resolveUserName(String username) throws NamingException{
// 		InitialDirContext InitialDirectoryContext = provider.getBinding();
// 		SearchControls searchCtls = new SearchControls();
// 		//Specify the search scope
// 		searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 
// 		//specify the LDAP search filter
// 		String searchFilter = "("+matchingElement+username+")";
// 
// 		//initialize counter to total the results
// 
// 		// Search for objects using the filter
// 		NamingEnumeration<SearchResult> answer = InitialDirectoryContext.search(userBase, searchFilter, searchCtls);
// 		SearchResult next = answer.next();
// 		return next.getNameInNamespace();
// 	}
// }
// 
// Path: src/main/java/com/asolutions/scmsshd/authorizors/AuthorizationLevel.java
// public enum AuthorizationLevel {
// 	AUTH_LEVEL_READ_ONLY, 
// 	AUTH_LEVEL_READ_WRITE;
// }


// Path: src/test/java/com/asolutions/scmsshd/ldap/LDAPProjectAuthorizerTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.InitialDirContext;
import org.jmock.Expectations;
import org.junit.Before;
import org.junit.Test;
import com.asolutions.MockTestCase;
import com.asolutions.scmsshd.authenticators.LDAPUsernameResolver;
import com.asolutions.scmsshd.authorizors.AuthorizationLevel;

		final NamingEnumeration<?> mockEnum = context.mock(NamingEnumeration.class);

		checking(new Expectations(){{
			one(ldapUsernameResolver).resolveUserName(usernameToCheck);
			will(returnValue(userToCheckDN));
			
			one(mockBinding).getAttributes("cn=proj-2-git,cn=Groups,DC=ldapserver,DC=lan");
			will(returnValue(mockAttrs));
			
			one(mockAttrs).get("member");
			will(returnValue(mockAttribute));
			
			one(mockAttribute).getAll();
			will(returnValue(mockEnum));
			
			one(mockEnum).hasMoreElements();
			will(returnValue(true));
			
			one(mockEnum).nextElement();
			will(returnValue(userToCheckDN));
			
			one(ldapBinding).getBinding();
			will(returnValue(mockBinding));
		}});
		
		LDAPProjectAuthorizer auth = new LDAPProjectAuthorizer(groupBaseDN,
															   "git",
															   AuthorizationLevel.AUTH_LEVEL_READ_ONLY,
															   ldapBinding,
															   ldapUsernameResolver);
		assertEquals(AuthorizationLevel.AUTH_LEVEL_READ_ONLY, auth.userIsAuthorizedForProject(usernameToCheck, "proj-2"));