package com.spotify.heroic.metric.bigtable;

import com.spotify.heroic.metric.MetricModule;
import com.spotify.heroic.metric.bigtable.credentials.JsonCredentialsBuilder;
import com.spotify.heroic.test.AbstractMetricBackendIT;
import com.spotify.heroic.test.TestProperties;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.UUID;

public class BigtableBackendIT extends AbstractMetricBackendIT {
    private final TestProperties properties = TestProperties.ofPrefix("it.bigtable");

    @Override
    protected Optional<Long> period() {
        return Optional.of(BigtableBackend.PERIOD);
    }

    @Override
    protected void setupSupport() {
        super.setupSupport();

        this.eventSupport = true;
        this.maxBatchSize = Optional.of(BigtableBackend.MAX_BATCH_SIZE);
        this.brokenSegmentsPr208 = true;
    }

    @Override
    public Optional<MetricModule> setupModule() {
        final String table = "heroic_it_" + UUID.randomUUID();

        final Optional<MetricModule> remote = properties.getOptionalString("remote").map(v -> {
            final String project = properties.getRequiredString("project");
            final String instance = properties.getRequiredString("instance");
            final Path credentials = Paths.get(properties.getRequiredString("credentials"));

            return BigtableMetricModule
                .builder()
                .configure(true)
                .project(project)
                .instance(instance)
                .table(table)
                .credentials(JsonCredentialsBuilder.builder().path(credentials).build())
                .build();
        });

        if (remote.isPresent()) {
            return remote;
        }

        final BigtableMetricModule module = BigtableMetricModule
            .builder()
            .configure(true)
            .project("fake")
            .table(table)
            .fake(true)
            .build();

        return Optional.of(module);
    }
}

@@ -85,16 +85,103 @@
 Module;%0A
+import com.spotify.heroic.metric.bigtable.credentials.ComputeEngineCredentialsBuilder;%0A
 import c
@@ -1334,24 +1334,224 @@
 instance%22);%0A
+            final Optional%3CString%3E credentialsFile = properties.getOptionalString(%22credentials%22);%0A            final CredentialsBuilder credentials;%0A%0A            if (credentialsFile.isPresent()) %7B%0A    
             
@@ -1568,20 +1568,24 @@
  credent
-ials
+ailsPath
  = Paths
@@ -1593,53 +1593,229 @@
 get(
-properties.getRequiredString(%22credentials%22));
+credentialsFile.get());%0A                credentials = JsonCredentialsBuilder.builder().path(credentailsPath).build();%0A            %7D else %7B%0A                credentials = new ComputeEngineCredentialsBuilder();%0A            %7D
 %0A%0A  
@@ -2045,46 +2045,8 @@
 als(
-JsonCredentialsBuilder.builder().path(
 cred
@@ -2057,17 +2057,8 @@
 als)
-.build())
 %0A   

Use default credentials if none specified