Skip to content

Commit

Permalink
Migrate test suite to end-to-end tests (#35)
Browse files Browse the repository at this point in the history
* Fix interaction between assertions and server thread

We can't throw assertion errors without affecting the server's stability, which means we need to move the expectations outside the stubbing of the fixtures.

This commit does all changes necessary and adapts all tests by first setting the server's behavior and then providing means to ask about what's happened.

Everything related to the expected request that the API should have done is made through a new object of type RecordedRequest that the server provides. Everything related to the response must be tested through the actual output of the client call, which supports the idea of these tests being end-to-end.
  • Loading branch information
ggalmazor committed May 27, 2020
1 parent 8eff5a1 commit 6c7a64d
Show file tree
Hide file tree
Showing 151 changed files with 1,921 additions and 2,380 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public HttpEndpointClient() {

/**
* Set the underlying transport mechanism.
*
* <p>
* This method is primarily used to specify a mocked transport layer during testing.
*
* @param transport The transport instance
Expand Down Expand Up @@ -123,7 +123,7 @@ protected HttpResponse delete(String path, Map<String, Object> options) throws D
protected HttpResponse request(String method, String url, Object data, Map<String, Object> options) throws DnsimpleException, IOException {
HttpContent content = null;
if (data != null) {
content = new JsonHttpContent(new GsonFactory(), data);
content = new JsonHttpContent(new GsonFactory(), data);
}

HttpRequest request = transport.createRequestFactory().buildRequest(method, buildUrl(url, options), content);
Expand All @@ -148,7 +148,7 @@ protected HttpResponse request(String method, String url, Object data, Map<Strin

try {
return request.execute();
} catch(HttpResponseException e) {
} catch (HttpResponseException e) {
throw DnsimpleException.transformException(e);
}
}
Expand All @@ -157,7 +157,7 @@ protected HttpResponse request(String method, String url, Object data, Map<Strin
* Parse the response from the HTTP call into an instance of the given class.
*
* @param response The parsed response object
* @param c The class to instantiate and use to build the response object
* @param c The class to instantiate and use to build the response object
* @return The ApiResponse object
* @throws IOException Any IO errors
*/
Expand Down Expand Up @@ -189,7 +189,7 @@ private ApiResponse buildTypeSafeApiResponse(Class<?> c) {
}

private String versionedPath(String path) {
return Dnsimple.getApiBase() + API_VERSION_PATH + path;
return Dnsimple.getApiBase() + API_VERSION_PATH + path;
}

private GenericUrl buildUrl(String url, Map<String, Object> options) {
Expand All @@ -199,7 +199,7 @@ private GenericUrl buildUrl(String url, Map<String, Object> options) {

UrlBuilder urlBuilder = UrlBuilder.fromString(url);
if (options.containsKey("filter")) {
Filter filter = (Filter)options.remove("filter");
Filter filter = (Filter) options.remove("filter");
urlBuilder = urlBuilder.addParameter(filter.name, filter.value);
}

Expand Down
24 changes: 11 additions & 13 deletions src/test/java/com/dnsimple/AccountsTest.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package com.dnsimple;

import static com.dnsimple.tools.HttpMethod.GET;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;

import com.dnsimple.data.Account;
import com.dnsimple.response.ListAccountsResponse;
import com.dnsimple.exception.DnsimpleException;

import com.dnsimple.response.ListAccountsResponse;
import java.io.IOException;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpMethods;

import static org.junit.Assert.assertEquals;

public class AccountsTest extends DnsimpleTestBase {

@Test
public void testListAccounts() throws DnsimpleException, IOException {
Client client = mockAndExpectClient("https://api.dnsimple.com/v2/accounts", HttpMethods.GET, resource("listAccounts/success-account.http"));
server.stubFixtureAt("listAccounts/success-account.http");

ListAccountsResponse response = client.accounts.listAccounts();
assertThat(server.getRecordedRequest().getMethod(), is(GET));
assertThat(server.getRecordedRequest().getPath(), is("/v2/accounts"));

List<Account> accounts = response.getData();
assertEquals(1, accounts.size());
assertEquals(123, accounts.get(0).getId().intValue());
assertThat(accounts, hasSize(1));
assertThat(accounts.get(0).getId(), is(1324));
}

}
328 changes: 195 additions & 133 deletions src/test/java/com/dnsimple/CertificatesTest.java

Large diffs are not rendered by default.

39 changes: 18 additions & 21 deletions src/test/java/com/dnsimple/ClientTest.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
package com.dnsimple;

import com.dnsimple.exception.DnsimpleException;

import junit.framework.Assert;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpMethods;
import static com.dnsimple.tools.HttpMethod.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;

import com.dnsimple.exception.DnsimpleException;
import com.dnsimple.tools.HttpMethod;
import java.io.IOException;
import org.junit.Test;

public class ClientTest extends DnsimpleTestBase {
@Test
public void testNewClient() {
Client client = new Client();
}
private static final String TEST_ACCESS_TOKEN = "test-access-token";

@Test
public void testAuthorizationHeader() throws DnsimpleException, IOException {
HttpHeaders headers = getDefaultHeaders();
headers.setAuthorization("Bearer " + TEST_ACCESS_TOKEN);
server.stubFixtureAt("listAccounts/success-account.http");
client.setAccessToken(TEST_ACCESS_TOKEN);

Client client = mockAndExpectClient("https://api.dnsimple.com/v2/accounts", HttpMethods.GET, headers, null, resource("listAccounts/success-account.http"));
client.accounts.listAccounts();
assertThat(server.getRecordedRequest().getMethod(), is(GET));
assertThat(server.getRecordedRequest().getPath(), is("/v2/accounts"));
assertThat(server.getRecordedRequest().getHeaders(), hasEntry("Authorization", "Bearer " + TEST_ACCESS_TOKEN));
}

@Test
public void testUserAgentHeader() throws DnsimpleException, IOException {
HttpHeaders headers = getDefaultHeaders();
headers.setUserAgent("my-user-agent dnsimple-java/" + Dnsimple.VERSION + " Google-HTTP-Java-Client/1.35.0 (gzip)");

Client client = mockAndExpectClient("https://api.dnsimple.com/v2/accounts", HttpMethods.GET, headers, null, resource("listAccounts/success-account.http"));
server.stubFixtureAt("listAccounts/success-account.http");
client.setUserAgent("my-user-agent");

client.accounts.listAccounts();
assertThat(server.getRecordedRequest().getMethod(), is(GET));
assertThat(server.getRecordedRequest().getPath(), is("/v2/accounts"));
assertThat(server.getRecordedRequest().getHeaders(), hasEntry("User-Agent", "my-user-agent dnsimple-java/" + Dnsimple.VERSION + " Google-HTTP-Java-Client/1.35.0 (gzip)"));
}
}
Loading

0 comments on commit 6c7a64d

Please sign in to comment.