From eedd3f30842b0d4bbf07dc700771119f2bfdb04b Mon Sep 17 00:00:00 2001 From: Ivan Bella Date: Wed, 20 Dec 2023 16:29:50 +0000 Subject: [PATCH] Added test case --- .../webservice/query/util/LookupUUIDUtil.java | 22 +++--- .../query/util/LookupUUIDUtilTest.java | 70 +++++++++++++++++++ 2 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 web-services/query/src/test/java/datawave/webservice/query/util/LookupUUIDUtilTest.java diff --git a/web-services/query/src/main/java/datawave/webservice/query/util/LookupUUIDUtil.java b/web-services/query/src/main/java/datawave/webservice/query/util/LookupUUIDUtil.java index c1c31f3750e..1c0584f3e2f 100644 --- a/web-services/query/src/main/java/datawave/webservice/query/util/LookupUUIDUtil.java +++ b/web-services/query/src/main/java/datawave/webservice/query/util/LookupUUIDUtil.java @@ -428,33 +428,33 @@ public T createUUIDQueryAndNext(final AbstractUUIDLookupCriteria unvalidated return response; } - private Query createSettings(MultivaluedMap queryParameters) { + public Query createSettings(MultivaluedMap queryParameters) { log.debug("Initial query parameters: " + queryParameters); Query query = responseObjectFactory.getQueryImpl(); if (queryParameters != null) { - MultivaluedMap optionalQueryParameters = new MultivaluedMapImpl<>(); + MultivaluedMap expandedQueryParameters = new MultivaluedMapImpl<>(); if (defaultOptionalParams != null) { - optionalQueryParameters.putAll(defaultOptionalParams); + expandedQueryParameters.putAll(defaultOptionalParams); } String delimitedParams = queryParameters.getFirst(QueryParameters.QUERY_PARAMS); if (delimitedParams != null) { for (QueryImpl.Parameter pm : QueryUtil.parseParameters(delimitedParams)) { - optionalQueryParameters.putSingle(pm.getParameterName(), pm.getParameterValue()); + expandedQueryParameters.putSingle(pm.getParameterName(), pm.getParameterValue()); } } - optionalQueryParameters.putAll(queryParameters); - log.debug("Final query parameters: " + optionalQueryParameters); - query.setOptionalQueryParameters(optionalQueryParameters); - for (String key : optionalQueryParameters.keySet()) { - if (optionalQueryParameters.get(key).size() == 1) { - query.addParameter(key, optionalQueryParameters.getFirst(key)); + 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 queryParameters, String queryAuths, Principal principal) { + public String getAuths(String logicName, MultivaluedMap queryParameters, String queryAuths, Principal principal) { String userAuths; try { QueryLogic logic = queryLogicFactory.getQueryLogic(logicName, principal); diff --git a/web-services/query/src/test/java/datawave/webservice/query/util/LookupUUIDUtilTest.java b/web-services/query/src/test/java/datawave/webservice/query/util/LookupUUIDUtilTest.java new file mode 100644 index 00000000000..ec65ded4a7d --- /dev/null +++ b/web-services/query/src/test/java/datawave/webservice/query/util/LookupUUIDUtilTest.java @@ -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 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 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()); + } +}