Search in sources :

Example 51 with QueryImpl

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

the class EdgeQueryFunctionalTest method testRelationStatsOn.

@Test
public void testRelationStatsOn() throws Exception {
    QueryImpl q = configQuery("(SOURCE =~ 'M.*') && (SINK == 'VENUS') && (RELATION == 'FROM-TO' || RELATION == 'TO-FROM')", auths);
    q.addParameter("stats", "true");
    EdgeQueryLogic logic = runLogic(q, auths);
    List<String> expected = new ArrayList<>();
    expected.add("mercury%00;venus AdjacentPlanets/FROM-TO:20150713/COSMOS_DATA-COSMOS_DATA [A]");
    expected.add("mars STATS/ACTIVITY/Planets/TO:20150713/COSMOS_DATA [B]");
    expected.add("mercury STATS/ACTIVITY/Planets/TO:20150713/COSMOS_DATA [B]");
    compareResults(logic, expected);
}
Also used : QueryImpl(datawave.webservice.query.QueryImpl) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 52 with QueryImpl

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

the class QueryExecutorBeanTest method testPredict.

@SuppressWarnings("unchecked")
@Test
public void testPredict() throws Exception {
    QueryImpl q = createNewQuery();
    MultivaluedMap p = createNewQueryParameterMap();
    p.putSingle(QueryParameters.QUERY_LOGIC_NAME, queryLogicName);
    MultivaluedMap<String, String> optionalParameters = createNewQueryParameters(q, p);
    @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));
    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(principal.getUserDN().subjectDN(), 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);
    BaseQueryMetric metric = new QueryMetricFactoryImpl().createMetric();
    metric.populate(q);
    metric.setQueryType(RunningQuery.class.getSimpleName());
    QueryMetric testMetric = new QueryMetric((QueryMetric) metric) {

        @Override
        public boolean equals(Object o) {
            // test for equality except for the create date
            if (null == o) {
                return false;
            }
            if (this == o) {
                return true;
            }
            if (o instanceof QueryMetric) {
                QueryMetric other = (QueryMetric) o;
                return new EqualsBuilder().append(this.getQueryId(), other.getQueryId()).append(this.getQueryType(), other.getQueryType()).append(this.getQueryAuthorizations(), other.getQueryAuthorizations()).append(this.getColumnVisibility(), other.getColumnVisibility()).append(this.getBeginDate(), other.getBeginDate()).append(this.getEndDate(), other.getEndDate()).append(this.getCreateDate(), other.getCreateDate()).append(this.getSetupTime(), other.getSetupTime()).append(this.getUser(), other.getUser()).append(this.getUserDN(), other.getUserDN()).append(this.getQuery(), other.getQuery()).append(this.getQueryLogic(), other.getQueryLogic()).append(this.getQueryName(), other.getQueryName()).append(this.getParameters(), other.getParameters()).append(this.getHost(), other.getHost()).append(this.getPageTimes(), other.getPageTimes()).append(this.getProxyServers(), other.getProxyServers()).append(this.getLifecycle(), other.getLifecycle()).append(this.getErrorMessage(), other.getErrorMessage()).append(this.getErrorCode(), other.getErrorCode()).append(this.getSourceCount(), other.getSourceCount()).append(this.getNextCount(), other.getNextCount()).append(this.getSeekCount(), other.getSeekCount()).append(this.getYieldCount(), other.getYieldCount()).append(this.getDocRanges(), other.getDocRanges()).append(this.getFiRanges(), other.getFiRanges()).append(this.getPlan(), other.getPlan()).append(this.getVersion(), other.getVersion()).append(this.getLoginTime(), other.getLoginTime()).append(this.getPredictions(), other.getPredictions()).isEquals();
            } else {
                return false;
            }
        }
    };
    Set<Prediction> predictions = new HashSet<>();
    predictions.add(new Prediction("source", 1));
    EasyMock.expect(predictor.predict(EasyMock.eq(testMetric))).andReturn(predictions);
    PowerMock.replayAll();
    GenericResponse<String> response = bean.predictQuery(queryLogicName, p);
    PowerMock.verifyAll();
    Object cachedRunningQuery = cache.get(q.getId().toString());
    Assert.assertNull(cachedRunningQuery);
    Assert.assertEquals(predictions.toString(), response.getResult());
}
Also used : DatawaveUser(datawave.security.authorization.DatawaveUser) Prediction(datawave.microservice.querymetric.BaseQueryMetric.Prediction) EqualsBuilder(org.apache.commons.lang.builder.EqualsBuilder) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) QueryLogic(datawave.webservice.query.logic.QueryLogic) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) QueryImpl(datawave.webservice.query.QueryImpl) BaseQueryMetric(datawave.microservice.querymetric.BaseQueryMetric) QueryMetric(datawave.microservice.querymetric.QueryMetric) EasyMock.anyObject(org.easymock.EasyMock.anyObject) BaseQueryMetric(datawave.microservice.querymetric.BaseQueryMetric) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 53 with QueryImpl

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

the class QueryExecutorBeanTest method createNewQuery.

private QueryImpl createNewQuery() throws Exception {
    Set<QueryImpl.Parameter> parameters = new HashSet<>();
    QueryImpl q = new QueryImpl();
    q.setBeginDate(beginDate);
    q.setEndDate(endDate);
    q.setExpirationDate(expirationDate);
    q.setPagesize(pagesize);
    q.setParameters(parameters);
    q.setQuery(query);
    q.setQueryAuthorizations(StringUtils.join(auths, ","));
    q.setQueryLogicName(queryLogicName);
    q.setUserDN(userDN);
    q.setDnList(Collections.singletonList(userDN));
    q.setId(UUID.randomUUID());
    return q;
}
Also used : QueryImpl(datawave.webservice.query.QueryImpl) HashSet(java.util.HashSet)

Example 54 with QueryImpl

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

the class QueryExecutorBeanTest method testDefine.

@SuppressWarnings("unchecked")
@Test
public void testDefine() throws Exception {
    QueryImpl q = createNewQuery();
    MultivaluedMap p = createNewQueryParameterMap();
    p.putSingle(QueryParameters.QUERY_LOGIC_NAME, "EventQueryLogic");
    defineTestRunner(q, p);
}
Also used : QueryImpl(datawave.webservice.query.QueryImpl) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 55 with QueryImpl

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

the class QueryExecutorBeanTest method testGoodListWithGet.

// @Test
@SuppressWarnings("unchecked")
public void testGoodListWithGet() throws URISyntaxException, CloneNotSupportedException, ParserConfigurationException, IOException, SAXException {
    String queryName = "Something";
    // need to call the getQueryByName() method. Maybe a partial mock of QueryExecutorBean would be better
    // setup principal mock
    String dn = "CN=Guy Some Other soguy, OU=MY_SUBDIVISION, OU=MY_DIVISION, O=ORG, C=US";
    String[] auths = new String[2];
    auths[0] = "PUBLIC";
    auths[1] = "PRIVATE";
    DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of(dn), UserType.USER, Arrays.asList(auths), null, null, 0L);
    DatawavePrincipal principal = new DatawavePrincipal(Collections.singletonList(user));
    EasyMock.expect(ctx.getCallerPrincipal()).andReturn(principal);
    EasyMock.replay(ctx);
    // setup persister with queries
    String logicName = "EventQuery";
    QueryImpl q1 = new QueryImpl();
    q1.setUserDN(principal.getShortName());
    q1.setQueryLogicName(logicName);
    q1.setQueryAuthorizations(auths.toString());
    q1.setId(new UUID(1, 1));
    QueryImpl q2 = new QueryImpl();
    q2.setUserDN(principal.getShortName());
    q2.setQueryLogicName(logicName);
    q2.setQueryAuthorizations(auths.toString());
    q2.setId(new UUID(1, 2));
    List<Query> queries = new ArrayList<>();
    queries.add(q1);
    queries.add(q2);
    EasyMock.expect(persister.findByName(queryName)).andReturn(queries);
    EasyMock.replay(persister);
    @SuppressWarnings("rawtypes") QueryLogic logic = createMock(BaseQueryLogic.class);
    EasyMock.expect(logic.getConnectionPriority()).andReturn(AccumuloConnectionFactory.Priority.NORMAL).times(2);
    EasyMock.expect(logic.getMaxPageSize()).andReturn(0);
    EasyMock.replay(logic);
    EasyMock.expect(queryLogicFactory.getQueryLogic(logicName, null)).andReturn(logic).times(2);
    EasyMock.replay(queryLogicFactory);
    // setup test
    request = MockHttpRequest.get("/DataWave/Query/list?name=" + queryName);
    // execute
    dispatcher.invoke(request, response);
    // assert
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    DocumentBuilder db = (DocumentBuilderFactory.newInstance()).newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(response.getContentAsString())));
    NodeList returnedQueries = doc.getElementsByTagName("query");
    assertEquals(queries.size(), returnedQueries.getLength());
}
Also used : InputSource(org.xml.sax.InputSource) Query(datawave.webservice.query.Query) DatawaveUser(datawave.security.authorization.DatawaveUser) NodeList(org.w3c.dom.NodeList) ArrayList(java.util.ArrayList) BaseQueryLogic(datawave.webservice.query.logic.BaseQueryLogic) QueryLogic(datawave.webservice.query.logic.QueryLogic) Document(org.w3c.dom.Document) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) QueryImpl(datawave.webservice.query.QueryImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) UUID(java.util.UUID)

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