Added unit tests for BluetoothLeDeviceStore.java

This commit is contained in:
Alexandros Schillings
2017-01-19 17:11:21 +00:00
parent 81d984efac
commit 47a4c124f6
3 changed files with 121 additions and 10 deletions
+3
View File
@@ -31,6 +31,9 @@ dependencies {
compile 'uk.co.alt236:easycursor-android:1.0.0'
compile fileTree(include: ['*.jar'], dir: 'libs')
compile project(':library')
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.9.5'
}
android {
@@ -1,5 +1,7 @@
package uk.co.alt236.btlescan.containers;
import android.support.annotation.NonNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -11,14 +13,14 @@ import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
import uk.co.alt236.easycursor.objectcursor.EasyObjectCursor;
public class BluetoothLeDeviceStore {
private static final BluetoothLeDeviceComparator DEFAULT_COMPARATOR = new BluetoothLeDeviceComparator();
private final Map<String, BluetoothLeDevice> mDeviceMap;
public BluetoothLeDeviceStore() {
mDeviceMap = new HashMap<>();
}
public void addDevice(final BluetoothLeDevice device) {
public void addDevice(@NonNull final BluetoothLeDevice device) {
if (mDeviceMap.containsKey(device.getAddress())) {
mDeviceMap.get(device.getAddress()).updateRssiReading(device.getTimestamp(), device.getRssi());
} else {
@@ -30,24 +32,42 @@ public class BluetoothLeDeviceStore {
mDeviceMap.clear();
}
public int getSize() {
return mDeviceMap.size();
}
@NonNull
public EasyObjectCursor<BluetoothLeDevice> getDeviceCursor() {
return getDeviceCursor(DEFAULT_COMPARATOR);
}
@NonNull
public EasyObjectCursor<BluetoothLeDevice> getDeviceCursor(@NonNull Comparator<BluetoothLeDevice> comparator) {
return new EasyObjectCursor<>(
BluetoothLeDevice.class,
getDeviceList(),
getDeviceList(comparator),
"address");
}
@NonNull
public List<BluetoothLeDevice> getDeviceList() {
return getDeviceList(DEFAULT_COMPARATOR);
}
@NonNull
public List<BluetoothLeDevice> getDeviceList(@NonNull Comparator<BluetoothLeDevice> comparator) {
final List<BluetoothLeDevice> methodResult = new ArrayList<>(mDeviceMap.values());
Collections.sort(methodResult, new Comparator<BluetoothLeDevice>() {
@Override
public int compare(final BluetoothLeDevice arg0, final BluetoothLeDevice arg1) {
return arg0.getAddress().compareToIgnoreCase(arg1.getAddress());
}
});
Collections.sort(methodResult, comparator);
return methodResult;
}
private static class BluetoothLeDeviceComparator implements Comparator<BluetoothLeDevice> {
@Override
public int compare(final BluetoothLeDevice arg0, final BluetoothLeDevice arg1) {
return arg0.getAddress().compareTo(arg1.getAddress());
}
}
}
@@ -0,0 +1,88 @@
package uk.co.alt236.btlescan.containers;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import uk.co.alt236.bluetoothlelib.device.BluetoothLeDevice;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
public class BluetoothLeDeviceStoreTest {
private BluetoothLeDeviceStore cut;
@Before
public void setUp() {
cut = new BluetoothLeDeviceStore();
}
@Test
public void testAddOne() {
assertStoreSize(0);
cut.addDevice(createDevice("foo"));
assertStoreSize(1);
cut.clear();
assertStoreSize(0);
}
@Test
public void testAddTwo() {
assertStoreSize(0);
cut.addDevice(createDevice("foo"));
assertStoreSize(1);
cut.addDevice(createDevice("bar"));
assertStoreSize(2);
cut.clear();
assertStoreSize(0);
}
@Test
public void testUpdateOne() {
assertStoreSize(0);
cut.addDevice(createDevice("foo", 100, 101));
assertStoreSize(1);
final BluetoothLeDevice device1 = cut.getDeviceList().get(0);
assertEquals(100, device1.getTimestamp());
assertEquals(101, device1.getRssi());
cut.addDevice(createDevice("foo", 200, 201));
assertStoreSize(1);
final BluetoothLeDevice device2 = cut.getDeviceList().get(0);
assertSame(device1, device2);
Mockito
.verify(device2, Mockito.times(1))
.updateRssiReading(200, 201);
cut.clear();
assertStoreSize(0);
}
private BluetoothLeDevice createDevice(final String mac) {
final BluetoothLeDevice mock = Mockito.mock(BluetoothLeDevice.class);
return createDevice(mac, 0, 0);
}
private BluetoothLeDevice createDevice(final String mac, long rssiTime, int rssi) {
final BluetoothLeDevice mock = Mockito.mock(BluetoothLeDevice.class);
Mockito.when(mock.getAddress()).thenReturn(mac);
Mockito.when(mock.getTimestamp()).thenReturn(rssiTime);
Mockito.when(mock.getRssi()).thenReturn(rssi);
return mock;
}
private void assertStoreSize(final int expected) {
assertEquals(expected, cut.getSize());
assertEquals(expected, cut.getDeviceCursor().getCount());
assertEquals(expected, cut.getDeviceList().size());
}
}