TestUtil
~ Formatting
package com.wrapper.spotify;
-import com.wrapper.spotify.exceptions.SpotifyWebApiException;
import org.apache.http.Header;
import org.apache.http.NameValuePair;
@@ -14,31 +13,34 @@ import static org.mockito.Mockito.when;
public class TestUtil {
- private static final String TEST_DATA_DIR = "src/test/fixtures/";
+ private static final String FIXTURE_DIR = "src/test/fixtures/";
- private static final int MAX_TEST_DATA_FILE_SIZE = 65536;
-
- public static String readTestData(String fileName) throws IOException {
- return readFromFile(new File(TEST_DATA_DIR, fileName));
+ private static String readTestData(String fileName) throws IOException {
+ return readFromFile(new File(FIXTURE_DIR, fileName));
}
private static String readFromFile(File file) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
StringBuilder out = new StringBuilder();
String line;
+
while ((line = in.readLine()) != null) {
out.append(line);
}
+
in.close();
+
return out.toString();
}
public static class MockedHttpManager {
public static IHttpManager returningJson(String jsonFixture) throws Exception {
+
// Mocked HTTP Manager to get predictable responses
final IHttpManager mockedHttpManager = mock(IHttpManager.class);
final String fixture = readTestData(jsonFixture);
+
when(mockedHttpManager.get(any(URI.class), any(Header[].class))).thenReturn(fixture);
when(mockedHttpManager.post(any(URI.class), any(Header[].class), anyListOf(NameValuePair.class))).thenReturn(fixture);
when(mockedHttpManager.put(any(URI.class), any(Header[].class), anyListOf(NameValuePair.class))).thenReturn(fixture);
@@ -46,17 +48,5 @@ public class TestUtil {
return mockedHttpManager;
}
-
- public static IHttpManager returningString(String returnedString) throws
- IOException,
- SpotifyWebApiException {
- final IHttpManager mockedHttpManager = mock(IHttpManager.class);
- when(mockedHttpManager.get(any(URI.class), any(Header[].class))).thenReturn(returnedString);
- when(mockedHttpManager.post(any(URI.class), any(Header[].class), anyListOf(NameValuePair.class))).thenReturn(returnedString);
- when(mockedHttpManager.put(any(URI.class), any(Header[].class), anyListOf(NameValuePair.class))).thenReturn(returnedString);
- when(mockedHttpManager.delete(any(URI.class), any(Header[].class))).thenReturn(returnedString);
-
- return mockedHttpManager;
- }
}
}
