Skip to content

Commit

Permalink
Updated to fill out the query parameters prior to creating Query obje…
Browse files Browse the repository at this point in the history
…ct (#2202)

* Updated to fill out the query parameters prior to creating Query object
* Added test case
  • Loading branch information
ivakegg authored Dec 20, 2023
1 parent ad5e9eb commit 0de9a2b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import datawave.webservice.common.exception.DatawaveWebApplicationException;
import datawave.webservice.common.exception.NoResultsException;
import datawave.webservice.query.Query;
import datawave.webservice.query.QueryImpl;
import datawave.webservice.query.QueryParameters;
import datawave.webservice.query.QueryParametersImpl;
import datawave.webservice.query.QueryPersistence;
Expand Down Expand Up @@ -427,20 +428,33 @@ public <T> T createUUIDQueryAndNext(final AbstractUUIDLookupCriteria unvalidated
return response;
}

private Query createSettings(MultivaluedMap<String,String> queryParameters) {
public Query createSettings(MultivaluedMap<String,String> queryParameters) {
log.debug("Initial query parameters: " + queryParameters);
Query query = responseObjectFactory.getQueryImpl();
if (queryParameters != null) {
query.setOptionalQueryParameters(queryParameters);
for (String key : queryParameters.keySet()) {
if (queryParameters.get(key).size() == 1) {
query.addParameter(key, queryParameters.get(key).get(0));
MultivaluedMap<String,String> expandedQueryParameters = new MultivaluedMapImpl<>();
if (defaultOptionalParams != null) {
expandedQueryParameters.putAll(defaultOptionalParams);
}
String delimitedParams = queryParameters.getFirst(QueryParameters.QUERY_PARAMS);
if (delimitedParams != null) {
for (QueryImpl.Parameter pm : QueryUtil.parseParameters(delimitedParams)) {
expandedQueryParameters.putSingle(pm.getParameterName(), pm.getParameterValue());
}
}
expandedQueryParameters.putAll(queryParameters);
log.debug("Final query parameters: " + expandedQueryParameters);
query.setOptionalQueryParameters(expandedQueryParameters);
for (String key : expandedQueryParameters.keySet()) {
if (expandedQueryParameters.get(key).size() == 1) {
query.addParameter(key, expandedQueryParameters.getFirst(key));
}
}
}
return query;
}

private String getAuths(String logicName, MultivaluedMap<String,String> queryParameters, String queryAuths, Principal principal) {
public String getAuths(String logicName, MultivaluedMap<String,String> queryParameters, String queryAuths, Principal principal) {
String userAuths;
try {
QueryLogic<?> logic = queryLogicFactory.getQueryLogic(logicName, principal);
Expand Down Expand Up @@ -601,7 +615,6 @@ private <T> T lookupContentByEvents(final AbstractUUIDLookupCriteria criteria, f
String sid = principal.getName();

// Initialize the reusable query input
final String userAuths = getAuths(CONTENT_QUERY, criteria.getQueryParameters(), null, principal);
final String queryName = sid + '-' + UUID.randomUUID();
final Date endDate = new Date();
final Date expireDate = new Date(endDate.getTime() + 1000 * 60 * 60);
Expand All @@ -616,7 +629,8 @@ private <T> T lookupContentByEvents(final AbstractUUIDLookupCriteria criteria, f
} else {
validatedCriteria = criteria;
}

final String userAuths = getAuths(CONTENT_QUERY, validatedCriteria.getQueryParameters(), null, principal);

// Perform the lookup
boolean allEventMockResponse = (uuidQueryResponse instanceof AllEventMockResponse);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package datawave.webservice.query.util;

import datawave.query.data.UUIDType;
import datawave.security.authorization.UserOperations;
import datawave.webservice.query.Query;
import datawave.webservice.query.QueryImpl;
import datawave.webservice.query.configuration.LookupUUIDConfiguration;
import datawave.webservice.query.logic.QueryLogicFactory;
import datawave.webservice.query.result.event.ResponseObjectFactory;
import datawave.webservice.query.runner.QueryExecutor;
import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
import org.jboss.resteasy.util.FindAnnotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.annotation.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.ejb.EJBContext;
import javax.ws.rs.core.MultivaluedMap;
import java.util.Collections;

import static junit.framework.TestCase.assertEquals;
import static org.easymock.EasyMock.expect;
import static org.powermock.api.easymock.PowerMock.replayAll;

@RunWith(PowerMockRunner.class)
@PrepareForTest(FindAnnotation.class)
public class LookupUUIDUtilTest {

@Mock
LookupUUIDConfiguration configuration;
@Mock
QueryExecutor queryExecutor;
@Mock
EJBContext context;
@Mock
ResponseObjectFactory responseObjectFactory;
@Mock
QueryLogicFactory queryLogicFactory;
@Mock
UserOperations userOperations;

@Test
public void testCreateSettings() {
expect(configuration.getUuidTypes()).andReturn(Collections.singletonList(new UUIDType("ID", "LuceneUUIDEventQuery", 28)));
expect(configuration.getBeginDate()).andReturn("20230101");
expect(configuration.getBatchLookupUpperLimit()).andReturn(10);
MultivaluedMap<String,String> defaultParams = new MultivaluedMapImpl<>();
defaultParams.putSingle("foo", "bar");
defaultParams.putSingle("foo2", "default");
expect(configuration.optionalParamsToMap()).andReturn(defaultParams);
expect(responseObjectFactory.getQueryImpl()).andReturn(new QueryImpl());
replayAll();
LookupUUIDUtil utils = new LookupUUIDUtil(configuration, queryExecutor, context, responseObjectFactory, queryLogicFactory, userOperations);

MultivaluedMap<String, String> properties = new MultivaluedMapImpl<>();
properties.putSingle("foo2", "bar2");
properties.add("foo3", "bar3");
properties.add("foo3", "bar3.1");
Query q = utils.createSettings(properties);

assertEquals(new QueryImpl.Parameter("foo", "bar"), q.findParameter("foo"));
assertEquals(new QueryImpl.Parameter("foo2", "bar2"), q.findParameter("foo2"));
assertEquals(new QueryImpl.Parameter("foo3", ""), q.findParameter("foo3"));
assertEquals(3, q.getOptionalQueryParameters().size());
properties.putSingle("foo", "bar");
assertEquals(properties, q.getOptionalQueryParameters());
}
}

0 comments on commit 0de9a2b

Please sign in to comment.