/*
 * 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$
 *  
 */
// https://issues.redhat.com/browse/TEIID-5480
// https://issues.redhat.com/browse/TEIID-3663
package org.teiid.dqp.internal.process;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Map;

import com.datavirtuality.dv.core.services.DVServices;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.teiid.adminapi.DataPolicy;
import org.teiid.adminapi.impl.DataPolicyMetadata;
import org.teiid.adminapi.impl.SessionMetadata;
import org.teiid.adminapi.impl.VDBMetaData;
import org.teiid.core.util.UnitTestUtil;
import org.teiid.query.processor.FakeUsersHandler;
import org.teiid.query.util.DVUsersHandlerIntf;
import org.wildfly.security.auth.realm.SimpleMapBackedSecurityRealm;
import org.wildfly.security.auth.server.SecurityDomain;
import org.wildfly.security.auth.server.SecurityIdentity;

@SuppressWarnings("nls")
public class TestDQPWorkContext {

	@BeforeEach
	void setUp() {
		DVServices.register(DVUsersHandlerIntf.class, new FakeUsersHandler());
	}

	public static DQPWorkContext example() {
		DQPWorkContext message = new DQPWorkContext();
		message.getSession().setVDBName("vdbName"); //$NON-NLS-1$
		message.getSession().setVDBVersion(1); 
		message.getSession().setApplicationName("querybuilder"); //$NON-NLS-1$
		message.getSession().setSessionId(String.valueOf(5));
		message.getSession().setUserName("userName"); //$NON-NLS-1$
		return message;
	}

	@Test public void testSerialize() throws Exception {
		DQPWorkContext copy = UnitTestUtil.helpSerialize(example());

		assertEquals("5", copy.getSessionId()); //$NON-NLS-1$
		assertEquals("userName", copy.getUserName()); //$NON-NLS-1$
		assertEquals("vdbName", copy.getVdbName()); //$NON-NLS-1$
		assertEquals(1, copy.getVdbVersion());
		assertEquals("querybuilder", copy.getAppName()); //$NON-NLS-1$
	}
	
	@Test public void testClearPolicies() {
		DQPWorkContext message = new DQPWorkContext();
		message.setSession(Mockito.mock(SessionMetadata.class));
		Mockito.when(message.getSession().getVdb()).thenReturn(new VDBMetaData());
		Map<String, DataPolicy> map = message.getAllowedDataPolicies();
		map.put("role", Mockito.mock(DataPolicy.class)); //$NON-NLS-1$
		assertFalse(map.isEmpty());
		
		message.setSession(Mockito.mock(SessionMetadata.class));
		Mockito.when(message.getSession().getVdb()).thenReturn(new VDBMetaData());
		map = message.getAllowedDataPolicies();
		assertTrue(map.isEmpty());
	}
	
	@Test public void testAnyAuthenticated() {
		DQPWorkContext message = new DQPWorkContext();
		message.setSession(Mockito.mock(SessionMetadata.class));
		VDBMetaData vdb = new VDBMetaData();
		DataPolicyMetadata dpm = new DataPolicyMetadata();
		dpm.setAnyAuthenticated(true);
		vdb.addDataPolicy(dpm);
		Mockito.when(message.getSession().getVdb()).thenReturn(vdb);
		
		Map<String, DataPolicy> map = message.getAllowedDataPolicies();
		assertEquals(1, map.size());
	}
	
	@Test public void testRestoreSecurityContext() {
		SecurityDomain domain = SecurityDomain.builder()
				.addRealm("Simple", new SimpleMapBackedSecurityRealm()).build()
				.setDefaultRealmName("Simple")
				.build();
		final String previousSC = "previousSC";
		SecurityIdentity previousIdentity = domain.createAdHocIdentity(previousSC);

		final String currentSC = "currentSC";
		DQPWorkContext message = new DQPWorkContext() {
		    @Override
            public SecurityIdentity getSubject() {
		    	return domain.createAdHocIdentity(currentSC);
		    }			
		};
		message.setSession(Mockito.mock(SessionMetadata.class));

		Runnable r = new Runnable() {
			@Override
			public void run() {
				assertEquals(currentSC, domain.getCurrentSecurityIdentity().getPrincipal().getName());
			}
		};
		
		message.runInContext(r);

		assertEquals(previousSC, previousIdentity.getPrincipal().getName());
	}

	@Disabled
	@Test public void testVersion() {
		assertEquals(4, DQPWorkContext.Version.getVersion("11.0").getClientSerializationVersion());
		assertEquals(5, DQPWorkContext.Version.getVersion("11.2").getClientSerializationVersion());
	}
	
}
