Skip to content

Commit

Permalink
Style changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Ingulfsen committed May 18, 2017
1 parent 2f3bb9f commit 6518b54
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 55 deletions.
7 changes: 4 additions & 3 deletions src/edu/stanford/dlss/was/DownloadResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
public class DownloadResponseHandler implements ResponseHandler<Boolean> {
private String outputPath;

public DownloadResponseHandler(String outputPath) {
this.outputPath = outputPath;
public DownloadResponseHandler(String outPath) {
this.outputPath = outPath;
}

@Override
public Boolean handleResponse(final HttpResponse response) throws ClientProtocolException, HttpResponseException, IOException {
HttpEntity entity = response.getEntity();

if(WasapiResponseValidator.validateResponse(response.getStatusLine(), (entity == null))) {

if (WasapiResponseValidator.validateResponse(response.getStatusLine(), entity == null)) {
FileOutputStream fouts = new FileOutputStream(outputPath);
entity.writeTo(fouts);
fouts.close();
Expand Down
4 changes: 2 additions & 2 deletions src/edu/stanford/dlss/was/JsonResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class JsonResponseHandler implements ResponseHandler<WasapiResponse> {
@Override
public WasapiResponse handleResponse(final HttpResponse response) throws ClientProtocolException, HttpResponseException, IOException {
HttpEntity entity = response.getEntity();
if(WasapiResponseValidator.validateResponse(response.getStatusLine(), (entity == null)))
return new WasapiResponseParser().parse(entity.getContent());
if (WasapiResponseValidator.validateResponse(response.getStatusLine(), entity == null))
return new WasapiResponseParser().parse(entity.getContent());
else return null;
}
}
2 changes: 0 additions & 2 deletions src/edu/stanford/dlss/was/WasapiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand Down
1 change: 0 additions & 1 deletion src/edu/stanford/dlss/was/WasapiConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;

import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpGet;
Expand Down
2 changes: 1 addition & 1 deletion src/edu/stanford/dlss/was/WasapiDownloaderSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String baseUrlString() {
}

public String authUrlString() {
return settings.getProperty("authurl");
return settings.getProperty("authurl");
}

public String username() {
Expand Down
5 changes: 0 additions & 5 deletions src/edu/stanford/dlss/was/WasapiFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* Class corresponding to JSON returned by WASAPI that represents WebdataFile
* @see https://github.com/WASAPI-Community/data-transfer-apis/blob/master/ait-implementation/wasapi/implemented-swagger.yaml#L132-L183
*/
@SuppressWarnings("checkstyle:HiddenField")
public class WasapiFile {

@JsonProperty("account")
Expand All @@ -30,10 +29,6 @@ public class WasapiFile {
private String[] locations;
private int size;


public WasapiFile() {
}

public int getAccountId() {
return accountId;
}
Expand Down
4 changes: 0 additions & 4 deletions src/edu/stanford/dlss/was/WasapiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* Class corresponding to JSON received after making WASAPI request ("FileSet")
* @see https://github.com/WASAPI-Community/data-transfer-apis/blob/master/ait-implementation/wasapi/implemented-swagger.yaml#L184-L213
*/
@SuppressWarnings("checkstyle:HiddenField")
public class WasapiResponse {

private int count;
Expand All @@ -18,9 +17,6 @@ public class WasapiResponse {
private String previous;
private WasapiFile[] files;

public WasapiResponse() {
}

public int getCount() {
return count;
}
Expand Down
7 changes: 5 additions & 2 deletions src/edu/stanford/dlss/was/WasapiResponseValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;

@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
public class WasapiResponseValidator {

private static final int STATUS_CODE_THRESHOLD = 300;

public static boolean validateResponse(StatusLine statusLine, boolean entityIsNull) throws ClientProtocolException, HttpResponseException {
if(statusLine.getStatusCode() >= 300) {
if(statusLine.getStatusCode() >= STATUS_CODE_THRESHOLD) {
throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
}
if(entityIsNull) {
if (entityIsNull) {
throw new ClientProtocolException("Response contains no content");
}
return true;
Expand Down
29 changes: 13 additions & 16 deletions test/edu/stanford/dlss/was/TestDownloadResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,57 @@

public class TestDownloadResponseHandler {
private static final char SEP = File.separatorChar;
private static final String outputDirectory = new String("test" + SEP + "tmp");
private static final String outputFilePath = new String(outputDirectory + SEP + "testDownloadResponseHandler.output");
private static final StatusLine validStatusLine = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 200, "OK");
private static final String OUTPUT_DIRECTORY = new String("test" + SEP + "tmp");
private static final String OUTPUT_FILE_PATH = new String(OUTPUT_DIRECTORY + SEP + "testDownloadResponseHandler.output");
private static final StatusLine VALID_STATUS_LINE = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 200, "OK");

private void writeToFile() throws IOException {
new File(outputFilePath).createNewFile();
new File(OUTPUT_FILE_PATH).createNewFile();
return;
}


@Before
public void setUp() {
new File(outputDirectory).mkdir();
new File(OUTPUT_DIRECTORY).mkdir();
}


@After
public void tearDown() {
new File(outputFilePath).delete();
new File(outputDirectory).delete();
new File(OUTPUT_FILE_PATH).delete();
new File(OUTPUT_DIRECTORY).delete();
}


@Test(expected=ClientProtocolException.class)
@Test(expected = ClientProtocolException.class)
public void nullEntityThrowsException() throws ClientProtocolException, HttpResponseException, IOException {
DownloadResponseHandler handler = new DownloadResponseHandler("/dev/null");
HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
Mockito.when(mockResponse.getEntity()).thenReturn(null);
Mockito.when(mockResponse.getStatusLine()).thenReturn(validStatusLine);
Mockito.when(mockResponse.getStatusLine()).thenReturn(VALID_STATUS_LINE);

handler.handleResponse(mockResponse);
}


@Test
public void validResponseDownloadsFile() throws ClientProtocolException, HttpResponseException, IOException {
DownloadResponseHandler handler = new DownloadResponseHandler(outputFilePath);
DownloadResponseHandler handler = new DownloadResponseHandler(OUTPUT_FILE_PATH);
HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
Mockito.when(mockResponse.getEntity()).thenReturn(mockEntity);
Mockito.when(mockResponse.getStatusLine()).thenReturn(validStatusLine);
Mockito.when(mockResponse.getStatusLine()).thenReturn(VALID_STATUS_LINE);

Mockito.doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) throws IOException {
new File(outputFilePath).createNewFile();
new File(OUTPUT_FILE_PATH).createNewFile();
return null;
// Object[] args = invocation.getArguments();
// System.out.println("called with arguments: " + Arrays.toString(args));
// return null;
}
}).when(mockEntity).writeTo(Matchers.<FileOutputStream>any());

boolean returnValue = handler.handleResponse(mockResponse);
assertEquals(returnValue, true);
assertEquals(new File(outputFilePath).exists(), true);
assertEquals(new File(OUTPUT_FILE_PATH).exists(), true);
}
}
11 changes: 6 additions & 5 deletions test/edu/stanford/dlss/was/TestJsonResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
public class TestJsonResponseHandler {

private static final char SEP = File.separatorChar;
private static final StatusLine validStatusLine = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 200, "OK");
private static final StatusLine VALID_STATUS_LINE = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 200, "OK");
private static final String FIXTURE_FILE = new String("test" + SEP + "fixtures" + SEP + "webdata_crawl_mult_files_response.json");

@Test(expected=ClientProtocolException.class)
@Test(expected = ClientProtocolException.class)
public void nullEntityThrowsException() throws ClientProtocolException, HttpResponseException, IOException {
JsonResponseHandler handler = new JsonResponseHandler();
HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
Mockito.when(mockResponse.getEntity()).thenReturn(null);
Mockito.when(mockResponse.getStatusLine()).thenReturn(validStatusLine);
Mockito.when(mockResponse.getStatusLine()).thenReturn(VALID_STATUS_LINE);

handler.handleResponse(mockResponse);
}
Expand All @@ -40,8 +41,8 @@ public void validResponseParsesCorrectly() throws ClientProtocolException, HttpR
HttpResponse mockResponse = Mockito.mock(HttpResponse.class);
HttpEntity mockEntity = Mockito.mock(HttpEntity.class);
Mockito.when(mockResponse.getEntity()).thenReturn(mockEntity);
Mockito.when(mockResponse.getStatusLine()).thenReturn(validStatusLine);
Mockito.when(mockEntity.getContent()).thenReturn(new FileInputStream(new File("test" + SEP + "fixtures" + SEP + "webdata_crawl_mult_files_response.json")));
Mockito.when(mockResponse.getStatusLine()).thenReturn(VALID_STATUS_LINE);
Mockito.when(mockEntity.getContent()).thenReturn(new FileInputStream(new File(FIXTURE_FILE)));

WasapiResponse parsedResponse = handler.handleResponse(mockResponse);
assertEquals(parsedResponse.getCount(), 5);
Expand Down
28 changes: 14 additions & 14 deletions test/edu/stanford/dlss/was/TestWasapiResponseValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@

public class TestWasapiResponseValidator {

private StatusLine validStatusLine = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 200, "OK");
private StatusLine invalidStatusLine = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 300, "Not Defined");
private StatusLine validStatusLine = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 200, "OK");
private StatusLine invalidStatusLine = new BasicStatusLine(new ProtocolVersion("HTTP 1/1", 1, 1), 300, "Not Defined");

@Test(expected=ClientProtocolException.class)
public void testNullEntity() throws ClientProtocolException, HttpResponseException {
WasapiResponseValidator.validateResponse(validStatusLine, true);
}
@Test(expected = ClientProtocolException.class)
public void testNullEntity() throws ClientProtocolException, HttpResponseException {
WasapiResponseValidator.validateResponse(validStatusLine, true);
}

@Test(expected=HttpResponseException.class)
public void testWrongResponseCode() throws ClientProtocolException, HttpResponseException {
WasapiResponseValidator.validateResponse(invalidStatusLine, false);
}
@Test(expected = HttpResponseException.class)
public void testWrongResponseCode() throws ClientProtocolException, HttpResponseException {
WasapiResponseValidator.validateResponse(invalidStatusLine, false);
}

@Test
public void testValidResponse() throws ClientProtocolException, HttpResponseException {
assertTrue(WasapiResponseValidator.validateResponse(validStatusLine, false));
}
@Test
public void testValidResponse() throws ClientProtocolException, HttpResponseException {
assertTrue(WasapiResponseValidator.validateResponse(validStatusLine, false));
}
}

0 comments on commit 6518b54

Please sign in to comment.