Search in sources :

Example 51 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class PreparationClientTest method getDetails.

/**
 * Return the details of a preparation at a given (optional) step.
 *
 * @param preparationId the wanted preparation id.
 * @param wantedStepId the optional wanted step id.
 * @return the details of a preparation at a given (optional) step.
 */
public PreparationMessage getDetails(String preparationId, String wantedStepId) {
    final RequestSpecification specs = given();
    if (StringUtils.isNotBlank(wantedStepId)) {
        specs.queryParam("stepId", wantedStepId);
    }
    final Response response = specs.when().get("/preparations/{id}/details", preparationId);
    if (response.getStatusCode() != 200) {
        throw new MockTDPException(response);
    }
    try {
        return mapper.readerFor(PreparationMessage.class).readValue(response.asString());
    } catch (IOException e) {
        throw new TDPException(UNABLE_TO_READ_CONTENT);
    }
}
Also used : Response(com.jayway.restassured.response.Response) TDPException(org.talend.dataprep.exception.TDPException) MockTDPException(org.talend.dataprep.test.MockTDPException) MockTDPException(org.talend.dataprep.test.MockTDPException) RequestSpecification(com.jayway.restassured.specification.RequestSpecification) PreparationMessage(org.talend.dataprep.api.preparation.PreparationMessage) IOException(java.io.IOException)

Example 52 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class JsonWriter method writeRowMetadataObject.

private void writeRowMetadataObject(RowMetadata rowMetadata) throws IOException {
    generator.writeFieldName(METADATA_FIELD_NAME);
    generator.writeStartObject();
    generator.writeFieldName(METADATA_COLUMNS_FIELD_NAME);
    generator.writeStartArray();
    rowMetadata.getColumns().forEach(col -> {
        try {
            generator.writeObject(col);
        } catch (IOException e) {
            try {
                // try to close JSon object before throwing an exception
                generator.writeEndArray();
                generator.writeEndObject();
                closeRootObject();
            } catch (IOException e1) {
                LOGGER.debug("Could not close JSon object after columns writing error.", e1);
            }
            throw new TDPException(CommonErrorCodes.UNABLE_TO_WRITE_JSON, e);
        }
    });
    generator.writeEndArray();
    generator.writeEndObject();
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) IOException(java.io.IOException)

Example 53 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class BaseExportStrategy method getActions.

/**
 * Returns the actions for the preparation with <code>preparationId</code> between <code>startStepId</code> and
 * <code>endStepId</code>.
 *
 * @param preparationId The preparation id, if <code>null</code> or blank, returns <code>{actions: []}</code>
 * @param startStepId A step id that must exist in given preparation id.
 * @param endStepId A step id that must exist in given preparation id.
 * @return The actions that can be parsed by ActionParser.
 * @see org.talend.dataprep.transformation.api.action.ActionParser
 */
protected String getActions(String preparationId, String startStepId, String endStepId) {
    if (Step.ROOT_STEP.id().equals(startStepId)) {
        return getActions(preparationId, endStepId);
    }
    String actions;
    if (StringUtils.isBlank(preparationId)) {
        actions = "{\"actions\": []}";
    } else {
        try {
            final PreparationGetActions startStepActions = applicationContext.getBean(PreparationGetActions.class, preparationId, startStepId);
            final PreparationGetActions endStepActions = applicationContext.getBean(PreparationGetActions.class, preparationId, endStepId);
            final StringWriter actionsAsString = new StringWriter();
            final Action[] startActions = mapper.readValue(startStepActions.execute(), Action[].class);
            final Action[] endActions = mapper.readValue(endStepActions.execute(), Action[].class);
            if (endActions.length > startActions.length) {
                final Action[] filteredActions = (Action[]) ArrayUtils.subarray(endActions, startActions.length, endActions.length);
                LOGGER.debug("Reduced actions list from {} to {} action(s)", endActions.length, filteredActions.length);
                mapper.writeValue(actionsAsString, filteredActions);
            } else {
                LOGGER.debug("Unable to reduce list of actions (has {})", endActions.length);
                mapper.writeValue(actionsAsString, endActions);
            }
            return "{\"actions\": " + actionsAsString + '}';
        } catch (IOException e) {
            final ExceptionContext context = ExceptionContext.build().put("id", preparationId).put("version", endStepId);
            throw new TDPException(UNABLE_TO_READ_PREPARATION, e, context);
        }
    }
    return actions;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Action(org.talend.dataprep.api.preparation.Action) StringWriter(java.io.StringWriter) ExceptionContext(org.talend.daikon.exception.ExceptionContext) PreparationGetActions(org.talend.dataprep.command.preparation.PreparationGetActions) IOException(java.io.IOException)

Example 54 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class AggregationService method aggregate.

/**
 * Process an aggregation.
 *
 * @param parameters the aggregation parameters.
 * @param dataset the dataset input.
 * @return the aggregation result.
 */
public AggregationResult aggregate(AggregationParameters parameters, DataSet dataset) {
    // check the parameters
    if (parameters.getOperations().isEmpty() || parameters.getGroupBy().isEmpty()) {
        throw new TDPException(CommonErrorCodes.BAD_AGGREGATION_PARAMETERS);
    }
    AggregationResult result = new AggregationResult(parameters.getOperations().get(0).getOperator());
    // get the aggregator
    Aggregator aggregator = factory.get(parameters);
    // Build optional filter
    final DataSetMetadata metadata = dataset.getMetadata();
    final RowMetadata rowMetadata = metadata != null ? metadata.getRowMetadata() : new RowMetadata();
    final Predicate<DataSetRow> filter = filterService.build(parameters.getFilter(), rowMetadata);
    // process the dataset
    dataset.getRecords().filter(filter).forEach(row -> aggregator.accept(row, result));
    // Normalize result (perform clean / optimization now that all input was processed).
    aggregator.normalize(result);
    return result;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) AggregationResult(org.talend.dataprep.transformation.aggregation.api.AggregationResult) Aggregator(org.talend.dataprep.transformation.aggregation.operation.Aggregator) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow)

Example 55 with TDPException

use of org.talend.dataprep.exception.TDPException in project data-prep by Talend.

the class DataSetServiceTest method createShouldFailBecauseNotEnoughSpaceAvailable.

@Test
public void createShouldFailBecauseNotEnoughSpaceAvailable() throws Exception {
    // given
    Mockito.reset(quotaService);
    TDPException exception = new TDPException(DataSetErrorCodes.MAX_STORAGE_MAY_BE_EXCEEDED);
    doThrow(exception).when(quotaService).checkIfAddingSizeExceedsAvailableStorage(500L);
    // when
    final InputStream content = this.getClass().getResourceAsStream(T_SHIRT_100_CSV);
    final Response post = // 
    given().body(// 
    IOUtils.toString(content, UTF_8)).queryParam("Content-Type", // 
    "text/csv").queryParam("name", // 
    "cespasfaux").queryParam("size", // 
    500L).post("/datasets");
    // then
    assertEquals(413, post.getStatusCode());
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Response(com.jayway.restassured.response.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DataSetBaseTest(org.talend.dataprep.dataset.DataSetBaseTest) Test(org.junit.Test)

Aggregations

TDPException (org.talend.dataprep.exception.TDPException)123 IOException (java.io.IOException)43 InputStream (java.io.InputStream)25 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)21 Test (org.junit.Test)17 ApiOperation (io.swagger.annotations.ApiOperation)16 Timed (org.talend.dataprep.metrics.Timed)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 DataSet (org.talend.dataprep.api.dataset.DataSet)13 ServiceBaseTest (org.talend.ServiceBaseTest)11 StringEntity (org.apache.http.entity.StringEntity)10 JsonParser (com.fasterxml.jackson.core.JsonParser)9 URISyntaxException (java.net.URISyntaxException)9 HttpPost (org.apache.http.client.methods.HttpPost)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 ColumnMetadata (org.talend.dataprep.api.dataset.ColumnMetadata)9 List (java.util.List)8 URIBuilder (org.apache.http.client.utils.URIBuilder)8 Marker (org.slf4j.Marker)8 ErrorCode (org.talend.daikon.exception.error.ErrorCode)8