Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up tests to run with client ID too #7

Merged
merged 4 commits into from
Aug 22, 2014
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ dependencies {
testCompile 'org.apache.httpcomponents:httpclient:4.3.5'
}

test {
useJUnit {
includeCategories 'com.google.maps.SmallTests'
includeCategories 'com.google.maps.MediumTests'
includeCategories 'com.google.maps.LargeTests'
}
}

task updateVersion(type: Copy) {
from('src') into "build/filtered/src"
filter(ReplaceTokens, tokens: [VERSION: project.version])
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/com/google/maps/AuthenticatedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.google.maps;

import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.util.ArrayList;
import java.util.Collection;

/**
* Base class for tests requiring automated authentication. Sub-classes need to implement a 1-arg
* constructor that takes a GeoApiContext that will be supplied with an appropriate API key or
* client ID and secret set.
*/
@RunWith(Parameterized.class) @Ignore
public class AuthenticatedTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Parameterized stuff seems a bit overkill. Can we just call this "BaseSomethingTest" and add a getter for the context with the logic you have already?

Or even just a util class with a static function, if we want to avoid base test classes (better, IMO)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed IRL, I've updated the commentary to describe what's happening.

@Parameters
public static Collection<Object[]> contexts() {
Collection<Object[]> contexts = new ArrayList<>();

if (System.getenv("API_KEY") != null) {
GeoApiContext context = new GeoApiContext()
.setApiKey(System.getenv("API_KEY"));
contexts.add(new Object[]{context});
}

if (!(System.getenv("CLIENT_ID") == null || System.getenv("CLIENT_SECRET") == null)) {
GeoApiContext context = new GeoApiContext()
.setEnterpriseCredentials(System.getenv("CLIENT_ID"), System.getenv("CLIENT_SECRET"));
contexts.add(new Object[]{context});
}

if (contexts.size() == 0) {
throw new IllegalArgumentException("No credentials found! Set the API_KEY or CLIENT_ID and "
+ "CLIENT_SECRET environment variables to run tests requiring authentication.");
}

return contexts;
}

protected AuthenticatedTest() {
}
}
11 changes: 6 additions & 5 deletions src/test/java/com/google/maps/DirectionsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@

import java.util.concurrent.TimeUnit;

// TODO(macd): seperate unit tests from integration tests
@Category(LargeTests.class)
public class DirectionsApiTest {
public class DirectionsApiTest extends AuthenticatedTest {

public static final double EPSILON = 0.000001;
private static GeoApiContext context = new GeoApiContext()
.setApiKey(System.getenv("API_KEY"))
private GeoApiContext context;

public DirectionsApiTest(GeoApiContext context) {
this.context = context
.setQueryRateLimit(3)
.setConnectTimeout(1, TimeUnit.SECONDS)
.setReadTimeout(1, TimeUnit.SECONDS)
.setWriteTimeout(1, TimeUnit.SECONDS);
}

@Test
public void testGetDirections() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@
import com.google.maps.model.Unit;

import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.util.concurrent.TimeUnit;

@Category(LargeTests.class)
public class DistanceMatrixApiIntegrationTest {
public class DistanceMatrixApiIntegrationTest extends AuthenticatedTest {

private GeoApiContext context;

@Before
public void setUp() {
context = new GeoApiContext()
.setApiKey(System.getenv("API_KEY"))
public DistanceMatrixApiIntegrationTest(GeoApiContext context) {
this.context = context
.setConnectTimeout(1, TimeUnit.SECONDS)
.setReadTimeout(1, TimeUnit.SECONDS)
.setWriteTimeout(1, TimeUnit.SECONDS);
Expand Down Expand Up @@ -83,7 +80,7 @@ public void testNewRequestWithAllPossibleParams() throws Exception {
.language("en-AU")
.avoid(RouteRestriction.TOLLS)
.units(Unit.IMPERIAL)
.departureTime(new DateTime().plusDays(5))
.departureTime(new DateTime().plusMinutes(2)) // this is ignored when an API key is used
.await();

assertEquals(8, matrix.rows.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.concurrent.TimeUnit;

@Category(LargeTests.class)
public class ElevationApiIntegrationTest {
public class ElevationApiIntegrationTest extends AuthenticatedTest {

public static final double SYDNEY_ELEVATION = 19.11174774169922;
public static final double SYDNEY_POINT_ELEVATION = 19.10829925537109;
Expand All @@ -54,12 +54,15 @@ public class ElevationApiIntegrationTest {
+ "hhBlxErfCf{BtsCjpEjtD|}Aja@xnAbdDt|ErMrdFh{CzgAnlCnr@`wEM~mE`bA`uD|MlwKxmBvuFlhB|sN`_@fvB"
+ "p`CxhCt_@loDsS|eDlmChgFlqCbjCxk@vbGxmCjbMba@rpBaoClcCk_DhgEzYdzBl\\vsA_JfGztAbShkGtEhlDzh"
+ "C~w@hnB{e@yF}`D`_Ayx@~vGqn@l}CafC");
private static GeoApiContext context = new GeoApiContext()
.setApiKey(System.getenv("API_KEY"))
private GeoApiContext context;

public ElevationApiIntegrationTest(GeoApiContext context) {
this.context = context
.setQueryRateLimit(3)
.setConnectTimeout(1, TimeUnit.SECONDS)
.setReadTimeout(1, TimeUnit.SECONDS)
.setWriteTimeout(1, TimeUnit.SECONDS);
}

@Test
public void testGetPoint() throws Exception {
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/google/maps/GeocodingApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@
import java.util.concurrent.TimeUnit;

@Category(LargeTests.class)
public class GeocodingApiTest {
public class GeocodingApiTest extends AuthenticatedTest {

public static final double EPSILON = 0.000001;
private static GeoApiContext context = new GeoApiContext()
.setApiKey(System.getenv("API_KEY"))

private GeoApiContext context;

public GeocodingApiTest(GeoApiContext context) {
this.context = context
.setQueryRateLimit(3)
.setConnectTimeout(1, TimeUnit.SECONDS)
.setReadTimeout(1, TimeUnit.SECONDS)
.setWriteTimeout(1, TimeUnit.SECONDS);
}

@Test
public void testSimpleGeocode() throws Exception {
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/com/google/maps/TimeZoneApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@
import java.util.concurrent.TimeUnit;

@Category(LargeTests.class)
public class TimeZoneApiTest {
public class TimeZoneApiTest extends AuthenticatedTest {

private static GeoApiContext context = new GeoApiContext()
.setApiKey(System.getenv("API_KEY"))
.setQueryRateLimit(3)
private GeoApiContext context;

private LatLng sydney;

public TimeZoneApiTest(GeoApiContext context) {
this.context = context.setQueryRateLimit(3)
.setConnectTimeout(1, TimeUnit.SECONDS)
.setReadTimeout(1, TimeUnit.SECONDS)
.setWriteTimeout(1, TimeUnit.SECONDS);

private LatLng sydney;
}

@Before
public void setUp() throws Exception {
Expand Down