All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.qameta.allure.CommonCsvExportAggregator2 Maven / Gradle / Ivy

There is a newer version: 2.30.0
Show newest version
/*
 *  Copyright 2016-2023 Qameta Software OÜ
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package io.qameta.allure;

import com.opencsv.bean.BeanField;
import com.opencsv.bean.ColumnPositionMappingStrategy;
import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.LaunchResults;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.reflect.FieldUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Csv exporter extension. Can be used to process results in csv file
 *
 * @param  type of result bean.
 * @since 2.0
 */
public abstract class CommonCsvExportAggregator2 implements Aggregator2 {

    private final String fileName;

    private final Class type;

    public CommonCsvExportAggregator2(final String fileName, final Class type) {
        this.fileName = fileName;
        this.type = type;
    }

    @SuppressWarnings("PMD.ExceptionAsFlowControl")
    @Override
    public void aggregate(final Configuration configuration,
                          final List launchesResults,
                          final ReportStorage storage) {
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (Writer writer = new OutputStreamWriter(bos, StandardCharsets.UTF_8)) {
            final StatefulBeanToCsvBuilder builder = new StatefulBeanToCsvBuilder<>(writer);
            final CustomMappingStrategy mappingStrategy = new CustomMappingStrategy<>();
            mappingStrategy.setType(type);
            final StatefulBeanToCsv beanWriter = builder.withMappingStrategy(mappingStrategy).build();
            try {
                beanWriter.write(getData(launchesResults));
            } catch (CsvRequiredFieldEmptyException | CsvDataTypeMismatchException e) {
                throw new IOException(e);
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        storage.addDataBinary(
                Constants.dataPath(fileName),
                bos.toByteArray()
        );
    }

    protected abstract List getData(List launchesResults);

    @SuppressWarnings("all")
    private static final class CustomMappingStrategy extends ColumnPositionMappingStrategy {

        @Override
        public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException {

            super.setColumnMapping(new String[FieldUtils.getAllFields(bean.getClass()).length]);
            final int numColumns = findMaxFieldIndex();
            if (!isAnnotationDriven() || numColumns == -1) {
                return super.generateHeader(bean);
            }

            String[] header = new String[numColumns + 1];

            BeanField beanField;
            for (int i = 0; i <= numColumns; i++) {
                beanField = findField(i);
                String columnHeaderName = extractHeaderName(beanField);
                header[i] = columnHeaderName;
            }
            return header;
        }

        private String extractHeaderName(final BeanField beanField) {
            if (beanField == null || beanField.getField() == null
                || beanField.getField().getDeclaredAnnotationsByType(CsvBindByName.class).length == 0) {
                return StringUtils.EMPTY;
            }

            final CsvBindByName bindByNameAnnotation = beanField.getField()
                    .getDeclaredAnnotationsByType(CsvBindByName.class)[0];
            return bindByNameAnnotation.column();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy