Skip to content

Commit

Permalink
#1557 - Suganthi/Kavitha - Modified sync functionality to handle sync…
Browse files Browse the repository at this point in the history
… failure
  • Loading branch information
Suganthi T committed Feb 19, 2013
1 parent 2df1983 commit c62ebd4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ public Child sync(Child child, User currentUser) throws IOException, JSONExcepti
repository.update(child);
throw new SyncFailedException(e.getMessage());
}
if (response != null && response.isSuccess()) {
String source = CharStreams.toString(new InputStreamReader(response.getEntity().getContent()));
child = new Child(source);
setChildAttributes(child);
repository.update(child);
setMedia(child);
return child;
} else {
throw new SyncFailedException(child.getUniqueId());
try {
if (response != null && response.isSuccess()) {
String source = CharStreams.toString(new InputStreamReader(response.getEntity().getContent()));
child = new Child(source);
setChildAttributes(child);
repository.update(child);
setMedia(child);
return child;
}
} catch (Exception e) {
child.setSynced(false);
child.setSyncLog(e.getMessage());
}
return child;
}

protected String getSyncPath(Child child, User currentUser) throws JSONException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.mockito.Mock;

import java.io.IOException;
import java.io.SyncFailedException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -72,12 +71,12 @@ public void shouldMarkChildAsSyncedWhenSyncing() throws IOException, JSONExcepti
verify(repository).update(child);
}

@Test(expected = SyncFailedException.class)
public void shouldRaiseExceptionUponSyncFailure() throws Exception {
@Test
public void shouldHandleSyncFailureAndReturnTheFailingChild() throws Exception {
Child child = new Child();
getFakeHttpLayer().setDefaultHttpResponse(503, "error");

new ChildService(mockContext(), repository, fluentRequest).sync(child, currentUser);
assertEquals(child, new ChildService(mockContext(), repository, fluentRequest).sync(child, currentUser));
}

@Test
Expand Down Expand Up @@ -118,7 +117,7 @@ public void shouldAddPhotoKeysToParam() throws JSONException, IOException, Gener
verify(mockFluentRequest).param("photo_keys", new JSONArray(Arrays.asList("abcd123", "1234ABC")).toString());
}

@Test(expected = SyncFailedException.class)
@Test
public void shouldNotAddPhotoParamIfThePhotoNameIsPresentInPhotoKeys() throws JSONException, IOException, GeneralSecurityException {
FluentRequest mockFluentRequest = spy(new FluentRequest());

Expand All @@ -134,7 +133,7 @@ public void shouldNotAddPhotoParamIfThePhotoNameIsPresentInPhotoKeys() throws JS
verify(mockFluentRequest, times(0)).param("current_photo_key", "1234ABC");
}

@Test(expected = SyncFailedException.class)
@Test
public void shouldAddAudioRecordedIfCreatedOnTheMobile() throws JSONException, IOException {
FluentRequest mockFluentRequest = spy(new FluentRequest());
RapidFtrApplication context = mockContext();
Expand All @@ -146,7 +145,7 @@ public void shouldAddAudioRecordedIfCreatedOnTheMobile() throws JSONException, I
verify(mockFluentRequest).param("recorded_audio", "123455");
}

@Test(expected = SyncFailedException.class)
@Test
public void shouldNotAddAudioRecordedToTheRequestIfItsAlreadyPresentInServer() throws JSONException, IOException {
FluentRequest mockFluentRequest = spy(new FluentRequest());
RapidFtrApplication context = mockContext();
Expand All @@ -158,7 +157,7 @@ public void shouldNotAddAudioRecordedToTheRequestIfItsAlreadyPresentInServer() t
verify(mockFluentRequest, times(0)).param("recorded_audio", "123455");
}

@Test(expected = SyncFailedException.class)
@Test
public void shouldRemoveUnusedParametersBeforeSync() throws JSONException, IOException {
FluentRequest mockFluentRequest = spy(new FluentRequest());
RapidFtrApplication context = mockContext();
Expand Down

0 comments on commit c62ebd4

Please sign in to comment.