package eu.dnetlib.enabling.aas.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import eu.dnetlib.enabling.aas.service.A2Constants;
import eu.dnetlib.enabling.aas.utils.QueryProvider;


public class QueryProviderTest {

	@Test
	public void testFindSecurityProfileForResourceQuery() {
		String res=QueryProvider.findSecurityProfileForResourceQuery("RESOURCE_ID");
		System.err.println(res);
		assertNotNull(res);
	}

	@Test
	public void testFindSecurityContextForResourceQuery() {
		String res=QueryProvider.findSecurityContextForResourceQuery("RESOURCE_ID");
		System.err.println(res);
		assertNotNull(res);
	}
	
	@Test
	public void testFindAllSecurityContextProfIds() {
		String res=QueryProvider.findAllSecurityContextProfIds();
		System.err.println(res);
		assertNotNull(res);
	}

	@Test
	public void testBuildCollectionName() {
		String profileKind = "someProfKind";
		String profileType = "someProfType";
		String res=QueryProvider.buildCollectionName(profileKind, profileType);
		assertNotNull(res);
		assertEquals(res,"db/DRIVER/"+profileKind+'/'+profileType);
		assertNull(QueryProvider.buildCollectionName(null, null));
	}
	
	@SuppressWarnings("deprecation")
	@Test
	public void testBuildQuery() {
		String profileKind = "someProfKind";
		String profileType = "someProfType";
		
		String path = "somePath";
		String res=QueryProvider.buildQuery(profileKind, profileType, null, null, path);
		assertNotNull(res);
		assertEquals(res,"for $el in fn:collection(\"DRIVER/"+profileKind+'/'+profileType + "\") return $el/" + path);
		String resourceId = "someResourceId";
		String conditionField = "RESOURCE_PROFILE/BODY/CONFIGURATION/resourceId";
		String conditionValue = resourceId;
		String res2=QueryProvider.buildQuery(profileKind, profileType, conditionField, conditionValue, path);
		assertNotNull(res2);
		assertEquals(res2,"for $el in fn:collection(\"DRIVER/"+profileKind+'/'+profileType + "\") where $el/"+conditionField+" = '"+conditionValue+"' return $el/" + path);
		
//		test userProfileQuery
		profileKind = "UserDSResources";
		profileType = "UserDSResourceType";
		conditionField = "RESOURCE_PROFILE/BODY/CONFIGURATION/PERSONAL_INFO/EMAIL/@value";
		conditionValue = "katerina.iatrop@gmail.com";
		path = A2Constants.IS_PROFILE_ID_XPATH;
		String res3=QueryProvider.buildQuery(profileKind, profileType, conditionField, conditionValue, path);
		assertNotNull(res3);
		System.out.println(res3);
	}
	
	@Test
	public void testValidateConditionField() {
		String value = "some/path";
		assertTrue(QueryProvider.validateConditionField(value));
		value = "someValue\nAND\tsomeOtherValue";
		assertFalse(QueryProvider.validateConditionField(value));
	}
	
	@Test
	public void testValidateConditionValue() {
		String value = "someValue";
		assertTrue(QueryProvider.validateConditionValue(value));
		value = "value'";
		assertFalse(QueryProvider.validateConditionValue(value));
		
	}
	
	@Test
	public void testBuildQuery_new() {
//		test userProfileQuery
		String profileKind = "UserDSResources";
		String profileType = "UserDSResourceType";
		String conditionField = "RESOURCE_PROFILE/BODY/CONFIGURATION/PERSONAL_INFO/EMAIL/@value";
		String conditionValue = "antleb@di.uoa.gr";
		String path = A2Constants.IS_PROFILE_ID_XPATH;
		String res3=QueryProvider.buildQuery_new(profileKind, profileType, conditionField, conditionValue, path);
		assertNotNull(res3);
		System.out.println(res3);
		
//		test with no condition value
		profileKind = "UserDSResources";
		profileType = "UserDSResourceType";
		conditionField = "RESOURCE_PROFILE/BODY/CONFIGURATION/PERSONAL_INFO/EMAIL/@value";
		conditionValue = null;
		path = A2Constants.IS_PROFILE_ID_XPATH;
		res3=QueryProvider.buildQuery_new(profileKind, profileType, conditionField, conditionValue, path);
		assertNotNull(res3);
		System.out.println(res3);
		
	}
	
	@Test
	public void testFindSecurityProfileForResourceQuery_new() {
		String res=QueryProvider.findSecurityProfileForResourceQuery_new("RESOURCE_ID");
		assertNotNull(res);
		System.out.println("New SecProf4ResourceQuery:");
		System.out.println(res);
	}
	
	@Test
	public void testFindSecurityContextForResourceQuery_new() {
		String res=QueryProvider.findSecurityContextForResourceQuery_new("RESOURCE_ID");
		assertNotNull(res);
		System.out.println("New SecCtx4ResourceQuery:");
		System.out.println(res);
	}
}
