Skip to content

Commit

Permalink
0.3.2 (#12)
Browse files Browse the repository at this point in the history
* Connect command divided into three connectors
* FileParamsDeviceConnector created
* FileParamsDeviceConnector readParamsFromFile_validFullFile_exactMatching test implemented
* readme updated with new -f parameter
* readme updated with new -f parameter description
* version updated to 0.3.2
  • Loading branch information
e13mort committed Sep 6, 2017
1 parent 0fb1ae0 commit 5355b2c
Show file tree
Hide file tree
Showing 25 changed files with 559 additions and 137 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# STF console client
A console client for the [Smartphone Test Farm](https://github.com/openstf/stf) service

[![Build Status](https://travis-ci.org/e13mort/stf-console-client.svg?branch=master)](https://travis-ci.org/e13mort/stf-console-client)
## Download
[Latest version](https://github.com/e13mort/stf-console-client/releases/latest)

Expand Down Expand Up @@ -75,6 +76,8 @@ stf [command] [command options]
-count
Filter devices by count
Default: 0
-f
Read connection params from a file
-l
Connect to devices by its indexes from the results of previous
"devices" command. E.g. "-l 1 2 5"
Expand All @@ -93,4 +96,26 @@ stf [command] [command options]
Filter devices by serial number


[![Build Status](https://travis-ci.org/e13mort/stf-console-client.svg?branch=master)](https://travis-ci.org/e13mort/stf-console-client)
#### Store connection parameters in a file
Connection parameters might be stored as a separate file:

confing.json:
{
"count": 2,
"api": 23,
"names": "nexus5,nexus6",
"minApi": 21,
"providers": "~support,autotests",
"serials": "3a4674bce45644",
"maxApi": 26,
"abi": "arm"
}

usage: stf connect -f config.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

"providers": "~support,autotests"

means 'use all available devices except the ones which belongs to the "support" and "autotests" groups'.
2 changes: 1 addition & 1 deletion 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.1'
def ARTIFACT_VERSION = '0.3.2'
def SCRIPT_NAME = 'stf'

buildscript {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.e13mort.stf.console.commands;

import com.github.e13mort.stf.console.StfCommanderContext;
import com.github.e13mort.stf.console.commands.connect.ConnectCommand;
import com.github.e13mort.stf.console.commands.devices.DevicesCommand;
import io.reactivex.Completable;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.e13mort.stf.console.commands.connect;

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.console.AdbRunner;
import com.github.e13mort.stf.console.commands.cache.DeviceListCache;
import com.github.e13mort.stf.model.device.Device;
import io.reactivex.Flowable;
import io.reactivex.Notification;
import org.reactivestreams.Publisher;

import java.util.List;
import java.util.logging.Logger;

class CacheDevicesConnector extends DeviceConnector {

private final List<Integer> devicesIndexesFromCache;
private final DeviceListCache cache;

protected CacheDevicesConnector(FarmClient client, AdbRunner runner, Logger logger, List<Integer> devicesIndexesFromCache, DeviceListCache cache) {
super(client, runner, logger);
this.devicesIndexesFromCache = devicesIndexesFromCache;
this.cache = cache;
}

@Override
protected Publisher<Notification<String>> createConnectionPublisher() {
return readParamsFromCache(cache.getCachedFiles()).flatMap(this::connectWithParams);
}

private Flowable<DevicesParams> readParamsFromCache(List<Device> cachedFiles) {
return Flowable.fromIterable(devicesIndexesFromCache)
.doOnNext(integer -> validate(cachedFiles, integer))
.map(integer -> --integer)
.map(cachedFiles::get)
.map(Device::getSerial)
.toList()
.map(this::createStringsFilterDescription)
.map(this::createDevicesParams)
.toFlowable();
}

private void validate(List<Device> cachedFiles, int index) throws InvalidCacheIndexException {
if (!isIndexValid(cachedFiles, index)) {
throw new InvalidCacheIndexException(index);
}
}

private boolean isIndexValid(List<Device> cachedFiles, Integer integer) {
return integer > 0 && integer <= cachedFiles.size();
}

private StringsFilterDescription createStringsFilterDescription(List<String> l) {
return new StringsFilterDescription(InclusionType.INCLUDE, l);
}

private DevicesParams createDevicesParams(StringsFilterDescription filter) {
final DevicesParamsImpl params = new DevicesParamsImpl();
params.setSerialFilterDescription(filter);
return params;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.github.e13mort.stf.console.commands.connect;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.beust.jcommander.ParametersDelegate;
import com.beust.jcommander.converters.FileConverter;
import com.beust.jcommander.converters.IntegerConverter;
import com.github.e13mort.stf.client.FarmClient;
import com.github.e13mort.stf.console.AdbRunner;
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;
import io.reactivex.Completable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

@Parameters(commandDescription = "Connect to devices")
public class ConnectCommand implements CommandContainer.Command {
private final FarmClient client;
private final AdbRunner adbRunner;
private final Logger logger;

@ParametersDelegate
private ConsoleDeviceParamsImpl params = new ConsoleDeviceParamsImpl();
@Parameter(names = "--my", description = "Connect to currently taken devices")
private Boolean connectToMyDevices = false;
@Parameter(names = "-l", variableArity = true, listConverter = IntegerConverter.class,
description = "Connect to devices by its indexes from the results of previous \"devices\" command. E.g. \"-l 1 2 5\"")
private List<Integer> devicesIndexesFromCache = new ArrayList<>();
@Parameter(names = "-f", description = "Read connection params from a file", converter = FileConverter.class)
private File storedConnectionParamsFile;
private DeviceListCache cache;

public ConnectCommand(FarmClient client, AdbRunner adbRunner, DeviceListCache cache, Logger logger) {
this.client = client;
this.adbRunner = adbRunner;
this.cache = cache;
this.logger = logger;
}

@Override
public Completable execute() {
DeviceConnector connector = chooseConnector();
return connector.connect();
}

void setDevicesIndexesFromCache(List<Integer> devicesIndexesFromCache) {
this.devicesIndexesFromCache = devicesIndexesFromCache;
}

private DeviceConnector chooseConnector() {
DeviceConnector connector;
if (connectToMyDevices) {
connector = new MyDevicesConnector(client, adbRunner, logger);
} else if (!devicesIndexesFromCache.isEmpty()) {
connector = new CacheDevicesConnector(client, adbRunner, logger, devicesIndexesFromCache, cache);
} else if (storedConnectionParamsFile != null) {
connector = new FileParamsDeviceConnector(client, adbRunner, logger, storedConnectionParamsFile);
} else {
connector = new ParamsConnector(client, adbRunner, logger, params);
}
return connector;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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.console.AdbRunner;
import com.github.e13mort.stf.console.commands.EmptyDevicesException;
import io.reactivex.Completable;
import io.reactivex.Flowable;
import io.reactivex.Notification;
import org.reactivestreams.Publisher;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

abstract class DeviceConnector {
protected final FarmClient client;
protected final AdbRunner runner;
protected final Logger logger;

protected DeviceConnector(FarmClient client, AdbRunner runner, Logger logger) {
this.client = client;
this.runner = runner;
this.logger = logger;
}

Completable connect() {
return Completable.fromPublisher(createConnectionPublisher());
}

protected abstract Publisher<Notification<String>> createConnectionPublisher();

protected Flowable<Notification<String>> getEmptyError() {
return Flowable.error(new EmptyDevicesException());
}

protected void handleDevices(Notification<String> deviceNotification) {
if (deviceNotification.isOnNext()) {
handleConnectedDevice(deviceNotification.getValue());
} else if (deviceNotification.isOnError()) {
handleNotConnectedDevice(deviceNotification.getError());
}
}

protected void handleConnectedDevice(String deviceIp) {
try {
runner.connectToDevice(deviceIp);
} catch (IOException e) {
logger.log(Level.WARNING, "Failed to connect to a device", e);
}
}

protected void handleNotConnectedDevice(Throwable error) {
logger.log(Level.WARNING, "Failed to lock a device", error);
}

protected Flowable<Notification<String>> connectWithParams(DevicesParams params) {
return client.connectToDevices(params)
.doOnNext(this::handleDevices)
.switchIfEmpty(getEmptyError());
}
}
Loading

0 comments on commit 5355b2c

Please sign in to comment.