use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.
the class ParseOkHttpClientTest method testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse.
@Test
public void testParseOkHttpClientExecuteWithExternalInterceptorAndGZIPResponse() throws Exception {
// Make mock response
Buffer buffer = new Buffer();
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
gzipOut.write("content".getBytes());
gzipOut.close();
buffer.write(byteOut.toByteArray());
MockResponse mockResponse = new MockResponse().setStatus("HTTP/1.1 " + 201 + " " + "OK").setBody(buffer).setHeader("Content-Encoding", "gzip");
// Start mock server
server.enqueue(mockResponse);
server.start();
ParseHttpClient client = new ParseOkHttpClient(10000, null);
final Semaphore done = new Semaphore(0);
// Add plain interceptor to disable decompress response stream
client.addExternalInterceptor(new ParseNetworkInterceptor() {
@Override
public ParseHttpResponse intercept(Chain chain) throws IOException {
done.release();
ParseHttpResponse parseResponse = chain.proceed(chain.getRequest());
// Make sure the response we get from the interceptor is the raw gzip stream
byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
assertArrayEquals(byteOut.toByteArray(), content);
// We need to set a new stream since we have read it
return new ParseHttpResponse.Builder().setContent(new ByteArrayInputStream(byteOut.toByteArray())).build();
}
});
// We do not need to add Accept-Encoding header manually, httpClient library should do that.
String requestUrl = server.url("/").toString();
ParseHttpRequest parseRequest = new ParseHttpRequest.Builder().setUrl(requestUrl).setMethod(ParseHttpRequest.Method.GET).build();
// Execute request
ParseHttpResponse parseResponse = client.execute(parseRequest);
// Make sure the response we get is ungziped by OkHttp library
byte[] content = ParseIOUtils.toByteArray(parseResponse.getContent());
assertArrayEquals("content".getBytes(), content);
// Make sure interceptor is called
assertTrue(done.tryAcquire(10, TimeUnit.SECONDS));
server.shutdown();
}
use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.
the class ParseUserTest method testLogInWithCallback.
@Test
public void testLogInWithCallback() throws Exception {
// Register a mock currentUserController to make setCurrentUser work
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.setAsync(any(ParseUser.class))).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);
// Register a mock userController to make logIn work
ParseUserController userController = mock(ParseUserController.class);
ParseUser.State newUserState = new ParseUser.State.Builder().put("newKey", "newValue").sessionToken("newSessionToken").build();
when(userController.logInAsync(anyString(), anyString())).thenReturn(Task.forResult(newUserState));
ParseCorePlugins.getInstance().registerUserController(userController);
final Semaphore done = new Semaphore(0);
ParseUser.logInInBackground("userName", "password", new LogInCallback() {
@Override
public void done(ParseUser user, ParseException e) {
done.release();
assertNull(e);
// Make sure user's data is correct
assertEquals("newSessionToken", user.getSessionToken());
assertEquals("newValue", user.get("newKey"));
}
});
assertTrue(done.tryAcquire(5, TimeUnit.SECONDS));
// Make sure user is login
verify(userController, times(1)).logInAsync("userName", "password");
// Make sure we set currentUser
verify(currentUserController, times(1)).setAsync(any(ParseUser.class));
}
use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testSendInBackgroundWithCallbackSuccess.
@Test
public void testSendInBackgroundWithCallbackSuccess() throws Exception {
// Mock controller
ParsePushController controller = mock(ParsePushController.class);
when(controller.sendInBackground(any(ParsePush.State.class), anyString())).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerPushController(controller);
// Make sample ParsePush data and call method
ParsePush push = new ParsePush();
JSONObject data = new JSONObject();
data.put("key", "value");
List<String> channels = new ArrayList<>();
channels.add("test");
channels.add("testAgain");
push.builder.expirationTime((long) 1000).data(data).pushToIOS(true).channelSet(channels);
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.sendInBackground(new SendCallback() {
@Override
public void done(ParseException e) {
exceptionCapture.set(e);
done.release();
}
});
// Make sure controller is executed and state parameter is correct
assertNull(exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
ArgumentCaptor<ParsePush.State> stateCaptor = ArgumentCaptor.forClass(ParsePush.State.class);
verify(controller, times(1)).sendInBackground(stateCaptor.capture(), anyString());
ParsePush.State state = stateCaptor.getValue();
assertTrue(state.pushToIOS());
assertEquals(data, state.data(), JSONCompareMode.NON_EXTENSIBLE);
assertEquals(2, state.channelSet().size());
assertTrue(state.channelSet().contains("test"));
assertTrue(state.channelSet().contains("testAgain"));
}
use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testSubscribeInBackgroundWithCallbackSuccess.
@Test
public void testSubscribeInBackgroundWithCallbackSuccess() throws Exception {
final ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerPushChannelsController(controller);
ParsePush push = new ParsePush();
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.subscribeInBackground("test", new SaveCallback() {
@Override
public void done(ParseException e) {
exceptionCapture.set(e);
done.release();
}
});
assertNull(exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
verify(controller, times(1)).subscribeInBackground("test");
}
use of java.util.concurrent.Semaphore in project Parse-SDK-Android by ParsePlatform.
the class ParsePushTest method testUnsubscribeInBackgroundWithCallbackSuccess.
@Test
public void testUnsubscribeInBackgroundWithCallbackSuccess() throws Exception {
final ParsePushChannelsController controller = mock(ParsePushChannelsController.class);
when(controller.unsubscribeInBackground(anyString())).thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerPushChannelsController(controller);
ParsePush push = new ParsePush();
final Semaphore done = new Semaphore(0);
final Capture<Exception> exceptionCapture = new Capture<>();
push.unsubscribeInBackground("test", new SaveCallback() {
@Override
public void done(ParseException e) {
exceptionCapture.set(e);
done.release();
}
});
assertNull(exceptionCapture.get());
assertTrue(done.tryAcquire(1, 10, TimeUnit.SECONDS));
verify(controller, times(1)).unsubscribeInBackground("test");
}
Aggregations