Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] MLIBZ-2513 - Locally created entity being pushed as a PUT rather than a POST. #169

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.kinvey.androidTest.cache;


import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.RenamingDelegatingContext;
import android.test.suitebuilder.annotation.SmallTest;

import com.kinvey.android.Client;
import com.kinvey.android.store.DataStore;
import com.kinvey.androidTest.TestManager;
import com.kinvey.androidTest.callback.DefaultKinveyClientCallback;
import com.kinvey.androidTest.callback.DefaultKinveyReadCallback;
import com.kinvey.androidTest.model.Person;
import com.kinvey.java.Constants;
import com.kinvey.java.Query;
import com.kinvey.java.cache.ICache;
import com.kinvey.java.core.KinveyJsonError;
import com.kinvey.java.core.KinveyJsonResponseException;
import com.kinvey.java.network.NetworkManager;
import com.kinvey.java.store.BaseDataStore;
import com.kinvey.java.store.QueryCacheItem;
import com.kinvey.java.store.StoreType;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID;

import static com.kinvey.androidTest.TestManager.PASSWORD;
import static com.kinvey.androidTest.TestManager.TEST_USERNAME;
import static com.kinvey.androidTest.TestManager.TEST_USERNAME_2;
import static com.kinvey.androidTest.TestManager.USERNAME;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertNotEquals;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class TempIdTest {

private static final String TEMP_ID = "temp_";
private Client client;
private TestManager<Person> testManager;
private DataStore<Person> store;

@Before
public void setUp() throws InterruptedException {
Context mMockContext = new RenamingDelegatingContext(InstrumentationRegistry.getInstrumentation().getTargetContext(), "test_");
client = new Client.Builder(mMockContext).build();
testManager = new TestManager<>();
testManager.login(USERNAME, PASSWORD, client);
}

@After
public void tearDown() {
client.performLockDown();
if (client.getKinveyHandlerThread() != null) {
try {
client.stopKinveyHandlerThread();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}

/* check that temp item is created with 'temp_' prefix for local keeping and after pushing, item id is updated */
@Test
public void testCreatingPrefixForTempId() throws InterruptedException {
store = DataStore.collection(Person.COLLECTION, Person.class, StoreType.SYNC, client);
testManager.cleanBackend(store, StoreType.SYNC);
Person person = new Person(TEST_USERNAME);
DefaultKinveyClientCallback saveCallback = testManager.save(store, person);
assertNotNull(saveCallback);
assertNull(saveCallback.getError());
assertNotNull(saveCallback.getResult());
Person savedPerson = saveCallback.getResult();
assertTrue(savedPerson.getId().startsWith(TEMP_ID));
DefaultKinveyReadCallback readCallback = testManager.find(store, client.query());
assertNotNull(readCallback);
assertNull(readCallback.getError());
assertNotNull(readCallback.getResult().getResult());
assertEquals(1, readCallback.getResult().getResult().size());
Person fromCache = readCallback.getResult().getResult().get(0);
assertTrue(fromCache.getId().startsWith(TEMP_ID));
assertEquals(savedPerson.getId(), fromCache.getId());
testManager.push(store);
readCallback = testManager.find(store, client.query());
assertNotNull(readCallback);
assertNull(readCallback.getError());
assertNotNull(readCallback.getResult().getResult());
assertEquals(1, readCallback.getResult().getResult().size());
fromCache = readCallback.getResult().getResult().get(0);
assertFalse(fromCache.getId().startsWith(TEMP_ID));
assertNotEquals(savedPerson.getId(), fromCache.getId());
assertEquals(0, client.getSyncManager().getCount(Person.COLLECTION));
}

/* check that old temp item which was created without 'temp_' prefix for local keeping will be pushed as expect */
@Test
public void testBackwardCompatibility() throws InterruptedException {
store = DataStore.collection(Person.COLLECTION, Person.class, StoreType.SYNC, client);
testManager.cleanBackend(store, StoreType.SYNC);
Person person = new Person(TEST_USERNAME);
person.setId(UUID.randomUUID().toString());
DefaultKinveyClientCallback saveCallback = testManager.save(store, person);
assertNotNull(saveCallback);
assertNull(saveCallback.getError());
assertNotNull(saveCallback.getResult());
Person savedPerson = saveCallback.getResult();
assertFalse(savedPerson.getId().startsWith(TEMP_ID));
assertEquals(person.getId(), savedPerson.getId());
DefaultKinveyReadCallback readCallback = testManager.find(store, client.query());
assertNotNull(readCallback);
assertNull(readCallback.getError());
assertNotNull(readCallback.getResult().getResult());
assertEquals(1, readCallback.getResult().getResult().size());
Person fromCache = readCallback.getResult().getResult().get(0);
assertFalse(fromCache.getId().startsWith(TEMP_ID));
assertEquals(person.getId(), fromCache.getId());
testManager.push(store);
readCallback = testManager.find(store, client.query());
assertNotNull(readCallback);
assertNull(readCallback.getError());
assertNotNull(readCallback.getResult().getResult());
assertEquals(1, readCallback.getResult().getResult().size());
fromCache = readCallback.getResult().getResult().get(0);
assertFalse(fromCache.getId().startsWith(TEMP_ID));
assertEquals(person.getId(), fromCache.getId());
assertEquals(0, client.getSyncManager().getCount(Person.COLLECTION));
}

/* check that old temp item which was created without 'temp_' prefix
and item with 'temp_' prefix will be pushed booth as expect */
@Test
public void testMixItemsWithOldAndNewTempIdPatterns() throws InterruptedException {
store = DataStore.collection(Person.COLLECTION, Person.class, StoreType.SYNC, client);
testManager.cleanBackend(store, StoreType.SYNC);
Person person = new Person(TEST_USERNAME);
person.setId(UUID.randomUUID().toString());
Person person2 = new Person(TEST_USERNAME_2);
DefaultKinveyClientCallback saveCallback = testManager.save(store, person);
assertNotNull(saveCallback);
assertNull(saveCallback.getError());
assertNotNull(saveCallback.getResult());
Person savedPerson = saveCallback.getResult();
saveCallback = testManager.save(store, person2);
assertNotNull(saveCallback);
assertNull(saveCallback.getError());
assertNotNull(saveCallback.getResult());
Person savedPerson2 = saveCallback.getResult();
assertFalse(savedPerson.getId().startsWith(TEMP_ID));
assertEquals(person.getId(), savedPerson.getId());
assertTrue(savedPerson2.getId().startsWith(TEMP_ID));
assertEquals(person2.getId(), savedPerson2.getId());
DefaultKinveyReadCallback readCallback = testManager.find(store, client.query());
assertNotNull(readCallback);
assertNull(readCallback.getError());
assertNotNull(readCallback.getResult().getResult());
assertEquals(2, readCallback.getResult().getResult().size());
assertEquals(2, client.getSyncManager().getCount(Person.COLLECTION));
readCallback = testManager.find(store, client.query().equals("username", TEST_USERNAME));
assertNotNull(readCallback);
assertNull(readCallback.getError());
assertNotNull(readCallback.getResult().getResult());
assertEquals(1, readCallback.getResult().getResult().size());
Person fromCache = readCallback.getResult().getResult().get(0);
assertFalse(fromCache.getId().startsWith(TEMP_ID));
assertEquals(person.getId(), fromCache.getId());
readCallback = testManager.find(store, client.query().equals("username", TEST_USERNAME_2));
assertNotNull(readCallback);
assertNull(readCallback.getError());
assertNotNull(readCallback.getResult().getResult());
assertEquals(1, readCallback.getResult().getResult().size());
Person fromCache2 = readCallback.getResult().getResult().get(0);
assertTrue(fromCache2.getId().startsWith(TEMP_ID));
assertEquals(person2.getId(), fromCache2.getId());
testManager.push(store);
readCallback = testManager.find(store, client.query());
assertNotNull(readCallback);
assertNull(readCallback.getError());
assertNotNull(readCallback.getResult().getResult());
assertEquals(2, readCallback.getResult().getResult().size());
assertFalse(readCallback.getResult().getResult().get(0).getId().startsWith(TEMP_ID));
assertFalse(readCallback.getResult().getResult().get(1).getId().startsWith(TEMP_ID));
assertEquals(0, client.getSyncManager().getCount(Person.COLLECTION));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,41 @@ public void testUpdateNetwork() throws InterruptedException {
testUpdate(StoreType.NETWORK);
}

@Test
public void testUpdateSyncPush() throws InterruptedException {
// Setup
DataStore<Person> store = DataStore.collection(Person.COLLECTION, Person.class, StoreType.SYNC, client);
clearBackend(store);
client.getSyncManager().clear(Person.COLLECTION);

// Save an entity locally
Person person = createPerson(TEST_USERNAME);
DefaultKinveyClientCallback callback = save(store, person);
assertNotNull(callback);
assertNotNull(callback.result);

// Record the temporary Realm-generated ID
Person updatedPerson = callback.result;
String tempID = updatedPerson.getId();

// Push the local entity to the backend
DefaultKinveyPushCallback pushCallback = push(store, 60);
assertNotNull(pushCallback);

// Find the item locally and verify that the permanent ID is in place
DefaultKinveyReadCallback findCallback = find(store,60);
assertNotNull(findCallback);
assertNotNull(findCallback.result);
KinveyReadResponse<Person> readResponse = findCallback.result;
assertNotNull(readResponse);
List<Person> people = readResponse.getResult();
assertNotNull(people);
assertEquals(1, people.size());
String permID = people.get(0).getId();
assertNotNull(permID);
assertNotEquals(tempID, permID);
}

private void testUpdate(StoreType storeType) throws InterruptedException {
DataStore<Person> store = DataStore.collection(Person.COLLECTION, Person.class, storeType, client);
client.getSyncManager().clear(Person.COLLECTION);
Expand Down Expand Up @@ -653,6 +688,21 @@ public void run() {
return callback;
}

private DefaultKinveyReadCallback find(final DataStore<Person> store, int seconds) throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final DefaultKinveyReadCallback callback = new DefaultKinveyReadCallback(latch);
LooperThread looperThread = new LooperThread(new Runnable() {
@Override
public void run() {
store.find(callback);
}
});
looperThread.start();
latch.await();
looperThread.mHandler.sendMessage(new Message());
return callback;
}

@Test
public void testFindByQuerySync() throws InterruptedException {
testFindByQuery(StoreType.SYNC);
Expand Down Expand Up @@ -2061,13 +2111,23 @@ public void testSyncCount() throws InterruptedException {
public void testSaveKmd() throws InterruptedException, IOException {
DataStore<Person> store = DataStore.collection(Person.COLLECTION, Person.class, StoreType.SYNC, client);
client.getSyncManager().clear(Person.COLLECTION);
client.getCacheManager().getCache(Person.COLLECTION, Person.class, 60L).clear();
clearBackend(DataStore.collection(Person.COLLECTION, Person.class, StoreType.NETWORK, client));
store.clear();
Person person = createPerson(TEST_TEMP_USERNAME);
Person savedPerson = store.save(person);
DefaultKinveyClientCallback saveCallback = save(store, person);
assertNotNull(saveCallback);
sync(store, DEFAULT_TIMEOUT);
DefaultKinveyClientCallback findCallback = find(store, savedPerson.getId(), DEFAULT_TIMEOUT, null);
assertNotNull(findCallback.result.get(KMD));
assertNotNull(((GenericJson)findCallback.result.get(KMD)).get(LMT));
delete(store, findCallback.result.getId(), DEFAULT_TIMEOUT);
DefaultKinveyReadCallback findCallback = find(store, DEFAULT_TIMEOUT);
assertNotNull(findCallback);
assertNotNull(findCallback.result);
List<Person> people = findCallback.result.getResult();
assertNotNull(people);
assertEquals(1, people.size());
Person savedPerson = people.get(0);
assertNotNull(savedPerson.get(KMD));
assertNotNull(((GenericJson)savedPerson.get(KMD)).get(LMT));
delete(store, savedPerson.getId(), DEFAULT_TIMEOUT);
push(store, DEFAULT_TIMEOUT);
client.getSyncManager().clear(Person.COLLECTION);
}
Expand All @@ -2076,22 +2136,31 @@ public void testSaveKmd() throws InterruptedException, IOException {
public void testUpdateLmt() throws InterruptedException, IOException {
DataStore<Person> store = DataStore.collection(Person.COLLECTION, Person.class, StoreType.SYNC, client);
client.getSyncManager().clear(Person.COLLECTION);
store.clear();
clearBackend(DataStore.collection(Person.COLLECTION, Person.class, StoreType.NETWORK, client));
Person person = createPerson(TEST_TEMP_USERNAME);
Person savedPerson = store.save(person);
sync(store, DEFAULT_TIMEOUT);
DefaultKinveyClientCallback findCallback = find(store, savedPerson.getId(), DEFAULT_TIMEOUT, null);
assertNotNull(findCallback.result.get(KMD));
String savedLmd = (String)((GenericJson)findCallback.result.get(KMD)).get(LMT);
DefaultKinveyReadCallback findCallback = find(store, DEFAULT_TIMEOUT);
assertNotNull(findCallback);
assertNotNull(findCallback.result);
List<Person> people = findCallback.result.getResult();
assertEquals(1, people.size());
Person syncedPerson = people.get(0);
assertNotNull(syncedPerson.get(KMD));
String savedLmd = (String)((GenericJson)syncedPerson.get(KMD)).get(LMT);
assertNotNull(savedLmd);
savedPerson.setUsername(TEST_TEMP_USERNAME + "_Change");
savedPerson = store.save(savedPerson);
syncedPerson.setUsername(TEST_TEMP_USERNAME + "_Change");
syncedPerson = store.save(syncedPerson);
sync(store, DEFAULT_TIMEOUT);
findCallback = find(store, savedPerson.getId(), DEFAULT_TIMEOUT, null);
assertNotNull(findCallback.result.get(KMD));
String updatedLmd = (String)((GenericJson)findCallback.result.get(KMD)).get(LMT);
findCallback = find(store, DEFAULT_TIMEOUT);
assertNotNull(findCallback.result.getResult());
Person updatedSyncedPerson = findCallback.result.getResult().get(0);
assertNotNull(updatedSyncedPerson.get(KMD));
String updatedLmd = (String)((GenericJson)updatedSyncedPerson.get(KMD)).get(LMT);
assertNotNull(updatedLmd);
assertNotEquals(savedLmd, updatedLmd);
delete(store, findCallback.result.getId(), DEFAULT_TIMEOUT);
delete(store, updatedSyncedPerson.getId(), DEFAULT_TIMEOUT);
push(store, DEFAULT_TIMEOUT);
client.getSyncManager().clear(Person.COLLECTION);
}
Expand Down Expand Up @@ -3196,13 +3265,16 @@ public void testUpdateInternalList() throws InterruptedException, IOException {
public void testCreateUpdateDeleteSync() throws InterruptedException {
DataStore<Person> store = DataStore.collection(Person.COLLECTION, Person.class, StoreType.SYNC, client);
client.getSyncManager().clear(Person.COLLECTION);
clearBackend(DataStore.collection(Person.COLLECTION, Person.class, StoreType.NETWORK, client));
store.clear();

Person person = createPerson(TEST_USERNAME);
DefaultKinveyClientCallback callback = save(store, person);
assertNotNull(callback.result);
assertNotNull(callback.result.getUsername());
sync(store, DEFAULT_TIMEOUT);

person = find(store, client.query().equals(Constants._ID, callback.result.getId()), DEFAULT_TIMEOUT).result.getResult().get(0);
person = find(store, DEFAULT_TIMEOUT).result.getResult().get(0);
person.setUsername(TEST_USERNAME_2);
callback = save(store, person);
assertNotNull(callback.result);
Expand Down
Loading