Search in sources :

Example 26 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testExecute_InvalidMediaType.

@Test
public void testExecute_InvalidMediaType() 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;
    String systemFrom = "systemFrom";
    String dataSetType = "dataSetType";
    String purpose = "purpose";
    String parentAuditId = "parentAuditId";
    UUID queryId = UUID.randomUUID();
    List<MediaType> mediaTypes = new ArrayList<>();
    mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM_TYPE);
    GenericResponse<String> createResponse = new GenericResponse<>();
    createResponse.setResult(queryId.toString());
    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).anyTimes();
    // 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 = null;
    try {
        MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl<>();
        queryParameters.putAll(QueryParametersImpl.paramsToMap(queryLogicName, query, queryName, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace));
        result1 = subject.execute(queryLogicName, queryParameters, httpHeaders);
        fail("Should have failed due to unsupported media type");
    } catch (DatawaveWebApplicationException e) {
        if (!(((QueryException) e.getCause()).getErrorCode().equals("500-13"))) {
            fail("Unknown exception type: " + e.getCause());
        }
    }
    PowerMock.verifyAll();
    // Verify results
    assertNull("Expected a non-null response", result1);
}
Also used : GenericResponse(datawave.webservice.result.GenericResponse) ArrayList(java.util.ArrayList) StreamingOutput(javax.ws.rs.core.StreamingOutput) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) Date(java.util.Date) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryPersistence(datawave.webservice.query.QueryPersistence) MediaType(javax.ws.rs.core.MediaType) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 27 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testNext_UserNotOwner.

@Test
public void testNext_UserNotOwner() throws Exception {
    // Set local test input
    String userName = "userName";
    String userSid = "userSid";
    String otherSid = "otherSid";
    UUID queryId = UUID.randomUUID();
    // Set expectations
    expect(this.context.getCallerPrincipal()).andReturn(this.principal).anyTimes();
    expect(this.principal.getName()).andReturn(userName);
    expect(this.principal.getShortName()).andReturn(otherSid);
    expect(this.principal.getProxyServers()).andReturn(new ArrayList<>(0));
    expect(this.context.getUserTransaction()).andReturn(this.transaction).anyTimes();
    this.transaction.begin();
    expect(this.cache.get(queryId.toString())).andReturn(this.runningQuery);
    expect(this.cache.lock(queryId.toString())).andReturn(true);
    expect(this.responseObjectFactory.getEventQueryResponse()).andReturn(new DefaultEventQueryResponse());
    expect(this.runningQuery.getConnection()).andReturn(this.connector);
    expect(this.runningQuery.getSettings()).andReturn(this.query);
    expect(this.query.getOwner()).andReturn(userSid);
    expect(this.runningQuery.getSettings()).andReturn(this.query);
    expect(this.query.getOwner()).andReturn(userSid);
    cache.unlock(queryId.toString());
    expect(this.transaction.getStatus()).andReturn(Status.STATUS_PREPARING).times(2);
    this.transaction.setRollbackOnly();
    this.transaction.commit();
    this.runningQuery.setActiveCall(false);
    expect(this.runningQuery.getLogic()).andReturn((QueryLogic) this.queryLogic1);
    expect(this.queryLogic1.getCollectQueryMetrics()).andReturn(true);
    expect(this.runningQuery.getMetric()).andReturn(this.queryMetric).times(2);
    expectLastCall();
    this.queryMetric.setError(isA(Throwable.class));
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, QueryCache.class, cache);
    setInternalState(subject, ClosedQueryCache.class, closedCache);
    setInternalState(subject, ResponseObjectFactory.class, responseObjectFactory);
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    Exception result1 = null;
    try {
        subject.next(queryId.toString());
    } catch (DatawaveWebApplicationException e) {
        result1 = e;
        assertTrue(e.getCause() instanceof QueryException);
        assertEquals("401-1", ((QueryException) e.getCause().getCause()).getErrorCode());
    }
    PowerMock.verifyAll();
    // Verify results
    assertNotNull("Expected a DatawaveWebApplicationException to be thrown due an unchecked exception", result1);
}
Also used : NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) DefaultEventQueryResponse(datawave.webservice.result.DefaultEventQueryResponse) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) UUID(java.util.UUID) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) JMSRuntimeException(javax.jms.JMSRuntimeException) HeuristicMixedException(javax.transaction.HeuristicMixedException) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) IOException(java.io.IOException) QueryException(datawave.webservice.query.exception.QueryException) BadRequestException(datawave.webservice.common.exception.BadRequestException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NoResultsException(datawave.webservice.common.exception.NoResultsException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 28 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testCreateQueryAndNext_UndefinedQueryLogic.

@Test
public void testCreateQueryAndNext_UndefinedQueryLogic() 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 + 999999);
    int pagesize = 1;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = null;
    boolean trace = false;
    // Set expectations
    expect(context.getCallerPrincipal()).andReturn(principal);
    expect(this.queryLogicFactory.getQueryLogic(queryLogicName, principal)).andThrow(new IllegalArgumentException("INTENTIONALLY THROWN TEST EXCEPTION: UNDEFINED QUERY LOGIC"));
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, QueryLogicFactory.class, queryLogicFactory);
    setInternalState(subject, EJBContext.class, context);
    setInternalState(subject, QueryExpirationConfiguration.class, queryExpirationConf);
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    Throwable result1 = null;
    try {
        MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl<>();
        queryParameters.putAll(QueryParametersImpl.paramsToMap(queryLogicName, query, queryName, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace));
        subject.createQueryAndNext(queryLogicName, queryParameters);
    } catch (DatawaveWebApplicationException e) {
        result1 = e.getCause();
    }
    PowerMock.verifyAll();
    // Verify results
    assertTrue("QueryException expected to have been thrown", result1 instanceof QueryException);
    assertTrue("Thrown exception expected to have been due to undefined query logic", result1.getCause().getMessage().toLowerCase().contains("undefined query logic"));
}
Also used : MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) NoResultsQueryException(datawave.webservice.query.exception.NoResultsQueryException) QueryException(datawave.webservice.query.exception.QueryException) QueryPersistence(datawave.webservice.query.QueryPersistence) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testDefineQuery_InvalidPageSize.

@Test
public void testDefineQuery_InvalidPageSize() 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);
    int pagesize = 0;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = null;
    boolean trace = false;
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    Throwable result1 = null;
    try {
        MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl<>();
        queryParameters.putAll(QueryParametersImpl.paramsToMap(queryLogicName, query, queryName, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace));
        subject.defineQuery(queryLogicName, queryParameters);
    } catch (DatawaveWebApplicationException e) {
        result1 = e;
    }
    PowerMock.verifyAll();
    // Verify results
    assertTrue("BadRequestException expected to have been thrown", result1 instanceof BadRequestException);
    assertEquals("Thrown exception expected to have been due to invalid page size", "400-2", ((QueryException) result1.getCause()).getErrorCode());
}
Also used : MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) QueryPersistence(datawave.webservice.query.QueryPersistence) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) BadRequestException(datawave.webservice.common.exception.BadRequestException) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 30 with DatawaveWebApplicationException

use of datawave.webservice.common.exception.DatawaveWebApplicationException in project datawave by NationalSecurityAgency.

the class ExtendedQueryExecutorBeanTest method testCreateQueryAndNext_InvalidPageSize.

@Test
public void testCreateQueryAndNext_InvalidPageSize() 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);
    int pagesize = 0;
    int pageTimeout = -1;
    Long maxResultsOverride = null;
    QueryPersistence persistenceMode = QueryPersistence.PERSISTENT;
    String parameters = null;
    boolean trace = false;
    MultivaluedMap<String, String> p = new MultivaluedMapImpl<>();
    p.putAll(QueryParametersImpl.paramsToMap(queryLogicName, query, queryName, queryVisibility, beginDate, endDate, queryAuthorizations, expirationDate, pagesize, pageTimeout, maxResultsOverride, persistenceMode, parameters, trace));
    // Run the test
    PowerMock.replayAll();
    QueryExecutorBean subject = new QueryExecutorBean();
    setInternalState(subject, QueryParameters.class, new QueryParametersImpl());
    setInternalState(subject, QueryMetricFactory.class, new QueryMetricFactoryImpl());
    Throwable result1 = null;
    try {
        subject.createQueryAndNext(queryLogicName, p);
    } catch (DatawaveWebApplicationException e) {
        result1 = e;
    }
    PowerMock.verifyAll();
    // Verify results
    assertTrue("BadRequestException expected to have been thrown", result1 instanceof BadRequestException);
    assertEquals("Thrown exception expected to have been due to invalid page size", "400-2", ((QueryException) result1.getCause()).getErrorCode());
}
Also used : MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) QueryParametersImpl(datawave.webservice.query.QueryParametersImpl) Date(java.util.Date) QueryPersistence(datawave.webservice.query.QueryPersistence) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) BadRequestException(datawave.webservice.common.exception.BadRequestException) QueryMetricFactoryImpl(datawave.microservice.querymetric.QueryMetricFactoryImpl) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)72 QueryException (datawave.webservice.query.exception.QueryException)64 NoResultsQueryException (datawave.webservice.query.exception.NoResultsQueryException)48 IOException (java.io.IOException)43 Produces (javax.ws.rs.Produces)43 NoResultsException (datawave.webservice.common.exception.NoResultsException)39 BadRequestQueryException (datawave.webservice.query.exception.BadRequestQueryException)37 UnauthorizedQueryException (datawave.webservice.query.exception.UnauthorizedQueryException)37 NotFoundQueryException (datawave.webservice.query.exception.NotFoundQueryException)36 GZIP (org.jboss.resteasy.annotations.GZIP)34 Interceptors (javax.interceptor.Interceptors)33 BadRequestException (datawave.webservice.common.exception.BadRequestException)32 UnauthorizedException (datawave.webservice.common.exception.UnauthorizedException)32 PreConditionFailedQueryException (datawave.webservice.query.exception.PreConditionFailedQueryException)31 Path (javax.ws.rs.Path)27 DatawavePrincipal (datawave.security.authorization.DatawavePrincipal)26 Principal (java.security.Principal)26 WebApplicationException (javax.ws.rs.WebApplicationException)25 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)24 HeuristicMixedException (javax.transaction.HeuristicMixedException)24