/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */

package org.teiid.resource.adapter.ldap;

import org.junit.jupiter.api.Test;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.spi.InitialContextFactory;
import javax.resource.ResourceException;
import java.util.Hashtable;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

public class TestLDAPConnection {

	private static final InitialLdapContext context = mock(InitialLdapContext.class);

	private static LDAPConnectionImpl connection;

	@Test
	public void testInitialization() throws Exception {
		assertNotNull(retrieveConnection());
	}

	@Test
	public void testIsAlive_WhenServerIsAlive() throws Exception {
		connection = new LDAPConnectionImpl(retrieveFactory(), context);
		NamingEnumeration<SearchResult> mockResult = mock(NamingEnumeration.class);
		when(context.search(anyString(), anyString(), any(SearchControls.class))).thenReturn(mockResult);
		when(mockResult.hasMore()).thenReturn(true);
		when(mockResult.next()).thenReturn(mock(SearchResult.class));

		boolean isConnected = connection.isAlive();

		assertTrue(isConnected);

		connection.close();
	}

	@Test
	void testIsAlive_WhenServerIsNotAlive() throws Exception {
		LDAPConnectionImpl connection = new LDAPConnectionImpl(retrieveFactory(), context);
		when(context.search(anyString(), anyString(), any(SearchControls.class))).thenThrow(NamingException.class);

		assertFalse(connection.isAlive());
	}

	public static class FakeFactory implements InitialContextFactory {
		@Override
		public Context getInitialContext(Hashtable<?, ?> environment) {
			return mock(DirContext.class);
		}
	}

	private static LDAPConnectionImpl retrieveConnection() throws ResourceException {
		LDAPManagedConnectionFactory config = retrieveFactory();
		return new LDAPConnectionImpl(config, null);
	}

	private static LDAPManagedConnectionFactory retrieveFactory() {
		LDAPManagedConnectionFactory config = new LDAPManagedConnectionFactory();
		config.setLdapUrl("ldap://foo");
		config.setLdapAdminUserDN("admin");
		config.setLdapAdminUserPassword("password");
		config.setLdapContextFactory(FakeFactory.class.getName());
		return config;
	}
}
