/*
 * Copyright 2008-2013 Microarray Informatics Team, EMBL-European Bioinformatics Institute
 *
 * 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.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.atlas.search.diffanalytics;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import oracle.sql.ARRAY;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import uk.ac.ebi.atlas.solr.query.conditions.IndexedAssayGroup;

import javax.inject.Inject;
import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:applicationContext.xml", "classpath:solrContextIT.xml", "classpath:oracleContext.xml"})
public class DatabaseQueryPrinterIT {

    @Inject
    DiffAnalyticsDao diffAnalyticsDao;

    @Test
    public void oracleArrayToString() throws Exception {
        ARRAY oracleArray = diffAnalyticsDao.createOracleArrayForIdentifiers(ImmutableList.of("A", "B", "C", "D", "E"));
        assertThat(DatabaseQueryPrinter.oracleArrayToString(oracleArray), is("IDENTIFIERS_TABLE('A', 'B', 'C', 'D', 'E')"));
    }

    @Test
    public void printDatabaseQueryWithGeneIds() throws Exception {
        IndexedAssayGroup iag1 = new IndexedAssayGroup("EXP1", "G1");
        DatabaseQuery<Object> databaseQuery = diffAnalyticsDao.buildSelect(Optional.of(Collections.singleton(iag1)), Optional.of(ImmutableList.of("A", "B", "C", "D", "E")));

        assertThat(databaseQuery.print(), is("SELECT IDENTIFIER, NAME, ORGANISM, EXPERIMENT, CONTRASTID, PVAL, LOG2FOLD, TSTAT FROM VW_DIFFANALYTICS JOIN EXPERIMENT on VW_DIFFANALYTICS.EXPERIMENT = EXPERIMENT.ACCESSION AND PRIVATE = 'F' JOIN TABLE(IDENTIFIERS_TABLE('A', 'B', 'C', 'D', 'E')) identifiersTable ON IDENTIFIER = identifiersTable.column_value JOIN TABLE(EXPR_CONTRAST_TABLE('oracle.sql.STRUCT@430d9b69')) exprContrast ON VW_DIFFANALYTICS.EXPERIMENT = exprContrast.EXPERIMENT AND VW_DIFFANALYTICS.CONTRASTID = exprContrast.CONTRASTID order by abs(LOG2FOLD) desc"));
    }

}

@@ -1347,17 +1347,16 @@
 llection
-s
 ;%0A%0Aimpor
@@ -2135,16 +2135,18 @@
         
+//
 IndexedA
@@ -2283,19 +2283,17 @@
 ptional.
-of(
+%3C
 Collecti
@@ -2298,25 +2298,35 @@
 tion
-s.singleton(iag1)
+%3CIndexedAssayGroup%3E%3Eabsent(
 ), O
@@ -2741,198 +2741,8 @@
 lue 
-JOIN TABLE(EXPR_CONTRAST_TABLE('oracle.sql.STRUCT@430d9b69')) exprContrast ON VW_DIFFANALYTICS.EXPERIMENT = exprContrast.EXPERIMENT AND VW_DIFFANALYTICS.CONTRASTID = exprContrast.CONTRASTID 
 orde

fix tests from change to use oracle array for contrasts