Skip to content

Commit

Permalink
Add tests for WasapiClient and WasapiConnection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Ingulfsen committed May 19, 2017
1 parent 87cb0ae commit 33674a8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/edu/stanford/dlss/was/TestWasapiClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package edu.stanford.dlss.was;

import java.io.IOException;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.*;
import static org.mockito.Mockito.*;

public class TestWasapiClient {
@Test
public void constructorInitializesCorrectly() throws IOException, SettingsLoadException {
WasapiDownloaderSettings settings = new WasapiDownloaderSettings(WasapiDownloader.SETTINGS_FILE_LOCATION, null);
WasapiClient testClient = new WasapiClient(settings);

assertNotNull(testClient.wasapiClient);
assertNotNull(testClient.wasapiContext);
assertNotNull(testClient.cookieStore);
assertEquals(testClient.settings, settings);
}

@Test
public void closeCloses() throws IOException, SettingsLoadException {
WasapiClient testClient = new WasapiClient(new WasapiDownloaderSettings(WasapiDownloader.SETTINGS_FILE_LOCATION, null));
CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class);
testClient.wasapiClient = mockHttpClient;

testClient.close();

verify(mockHttpClient, times(1)).close();
}

@Test
public void executeUsesContext() throws IOException, SettingsLoadException {
WasapiClient testClient = new WasapiClient(new WasapiDownloaderSettings(WasapiDownloader.SETTINGS_FILE_LOCATION, null));
CloseableHttpClient mockHttpClient = mock(CloseableHttpClient.class);
testClient.wasapiClient = mockHttpClient;

JsonResponseHandler mockHandler = mock(JsonResponseHandler.class);
HttpGet mockRequest = mock(HttpGet.class);

testClient.execute(mockRequest, mockHandler);

verify(mockHttpClient, times(1)).execute(mockRequest, mockHandler, testClient.wasapiContext);
}
}
46 changes: 46 additions & 0 deletions test/edu/stanford/dlss/was/TestWasapiConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package edu.stanford.dlss.was;

import java.io.IOException;

import org.apache.http.client.methods.HttpGet;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.*;
import static org.mockito.Mockito.*;
import org.mockito.ArgumentMatchers;

public class TestWasapiConnection {

private static final String JSON_QUERY = "http://example.com/example.json";
private static final String DOWNLOAD_QUERY = "http://example.com/download?file=example.warc";
private static final String OUTPUT_PATH = "/dev/null";

@Test
public void constructorCallsLogin() throws IOException {
WasapiClient mockClient = mock(WasapiClient.class);
WasapiConnection testConnection = new WasapiConnection(mockClient);

verify(mockClient, times(1)).login();
}

@Test
public void jsonQueryCallsExecute() throws IOException {
WasapiClient mockClient = mock(WasapiClient.class);
WasapiConnection testConnection = new WasapiConnection(mockClient);
testConnection.jsonQuery(JSON_QUERY);

verify(mockClient, times(1)).execute(ArgumentMatchers.<HttpGet>any(HttpGet.class),
ArgumentMatchers.<JsonResponseHandler>any(JsonResponseHandler.class));
}

@Test
public void downloadQueryCallsExecute() throws IOException {
WasapiClient mockClient = mock(WasapiClient.class);
WasapiConnection testConnection = new WasapiConnection(mockClient);
testConnection.downloadQuery(JSON_QUERY, OUTPUT_PATH);

verify(mockClient, times(1)).execute(ArgumentMatchers.<HttpGet>any(HttpGet.class),
ArgumentMatchers.<DownloadResponseHandler>any(DownloadResponseHandler.class));
}
}

0 comments on commit 33674a8

Please sign in to comment.