|
20 | 20 | import org.apache.accumulo.core.security.ColumnVisibility;
|
21 | 21 | import org.apache.accumulo.core.security.VisibilityEvaluator;
|
22 | 22 | import org.apache.accumulo.core.security.VisibilityParseException;
|
| 23 | +import org.apache.commons.collections4.Transformer; |
23 | 24 | import org.apache.commons.collections4.iterators.TransformIterator;
|
24 | 25 | import org.junit.Assert;
|
25 | 26 | import org.junit.Before;
|
|
57 | 58 | import datawave.webservice.query.result.edge.EdgeBase;
|
58 | 59 | import datawave.webservice.result.BaseQueryResponse;
|
59 | 60 | import datawave.webservice.result.GenericResponse;
|
| 61 | +import datawave.webservice.result.QueryValidationResponse; |
60 | 62 |
|
61 | 63 | public class CompositeQueryLogicTest {
|
62 | 64 |
|
@@ -88,6 +90,8 @@ public class CompositeQueryLogicTest {
|
88 | 90 | private Value valueFailure = new Value(keyFailure.getRowData().getBackingArray());
|
89 | 91 | private Value valueSpecial = new Value(keySpecial.getRowData().getBackingArray());
|
90 | 92 |
|
| 93 | + private static final String VALIDATION_MESSAGE = "Light is green, the trap is clean"; |
| 94 | + |
91 | 95 | public static class TestQueryConfiguration extends GenericQueryConfiguration {
|
92 | 96 |
|
93 | 97 | }
|
@@ -454,6 +458,33 @@ public Set<String> getExampleQueries() {
|
454 | 458 | return Collections.emptySet();
|
455 | 459 | }
|
456 | 460 |
|
| 461 | + @Override |
| 462 | + public Object validateQuery(AccumuloClient client, Query query, Set<Authorizations> auths) throws Exception { |
| 463 | + // return something valid |
| 464 | + return VALIDATION_MESSAGE; |
| 465 | + } |
| 466 | + |
| 467 | + @Override |
| 468 | + public Transformer<Object,QueryValidationResponse> getQueryValidationResponseTransformer() { |
| 469 | + return new TestQueryValidationResultTransformer(); |
| 470 | + } |
| 471 | + |
| 472 | + } |
| 473 | + |
| 474 | + public static class TestQueryValidationResultTransformer implements Transformer<Object,QueryValidationResponse> { |
| 475 | + |
| 476 | + @Override |
| 477 | + public QueryValidationResponse transform(Object input) { |
| 478 | + String validation = String.valueOf(input); |
| 479 | + QueryValidationResponse.Result result = new QueryValidationResponse.Result(); |
| 480 | + result.setMessages(Collections.singletonList(validation)); |
| 481 | + |
| 482 | + QueryValidationResponse response = new QueryValidationResponse(); |
| 483 | + response.setHasResults(true); |
| 484 | + response.setResults(Collections.singletonList(result)); |
| 485 | + |
| 486 | + return response; |
| 487 | + } |
457 | 488 | }
|
458 | 489 |
|
459 | 490 | public static class TestFilteredQueryLogic extends FilteredQueryLogic {
|
@@ -1767,4 +1798,50 @@ public void testAuthorizationsUpdate() throws Exception {
|
1767 | 1798 | c.close();
|
1768 | 1799 | }
|
1769 | 1800 |
|
| 1801 | + @Test(expected = UnsupportedOperationException.class) |
| 1802 | + public void testValidationFails() throws Exception { |
| 1803 | + Map<String,QueryLogic<?>> logics = new HashMap<>(); |
| 1804 | + TestQueryLogic logic1 = new TestQueryLogic(); |
| 1805 | + TestQueryLogic2 logic2 = new TestQueryLogic2(); |
| 1806 | + logics.put("TestQueryLogic", logic1); |
| 1807 | + logics.put("TestQueryLogic2", logic2); |
| 1808 | + |
| 1809 | + QueryImpl settings = new QueryImpl(); |
| 1810 | + settings.setPagesize(100); |
| 1811 | + settings.setQueryAuthorizations(auths.toString()); |
| 1812 | + settings.setQuery("FOO == 'BAR'"); |
| 1813 | + settings.setParameters(new HashSet<>()); |
| 1814 | + settings.setId(UUID.randomUUID()); |
| 1815 | + |
| 1816 | + CompositeQueryLogic c = new CompositeQueryLogic(); |
| 1817 | + c.setQueryLogics(logics); |
| 1818 | + c.setCurrentUser(principal); |
| 1819 | + |
| 1820 | + c.validateQuery(null, settings, Collections.singleton(auths)); |
| 1821 | + } |
| 1822 | + |
| 1823 | + @Test |
| 1824 | + public void testValidationHappyPath() throws Exception { |
| 1825 | + Map<String,QueryLogic<?>> logics = new HashMap<>(); |
| 1826 | + TestQueryLogic logic1 = new TestQueryLogic(); |
| 1827 | + DifferentTestQueryLogic logic2 = new DifferentTestQueryLogic(); |
| 1828 | + logics.put("TestQueryLogic", logic1); |
| 1829 | + logics.put("TestQueryLogic2", logic2); |
| 1830 | + |
| 1831 | + QueryImpl settings = new QueryImpl(); |
| 1832 | + settings.setPagesize(100); |
| 1833 | + settings.setQueryAuthorizations(auths.toString()); |
| 1834 | + settings.setQuery("FOO == 'BAR'"); |
| 1835 | + settings.setParameters(new HashSet<>()); |
| 1836 | + settings.setId(UUID.randomUUID()); |
| 1837 | + |
| 1838 | + CompositeQueryLogic c = new CompositeQueryLogic(); |
| 1839 | + c.setQueryLogics(logics); |
| 1840 | + c.setCurrentUser(principal); |
| 1841 | + |
| 1842 | + Object validation = c.validateQuery(null, settings, Collections.singleton(auths)); |
| 1843 | + Transformer<Object,QueryValidationResponse> transformer = c.getQueryValidationResponseTransformer(); |
| 1844 | + QueryValidationResponse response = transformer.transform(validation); |
| 1845 | + Assert.assertEquals(VALIDATION_MESSAGE, response.getResults().get(0).getMessages().get(0)); |
| 1846 | + } |
1770 | 1847 | }
|
0 commit comments