use of org.teiid.query.sql.LanguageObject in project teiid by teiid.
the class TestAuthorizationValidationVisitor method helpTest.
private void helpTest(String sql, QueryMetadataInterface metadata, String[] expectedInaccesible, VDBMetaData vdb, DataPolicyMetadata... roles) throws QueryParserException, QueryResolverException, TeiidComponentException {
QueryParser parser = QueryParser.getQueryParser();
Command command = parser.parseCommand(sql);
QueryResolver.resolveCommand(command, metadata);
DataRolePolicyDecider dataRolePolicyDecider = createPolicyDecider(metadata, vdb, roles);
// $NON-NLS-1$
AuthorizationValidationVisitor visitor = new AuthorizationValidationVisitor(dataRolePolicyDecider, context);
ValidatorReport report = Validator.validate(command, metadata, visitor);
if (report.hasItems()) {
ValidatorFailure firstFailure = report.getItems().iterator().next();
// strings
Set<String> expected = new HashSet<String>(Arrays.asList(expectedInaccesible));
// elements
Set<String> actual = new HashSet<String>();
for (LanguageObject obj : firstFailure.getInvalidObjects()) {
if (obj instanceof ElementSymbol) {
actual.add(((ElementSymbol) obj).getName());
} else {
actual.add(obj.toString());
}
}
assertEquals(expected, actual);
} else if (expectedInaccesible.length > 0) {
// $NON-NLS-1$
fail("Expected inaccessible objects, but got none.");
}
}
use of org.teiid.query.sql.LanguageObject in project teiid by teiid.
the class TestOptimizer method helpGetCommand.
public static Command helpGetCommand(String sql, QueryMetadataInterface md, List<String> bindings) throws TeiidComponentException, TeiidProcessingException {
// $NON-NLS-1$
if (DEBUG)
System.out.println("\n####################################\n" + sql);
Command command = null;
if (bindings != null && !bindings.isEmpty()) {
command = TestResolver.helpResolveWithBindings(sql, md, bindings);
} else {
command = QueryParser.getQueryParser().parseCommand(sql);
QueryResolver.resolveCommand(command, md);
}
ValidatorReport repo = Validator.validate(command, md);
Collection<LanguageObject> failures = new ArrayList<LanguageObject>();
repo.collectInvalidObjects(failures);
if (failures.size() > 0) {
// $NON-NLS-1$
fail("Exception during validation (" + repo);
}
// rewrite
command = QueryRewriter.rewrite(command, md, new CommandContext());
return command;
}
use of org.teiid.query.sql.LanguageObject in project teiid by teiid.
the class TestExpressionMappingVisitor method testSelectAlias1.
@Test
public void testSelectAlias1() {
// $NON-NLS-1$
ElementSymbol x = new ElementSymbol("y.x");
// $NON-NLS-1$
ElementSymbol y = new ElementSymbol("z.x");
HashMap<ElementSymbol, ElementSymbol> map = new HashMap<ElementSymbol, ElementSymbol>();
map.put(x, y);
LanguageObject toMap = new Select(Arrays.asList(x));
ExpressionMappingVisitor.mapExpressions(toMap, map);
// $NON-NLS-1$ //$NON-NLS-2$
assertEquals("Did not get expected mapped expression", " z.x", toMap.toString());
}
use of org.teiid.query.sql.LanguageObject in project teiid by teiid.
the class TestExpressionMappingVisitor method testSelectAlias.
@Test
public void testSelectAlias() {
// $NON-NLS-1$
ElementSymbol x = new ElementSymbol("y.x");
// $NON-NLS-1$
ElementSymbol y = new ElementSymbol("z.X");
HashMap<Expression, Expression> map = new HashMap<Expression, Expression>();
map.put(x, y);
LanguageObject toMap = new Select(Arrays.asList(x));
ExpressionMappingVisitor.mapExpressions(toMap, map);
// $NON-NLS-1$ //$NON-NLS-2$
assertEquals("Did not get expected mapped expression", " z.X AS x", toMap.toString());
}
use of org.teiid.query.sql.LanguageObject in project teiid by teiid.
the class DefaultAuthorizationValidator method validate.
@Override
public boolean validate(String[] originalSql, Command command, QueryMetadataInterface metadata, CommandContext commandContext, CommandType commandType) throws QueryValidatorException, TeiidComponentException {
boolean modified = false;
if (policyDecider != null && policyDecider.validateCommand(commandContext)) {
if (ignoreUnathorizedInAsterisk(command, commandContext)) {
Query query = (Query) command;
HashMap<String, LanguageObject> map = null;
for (Expression ex : query.getSelect().getSymbols()) {
if (ex instanceof MultipleElementSymbol) {
MultipleElementSymbol mes = (MultipleElementSymbol) ex;
if (map == null) {
map = new HashMap<String, LanguageObject>();
}
for (Iterator<ElementSymbol> iter = mes.getElementSymbols().iterator(); iter.hasNext(); ) {
ElementSymbol es = iter.next();
Object metadataObject = es.getMetadataID();
if (metadataObject instanceof MultiSourceElement || metadataObject instanceof TempMetadataID) {
continue;
}
map.clear();
AuthorizationValidationVisitor.addToNameMap(metadataObject, es, map, commandContext.getMetadata());
Set<String> results = this.policyDecider.getInaccessibleResources(PermissionType.READ, map.keySet(), Context.QUERY, commandContext);
if (!results.isEmpty()) {
// remove from the select
iter.remove();
modified = true;
}
}
}
}
if (query.getProjectedSymbols().isEmpty()) {
throw new QueryValidatorException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31151));
}
}
AuthorizationValidationVisitor visitor = new AuthorizationValidationVisitor(this.policyDecider, commandContext);
Request.validateWithVisitor(visitor, metadata, command);
}
return modified;
}
Aggregations