/*
 * Copyright 2015 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.frontend.find.core.export.service;

import com.hp.autonomy.searchcomponents.core.search.QueryRequest;
import com.hp.autonomy.searchcomponents.core.search.QueryRequestBuilder;
import com.hp.autonomy.searchcomponents.core.search.QueryRestrictions;
import com.hp.autonomy.searchcomponents.core.test.TestUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.Collections;

import static org.junit.Assert.assertNotNull;

@SuppressWarnings("SpringJavaAutowiredMembersInspection")
@RunWith(SpringRunner.class)
@JsonTest
@AutoConfigureJsonTesters(enabled = false)
public abstract class PlatformDataExportServiceIT<R extends QueryRequest<Q>, Q extends QueryRestrictions<?>, E extends Exception> {
    @Autowired
    private PlatformDataExportService<R, E> exportService;

    @Autowired
    private TestUtils<Q> testUtils;

    @Autowired
    private ObjectFactory<QueryRequestBuilder<R, Q, ?>> queryRequestBuilderFactory;

    @Test
    public void exportToCsv() throws E, IOException {
        final R queryRequest = queryRequestBuilderFactory.getObject()
                .queryRestrictions(testUtils.buildQueryRestrictions())
                .queryType(QueryRequest.QueryType.MODIFIED)
                .build();

        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        exportService.exportQueryResults(outputStream, queryRequest, ExportFormat.CSV, Collections.emptyList(), 1001L);
        assertNotNull(outputStream.toString());
    }
}

@@ -508,16 +508,139 @@
 tUtils;%0A
+import org.apache.commons.csv.CSVFormat;%0Aimport org.apache.commons.csv.CSVParser;%0Aimport org.apache.commons.csv.CSVRecord;%0A
 import o
@@ -1137,53 +1137,194 @@
 ns;%0A
-%0Aimport static org.junit.Assert.assertNotNull
+import java.util.List;%0A%0Aimport static org.hamcrest.Matchers.empty;%0Aimport static org.hamcrest.Matchers.endsWith;%0Aimport static org.hamcrest.Matchers.not;%0Aimport static org.junit.Assert.*
 ;%0A%0A@
@@ -2325,47 +2325,921 @@
     
-assertNotNull(outputStream.toString());
+final String output = outputStream.toString();%0A        assertNotNull(output);%0A%0A        try (final CSVParser csvParser = CSVParser.parse(output, CSVFormat.EXCEL)) %7B%0A            final List%3CCSVRecord%3E records = csvParser.getRecords();%0A            assertThat(records, not(empty()));%0A            final CSVRecord headerRecord = records.get(0);%0A            assertThat(headerRecord.get(0), endsWith(%22Reference%22)); // byte-order mark may get in the way%0A            assertEquals(%22Database%22, headerRecord.get(1));%0A            final CSVRecord firstDataRecord = records.get(1);%0A            final String firstDataRecordReference = firstDataRecord.get(0);%0A            assertNotNull(firstDataRecordReference);%0A            assertFalse(firstDataRecordReference.trim().isEmpty());%0A            final String firstDataRecordDatabase = firstDataRecord.get(1);%0A            assertFalse(firstDataRecordDatabase.trim().isEmpty());%0A        %7D
 %0A   

Make service integration test for field export more rigorous (FIND-934)