package org.ga4gh.cts.api.variants;

import junitparams.JUnitParamsRunner;
import org.apache.avro.AvroRemoteException;
import org.ga4gh.ctk.transport.URLMAPPING;
import org.ga4gh.ctk.transport.protocols.Client;
import org.ga4gh.cts.api.TestData;
import org.ga4gh.methods.*;
import org.ga4gh.models.Variant;
import org.ga4gh.models.VariantSet;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Tests for <tt>GET /variants/{id}</tt>.
 *
 * @author Herb Jellinek
 */
@Category(VariantsTests.class)
@RunWith(JUnitParamsRunner.class)
public class VariantsGetByIdIT {

    private static Client client = new Client(URLMAPPING.getInstance());

    /**
     * Verify that Variants that we obtain by way of {@link SearchVariantsRequest} match the ones
     * we get via <tt>GET /variants/{id}</tt>.
     * @throws AvroRemoteException if there's a communication problem or server exception ({@link GAException})
     */
    @Test
    public void checkVariantsGetResultsMatchSearchResults() throws AvroRemoteException {
        final long start = 50;
        final long end = 100;
        final int expectedNumberOfVariants = 6;

        // first get a variant set ID
        final SearchVariantSetsRequest searchVariantSetsReq =
                SearchVariantSetsRequest.newBuilder()
                                        .setDatasetId(TestData.getDatasetId())
                                        .build();
        final SearchVariantSetsResponse searchVariantSetsResp =
                client.variants.searchVariantSets(searchVariantSetsReq);

        final List<VariantSet> variantSets = searchVariantSetsResp.getVariantSets();
        assertThat(variantSets).isNotEmpty();
        final String variantSetId = variantSets.get(0).getId();

        // then gather the variants that are part of the VariantSet with that ID
        final SearchVariantsRequest req =
                SearchVariantsRequest.newBuilder()
                                     .setVariantSetId(variantSetId)
                                     .setReferenceName(TestData.REFERENCE_NAME)
                                     .setStart(start)
                                     .setEnd(end)
                                     .build();
        final SearchVariantsResponse resp = client.variants.searchVariants(req);

        final List<Variant> variants = resp.getVariants();
        assertThat(variants).isNotEmpty();

        for (final Variant variantFromSearch : variants) {
            final Variant variantFromGet = client.variants.getVariant(variantFromSearch.getId());
            assertThat(variantFromGet).isNotNull();

            // XXX fails due to server issue #619
            assertThat(variantFromGet).isEqualTo(variantFromSearch);
        }

    }


}

@@ -229,24 +229,21 @@
 cts.api.
-TestData
+Utils
 ;%0Aimport
@@ -261,17 +261,27 @@
 methods.
-*
+GAException
 ;%0Aimport
@@ -292,28 +292,43 @@
 .ga4gh.m
-odels.Varian
+ethods.SearchVariantsReques
 t;%0Aimpor
@@ -353,19 +353,16 @@
 .Variant
-Set
 ;%0Aimport
@@ -1291,421 +1291,67 @@
     
-// first get a variant set ID%0A        final SearchVariantSetsRequest searchVariantSetsReq =%0A                SearchVariantSetsRequest.newBuilder()%0A                                        .setDatasetId(TestData.getDatasetId())%0A                                        .build();%0A        final SearchVariantSetsResponse searchVariantSetsResp =%0A                client.variants.searchVariantSets(searchVariantSetsReq
+final String variantSetId = Utils.getVariantSetId(client
 );%0A
-%0A
     
@@ -1372,19 +1372,16 @@
 %3CVariant
-Set
 %3E varian
@@ -1385,820 +1385,137 @@
 iant
-Set
 s = 
-searchVariantSetsResp
+Utils
 .get
+All
 Variant
-Sets();%0A        assertThat(variantSets).isNotEmpty();%0A        final String variantSetId = variantSets.get(0).getId();%0A%0A        // then gather the variants that are part of the VariantSet with that ID%0A        final SearchVariantsRequest req =%0A                SearchVariantsRequest.newBuilder()%0A                                     .setVariantSetId(variantSetId)%0A                                     .setReferenceName(TestData.REFERENCE_NAME)%0A                                     .setStart(start)%0A                                     .setEnd(end)%0A                                     .build();%0A        final SearchVariantsResponse resp = client.variants.searchVariants(req);%0A%0A        final List%3CVariant%3E variants = resp.getVariants();%0A        assertThat(variants).isNotEmpty(
+sInRange(client, variantSetId, start, end);%0A%0A        assertThat(variants).hasSize(expectedNumberOfVariants
 );%0A%0A

Use Utils.getVariantSetId(client) and getAllVariantsInRange to make the test more clear.