Skip to content

Commit

Permalink
#1477 - Ashok - Migrate unverified data to verified
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashok committed Feb 13, 2013
1 parent c313c6f commit dedabaf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.rapidftr.task;

import android.os.AsyncTask;
import com.rapidftr.model.User;
import org.json.JSONObject;

public class MigrateUnverifiedDataToVerified extends AsyncTask<Void, Void, Void> {
private JSONObject responseFromServer;
private User unVerifiedUser;

public MigrateUnverifiedDataToVerified(JSONObject responseFromServer, User unVerifiedUser){
this.responseFromServer = responseFromServer;
this.unVerifiedUser = unVerifiedUser;
}

@Override
protected Void doInBackground(Void... voids) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.rapidftr.task;

import com.google.common.io.CharStreams;
import com.google.inject.Inject;
import com.rapidftr.R;
import com.rapidftr.RapidFtrApplication;
import com.rapidftr.activity.RapidFtrActivity;
import com.rapidftr.model.Child;
import com.rapidftr.model.User;
import com.rapidftr.repository.ChildRepository;
import com.rapidftr.service.ChildService;
import com.rapidftr.service.FormService;
import com.rapidftr.service.LoginService;
import com.rapidftr.service.RegisterUserService;
import com.rapidftr.service.*;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;
import java.io.InputStreamReader;

import static com.rapidftr.RapidFtrApplication.SERVER_URL_PREF;
import static org.apache.http.HttpStatus.SC_OK;
Expand Down Expand Up @@ -45,10 +44,23 @@ public void setContext(RapidFtrActivity context) {
protected void sync() throws JSONException, IOException {
setProgressAndNotify(context.getString(R.string.synchronize_step_1), 0);
registerUser();
login();
JSONObject response = login();
getFormSections();
sendChildrenToServer(childRepository.currentUsersUnsyncedRecords());
setProgressAndNotify(context.getString(R.string.sync_complete), maxProgress);
RapidFtrApplication application = RapidFtrApplication.getApplicationInstance();
if(response.getBoolean("user_status") && !application.getCurrentUser().isVerified()){
new MigrateUnverifiedDataToVerified(response, application.getCurrentUser()).execute();
setNewCurrentUser(response, application.getCurrentUser());
}

}

private void setNewCurrentUser(JSONObject userFromResponse, User currentUser) throws JSONException {
currentUser.setDbKey(userFromResponse.getString("db_key"));
currentUser.setVerified(userFromResponse.getBoolean("user_status"));
currentUser.setOrganisation(userFromResponse.getString("organisation"));
currentUser.setLanguage(userFromResponse.getString("language"));
}

private void registerUser() throws IOException {
Expand All @@ -61,8 +73,19 @@ private void registerUser() throws IOException {
}
}

private void login() throws IOException {
loginService.login(context, currentUser.getUserName(), currentUser.getUnauthenticatedPassword(), currentUser.getServerUrl());
private JSONObject login() throws IOException, JSONException {
HttpResponse response = loginService.login(context, currentUser.getUserName(), currentUser.getUnauthenticatedPassword(), currentUser.getServerUrl());
if(response !=null && (response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED || response.getStatusLine().getStatusCode() == HttpStatus.SC_OK))
return new JSONObject(getResponse(response));
return null;
}

protected String getResponse(HttpResponse response) throws IOException {
return CharStreams.toString(new InputStreamReader(response.getEntity().getContent()));
}

private User getUser() {
return RapidFtrApplication.getApplicationInstance().getCurrentUser();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
import com.rapidftr.service.RegisterUserService;
import com.rapidftr.utils.http.FluentResponse;
import com.xtremelabs.robolectric.tester.org.apache.http.TestHttpResponse;
import org.apache.http.HttpResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;

import java.io.IOException;

import static com.google.common.collect.Lists.newArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
Expand Down Expand Up @@ -93,4 +98,18 @@ public void shouldSyncAllChildrenForGivenUser() throws Exception {
verify(childService).sync(child, currentUser);
}

@Test
public void shouldUpdateUserIfHasBeenChangedAsVerifiedInServer() throws IOException {
task = spy(task);
HttpResponse response = new TestHttpResponse(201, "{\"user_status\":true, \"db_key\":\"hey_from_server\", \"organisation\":\"tw\",\"language\":\"en\"}");
doReturn(response).when(loginService).login(rapidFtrActivity, "username", "password", "serverUrl");
task.onPreExecute();
task.execute();
User newUser = applicationContext.getCurrentUser();
assertTrue(newUser.isVerified());
assertEquals(newUser.getDbKey(), "hey_from_server");
assertEquals(newUser.getOrganisation(), "tw");
assertEquals(newUser.getLanguage(), "en");
}

}

0 comments on commit dedabaf

Please sign in to comment.