/*
 * 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.adminapi.impl;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.teiid.adminapi.DataPolicy.PermissionType;
import org.teiid.adminapi.impl.DataPolicyMetadata.PermissionMetaData;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;
import static org.teiid.adminapi.impl.DataPolicyMetadata.getResourceOneLevelUp;

public class TestDataPolicyMetaData {

	@Test
	public void testAllowed() {
		DataPolicyMetadata policy = new DataPolicyMetadata();
		policy.setName("readOnly"); //$NON-NLS-1$
		policy.setDescription("Only has read only permissions"); //$NON-NLS-1$
		policy.setMappedRoleNames(Arrays.asList("jack", "susan")); //$NON-NLS-1$ //$NON-NLS-2$
		
		
		PermissionMetaData perm1 = new PermissionMetaData();
		perm1.setResourceName("catalog.schema.Table1"); //$NON-NLS-1$
		perm1.setAllowRead(true);
		
		PermissionMetaData perm2 = new PermissionMetaData();
		perm2.setResourceName("catalog.schema.Table2"); //$NON-NLS-1$
		perm2.setAllowRead(false);
		
		PermissionMetaData perm3 = new PermissionMetaData();
		perm3.setResourceName("catalog.schema.Table3"); //$NON-NLS-1$
		perm3.setAllowRead(true);
		
		PermissionMetaData perm4 = new PermissionMetaData();
		perm4.setResourceName("catalog.schema.Table4"); //$NON-NLS-1$
		perm4.setAllowRead(true);
		
		PermissionMetaData perm5 = new PermissionMetaData();
		perm5.setResourceName("catalog.schema.Table5.column1"); //$NON-NLS-1$
		perm5.setAllowRead(true);	
		
		policy.addPermission(perm1, perm2, perm3, perm4, perm5);
		
		// EXTENSION
		// ISSUE: DVCORE-1078
		// FIXED BY: Roman Pedchenko
		// DATE: 2014.03.26
		// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-2141
		// assertTrue(policy.allows("catalog.schema.Table1", PermissionType.READ)); //$NON-NLS-1$
		// assertFalse(policy.allows("catalog.schema.Table1", PermissionType.CREATE)); //$NON-NLS-1$
		assertTrue(policy.allows("catalog.schema.Table1".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		assertNull(policy.allows("catalog.schema.Table1".toLowerCase(), PermissionType.CREATE)); //$NON-NLS-1$
		// END EXTENSION
		
		// EXTENSION
		// ISSUE: DVCORE-1078
		// FIXED BY: Roman Pedchenko
		// DATE: 2014.03.26
		// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-2141
		// assertFalse(policy.allows("catalog.schema", PermissionType.READ)); //$NON-NLS-1$
		assertNull(policy.allows("catalog.schema", PermissionType.READ)); //$NON-NLS-1$
		// END EXTENSION
		
		// EXTENSION
		// ISSUE: DVCORE-1078
		// FIXED BY: Roman Pedchenko
		// DATE: 2014.03.26
		// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-2141
		// assertFalse(policy.allows("catalog.schema.Table2.column", PermissionType.READ)); //$NON-NLS-1$
		// assertFalse(policy.allows("catalog.schema.Table2", PermissionType.READ)); //$NON-NLS-1$
		assertNull(policy.allows("catalog.schema.Table2.column".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		assertFalse(policy.allows("catalog.schema.Table2".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		// END EXTENSION
		
		// EXTENSION
		// ISSUE: DVCORE-1078
		// FIXED BY: Roman Pedchenko
		// DATE: 2014.03.26
		// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-2141
		// assertTrue(policy.allows("catalog.schema.Table3.column", PermissionType.READ)); //$NON-NLS-1$
		// assertTrue(policy.allows("catalog.schema.Table3", PermissionType.READ)); //$NON-NLS-1$
		assertNull(policy.allows("catalog.schema.Table3.column".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		assertTrue(policy.allows("catalog.schema.Table3".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		// END EXTENSION
		
		// EXTENSION
		// ISSUE: DVCORE-1078
		// FIXED BY: Roman Pedchenko
		// DATE: 2014.03.26
		// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-2141
		// assertTrue(policy.allows("catalog.schema.Table4.column", PermissionType.READ)); //$NON-NLS-1$
		// assertTrue(policy.allows("catalog.schema.Table4", PermissionType.READ)); //$NON-NLS-1$
		// assertFalse(policy.allows("catalog.schema.Table4", PermissionType.DELETE)); //$NON-NLS-1$		
		assertTrue(policy.allows("catalog.schema.Table4".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		assertNull(policy.allows("catalog.schema.Table4".toLowerCase(), PermissionType.DELETE)); //$NON-NLS-1$
		// END EXTENSION
		
		// EXTENSION
		// ISSUE: DVCORE-1078
		// FIXED BY: Roman Pedchenko
		// DATE: 2014.03.26
		// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-2141
		// assertTrue(policy.allows("catalog.schema.Table5.column1", PermissionType.READ)); //$NON-NLS-1$
		// assertFalse(policy.allows("catalog.schema.Table5.column2", PermissionType.READ)); //$NON-NLS-1$
		// assertFalse(policy.allows("catalog.schema.Table5", PermissionType.READ)); //$NON-NLS-1$		
		assertTrue(policy.allows("catalog.schema.Table5.column1".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		assertNull(policy.allows("catalog.schema.Table5.column2".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		assertNull(policy.allows("catalog.schema.Table5".toLowerCase(), PermissionType.READ)); //$NON-NLS-1$
		// END EXTENSION
	}

	@Test
	public void testChoppingResourceForPermissionValidation() {
		assertEquals("datasource", getResourceOneLevelUp("datasource.table", null));
		assertEquals("*", getResourceOneLevelUp("datasource", null));
		assertEquals("", getResourceOneLevelUp("*", null));
		assertEquals("some-job", getResourceOneLevelUp("job:some-job", "job"));
		assertEquals("job:*", getResourceOneLevelUp("some-job", "job"));
		assertEquals("*", getResourceOneLevelUp("job:*", "job"));
		assertEquals("", getResourceOneLevelUp("*", "job"));
		assertEquals("some.job", getResourceOneLevelUp("job:some.job", "job"));
		assertEquals("job:some", getResourceOneLevelUp("some.job", "job"));
		assertEquals("some", getResourceOneLevelUp("job:some", "job"));
		assertEquals("job:*", getResourceOneLevelUp("some", "job"));
		assertEquals("procedure:schema", getResourceOneLevelUp("schema.name", "procedure"));
		assertEquals("schema", getResourceOneLevelUp("procedure:schema", "procedure"));
		assertEquals("procedure:*", getResourceOneLevelUp("schema", "procedure"));
		assertEquals("*", getResourceOneLevelUp("procedure:*", "procedure"));
		assertEquals("", getResourceOneLevelUp("*", "procedure"));
		assertEquals("type-name", getResourceOneLevelUp("some-other-type:type-name", "some-other-type"));
		assertEquals("some-other-type:*", getResourceOneLevelUp("type-name", "some-other-type"));
		assertEquals("*", getResourceOneLevelUp("some-other-type:*", "some-other-type"));
		assertEquals("", getResourceOneLevelUp("*", "some-other-type"));
	}

	@Test
	public void testHasPermissionThatDisallowsGrant () {
		DataPolicyMetadata policy = createPolicyWithResources("resource");
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant("resource", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant("resource.some", null));

		policy = createPolicyWithResources("*", "ds1", "ds.table");
		Assertions.assertTrue(policy.hasPermissionThatDisallowsGrant("*", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant("ds2", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant("ds2.a", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant( "ds1", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant("ds1.a", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant("ds.table", null));
		Assertions.assertTrue(policy.hasPermissionThatDisallowsGrant( "ds", null));

		policy = createPolicyWithResources("resource", "resource.someotherresource");
		Assertions.assertTrue(policy.hasPermissionThatDisallowsGrant("resource", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant( "resource.someresource", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant( "resource.someotherresource", null));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant( "resource.someresource.t", null));

		policy = createPolicyWithResources("*", "job:*");
		Assertions.assertTrue(policy.hasPermissionThatDisallowsGrant("*", null));
		Assertions.assertTrue(policy.hasPermissionThatDisallowsGrant("*", "job"));

		policy = createPolicyWithResources("*", "job:*", "job:some");
		Assertions.assertTrue(policy.hasPermissionThatDisallowsGrant("*", null));
		Assertions.assertTrue(policy.hasPermissionThatDisallowsGrant("job:*", "job"));
		Assertions.assertFalse(policy.hasPermissionThatDisallowsGrant("job:some", "job"));
	}

	private DataPolicyMetadata createPolicyWithResources(String ... resources) {
		DataPolicyMetadata policy = new DataPolicyMetadata();
		for (String resource : resources) {
			policy.addPermission(createPermissionMetadata(resource));
		}
		return policy;
	}

	private DataPolicyMetadata.PermissionMetaData createPermissionMetadata(String resource) {
		DataPolicyMetadata.PermissionMetaData permissionMetaData = new DataPolicyMetadata.PermissionMetaData();
		permissionMetaData.setResourceName(resource);
		permissionMetaData.setAllowCreate(true);
		return permissionMetaData;
	}

}
