Search in sources :

Example 76 with Semaphore

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();
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) ParseHttpRequest(com.parse.http.ParseHttpRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Semaphore(java.util.concurrent.Semaphore) IOException(java.io.IOException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ParseNetworkInterceptor(com.parse.http.ParseNetworkInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseHttpResponse(com.parse.http.ParseHttpResponse) Test(org.junit.Test)

Example 77 with Semaphore

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));
}
Also used : Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 78 with Semaphore

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"));
}
Also used : ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Semaphore(java.util.concurrent.Semaphore) Capture(bolts.Capture) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 79 with Semaphore

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");
}
Also used : Semaphore(java.util.concurrent.Semaphore) Capture(bolts.Capture) Test(org.junit.Test)

Example 80 with Semaphore

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");
}
Also used : Semaphore(java.util.concurrent.Semaphore) Capture(bolts.Capture) Test(org.junit.Test)

Aggregations

Semaphore (java.util.concurrent.Semaphore)511 Test (org.junit.Test)198 IOException (java.io.IOException)55 Context (android.content.Context)39 InvocationOnMock (org.mockito.invocation.InvocationOnMock)38 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)31 AtomicReference (java.util.concurrent.atomic.AtomicReference)31 ExecutionException (java.util.concurrent.ExecutionException)30 File (java.io.File)29 Intent (android.content.Intent)27 List (java.util.List)27 Map (java.util.Map)26 CountDownLatch (java.util.concurrent.CountDownLatch)25 Handler (android.os.Handler)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 HazelcastInstance (com.hazelcast.core.HazelcastInstance)21 BroadcastReceiver (android.content.BroadcastReceiver)20 IntentFilter (android.content.IntentFilter)20