Add Device class with tests and some refactorings.
This commit is contained in:
parent
058d91d0a9
commit
d87b748b64
10 changed files with 147 additions and 17 deletions
|
@ -0,0 +1,58 @@
|
|||
package dev.nuculabs.nucuhub.domain;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Device represents a NucuHub device.
|
||||
*/
|
||||
public class Device {
|
||||
private String target;
|
||||
|
||||
public Device() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new device instance.
|
||||
*
|
||||
* @param url Url of the form http://localhost:port
|
||||
*/
|
||||
public Device(String url) {
|
||||
validateUrlAgainstRegex(url);
|
||||
target = url;
|
||||
}
|
||||
|
||||
public Device(String host, int port) {
|
||||
String temp = String.format(Locale.ENGLISH, "%s:%d", host, port);
|
||||
validateUrlAgainstRegex(temp);
|
||||
target = temp;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public void setTarget(String target) {
|
||||
validateUrlAgainstRegex(target);
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public boolean testConnection() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void validateUrlAgainstRegex(String url) {
|
||||
String urlValidationRegex = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
|
||||
Pattern p = Pattern.compile(urlValidationRegex);
|
||||
Matcher m = p.matcher(url);
|
||||
if (m.matches()) {
|
||||
if (url.contains("http://") || url.contains("https://")) {
|
||||
throw new IllegalArgumentException("Don't include schema with URL");
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
|
||||
"Malformed URL provided for device: %s", url));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package dev.nuculabs.nucuhub.domain;
|
||||
|
||||
public class SettingValues {
|
||||
public static final String NAME = "settings";
|
||||
public static final String CURRENT_DEVICE_LIST = "current_device_list";
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package dev.nuculabs.nucuhub.domain;
|
||||
|
||||
public class Utils {
|
||||
public static boolean isNullOrEmpty(String str) {
|
||||
return str == null || str.isEmpty();
|
||||
}
|
||||
}
|
|
@ -30,6 +30,11 @@ public class EnvironmentSensorService {
|
|||
Log.i(TAG, String.format(Locale.ENGLISH, "Initializing channel host=%s port=%d", host, port));
|
||||
}
|
||||
|
||||
public EnvironmentSensorService(String target) {
|
||||
this(ManagedChannelBuilder.forTarget(target).usePlaintext());
|
||||
Log.i(TAG, String.format(Locale.ENGLISH, "Initializing channel target=%s", target));
|
||||
}
|
||||
|
||||
public EnvironmentSensorService(ManagedChannelBuilder<?> channelBuilder) {
|
||||
ManagedChannel channel = channelBuilder.build();
|
||||
blockingStub = EnvironmentSensorGrpcServiceGrpc.newBlockingStub(channel);
|
||||
|
|
|
@ -37,7 +37,7 @@ public class SensorsFragment extends Fragment {
|
|||
sensorStatusImageView = root.findViewById(R.id.sensorStatusImageView);
|
||||
sensorStatusText = root.findViewById(R.id.sensorStatusChip);
|
||||
|
||||
environmentSensorService = new EnvironmentSensorService("192.168.0.100", 8000);
|
||||
environmentSensorService = new EnvironmentSensorService("192.168.0.100:8000");
|
||||
|
||||
setupSensorStatusImage();
|
||||
setupSensorMeasurementsDisplays();
|
||||
|
|
|
@ -5,12 +5,14 @@ import android.content.SharedPreferences;
|
|||
import android.os.Bundle;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import dev.nuculabs.nucuhub.R;
|
||||
import dev.nuculabs.nucuhub.domain.SettingValues;
|
||||
import dev.nuculabs.nucuhub.ui.settings.device.DeviceListPreference;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public class SettingsFragment extends PreferenceFragmentCompat {
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
public static final String DEVICE_LIST_PREFERENCE = "device_list";
|
||||
|
||||
@Override
|
||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
||||
|
@ -18,15 +20,15 @@ public class SettingsFragment extends PreferenceFragmentCompat {
|
|||
// SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
setPreferencesFromResource(R.xml.root_preferences, rootKey);
|
||||
updateDeviceListItems();
|
||||
initializeDeviceListItems();
|
||||
}
|
||||
|
||||
private void updateDeviceListItems() {
|
||||
// dummy code to update device list entries
|
||||
SharedPreferences sp = getContext().getSharedPreferences("settings", Activity.MODE_PRIVATE);
|
||||
private void initializeDeviceListItems() {
|
||||
// TODO dummy code to update device list entries
|
||||
SharedPreferences sp = Objects.requireNonNull(getContext()).getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE);
|
||||
SharedPreferences.Editor spe = sp.edit();
|
||||
DeviceListPreference devicesList = findPreference("device_list");
|
||||
Set<String> savedEntries = sp.getStringSet("current_device_list", null);
|
||||
DeviceListPreference devicesList = findPreference(DEVICE_LIST_PREFERENCE);
|
||||
Set<String> savedEntries = sp.getStringSet(SettingValues.CURRENT_DEVICE_LIST, null);
|
||||
if (savedEntries != null) {
|
||||
CharSequence[] items = new CharSequence[savedEntries.size()];
|
||||
int index = 0;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package dev.nuculabs.nucuhub.ui.settings;
|
||||
package dev.nuculabs.nucuhub.ui.settings.device;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
|
@ -1,4 +1,4 @@
|
|||
package dev.nuculabs.nucuhub.ui.settings;
|
||||
package dev.nuculabs.nucuhub.ui.settings.device;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
|
@ -15,6 +15,7 @@ import androidx.annotation.NonNull;
|
|||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.preference.ListPreference;
|
||||
import dev.nuculabs.nucuhub.R;
|
||||
import dev.nuculabs.nucuhub.domain.SettingValues;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
|
@ -34,13 +35,13 @@ public class DeviceManagementDialog extends Dialog {
|
|||
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
|
||||
setupToolbar();
|
||||
|
||||
sharedPreferences = getContext().getSharedPreferences("settings", Activity.MODE_PRIVATE);
|
||||
sharedPreferences = getContext().getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE);
|
||||
|
||||
final ListView deviceListView = findViewById(R.id.settings_device_dialog_list);
|
||||
adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1);
|
||||
adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1);
|
||||
deviceListView.setAdapter(adapter);
|
||||
|
||||
// dummy code to add simple items.
|
||||
// todo: dummy code to add simple items.
|
||||
final CharSequence [] preferenceCharSeq = preference.getEntries();
|
||||
for (CharSequence item : preferenceCharSeq) {
|
||||
adapter.add(item.toString());
|
||||
|
@ -83,7 +84,7 @@ public class DeviceManagementDialog extends Dialog {
|
|||
}
|
||||
|
||||
private void updatePreferenceEntryValues() {
|
||||
// dummy code to update the entries.
|
||||
// todo dummy code to update the entries.
|
||||
int itemsLength = adapter.getCount();
|
||||
CharSequence[] entries = new CharSequence[itemsLength];
|
||||
HashSet<String> entriesSet = new HashSet<>();
|
||||
|
@ -94,7 +95,7 @@ public class DeviceManagementDialog extends Dialog {
|
|||
preference.setEntries(entries);
|
||||
|
||||
SharedPreferences.Editor spe = sharedPreferences.edit();
|
||||
spe.putStringSet("current_device_list", entriesSet);
|
||||
spe.putStringSet(SettingValues.CURRENT_DEVICE_LIST, entriesSet);
|
||||
}
|
||||
|
||||
private void setupToolbar() {
|
|
@ -38,7 +38,7 @@
|
|||
<PreferenceCategory
|
||||
app:title="@string/devices_header">
|
||||
|
||||
<dev.nuculabs.nucuhub.ui.settings.DeviceListPreference
|
||||
<dev.nuculabs.nucuhub.ui.settings.device.DeviceListPreference
|
||||
app:key="device_list"
|
||||
app:title="@string/device_selection_title"
|
||||
app:entries="@array/reply_entries"
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package dev.nuculabs.nucuhub.domain;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
public class DeviceTest {
|
||||
@Test
|
||||
public void test_constructionValidTargets() {
|
||||
String[] testCases = {"localhost:8900/cool", "nuculabs.dev", "www.nuculabs.dev/", "user:pass@nuculabs.dev"};
|
||||
for (String target : testCases) {
|
||||
Device device = new Device(target);
|
||||
assertEquals(device.getTarget(), target);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_constructionValidHostsAndPorts() {
|
||||
Random random = new Random();
|
||||
String[] testCases = {"localhost", "nuculabs.dev", "www.nuculabs.dev", "user:pass@nuculabs.dev"};
|
||||
for (String target : testCases) {
|
||||
int port = random.nextInt(65535);
|
||||
Device device = new Device(target, port);
|
||||
assertEquals(device.getTarget(), String.format(Locale.ENGLISH,"%s:%d", target, port));
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void test_constructionInvalidHostAndPortHttps() {
|
||||
new Device("https://google.com", 443);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void test_constructionInvalidHostAndPortHttp() {
|
||||
new Device("http://google.com", 443);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void test_constructionInvalidTargetHttp() {
|
||||
new Device("http://localhost:8900/cool");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void test_constructionInvalidTargetHttps() {
|
||||
new Device("https://nuculabs.dev");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue