Skip to content

Commit

Permalink
0.3.3 (#13)
Browse files Browse the repository at this point in the history
* stf-client version updated to 0.3.3

* Read parameters from an url with "connect -u <url>" command

* Readme updated
  • Loading branch information
e13mort committed Oct 26, 2017
1 parent 5355b2c commit 2472d0b
Show file tree
Hide file tree
Showing 26 changed files with 91 additions and 275 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ stf [command] [command options]
Filter devices by provider
-serial
Filter devices by serial number
-u
Read connection params from an url


#### Store connection parameters in a file
Expand All @@ -112,6 +114,7 @@ Connection parameters might be stored as a separate file:
}

usage: stf connect -f config.json
stf connect -u http://sample.com/params.json

All fields are optional. For the "providers", "serials" and "names" fields the `~` sign might be used to inverse filter.
E.g config with parameter
Expand Down
4 changes: 2 additions & 2 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

def ARTIFACT_ID = 'stf-console-client'
def ARTIFACT_VERSION = '0.3.2'
def ARTIFACT_VERSION = '0.3.3'
def SCRIPT_NAME = 'stf'

buildscript {
Expand All @@ -33,7 +33,7 @@ repositories {
}

dependencies {
compile 'com.github.e13mort:open-stf-client:0.3.2'
compile 'com.github.e13mort:open-stf-client:0.3.3'

compile 'io.reactivex.rxjava2:rxjava:2.0.7'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.beust.jcommander.Parameter;
import com.github.e13mort.stf.adapter.filters.StringsFilterDescription;
import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.parameters.DevicesParams;

public class ConsoleDeviceParamsImpl implements DevicesParams {
@Parameter(names = "--all", description = "Show all devices. By default only available devices are returned.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.github.e13mort.stf.adapter.filters.InclusionType;
import com.github.e13mort.stf.adapter.filters.StringsFilterDescription;
import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.DevicesParamsImpl;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.client.parameters.DevicesParamsImpl;
import com.github.e13mort.stf.console.AdbRunner;
import com.github.e13mort.stf.console.commands.cache.DeviceListCache;
import com.github.e13mort.stf.model.device.Device;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.beust.jcommander.ParametersDelegate;
import com.beust.jcommander.converters.FileConverter;
import com.beust.jcommander.converters.IntegerConverter;
import com.beust.jcommander.converters.URLConverter;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.console.AdbRunner;
import com.github.e13mort.stf.console.commands.CommandContainer;
Expand All @@ -13,6 +14,7 @@
import io.reactivex.Completable;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
Expand All @@ -32,6 +34,8 @@ public class ConnectCommand implements CommandContainer.Command {
private List<Integer> devicesIndexesFromCache = new ArrayList<>();
@Parameter(names = "-f", description = "Read connection params from a file", converter = FileConverter.class)
private File storedConnectionParamsFile;
@Parameter(names = "-u", description = "Read connection params from an url", converter = URLConverter.class)
private URL storedConnectionParamsUrl;
private DeviceListCache cache;

public ConnectCommand(FarmClient client, AdbRunner adbRunner, DeviceListCache cache, Logger logger) {
Expand All @@ -58,7 +62,9 @@ private DeviceConnector chooseConnector() {
} else if (!devicesIndexesFromCache.isEmpty()) {
connector = new CacheDevicesConnector(client, adbRunner, logger, devicesIndexesFromCache, cache);
} else if (storedConnectionParamsFile != null) {
connector = new FileParamsDeviceConnector(client, adbRunner, logger, storedConnectionParamsFile);
connector = FileParamsDeviceConnector.of(client, adbRunner, logger, storedConnectionParamsFile);
} else if (storedConnectionParamsUrl != null) {
connector = FileParamsDeviceConnector.of(client, adbRunner, logger, storedConnectionParamsUrl);
} else {
connector = new ParamsConnector(client, adbRunner, logger, params);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.e13mort.stf.console.commands.connect;

import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.console.AdbRunner;
import com.github.e13mort.stf.console.commands.EmptyDevicesException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,140 +1,46 @@
package com.github.e13mort.stf.console.commands.connect;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.github.e13mort.stf.adapter.filters.StringsFilterDescription;
import com.github.e13mort.stf.adapter.filters.StringsFilterParser;
import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.client.parameters.JsonDeviceParametersReader;
import com.github.e13mort.stf.console.AdbRunner;
import io.reactivex.Flowable;
import io.reactivex.Notification;

import org.reactivestreams.Publisher;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Logger;

import io.reactivex.Flowable;
import io.reactivex.Notification;

class FileParamsDeviceConnector extends DeviceConnector {

private final File paramsFile;
private final ParametersReader reader;

static DeviceConnector of(FarmClient client, AdbRunner adbRunner, Logger logger, File paramsFile) {
return new FileParamsDeviceConnector(client, adbRunner, logger, () -> new JsonDeviceParametersReader().read(paramsFile));
}

static DeviceConnector of(FarmClient client, AdbRunner adbRunner, Logger logger, URL paramsUrl) {
return new FileParamsDeviceConnector(client, adbRunner, logger, () -> new JsonDeviceParametersReader().read(paramsUrl));
}

FileParamsDeviceConnector(FarmClient client, AdbRunner adbRunner, Logger logger, File paramsFile) {
private FileParamsDeviceConnector(FarmClient client, AdbRunner adbRunner, Logger logger, ParametersReader reader) {
super(client, adbRunner, logger);
this.paramsFile = paramsFile;
this.reader = reader;
}

@Override
protected Publisher<Notification<String>> createConnectionPublisher() {
try {
return connectWithParams(readParamsFromFile());
} catch (IOException e) {
return connectWithParams(reader.read());
} catch (JsonDeviceParametersReader.JsonParamsReaderException e) {
return Flowable.error(e);
}
}

DevicesParams readParamsFromFile() throws IOException {
final ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
final SimpleModule module = new SimpleModule();
module.addDeserializer(StringsFilterDescription.class, new StringFilterDescriptionDeserializer());
module.addDeserializer(String.class, new StrictStringDeserializer());
mapper.registerModule(module);
return mapper.readValue(paramsFile, JsonDevicesParams.class);
}

static class JsonDevicesParams implements DevicesParams {

@JsonProperty("names")
private StringsFilterDescription nameFilterDescription;
@JsonProperty
private String abi;
@JsonProperty
private int api;
@JsonProperty
private int count;
@JsonProperty
private int minApi;
@JsonProperty
private int maxApi;
@JsonProperty("providers")
private StringsFilterDescription providerFilterDescription;
@JsonProperty("serials")
private StringsFilterDescription serialFilterDescription;

@Override
public boolean isAllDevices() {
//there's no sense to be able to connect to unavailable devices
return false;
}

@Override
public String getAbi() {
return abi;
}

@Override
public int getApiVersion() {
return api;
}

@Override
public int getCount() {
return count;
}

@Override
public StringsFilterDescription getNameFilterDescription() {
return nameFilterDescription;
}

@Override
public int getMinApiVersion() {
return minApi;
}

@Override
public int getMaxApiVersion() {
return maxApi;
}

@Override
public StringsFilterDescription getProviderFilterDescription() {
return providerFilterDescription;
}

@Override
public StringsFilterDescription getSerialFilterDescription() {
return serialFilterDescription;
}
}

static class StringFilterDescriptionDeserializer extends JsonDeserializer<StringsFilterDescription> {

private StringsFilterParser parser = new StringsFilterParser();

@Override
public StringsFilterDescription deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
final String value = p.getCodec().readValue(p, String.class);
return parser.parse(value);
}
}

static class StrictStringDeserializer extends JsonDeserializer<String> {

@Override
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if (p.getCurrentToken() == JsonToken.VALUE_STRING) {
return p.getText();
}
throw ctxt.mappingException("Invalid token type: " + p.getCurrentToken());
}
private interface ParametersReader {
DevicesParams read() throws JsonDeviceParametersReader.JsonParamsReaderException;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.e13mort.stf.console.commands.connect;

import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.console.AdbRunner;
import io.reactivex.Notification;
import org.reactivestreams.Publisher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.beust.jcommander.ParametersDelegate;
import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.console.commands.CommandContainer;
import com.github.e13mort.stf.console.commands.ConsoleDeviceParamsImpl;
import com.github.e13mort.stf.console.commands.cache.DeviceListCache;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.e13mort.stf.console.commands.devices;

import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.console.commands.cache.DeviceListCache;
import io.reactivex.Flowable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.e13mort.stf.console;

import com.beust.jcommander.JCommander;
import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.console.commands.CommandContainer;
import com.github.e13mort.stf.console.commands.HelpCommandCreator;
import com.github.e13mort.stf.console.commands.UnknownCommandException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.beust.jcommander.ParameterException;
import com.github.e13mort.stf.adapter.filters.StringsFilterDescription;
import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.console.commands.UnknownCommandException;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -13,7 +14,13 @@

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -194,6 +201,36 @@ void testConnectCommandWithInvalidValidCacheParam() throws IOException {
assertThrows(ParameterException.class, () -> createCommander("connect -l str"));
}

@DisplayName("Connect command with valid -u parameter will be parsed successfully")
@Test
void testValidUrlParamsIsParsed() throws IOException, UnknownCommandException {
createCommander("connect -u http://google.com");
}

@DisplayName("Connect command with valid -f parameter will be parsed successfully")
@Test
void testValidFileParamsIsParsed() throws IOException, UnknownCommandException {
createCommander("connect -f some/file.json");
}

@DisplayName("Connect command with invalid -u parameter will throw an error")
@Test
void testInvalidUrlParamsIsFailedToParse() throws IOException, UnknownCommandException {
assertThrows(ParameterException.class, () -> createCommander(" connect -u not_a_url"));
}

@DisplayName("Connect command with empty -u parameter will throw an error")
@Test
void testEmptyUrlParamsIsFailedToParse() throws IOException, UnknownCommandException {
assertThrows(ParameterException.class, () -> createCommander(" connect -u"));
}

@DisplayName("Connect command with empty -f parameter will throw an error")
@Test
void testEmptyFileParamsIsFailedToParse() throws IOException, UnknownCommandException {
assertThrows(ParameterException.class, () -> createCommander(" connect -u"));
}

@Test
void testDisconnectCommand() throws IOException, UnknownCommandException {
createCommander("disconnect").execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.github.e13mort.stf.console.commands.connect;

import com.github.e13mort.stf.client.DevicesParams;
import com.github.e13mort.stf.client.parameters.DevicesParams;
import com.github.e13mort.stf.console.BaseStfCommanderTest;
import com.github.e13mort.stf.model.device.Device;
import io.reactivex.Flowable;
import io.reactivex.Notification;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -19,9 +18,16 @@
import java.util.List;
import java.util.stream.Stream;

import io.reactivex.Flowable;
import io.reactivex.Notification;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SuppressWarnings("unused")
@DisplayName("Detailed \"connect\" command cases")
class ConnectCommandTest extends BaseStfCommanderTest {

Expand Down
Loading

0 comments on commit 2472d0b

Please sign in to comment.