package com.kth.baasio.test; import com.kth.baasio.Baas; import com.kth.baasio.callback.BaasioCallback; import com.kth.baasio.callback.BaasioDeviceCallback; import com.kth.baasio.callback.BaasioSignInCallback; import com.kth.baasio.callback.BaasioSignUpCallback; import com.kth.baasio.entity.push.BaasioDevice; import com.kth.baasio.entity.user.BaasioUser; import com.kth.baasio.exception.BaasioError; import com.kth.baasio.exception.BaasioException; import com.kth.baasio.preferences.BaasioPreferences; import com.kth.baasio.utils.JsonUtils; import com.kth.baasio.utils.ObjectUtils; import com.kth.common.utils.LogUtils; import android.os.AsyncTask; import android.test.InstrumentationTestCase; import java.util.concurrent.CountDownLatch; public class Test000Auth extends InstrumentationTestCase { private static final String TAG = LogUtils.makeLogTag(Test000Auth.class); private static boolean mHasAccessToken; private static AsyncTask mGCMRegisterTask; public Test000Auth() { super(); } @Override protected void setUp() throws Exception { super.setUp(); } @Override protected void tearDown() throws Exception { super.tearDown(); } public void test000Init() throws InterruptedException { Baas.io().init(getInstrumentation().getContext(), BaasioConfig.BAASIO_URL, BaasioConfig.BAASIO_ID, BaasioConfig.APPLICATION_ID); final CountDownLatch signal = new CountDownLatch(1); mGCMRegisterTask = Baas.io().setGcmEnabled(getInstrumentation().getContext(), UnitTestConfig.PUSH_SHOULD_RECEIVE_TAG, new BaasioDeviceCallback() { @Override public void onException(BaasioException e) { if (!BaasioError.ERROR_GCM_ALREADY_REGISTERED.equals(e.getMessage())) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); } else { LogUtils.LOGV(TAG, e.toString()); } signal.countDown(); } @Override public void onResponse(BaasioDevice response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }, BaasioConfig.GCM_SENDER_ID); if (mGCMRegisterTask != null) { signal.await(); } } public void test001Init_User1SignIn() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signOut(getInstrumentation().getContext()); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER1_USERNAME, UnitTestConfig.COMMON_PASSWORD, new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGV(TAG, e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test002Init_User1Unsubscribe() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser user = Baas.io().getSignedInUser(); if (!ObjectUtils.isEmpty(user)) { user.unsubscribeInBackground(getInstrumentation().getContext(), new BaasioCallback<BaasioUser>() { @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } @Override public void onException(BaasioException e) { LogUtils.LOGV(TAG, e.toString()); signal.countDown(); } }); signal.await(); } } public void test003Init_User2SignIn() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER2_USERNAME, UnitTestConfig.COMMON_PASSWORD, new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGV(TAG, e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test004Init_User2Unsubscribe() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser user = Baas.io().getSignedInUser(); if (!ObjectUtils.isEmpty(user)) { user.unsubscribeInBackground(getInstrumentation().getContext(), new BaasioCallback<BaasioUser>() { @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } @Override public void onException(BaasioException e) { LogUtils.LOGV(TAG, e.toString()); signal.countDown(); } }); signal.await(); } } public void test300User1SignUp() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signUpInBackground(UnitTestConfig.USER1_USERNAME, UnitTestConfig.USER1_USERNAME, UnitTestConfig.USER1_EMAIL, UnitTestConfig.COMMON_PASSWORD, new BaasioSignUpCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test301User1SignIn() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER1_USERNAME, UnitTestConfig.COMMON_PASSWORD, new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test302User1ChangePassword() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.changePasswordInBackground(UnitTestConfig.COMMON_PASSWORD, UnitTestConfig.CHANGE_PASSWORD, new BaasioCallback<Boolean>() { @Override public void onResponse(Boolean response) { if (!response) { fail("changePasswordInBackground response is false"); } signal.countDown(); } @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } }); signal.await(); } public void test303Uesr1SignOut() { BaasioUser.signOut(getInstrumentation().getContext()); } public void test304User1SignIn() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER1_USERNAME, UnitTestConfig.CHANGE_PASSWORD, new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test305User1Unsubscribe() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser user = Baas.io().getSignedInUser(); user.unsubscribeInBackground(getInstrumentation().getContext(), new BaasioCallback<BaasioUser>() { @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } }); signal.await(); } public void test306User1SignIn() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER1_USERNAME, UnitTestConfig.COMMON_PASSWORD, new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGV(TAG, e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGE(TAG, response.toString()); fail(response.toString()); signal.countDown(); } }); signal.await(); } public void test307User1SignUp() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signUpInBackground(UnitTestConfig.USER1_USERNAME, UnitTestConfig.USER1_USERNAME, UnitTestConfig.USER1_EMAIL, UnitTestConfig.COMMON_PASSWORD, new BaasioSignUpCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test308User2SignUp() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signUpInBackground(UnitTestConfig.USER2_USERNAME, UnitTestConfig.USER2_USERNAME, UnitTestConfig.USER2_EMAIL, UnitTestConfig.COMMON_PASSWORD, new BaasioSignUpCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test309User1SignIn() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER1_USERNAME, UnitTestConfig.COMMON_PASSWORD, new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test310User1GetUserByUuid() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser user = new BaasioUser(); user.setUuid(Baas.io().getSignedInUser().getUuid()); user.getInBackground(new BaasioCallback<BaasioUser>() { @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); if (!response.getUuid().equals(Baas.io().getSignedInUser().getUuid())) { fail("miss mactched"); } } @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } }); signal.await(); } public void test311User1GetUserByUsername() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser user = new BaasioUser(); user.setUsername(Baas.io().getSignedInUser().getUsername()); user.getInBackground(new BaasioCallback<BaasioUser>() { @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); if (!response.getUsername().equals(Baas.io().getSignedInUser().getUsername())) { fail("miss mactched"); } } @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } }); signal.await(); } public void test312User1Update() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser user = Baas.io().getSignedInUser(); user.setProperty(UnitTestConfig.USER1_UPDATED_PROPERTY_NAME, UnitTestConfig.USER1_UPDATED_PROPERTY_VALUE); user.updateInBackground(getInstrumentation().getContext(), new BaasioCallback<BaasioUser>() { @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); String value = response.getProperty( UnitTestConfig.USER1_UPDATED_PROPERTY_NAME).getTextValue(); if (!value.equals(UnitTestConfig.USER1_UPDATED_PROPERTY_VALUE)) { fail("Updated property value is not matching."); } BaasioUser userFromSignedInUser = Baas.io().getSignedInUser(); value = userFromSignedInUser.getProperty( UnitTestConfig.USER1_UPDATED_PROPERTY_NAME).getTextValue(); if (!value.equals(UnitTestConfig.USER1_UPDATED_PROPERTY_VALUE)) { fail("Current user is not updated."); } String userString = BaasioPreferences.getUserString(getInstrumentation() .getContext()); BaasioUser userFromPreferences = JsonUtils.parse(userString, BaasioUser.class); value = userFromPreferences.getProperty( UnitTestConfig.USER1_UPDATED_PROPERTY_NAME).getTextValue(); if (!value.equals(UnitTestConfig.USER1_UPDATED_PROPERTY_VALUE)) { fail("Current user from Preferences is not updated."); } signal.countDown(); } @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } }); signal.await(); } public void test313User1ResetPassword() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.resetPasswordInBackground(UnitTestConfig.USER1_EMAIL, new BaasioCallback<Boolean>() { @Override public void onResponse(Boolean response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } @Override public void onException(BaasioException e) { fail(e.toString()); signal.countDown(); } }); signal.await(); } public void test314User1WrongPassword() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER1_USERNAME, UnitTestConfig.COMMON_PASSWORD + "wrong", new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); if (!e.getStatusCode().equals("401")) { fail("wrong status code"); } if (!(e.getErrorCode() == 201)) { fail("wrong error code"); } signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); fail("Should not be signined"); signal.countDown(); } }); signal.await(); } public void test994Uninit_UserSignOut() { BaasioUser.signOut(getInstrumentation().getContext()); } public void test995Uninit_User1SignIn() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER1_USERNAME, UnitTestConfig.COMMON_PASSWORD, new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGE(TAG, e.toString()); fail(e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test996Uninit_User1Unsubscribe() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser user = Baas.io().getSignedInUser(); if (!ObjectUtils.isEmpty(user)) { user.unsubscribeInBackground(getInstrumentation().getContext(), new BaasioCallback<BaasioUser>() { @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } @Override public void onException(BaasioException e) { LogUtils.LOGV(TAG, e.toString()); signal.countDown(); } }); signal.await(); } } public void test997Uninit_User2SignIn() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser.signInInBackground(getInstrumentation().getContext(), UnitTestConfig.USER2_USERNAME, UnitTestConfig.COMMON_PASSWORD, new BaasioSignInCallback() { @Override public void onException(BaasioException e) { LogUtils.LOGV(TAG, e.toString()); signal.countDown(); } @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } }); signal.await(); } public void test998Uninit_User2Unsubscribe() throws InterruptedException { final CountDownLatch signal = new CountDownLatch(1); BaasioUser user = Baas.io().getSignedInUser(); if (!ObjectUtils.isEmpty(user)) { user.unsubscribeInBackground(getInstrumentation().getContext(), new BaasioCallback<BaasioUser>() { @Override public void onResponse(BaasioUser response) { LogUtils.LOGV(TAG, response.toString()); signal.countDown(); } @Override public void onException(BaasioException e) { LogUtils.LOGV(TAG, e.toString()); signal.countDown(); } }); signal.await(); } } public void test999Uninit() throws InterruptedException { if (mGCMRegisterTask != null) { mGCMRegisterTask.cancel(true); } Baas.io().uninit(getInstrumentation().getContext()); } }