Change device update api, add tests, fix version, add migration.
LocationBasedServices/maptology/master This commit looks good

This commit is contained in:
Marcus Handte
2019-11-19 19:06:02 +01:00
parent 6e5c90acd9
commit 077cecb670
10 changed files with 135 additions and 40 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ android {
applicationId "com.maptology.android.app"
minSdkVersion 14
targetSdkVersion 28
versionCode 13
versionCode 9
versionName "${version}"
buildConfigField "long", "BUILD_TIME", System.currentTimeMillis() + "L"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -177,7 +177,7 @@ public class LocationContract {
/**
* The db schema version.
*/
public static int VERSION = 6;
public static int VERSION = 7;
/**
* The name of the database.
@@ -184,22 +184,36 @@ public class LocationHelper extends SQLiteOpenHelper {
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "Migrating database from " + oldVersion + " to " + newVersion + ".");
// a quite crude "upgrade" script
db.beginTransaction();
try {
// drop indexes
db.execSQL(SQL_DROP_METADATA_UI);
db.execSQL(SQL_DROP_NETWORK_BS);
db.execSQL(SQL_DROP_POSITION_UT);
// drop tables
db.execSQL(SQL_DROP_NETWORK);
db.execSQL(SQL_DROP_POSITION);
db.execSQL(SQL_DROP_METADATA);
db.execSQL(SQL_DROP_UPDATE);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
if (oldVersion == 6 && newVersion == 7) {
// only add new table from 6 to 7
db.beginTransaction();
try {
db.execSQL(SQL_CREATE_METADATA);
db.execSQL(SQL_INDEX_METADATA_UI);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
} else {
// otherwise recreate database
db.beginTransaction();
try {
// drop indexes
db.execSQL(SQL_DROP_METADATA_UI);
db.execSQL(SQL_DROP_NETWORK_BS);
db.execSQL(SQL_DROP_POSITION_UT);
// drop tables
db.execSQL(SQL_DROP_NETWORK);
db.execSQL(SQL_DROP_POSITION);
db.execSQL(SQL_DROP_METADATA);
db.execSQL(SQL_DROP_UPDATE);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
// recreate everything
onCreate(db);
}
// recreate everything
onCreate(db);
}
}
@@ -101,7 +101,7 @@ public class UpdateDeviceTask extends AbstractApiTask<Void, Void, AsyncError> {
return getError(e);
}
try {
device = apiClient.setCurrentDeviceUser();
device = apiClient.setCurrentUserDevice(device);
Log.d(TAG, "Device associated for user id " + device.getUser() + ".");
} catch (FeignException e) {
Log.d(TAG, "Device creation failure: " + e.status() + " " + e.getMessage());
@@ -0,0 +1,74 @@
package com.maptology.android.app.test.codec;
import com.maptology.android.app.util.DeltaCodec;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* Test cases for the delta codec.
*
* @author Marcus
*/
public class DeltaCodecTest {
/**
* Tests that the delta codec can encode and decode data.
*/
@Test
public void testReadWriteDeltaCodec() {
for (int i = 1; i < 5; i++) {
DeltaCodec codec = new DeltaCodec(i);
List<long[]> expected = generateTestData(i, 100);
String encoded = codec.encode(expected);
List<long[]> actual = codec.decode(encoded, new ArrayList<long[]>());
assertEquals(expected, actual);
}
}
/**
* Generates some random data for the tests.
*
* @param length The length of the arrays.
* @param size The size of the list.
* @return The list with arrays of the specified length.
*/
public List<long[]> generateTestData(int length, int size) {
List<long[]> result = new ArrayList<>();
for (int i = 0; i < size; i++) {
long[] data = new long[length];
result.add(data);
for (int j = 0; j < data.length; j++) {
data[j] = i * j;
if (i % 2 == 0) data[j] = -data[j];
}
}
return result;
}
/**
* Ensures that the two lists of arrays are identical in terms of contents.
*
* @param expected The expected list.
* @param actual The actual list.
*/
public void assertEquals(List<long[]> expected, List<long[]> actual) {
Assert.assertEquals("Size must match.", expected.size(), actual.size());
for (int i = 0; i < expected.size(); i++) {
long[] e = expected.get(i);
long[] a = actual.get(i);
Assert.assertEquals("Length must match.", e.length, a.length);
for (int j = 0; j < e.length; j++)
Assert.assertEquals("Value must match.", e[j], a[j]);
}
}
}
@@ -66,10 +66,11 @@ public interface IDeviceService {
/**
* Sets the user account for a previously registered device.
*
* @param device The device to set.
* @return A response.
*/
@PUT
@Path("devices/current/user")
@POST
@Path("users/current/devices")
@ApiOperation(value = "Connects a device created without user account with a user account.",
notes = "Executing this is operation requires a valid device and a valid user authentication to be transmitted simultaneously. ",
tags = {Tags.DEFAULT, Tags.DEVICE})
@@ -80,7 +81,7 @@ public interface IDeviceService {
})
@PermitAll
@RolesAllowed({Roles.DEVICE, Roles.USER})
Response setCurrentDeviceUser();
Response setCurrentUserDevice(@ApiParam("The device values.") @Valid @NotNull Device device);
/**
* Returns the current device.
@@ -59,22 +59,24 @@ public class DeviceSessionBean extends AbstractUserSpecificSessionBean {
/**
* Connects a device that has been created without user account with
* an existing user account.
*
*
* @param device The device.
* @return The device, if successful.
* @throws EntityForbiddenException Thrown if the user or device is not valid.
* @throws EntityConflictException Thrown if the device is already connected with
* a user account.
*/
@RolesAllowed({Roles.DEVICE, Roles.USER})
public Device setCurrentDeviceUser() throws EntityForbiddenException, EntityConflictException {
DeviceEntity device = getPrincipalDevice();
public Device setCurrentUserDevice(Device device) throws EntityForbiddenException, EntityConflictException {
DeviceEntity principalDevice = getPrincipalDevice();
UserEntity user = getPrincipalUser();
if (device == null) throw new EntityForbiddenException(DeviceEntity.class, null, "Cannot find principal device.");
if (principalDevice == null) throw new EntityForbiddenException(DeviceEntity.class, null, "Cannot find principal device.");
if (user == null) throw new EntityForbiddenException(UserEntity.class, null, "Cannot find principal user.");
if (device.getUser() != null) throw new EntityConflictException(DeviceEntity.class, device.getId());
device.setUser(user);
if (device.getId() != principalDevice.getId()) throw new EntityForbiddenException(DeviceEntity.class, device.getId(), "Invalid device specification.");
if (principalDevice.getUser() != null) throw new EntityConflictException(DeviceEntity.class, principalDevice.getId());
principalDevice.setUser(user);
em.flush();
return new DeviceConverter(true).convert(device);
return new DeviceConverter(true).convert(principalDevice);
}
@@ -116,7 +116,7 @@ public class DeviceSessionTest extends AbstractSessionTest {
@Override
public void run() {
try {
deviceSession.setCurrentDeviceUser();
deviceSession.setCurrentUserDevice(handle);
Assert.fail("Setting a device without user or device should fail.");
} catch (Exception e) {
Assert.assertEquals("Setting device without user or device should be forbidden.", EntityForbiddenException.class, e.getClass());
@@ -129,7 +129,7 @@ public class DeviceSessionTest extends AbstractSessionTest {
@Override
public void run() {
try {
Device device = deviceSession.setCurrentDeviceUser();
Device device = deviceSession.setCurrentUserDevice(handle);
Assert.assertNotNull("Device user should be set after association.", device.getUser());
} catch (Exception e) {
Assert.fail("Setting a device user should succeed.");
@@ -140,7 +140,7 @@ public class DeviceSessionTest extends AbstractSessionTest {
@Override
public void run() {
try {
deviceSession.setCurrentDeviceUser();
deviceSession.setCurrentUserDevice(handle);
Assert.fail("Setting a device user for a connected device should fail.");
} catch (Exception e) {
Assert.assertEquals("Setting a user for a connected device should raise conflict.", EntityConflictException.class, e.getClass());
@@ -66,10 +66,10 @@ public class DeviceResource implements IDeviceService {
}
@Override
public Response setCurrentDeviceUser() {
public Response setCurrentUserDevice(Device device) {
try {
Device result = deviceSession.setCurrentDeviceUser();
return Response.ok(result).build();
Device result = deviceSession.setCurrentUserDevice(device);
return Response.ok(result).build();
} catch (EntityForbiddenException e) {
return Response.status(Status.FORBIDDEN).build();
} catch (EntityConflictException e) {
@@ -81,7 +81,7 @@ public class DeviceResource implements IDeviceService {
}
}
}
@Override
public Response getCurrentDevice() {
try {
@@ -81,19 +81,23 @@ public class DeviceResourceTest extends AbstractResourceTest {
Response r = anonProxy.createDevice(createValidDeviceInfo());
AssertResponse.isStatus(r, Status.CREATED);
DeviceHandle d = AssertResponse.hasBody(r, DeviceHandle.class);
Response noDevice = userProxy.setCurrentDeviceUser();
Device bad = new Device();
bad.setOs(OperatingSystem.ANDROID);
Response noDevice = userProxy.setCurrentUserDevice(bad);
AssertResponse.isStatus(noDevice, Status.FORBIDDEN);
Response noUser = deviceProxy.setCurrentDeviceUser();
Response noUser = deviceProxy.setCurrentUserDevice(bad);
AssertResponse.isStatus(noUser, Status.FORBIDDEN);
IDeviceService deviceService = getTarget("handte", "secure", d.getToken()).proxy(IDeviceService.class);
Response ok = deviceService.setCurrentDeviceUser();
Response deviceResponse = deviceService.getCurrentDevice();
Device currentDevice = AssertResponse.hasBody(deviceResponse, Device.class);
Response ok = deviceService.setCurrentUserDevice(currentDevice);
AssertResponse.isStatus(ok, Status.OK);
Device device = AssertResponse.hasBody(ok, Device.class);
Assert.assertNotNull("User must be set.", device.getUser());
IUserService userService = getTarget("handte", "secure").proxy(IUserService.class);
User user = AssertResponse.hasBody(userService.getCurrentUser(), User.class);
Assert.assertEquals("User must be correct after update.", (long)user.getId(), (long)device.getUser());
Response conflict = deviceService.setCurrentDeviceUser();
Response conflict = deviceService.setCurrentUserDevice(device);
AssertResponse.isStatus(conflict, Status.CONFLICT);
Device result = AssertResponse.hasBody(conflict, Device.class);
Assert.assertNotNull("User must be set.", result.getUser());