package org.gbif.metrics.ws.client;

import org.gbif.api.model.metrics.cube.OccurrenceCube;
import org.gbif.api.model.metrics.cube.ReadBuilder;
import org.gbif.api.service.metrics.CubeService;
import org.gbif.metrics.ws.client.guice.MetricsWsClientModule;
import org.gbif.metrics.ws.guice.MetricsWsServletListener;
import org.gbif.utils.file.properties.PropertiesUtil;
import org.gbif.ws.client.BaseResourceTest;
import org.gbif.ws.client.guice.UrlBindingModule;

import java.io.IOException;
import java.util.UUID;

import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertTrue;

/**
 * A simple Grizzly based WS IT test 
 */
public class CubeWsClientIT extends BaseResourceTest {
  private static final String PROPERTIES_FILE = "metrics.properties";
  private static final String CONTEXT = "metrics";
  // should this change then we are really in trouble
  private static final int ANIMALIA_KEY = 1;
  private CubeService wsClient; // under test
  
  public CubeWsClientIT() {
    super("org.gbif.metrics.ws.resources", CONTEXT, MetricsWsServletListener.class);
  }
  
  @Before
  public void init() throws IOException {
    // This uses UrlBindingModule to dynamically set the named property (metrics.ws.url) taking into account that the 
    // port might have been set at runtime with system properties (hence the metrics-ws.url is omitted
    // in the properties)
    Injector clientInjector = Guice.createInjector(
      new UrlBindingModule(getBaseURI().toString() + CONTEXT, "metrics.ws.url"),
      new MetricsWsClientModule(PropertiesUtil.loadProperties(PROPERTIES_FILE)));
    wsClient = clientInjector.getInstance(CubeService.class);
  }
  
  /**
   * This simply does lookups to verify we can read something without error.
   * This is an IT of the total wiring, and not intended to check any business logic of the cube.
   */
  @Ignore("Tried to connect to table outside Grizzly that no longer existed throwing TableNotFoundException")
  public void basicLookup() {
    wsClient.get(new ReadBuilder().at(OccurrenceCube.NUB_KEY, ANIMALIA_KEY));
    wsClient.get(new ReadBuilder().at(OccurrenceCube.DATASET_KEY, UUID.randomUUID()));
    wsClient.get(new ReadBuilder());
  }
  
  /**
   * An IT to simply check the scheme can be read, and that some rollups exist.
   * Is not meant to test any business logic.
   */
  @Test
  public void schema() {
    assertTrue("CubeIo schema says no rollups which can't be true", 
      wsClient.getSchema().size() > 0);
  }
}
@@ -734,18 +734,17 @@
  IT test
- 
 %0A
+
  */%0Apubl
@@ -1060,19 +1060,17 @@
 er test%0A
-  
 %0A
+
   public
@@ -1178,19 +1178,17 @@
 s);%0A  %7D%0A
-  
 %0A
+
   @Befor
@@ -1348,17 +1348,16 @@
 that the
- 
 %0A    // 
@@ -1755,27 +1755,25 @@
 class);%0A  %7D%0A
-  
 %0A
+
   /**%0A   * T
@@ -2144,11 +2144,13 @@
 ube.
-NUB
+TAXON
 _KEY
@@ -2299,11 +2299,9 @@
   %7D%0A
-  
 %0A
+
   /*
@@ -2463,24 +2463,24 @@
  schema() %7B%0A
+
     assertTr
@@ -2534,17 +2534,16 @@
 e true%22,
- 
 %0A      w

Fix more tests