Skip to content

Instantly share code, notes, and snippets.

@jwir3
Created April 22, 2014 19:39
Show Gist options
  • Save jwir3/11191699 to your computer and use it in GitHub Desktop.
Save jwir3/11191699 to your computer and use it in GitHub Desktop.
WebAPICommunicatorAuthenticatedTest
package com.jingit.mobile.api.test;
import java.util.concurrent.*;
import org.json.JSONException;
import org.json.JSONObject;
import android.location.Location;
import android.os.Handler;
import android.test.InstrumentationTestCase;
import android.util.Log;
import com.jingit.mobile.api.LoginSuccessResponse;
import com.jingit.mobile.api.WebAPICommunicator;
import com.jingit.mobile.auth.AuthToken;
import com.jingit.mobile.events.FutureWebResult;
import com.jingit.mobile.events.WebCallback;
import com.jingit.mobile.location.DefaultAndroidLocationProvider;
import com.jingit.mobile.model.channel.JingitChannel;
import com.jingit.mobile.model.engagement.JingitEngagement;
import com.jingit.mobile.model.offers.JingitOffer;
import com.jingit.mobile.model.shared.Account;
/**
* Tests for each call in the WebAPICommunicator for which authentication is required
*/
public class WebAPICommunicatorAuthenticatedTest extends InstrumentationTestCase {
private static final String LOGTAG = "WebAPICommunicatorAuthenticatedTest";
private Handler mHandler;
@Override
protected synchronized void setUp() throws Exception {
super.setUp();
Log.d(LOGTAG, "setUp start");
DefaultAndroidLocationProvider mLocationProvider = new DefaultAndroidLocationProvider();
loc = mLocationProvider.getLocation();
FutureWebResult<LoginSuccessResponse> result
= WebAPICommunicator.logUserIn("dreeves@sevenventures.net", "zzzzzz", null);
result.get(5, TimeUnit.SECONDS);
Log.d(LOGTAG, "setUp done");
}
@Override
protected synchronized void tearDown() throws Exception {
super.tearDown();
}
public synchronized void testLogin() throws Throwable {
final CountDownLatch cdl = new CountDownLatch(1);
// Set up our handler so we can use it with the WebCallback later.
setupHandler();
WebAPICommunicator.getAuthToken("6madina@list.ru", "123456",
new WebCallback<AuthToken>(mHandler) {
@Override
public void onSuccess(AuthToken data) {
Log.d(LOGTAG, "AUTHTOKEN RETR: " + data.toString());
WebAPICommunicator.getProfile(data, new WebCallback<Account>(mHandler) {
@Override
public void onSuccess(Account data) {
Log.d(LOGTAG, "ACCOUNT RETRIEVED: " + data);
cdl.countDown();
}
});
}
});
cdl.await(30, TimeUnit.SECONDS);
assertEquals(0, cdl.getCount());
}
public synchronized void testInvalidLogin() throws Throwable {
// Set up our handler so we can use it with the WebCallback later.
setupHandler();
final CountDownLatch invalidPasswordSeen = new CountDownLatch(1);
Log.d(LOGTAG, "Handler is: " + mHandler);
WebAPICommunicator.logUserIn("sjohnson+test@jingit.com", "this is not a valid password", new WebCallback<LoginSuccessResponse>(mHandler) {
@Override
public void onSuccess(LoginSuccessResponse data) {
Log.d(LOGTAG, "Inside of onSuccess");
}
@Override
public void onFailure() {
Log.d(LOGTAG, "Inside of onFailure() NO PARAMS");
}
@Override
public void onFailure(FutureWebResult.ErrorReason reason, String responseBody) {
try {
Log.d(LOGTAG, "Inside of onFailure");
JSONObject responseMsg = new JSONObject(responseBody);
String errorCode = responseMsg.getString("errorCode");
if (errorCode.equals("WRONG_CREDENTIALS") || errorCode.equals("WRONG_PASSWORD")) {
invalidPasswordSeen.countDown();
}
} catch (JSONException e) {
Log.e(LOGTAG, "Parsing of JSON failed", e);
}
}
});
invalidPasswordSeen.await(100, TimeUnit.SECONDS);
assertEquals(0, invalidPasswordSeen.getCount());
}
public synchronized void testGetChannelZero() {
Log.d(LOGTAG, "getChannelZero");
FutureWebResult<JingitChannel> futureResult = WebAPICommunicator.getChannel(0, loc, null);
verifyChannelResults(futureResult);
}
public synchronized void testGetChannelByUrl() {
Log.d(LOGTAG, "getChannelZero");
String channelUrl = "https://test.jingit.com/ws/channel/11121?ccKey=1ja";
FutureWebResult<JingitChannel> futureResult = WebAPICommunicator.getChannelByUrl(channelUrl, loc, null);
verifyChannelResults(futureResult);
}
private void verifyChannelResults(FutureWebResult<JingitChannel> futureResult) {
JingitChannel result = null;
try {
result = futureResult.get(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
fail("InterruptedException");
} catch (ExecutionException e) {
e.printStackTrace();
fail("ExecutionException");
} catch (TimeoutException e) {
e.printStackTrace();
fail("TimeoutException");
}
assertEquals("Result Code: " + futureResult.getResultCode(), 200, futureResult.getResultCode());
Log.d(LOGTAG, "Response body: " + futureResult.getResponseBody());
assertNotNull(result);
}
public synchronized void testGetEngagementWithURL() {
String engagementUrl = "https://test.jingit.com/ws/engagement/target/17772?ccKey=1ja";
FutureWebResult<JingitEngagement> futureResult = WebAPICommunicator.getEngagementWithURL(engagementUrl, loc, null);
JingitEngagement result = null;
try {
result = futureResult.get(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
fail("InterruptedException");
} catch (ExecutionException e) {
e.printStackTrace();
fail("ExecutionException");
} catch (TimeoutException e) {
e.printStackTrace();
fail("TimeoutException");
}
assertEquals(futureResult.getResultCode(), 200);
assertNotNull(result);
}
public synchronized void testGetOfferWithURL() {
String offerUrl = "https://test.jingit.com/ws/offer/target/8079?ccKey=1ja";
FutureWebResult<JingitOffer> futureResult = WebAPICommunicator.getOfferWithURL(offerUrl, loc, null);
JingitOffer result = null;
try {
result = futureResult.get(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
fail("InterruptedException");
} catch (ExecutionException e) {
e.printStackTrace();
fail("ExecutionException");
} catch (TimeoutException e) {
e.printStackTrace();
fail("TimeoutException");
}
assertEquals(200, futureResult.getResultCode());
assertNotNull(result);
}
private void setupHandler() throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
mHandler = new Handler();
}
});
}
private Future<Location> loc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment