use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.
the class DatawaveSelectorExtractorTest method extractSelectorsJEXLQuery1.
@Test
public void extractSelectorsJEXLQuery1() {
DatawaveSelectorExtractor extractor = new DatawaveSelectorExtractor();
QueryImpl q = new QueryImpl();
q.setQuery("FIELD1 == 'selector1'");
List<String> selectorList = extractor.extractSelectors(q);
List<String> expected = Lists.newArrayList("selector1");
Assert.assertEquals(expected, selectorList);
}
use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.
the class DatawaveSelectorExtractorTest method extractSelectorsJEXLQuery2.
@Test
public void extractSelectorsJEXLQuery2() {
DatawaveSelectorExtractor extractor = new DatawaveSelectorExtractor();
QueryImpl q = new QueryImpl();
q.setQuery("FIELD1 == 'selector1' && _ANY_FIELD_ == 'selector2' && _ANY_FIELD_ == 'selector3'");
List<String> selectorList = extractor.extractSelectors(q);
List<String> expected = Lists.newArrayList("selector1", "selector2", "selector3");
Assert.assertEquals(expected, selectorList);
}
use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.
the class DatawaveSelectorExtractorTest method extract10kSelectors.
@Test
public void extract10kSelectors() {
List<String> uuids = new ArrayList<>();
for (int i = 0; i < 10000; i++) uuids.add("_ANY_FIELD_ == '" + UUID.randomUUID().toString() + "'");
String query = String.join(" || ", uuids);
DatawaveSelectorExtractor extractor = new DatawaveSelectorExtractor();
QueryImpl q = new QueryImpl();
q.setQuery(query);
List<String> selectorList = extractor.extractSelectors(q);
List<String> expected = Lists.newArrayList(uuids.stream().map(x -> x.substring("_ANY_FIELD_ == '".length(), x.length() - 1)).collect(Collectors.toList()));
Assert.assertEquals(expected, selectorList);
}
use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.
the class GroupingTest method runTestQueryWithGrouping.
protected BaseQueryResponse runTestQueryWithGrouping(Map<String, Integer> expected, String querystr, Date startDate, Date endDate, Map<String, String> extraParms, Connector connector) throws Exception {
log.debug("runTestQueryWithGrouping");
QueryImpl settings = new QueryImpl();
settings.setBeginDate(startDate);
settings.setEndDate(endDate);
settings.setPagesize(Integer.MAX_VALUE);
settings.setQueryAuthorizations(auths.serialize());
settings.setQuery(querystr);
settings.setParameters(extraParms);
settings.setId(UUID.randomUUID());
log.debug("query: " + settings.getQuery());
log.debug("logic: " + settings.getQueryLogicName());
GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
logic.setupQuery(config);
DocumentTransformer transformer = (DocumentTransformer) (logic.getTransformer(settings));
TransformIterator iter = new DatawaveTransformIterator(logic.iterator(), transformer);
List<Object> eventList = new ArrayList<>();
while (iter.hasNext()) {
eventList.add(iter.next());
}
BaseQueryResponse response = transformer.createResponse(eventList);
// un-comment to look at the json output
ObjectMapper mapper = new ObjectMapper();
mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);
mapper.writeValue(temporaryFolder.newFile(), response);
Assert.assertTrue(response instanceof DefaultEventQueryResponse);
DefaultEventQueryResponse eventQueryResponse = (DefaultEventQueryResponse) response;
Assert.assertEquals("Got the wrong number of events", expected.size(), (long) eventQueryResponse.getReturnedEvents());
for (EventBase event : eventQueryResponse.getEvents()) {
String firstKey = "";
String secondKey = "";
Integer value = null;
for (Object field : event.getFields()) {
FieldBase fieldBase = (FieldBase) field;
switch(fieldBase.getName()) {
case "COUNT":
value = Integer.valueOf(fieldBase.getValueString());
break;
case "GENDER":
case "GEN":
case "BIRTHDAY":
firstKey = fieldBase.getValueString();
break;
case "AGE":
case "AG":
case "RECORD":
secondKey = fieldBase.getValueString();
break;
}
}
log.debug("mapping is " + firstKey + "-" + secondKey + " count:" + value);
String key;
if (!firstKey.isEmpty() && !secondKey.isEmpty()) {
key = firstKey + "-" + secondKey;
} else if (!firstKey.isEmpty()) {
key = firstKey;
} else {
key = secondKey;
}
Assert.assertEquals(expected.get(key), value);
}
return response;
}
use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.
the class CompositeFunctionsTest method runTestQuery.
protected void runTestQuery(List<String> expected, String querystr, Date startDate, Date endDate, Map<String, String> extraParms, Connector connector, ShardQueryLogic logic) throws Exception {
log.debug("runTestQuery");
log.trace("Creating QueryImpl");
QueryImpl settings = new QueryImpl();
settings.setBeginDate(startDate);
settings.setEndDate(endDate);
settings.setPagesize(Integer.MAX_VALUE);
settings.setQueryAuthorizations(auths.serialize());
settings.setQuery(querystr);
settings.setParameters(extraParms);
settings.setId(UUID.randomUUID());
log.debug("query: " + settings.getQuery());
log.debug("logic: " + settings.getQueryLogicName());
logic.setMaxEvaluationPipelines(1);
GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
logic.setupQuery(config);
HashSet<String> expectedSet = new HashSet<>(expected);
HashSet<String> resultSet;
resultSet = new HashSet<>();
Set<Document> docs = new HashSet<>();
for (Entry<Key, Value> entry : logic) {
Document d = deserializer.apply(entry).getValue();
log.debug(entry.getKey() + " => " + d);
Attribute<?> attr = d.get("UUID");
if (attr == null) {
attr = d.get("UUID.0");
}
Assert.assertNotNull("Result Document did not contain a 'UUID'", attr);
Assert.assertTrue("Expected result to be an instance of DatwawaveTypeAttribute, was: " + attr.getClass().getName(), attr instanceof TypeAttribute || attr instanceof PreNormalizedAttribute);
TypeAttribute<?> UUIDAttr = (TypeAttribute<?>) attr;
String UUID = UUIDAttr.getType().getDelegate().toString();
Assert.assertTrue("Received unexpected UUID: " + UUID, expected.contains(UUID));
resultSet.add(UUID);
docs.add(d);
}
if (expected.size() > resultSet.size()) {
expectedSet.addAll(expected);
expectedSet.removeAll(resultSet);
for (String s : expectedSet) {
log.warn("Missing: " + s);
}
}
if (!expected.containsAll(resultSet)) {
log.error("Expected results " + expected + " differ form actual results " + resultSet);
}
Assert.assertTrue("Expected results " + expected + " differ form actual results " + resultSet, expected.containsAll(resultSet));
Assert.assertEquals("Unexpected number of records", expected.size(), resultSet.size());
}
Aggregations