Search in sources :

Example 56 with QueryImpl

use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.

the class QueryExecutorBeanTest method testCreateWithNoSelectedAuths.

@SuppressWarnings("unchecked")
@Test(expected = DatawaveWebApplicationException.class)
public void testCreateWithNoSelectedAuths() throws Exception {
    String queryLogicName = "EventQueryLogic";
    String queryName = "Something";
    String query = "FOO == BAR";
    Date beginDate = new Date();
    Date expirationDate = DateUtils.addDays(new Date(), 1);
    int pagesize = 10;
    QueryPersistence persist = QueryPersistence.TRANSIENT;
    Set<QueryImpl.Parameter> parameters = new HashSet<>();
    // need to call the getQueryByName() method. Maybe a partial mock of QueryExecutorBean would be better
    // setup principal mock
    String userDN = "CN=Guy Some Other soguy, OU=MY_SUBDIVISION, OU=MY_DIVISION, O=ORG, C=US";
    String[] auths = new String[2];
    auths[0] = "PRIVATE";
    auths[1] = "PUBLIC";
    QueryImpl q = new QueryImpl();
    q.setBeginDate(beginDate);
    q.setEndDate(beginDate);
    q.setExpirationDate(expirationDate);
    q.setPagesize(pagesize);
    q.setParameters(parameters);
    q.setQuery(query);
    q.setQueryAuthorizations(StringUtils.join(auths, ","));
    q.setQueryLogicName(queryLogicName);
    q.setUserDN(userDN);
    q.setId(UUID.randomUUID());
    @SuppressWarnings("rawtypes") QueryLogic logic = createMock(BaseQueryLogic.class);
    MultivaluedMap<String, String> p = new MultivaluedMapImpl<>();
    p.putSingle(QueryParameters.QUERY_AUTHORIZATIONS, "");
    p.putSingle(QueryParameters.QUERY_BEGIN, QueryParametersImpl.formatDate(beginDate));
    p.putSingle(QueryParameters.QUERY_END, QueryParametersImpl.formatDate(beginDate));
    p.putSingle(QueryParameters.QUERY_EXPIRATION, QueryParametersImpl.formatDate(expirationDate));
    p.putSingle(QueryParameters.QUERY_NAME, queryName);
    p.putSingle(QueryParameters.QUERY_PAGESIZE, Integer.toString(pagesize));
    p.putSingle(QueryParameters.QUERY_STRING, query);
    p.putSingle(QueryParameters.QUERY_PERSISTENCE, persist.name());
    p.putSingle(ColumnVisibilitySecurityMarking.VISIBILITY_MARKING, "PRIVATE|PUBLIC");
    InMemoryInstance instance = new InMemoryInstance();
    Connector c = instance.getConnector("root", new PasswordToken(""));
    QueryParameters qp = new QueryParametersImpl();
    MultivaluedMap<String, String> optionalParameters = new MultivaluedMapImpl<>();
    optionalParameters.putAll(qp.getUnknownParameters(p));
    DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of(userDN, "<CN=MY_CA, OU=MY_SUBDIVISION, OU=MY_DIVISION, O=ORG, C=US>"), UserType.USER, Arrays.asList(auths), null, null, 0L);
    DatawavePrincipal principal = new DatawavePrincipal(Collections.singletonList(user));
    String[] dns = principal.getDNs();
    Arrays.sort(dns);
    List<String> dnList = Arrays.asList(dns);
    PowerMock.resetAll();
    EasyMock.expect(ctx.getCallerPrincipal()).andReturn(principal).anyTimes();
    suppress(constructor(QueryParametersImpl.class));
    EasyMock.expect(persister.create(userDN, dnList, (SecurityMarking) Whitebox.getField(bean.getClass(), "marking").get(bean), queryLogicName, (QueryParameters) Whitebox.getField(bean.getClass(), "qp").get(bean), optionalParameters)).andReturn(q);
    EasyMock.expect(queryLogicFactory.getQueryLogic(queryLogicName, principal)).andReturn(logic);
    EasyMock.expect(logic.getRequiredQueryParameters()).andReturn(Collections.EMPTY_SET);
    EasyMock.expect(logic.containsDNWithAccess(dnList)).andReturn(true);
    EasyMock.expect(logic.getMaxPageSize()).andReturn(0);
    EasyMock.expect(logic.getAuditType(EasyMock.<Query>anyObject())).andReturn(AuditType.ACTIVE).anyTimes();
    EasyMock.expect(logic.getSelectors(anyObject())).andReturn(null);
    Map<String, String> auditParams = new HashMap<>();
    auditParams.put(QueryParameters.QUERY_STRING, p.getFirst(QueryParameters.QUERY_STRING));
    auditParams.put(AuditParameters.USER_DN, userDN);
    auditParams.put(AuditParameters.QUERY_SECURITY_MARKING_COLVIZ, "PRIVATE|PUBLIC");
    auditParams.put(AuditParameters.QUERY_AUDIT_TYPE, AuditType.ACTIVE.name());
    auditParams.put(AuditParameters.QUERY_LOGIC_CLASS, "EventQueryLogic");
    EasyMock.expect(auditService.audit(eq(auditParams))).andReturn(null);
    logic.close();
    EasyMock.expectLastCall();
    persister.remove(anyObject(Query.class));
    PowerMock.replayAll();
    bean.createQuery(queryLogicName, p);
    PowerMock.verifyAll();
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Query(datawave.webservice.query.Query) HashMap(java.util.HashMap) DatawaveUser(datawave.security.authorization.DatawaveUser) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) InMemoryInstance(datawave.accumulo.inmemory.InMemoryInstance) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) QueryLogic(datawave.webservice.query.logic.QueryLogic) QueryParameters(datawave.webservice.query.QueryParameters) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) QueryImpl(datawave.webservice.query.QueryImpl) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) QueryPersistence(datawave.webservice.query.QueryPersistence) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 57 with QueryImpl

use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.

the class QueryExecutorBeanTest method testCloseActuallyCloses.

@SuppressWarnings("unchecked")
@Test(timeout = 5000)
public void testCloseActuallyCloses() throws Exception {
    QueryImpl q = createNewQuery();
    final MultivaluedMap<String, String> queryParameters = createNewQueryParameterMap();
    queryParameters.putSingle(QueryParameters.QUERY_LOGIC_NAME, "EventQueryLogic");
    final Thread createQuery = new Thread(() -> {
        try {
            bean.createQuery("EventQueryLogic", queryParameters);
        } catch (Exception e) {
            // ok if we fail the call
            log.debug("createQuery terminated with " + e);
        }
    });
    final Throwable[] createQueryException = { null };
    createQuery.setUncaughtExceptionHandler((t, e) -> createQueryException[0] = e);
    @SuppressWarnings("rawtypes") QueryLogic logic = createMock(BaseQueryLogic.class);
    DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of(userDN, "<CN=MY_CA, OU=MY_SUBDIVISION, OU=MY_DIVISION, O=ORG, C=US>"), UserType.USER, Arrays.asList(auths), null, null, 0L);
    DatawavePrincipal principal = new DatawavePrincipal(Collections.singletonList(user));
    principal.getShortName();
    String[] dns = principal.getDNs();
    Arrays.sort(dns);
    List<String> dnList = Arrays.asList(dns);
    InMemoryInstance instance = new InMemoryInstance();
    Connector c = instance.getConnector("root", new PasswordToken(""));
    MultivaluedMap<String, String> optionalParameters = createNewQueryParameters(q, queryParameters);
    PowerMock.resetAll();
    EasyMock.expect(ctx.getCallerPrincipal()).andReturn(principal).anyTimes();
    EasyMock.expect(logic.getAuditType(null)).andReturn(AuditType.NONE);
    EasyMock.expect(persister.create(principal.getUserDN().subjectDN(), dnList, Whitebox.getInternalState(bean, SecurityMarking.class), queryLogicName, Whitebox.getInternalState(bean, QueryParameters.class), optionalParameters)).andReturn(q);
    EasyMock.expect(persister.findById(EasyMock.anyString())).andReturn(null).anyTimes();
    EasyMock.expect(connectionFactory.getTrackingMap(anyObject())).andReturn(Maps.newHashMap()).anyTimes();
    BaseQueryMetric metric = new QueryMetricFactoryImpl().createMetric();
    metric.populate(q);
    EasyMock.expectLastCall();
    metric.setQueryType(RunningQuery.class.getSimpleName());
    metric.setLifecycle(Lifecycle.DEFINED);
    System.out.println(metric);
    Set<Prediction> predictions = new HashSet<>();
    predictions.add(new Prediction("source", 1));
    EasyMock.expect(predictor.predict(metric)).andReturn(predictions);
    connectionRequestBean.requestBegin(q.getId().toString());
    EasyMock.expectLastCall();
    EasyMock.expect(connectionFactory.getConnection(eq("connPool1"), anyObject(), anyObject())).andReturn(c).anyTimes();
    connectionRequestBean.requestEnd(q.getId().toString());
    EasyMock.expectLastCall();
    connectionFactory.returnConnection(c);
    EasyMock.expectLastCall();
    EasyMock.expect(queryLogicFactory.getQueryLogic(queryLogicName, principal)).andReturn(logic);
    EasyMock.expect(logic.getRequiredQueryParameters()).andReturn(Collections.emptySet());
    EasyMock.expect(logic.getConnectionPriority()).andReturn(AccumuloConnectionFactory.Priority.NORMAL).atLeastOnce();
    EasyMock.expect(logic.containsDNWithAccess(dnList)).andReturn(true);
    EasyMock.expect(logic.getMaxPageSize()).andReturn(0);
    EasyMock.expect(logic.getAuditType(q)).andReturn(AuditType.NONE);
    EasyMock.expect(logic.getConnPoolName()).andReturn("connPool1");
    EasyMock.expect(logic.getResultLimit(eq(q.getDnList()))).andReturn(-1L).anyTimes();
    EasyMock.expect(logic.getMaxResults()).andReturn(-1L).anyTimes();
    EasyMock.expect(connectionRequestBean.cancelConnectionRequest(q.getId().toString(), principal)).andReturn(false).anyTimes();
    connectionFactory.returnConnection(EasyMock.isA(Connector.class));
    final AtomicBoolean initializeLooping = new AtomicBoolean(false);
    // During initialize, mark that we get here, and then sleep
    final IAnswer<GenericQueryConfiguration> initializeAnswer = () -> {
        initializeLooping.set(true);
        try {
            while (true) {
                Thread.sleep(1000);
                log.debug("Initialize: woke up");
            }
        } catch (InterruptedException e) {
            throw new QueryException("EXPECTED EXCEPTION: initialize interrupted");
        }
    };
    EasyMock.expect(logic.initialize(anyObject(Connector.class), anyObject(Query.class), anyObject(Set.class))).andAnswer(initializeAnswer);
    EasyMock.expect(logic.getCollectQueryMetrics()).andReturn(Boolean.FALSE);
    // On close, interrupt the thread to simulate the ScannerFactory cleaning up
    final IAnswer<Object> closeAnswer = () -> {
        if (null != createQuery) {
            log.debug("createQuery thread is not null. interrupting");
            createQuery.interrupt();
        } else {
            log.debug("createQuery thread is null. not interrupting");
        }
        return null;
    };
    logic.close();
    EasyMock.expectLastCall().andAnswer(closeAnswer).anyTimes();
    // Make the QueryLogic mock not threadsafe, otherwise it will be blocked infinitely
    // trying to get the lock on the infinite loop
    EasyMock.makeThreadSafe(logic, false);
    metrics.updateMetric(EasyMock.isA(QueryMetric.class));
    PowerMock.replayAll();
    try {
        createQuery.start();
        // Wait for the create call to get to initialize
        while (!initializeLooping.get()) {
            if (!createQuery.isAlive() && !initializeLooping.get()) {
                Assert.fail("createQuery thread died before reaching initialize: " + createQueryException[0]);
            }
            Thread.sleep(50);
        }
        // initialize has not completed yet so it will not appear in the cache
        Object cachedRunningQuery = cache.get(q.getId().toString());
        Assert.assertNull(cachedRunningQuery);
        Pair<QueryLogic<?>, Connector> pair = qlCache.poll(q.getId().toString());
        Assert.assertNotNull(pair);
        Assert.assertEquals(logic, pair.getFirst());
        Assert.assertEquals(c, pair.getSecond());
        // Have to add these back because poll was destructive
        qlCache.add(q.getId().toString(), principal.getShortName(), pair.getFirst(), pair.getSecond());
        // Call close
        bean.close(q.getId().toString());
        // Make sure that it's gone from the qlCache
        pair = qlCache.poll(q.getId().toString());
        Assert.assertNull("Still found an entry in the qlCache: " + pair, pair);
        // Should have already joined by now, but just to be sure
        createQuery.join();
    } finally {
        if (null != createQuery && createQuery.isAlive()) {
            createQuery.interrupt();
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Set(java.util.Set) HashSet(java.util.HashSet) Query(datawave.webservice.query.Query) InMemoryInstance(datawave.accumulo.inmemory.InMemoryInstance) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) GenericQueryConfiguration(datawave.webservice.query.configuration.GenericQueryConfiguration) QueryImpl(datawave.webservice.query.QueryImpl) PasswordToken(org.apache.accumulo.core.client.security.tokens.PasswordToken) SecurityMarking(datawave.marking.SecurityMarking) ColumnVisibilitySecurityMarking(datawave.marking.ColumnVisibilitySecurityMarking) BaseQueryMetric(datawave.microservice.querymetric.BaseQueryMetric) QueryMetric(datawave.microservice.querymetric.QueryMetric) BaseQueryMetric(datawave.microservice.querymetric.BaseQueryMetric) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) HashSet(java.util.HashSet) DatawaveUser(datawave.security.authorization.DatawaveUser) Prediction(datawave.microservice.querymetric.BaseQueryMetric.Prediction) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) QueryLogic(datawave.webservice.query.logic.QueryLogic) QueryParameters(datawave.webservice.query.QueryParameters) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) URISyntaxException(java.net.URISyntaxException) BadRequestException(datawave.webservice.common.exception.BadRequestException) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueryException(datawave.webservice.query.exception.QueryException) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 58 with QueryImpl

use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testExecute_HappyPath.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testExecute_HappyPath() throws Exception {
    // Set local test input
    String queryLogicName = "queryLogicName";
    String query = "query";
    String queryName = "queryName";
    String queryVisibility = "A&B";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    String queryAuthorizations = "AUTH_1";
    Date expirationDate = new Date(currentTime + 9999);
    int pagesize = 10;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = "invalidparam; valid:param";
    boolean trace = false;
    UUID queryId = UUID.randomUUID();
    List<MediaType> mediaTypes = new ArrayList<>();
    mediaTypes.add(MediaType.APPLICATION_XML_TYPE);
    GenericResponse<String> createResponse = new GenericResponse<>();
    createResponse.setResult(queryId.toString());
    QueryImpl qp = new QueryImpl();
    qp.setQuery(query);
    qp.setQueryName(queryName);
    qp.setDnList(Collections.singletonList("someDN"));
    qp.setQueryAuthorizations(queryAuthorizations);
    qp.setColumnVisibility(queryVisibility);
    qp.setBeginDate(beginDate);
    qp.setEndDate(endDate);
    qp.setExpirationDate(expirationDate);
    qp.setPagesize(pagesize);
    qp.setPageTimeout(pageTimeout);
    qp.setColumnVisibility(queryAuthorizations);
    MultivaluedMap<String, String> params = new MultivaluedMapImpl<>();
    params.putAll(qp.toMap());
    params.putSingle(QueryParameters.QUERY_TRACE, Boolean.toString(trace));
    params.putSingle(QueryParameters.QUERY_PERSISTENCE, persistenceMode.name());
    params.putSingle(QueryParameters.QUERY_PARAMS, parameters);
    QueryExecutorBean subject = PowerMock.createPartialMock(QueryExecutorBean.class, "createQuery");
    // Set expectations of the create logic
    expect(this.context.getCallerPrincipal()).andReturn(this.principal).anyTimes();
    expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0)).anyTimes();
    expect(this.httpHeaders.getAcceptableMediaTypes()).andReturn(mediaTypes);
    expect(this.queryLogicFactory.getQueryLogic(queryLogicName, principal)).andReturn((QueryLogic) this.queryLogic1);
    expect(this.queryLogic1.getTransformer(isA(Query.class))).andReturn(this.transformer);
    expect(this.transformer.createResponse(isA(ResultsPage.class))).andReturn(this.baseResponse);
    expect(subject.createQuery(queryLogicName, params, httpHeaders)).andReturn(createResponse);
    expect(this.cache.get(eq(queryId.toString()))).andReturn(this.runningQuery);
    expect(this.runningQuery.getMetric()).andReturn(this.queryMetric);
    this.queryMetric.setCreateCallTime(EasyMock.geq(0L));
    // return streaming response
    // Run the test
    PowerMock.replayAll();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionFactory.class, connectionFactory);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, CreatedQueryLogicCacheBean.class, qlCache);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, Persister.class, persister);
    setInternalState(subject, QueryLogicFactory.class, queryLogicFactory);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, AuditBean.class, auditor);
    setInternalState(subject, QueryMetricsBean.class, metrics);
    setInternalState(subject, Multimap.class, traceInfos);
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    StreamingOutput result1 = subject.execute(queryLogicName, params, httpHeaders);
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a non-null response", result1);
}
Also used : Query(datawave.webservice.query.Query) GenericResponse(datawave.webservice.result.GenericResponse) ArrayList(java.util.ArrayList) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) ResultsPage(datawave.webservice.query.cache.ResultsPage) StreamingOutput(javax.ws.rs.core.StreamingOutput) Date(java.util.Date) QueryImpl(datawave.webservice.query.QueryImpl) QueryPersistence(datawave.webservice.query.QueryPersistence) MediaType(javax.ws.rs.core.MediaType) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 59 with QueryImpl

use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testDuplicateQuery_HappyPath.

@Ignore
// TODO: Fix Query duplicate method
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDuplicateQuery_HappyPath() throws Exception {
    // Set local test input
    String queryLogicName = "queryLogicName";
    String query = "query";
    String newQueryName = "newQueryName";
    String queryVisibility = "A&B";
    long currentTime = System.currentTimeMillis();
    Date beginDate = new Date(currentTime - 5000);
    Date endDate = new Date(currentTime - 1000);
    String queryAuthorizations = "AUTH_1";
    Date expirationDate = new Date(currentTime + 9999);
    int pagesize = 10;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = "invalidparam; valid:param";
    boolean trace = true;
    String userName = "userName";
    String userSid = "userSid";
    String userDN = "userDN";
    SubjectIssuerDNPair userDNpair = SubjectIssuerDNPair.of(userDN);
    List<String> dnList = Collections.singletonList(userDN);
    UUID queryId = UUID.randomUUID();
    MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl<>();
    queryParameters.putSingle(QueryParameters.QUERY_STRING, query);
    queryParameters.putSingle(QueryParameters.QUERY_NAME, newQueryName);
    queryParameters.putSingle(QueryParameters.QUERY_BEGIN, QueryParametersImpl.formatDate(beginDate));
    queryParameters.putSingle(QueryParameters.QUERY_END, QueryParametersImpl.formatDate(endDate));
    queryParameters.putSingle(QueryParameters.QUERY_EXPIRATION, QueryParametersImpl.formatDate(expirationDate));
    queryParameters.putSingle(QueryParameters.QUERY_AUTHORIZATIONS, queryAuthorizations);
    queryParameters.putSingle(QueryParameters.QUERY_PARAMS, parameters);
    queryParameters.putSingle(QueryParameters.QUERY_PAGESIZE, String.valueOf(pagesize));
    queryParameters.putSingle(QueryParameters.QUERY_PAGETIMEOUT, String.valueOf(pageTimeout));
    queryParameters.putSingle(QueryParameters.QUERY_PERSISTENCE, persistenceMode.name());
    queryParameters.putSingle(QueryParameters.QUERY_TRACE, String.valueOf(trace));
    queryParameters.putSingle(ColumnVisibilitySecurityMarking.VISIBILITY_MARKING, queryVisibility);
    ColumnVisibilitySecurityMarking marking = new ColumnVisibilitySecurityMarking();
    marking.validate(queryParameters);
    QueryParameters qp = new QueryParametersImpl();
    qp.validate(queryParameters);
    MultivaluedMap<String, String> op = new MultivaluedMapImpl<>();
    op.putAll(qp.getUnknownParameters(queryParameters));
    op.putSingle(PrivateAuditConstants.LOGIC_CLASS, queryLogic1.getClass().getSimpleName());
    op.putSingle(PrivateAuditConstants.COLUMN_VISIBILITY, queryVisibility);
    op.putSingle(PrivateAuditConstants.USER_DN, userDN);
    // Set expectations of the create logic
    expect(this.context.getCallerPrincipal()).andReturn(this.principal).times(2);
    expect(this.principal.getName()).andReturn(userName).times(2);
    expect(this.principal.getShortName()).andReturn(userSid).times(2);
    expect(this.principal.getUserDN()).andReturn(userDNpair);
    expect(this.principal.getDNs()).andReturn(new String[] { userDN });
    expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0)).anyTimes();
    queryLogic1.validate(queryParameters);
    expect(this.queryLogic1.getAuditType(null)).andReturn(AuditType.NONE);
    expect(this.principal.getAuthorizations()).andReturn((Collection) Arrays.asList(Arrays.asList(queryAuthorizations))).times(2);
    expect(this.cache.get(queryId.toString())).andReturn(this.runningQuery);
    expect(this.runningQuery.getSettings()).andReturn(this.query).times(2);
    expect(this.query.getOwner()).andReturn(userSid);
    QueryImpl newQuery1 = new QueryImpl();
    newQuery1.setId(UUID.randomUUID());
    newQuery1.setQuery(query);
    newQuery1.setQueryName(newQueryName);
    newQuery1.setBeginDate(beginDate);
    newQuery1.setEndDate(endDate);
    newQuery1.setExpirationDate(expirationDate);
    newQuery1.setDnList(Collections.singletonList(userDN));
    expect(this.query.duplicate(newQueryName)).andReturn(newQuery1);
    expect(context.getCallerPrincipal()).andReturn(principal).times(2);
    expect(this.queryLogicFactory.getQueryLogic(queryLogicName, principal)).andReturn((QueryLogic) this.queryLogic1).times(2);
    expect(this.queryLogic1.getLogicName()).andReturn(queryLogicName).times(2);
    expect(this.queryLogic1.getMaxPageSize()).andReturn(100).times(2);
    QueryImpl newQuery2 = new TestQuery(newQuery1);
    expect(persister.create(eq(userDNpair.subjectDN()), eq(dnList), eq(marking), eq(queryLogicName), eq(qp), eq(op))).andReturn(newQuery2);
    expect(this.queryLogic1.getAuditType(newQuery2)).andReturn(AuditType.NONE);
    expect(this.queryLogic1.getConnectionPriority()).andReturn(Priority.NORMAL);
    expect(this.queryLogic1.getConnPoolName()).andReturn("connPool1");
    expect(this.connectionFactory.getTrackingMap(isA(StackTraceElement[].class))).andReturn(null);
    expect(this.connectionFactory.getConnection("connPool1", Priority.NORMAL, null)).andReturn(this.connector);
    expect(this.qlCache.add(newQuery1.getId().toString(), userSid, this.queryLogic1, this.connector)).andReturn(true);
    expect(this.queryLogic1.getCollectQueryMetrics()).andReturn(false);
    expect(this.queryLogic1.initialize(eq(this.connector), isA(Query.class), isA(Set.class))).andReturn(this.genericConfiguration);
    this.queryLogic1.setupQuery(this.genericConfiguration);
    expect(this.queryLogic1.getTransformIterator(eq(newQuery2))).andReturn(this.transformIterator);
    expect(this.genericConfiguration.getQueryString()).andReturn(query).once();
    this.cache.put(eq(newQuery2.getId().toString()), isA(RunningQuery.class));
    expect(this.qlCache.poll(newQuery1.getId().toString())).andReturn(null);
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, AccumuloConnectionFactory.class, connectionFactory);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, CreatedQueryLogicCacheBean.class, qlCache);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, Persister.class, persister);
    setInternalState(subject, QueryLogicFactory.class, queryLogicFactory);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, QueryMetricsBean.class, metrics);
    setInternalState(subject, Multimap.class, traceInfos);
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    GenericResponse<String> result1 = subject.duplicateQuery(queryId.toString(), newQueryName, queryLogicName, query, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace);
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a non-null response", result1);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Query(datawave.webservice.query.Query) ArrayList(java.util.ArrayList) QueryImpl(datawave.webservice.query.QueryImpl) QueryPersistence(datawave.webservice.query.QueryPersistence) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) SubjectIssuerDNPair(datawave.security.authorization.SubjectIssuerDNPair) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) QueryParameters(datawave.webservice.query.QueryParameters) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) QueryLogic(datawave.webservice.query.logic.QueryLogic) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) ColumnVisibilitySecurityMarking(datawave.marking.ColumnVisibilitySecurityMarking) Collection(java.util.Collection) PowerMockIgnore(org.powermock.core.classloader.annotations.PowerMockIgnore) Ignore(org.junit.Ignore) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 60 with QueryImpl

use of datawave.webservice.query.QueryImpl in project datawave by NationalSecurityAgency.

the class ExecutableExpansionVisitorTest method runTestQuery.

protected void runTestQuery(List<String> expected, String querystr, Date startDate, Date endDate, Map<String, String> extraParms, Connector connector) throws ParseException, 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());
    GenericQueryConfiguration config = logic.initialize(connector, settings, authSet);
    logic.setupQuery(config);
    HashSet<String> expectedSet = new HashSet<String>(expected);
    HashSet<String> resultSet;
    resultSet = new HashSet<String>();
    Set<Document> docs = new HashSet<Document>();
    for (Map.Entry<Key, Value> entry : logic) {
        Document d = deserializer.apply(entry).getValue();
        log.trace(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());
}
Also used : PreNormalizedAttribute(datawave.query.attributes.PreNormalizedAttribute) Document(datawave.query.attributes.Document) GenericQueryConfiguration(datawave.webservice.query.configuration.GenericQueryConfiguration) QueryImpl(datawave.webservice.query.QueryImpl) TypeAttribute(datawave.query.attributes.TypeAttribute) Value(org.apache.accumulo.core.data.Value) Map(java.util.Map) HashMap(java.util.HashMap) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet)

Aggregations

QueryImpl (datawave.webservice.query.QueryImpl)131 Test (org.junit.Test)96 ArrayList (java.util.ArrayList)54 Query (datawave.webservice.query.Query)40 HashSet (java.util.HashSet)27 BaseQueryLogic (datawave.webservice.query.logic.BaseQueryLogic)18 GenericQueryConfiguration (datawave.webservice.query.configuration.GenericQueryConfiguration)16 Date (java.util.Date)16 QueryParametersImpl (datawave.webservice.query.QueryParametersImpl)14 MultivaluedMapImpl (org.jboss.resteasy.specimpl.MultivaluedMapImpl)13 QueryMetricFactoryImpl (datawave.microservice.querymetric.QueryMetricFactoryImpl)12 QueryParameters (datawave.webservice.query.QueryParameters)12 Value (org.apache.accumulo.core.data.Value)10 Document (datawave.query.attributes.Document)9 ShardQueryConfiguration (datawave.query.config.ShardQueryConfiguration)9 EdgeQueryFunctionalTest (datawave.query.tables.edge.EdgeQueryFunctionalTest)9 QueryLogic (datawave.webservice.query.logic.QueryLogic)9 HashMap (java.util.HashMap)9 Key (org.apache.accumulo.core.data.Key)9 Authorizations (org.apache.accumulo.core.security.Authorizations)9