Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/manual add peer #479

Merged
merged 15 commits into from Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions config-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<artifactId>config</artifactId>
</dependency>

<dependency>
<groupId>com.quorum.tessera</groupId>
<artifactId>jaxrs-client</artifactId>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
Expand All @@ -44,11 +49,29 @@
<artifactId>test-util</artifactId>
</dependency>

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
</dependency>

<dependency>
<groupId>com.quorum.tessera</groupId>
<artifactId>key-generation</artifactId>
</dependency>

<dependency>
<groupId>com.quorum.tessera</groupId>
<artifactId>jersey-server</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>2.27</version>
<scope>test</scope>
<type>jar</type>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.quorum.tessera.config.cli;

import com.quorum.tessera.config.Config;
import com.quorum.tessera.config.Peer;
import com.quorum.tessera.config.ServerConfig;
import com.quorum.tessera.config.cli.parsers.ConfigurationParser;
import com.quorum.tessera.jaxrs.client.ClientFactory;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;

/**
* Cli Adapter to be used for runtime updates
*/
public class AdminCliAdapter implements CliAdapter {

private final ClientFactory clientFactory;

public AdminCliAdapter(ClientFactory clientFactory) {
this.clientFactory = Objects.requireNonNull(clientFactory);
}

/**
*
* @param args
* @return CliResult with config object always null.
* @throws Exception
*/
@Override
public CliResult execute(String... args) throws Exception {

final Options options = new Options();

options.addOption(
Option.builder("configfile")
.desc("Path to node configuration file")
.hasArg(true)
.required()
.optionalArg(false)
.numberOfArgs(1)
.argName("PATH")
.build());

options.addOption(Option.builder("addpeer")
.desc("Add peer to running node")
.hasArg(true)
.optionalArg(false)
.numberOfArgs(1)
.argName("URL")
.build());

final List<String> argsList = Arrays.asList(args);

if (argsList.contains("help") || argsList.isEmpty()) {
HelpFormatter formatter = new HelpFormatter();
formatter.setWidth(200);
formatter.printHelp("tessera admin", options);
return new CliResult(0, true, null);
}

final CommandLine line = new DefaultParser().parse(options, args);
if(!line.hasOption("addpeer")) {
System.out.println("No peer defined");
return new CliResult(1, true, null);
}

Config config = new ConfigurationParser().parse(line);

Client restClient = clientFactory.buildFrom(config.getServerConfig());

String peerUrl = line.getOptionValue("addpeer");

final Peer peer = new Peer(peerUrl);

String scheme = Optional.of(config)
.map(Config::getServerConfig)
.map(ServerConfig::getServerUri)
.map(URI::getScheme)
.orElse("http");

Integer port = Optional.of(config)
.map(Config::getServerConfig)
.map(ServerConfig::getPort)
.orElse(80);

URI uri = UriBuilder.fromPath("/")
.port(port)
.host("localhost")
.scheme(scheme).build();


Response response = restClient.target(uri)
.path("config")
.path("peers")
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.put(Entity.entity(peer, MediaType.APPLICATION_JSON));

if(response.getStatus() == Response.Status.CREATED.getStatusCode()) {

System.out.printf("Peer %s added.",response.getLocation());
System.out.println();

return new CliResult(0, true, null);
}

System.err.println("Unable to create peer");

return new CliResult(1, true, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ public interface CliAdapter {

CliResult execute(String... args) throws Exception;

static CliAdapter create() {
return new DefaultCliAdapter();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.quorum.tessera.config.cli;

import com.quorum.tessera.config.Config;
import com.quorum.tessera.jaxrs.client.ClientFactory;
import java.util.Arrays;
import java.util.List;

public enum CliDelegate {

Expand All @@ -18,7 +21,15 @@ public Config getConfig() {

public CliResult execute(String... args) throws Exception {

final CliAdapter cliAdapter = CliAdapter.create();
final List<String> argsList = Arrays.asList(args);

final CliAdapter cliAdapter;

if(argsList.contains("admin")) {
cliAdapter = new AdminCliAdapter(new ClientFactory());
} else {
cliAdapter = new DefaultCliAdapter();
}

final CliResult result = cliAdapter.execute(args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,25 @@
public class CliResult {

private final Integer status;
private final boolean isHelpOn;
private final boolean isKeyGenOn;

private final boolean suppressStartup;

private final Config config;

public CliResult(Integer status, boolean isHelpOn, boolean isKeyGenOn, Config config) {
public CliResult(Integer status, boolean suppressStartup,Config config) {
this.status = Objects.requireNonNull(status);
this.isHelpOn = isHelpOn;
this.isKeyGenOn = isKeyGenOn;
this.suppressStartup = suppressStartup;
this.config = config;
}

public Integer getStatus() {
return status;
}

public boolean isHelpOn() {
return isHelpOn;
public boolean isSuppressStartup() {
return suppressStartup;
}

public boolean isKeyGenOn() {return isKeyGenOn;}

public Optional<Config> getConfig() {
return Optional.ofNullable(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CliResult execute(String... args) throws Exception {
HelpFormatter formatter = new HelpFormatter();
formatter.setWidth(200);
formatter.printHelp("tessera -configfile <PATH> [-keygen <PATH>] [-pidfile <PATH>]", options);
return new CliResult(0, true, false, null);
return new CliResult(0, true, null);
}

try {
Expand Down Expand Up @@ -87,7 +87,7 @@ public CliResult execute(String... args) throws Exception {

new PidFileParser().parse(line);

return new CliResult(0, false, line.hasOption("keygen"), config);
return new CliResult(0, line.hasOption("keygen"), config);

} catch (ParseException exp) {
throw new CliException(exp.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.quorum.tessera.config.Config;
import com.quorum.tessera.config.ConfigFactory;
import com.quorum.tessera.config.util.ConfigFileStore;
import com.quorum.tessera.config.keypairs.ConfigKeyPair;
import com.quorum.tessera.config.util.JaxbUtil;
import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -50,6 +51,8 @@ public Config parse(final CommandLine commandLine) throws IOException {
//we have generated new keys, so we need to output the new configuration
output(commandLine, config);
}

ConfigFileStore.create(path);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.quorum.tessera.config.cli;

import com.quorum.tessera.config.Peer;
import com.quorum.tessera.config.ServerConfig;
import com.quorum.tessera.jaxrs.client.ClientFactory;
import com.quorum.tessera.test.util.ElUtil;
import java.net.URI;
import java.nio.file.Path;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class AdminCliAdapterTest {

private AdminCliAdapter adminCliAdapter;

private ClientFactory clientFactory;

private Invocation.Builder invocationBuilder;

@Before
public void onSetUp() {

invocationBuilder = mock(Invocation.Builder.class);

clientFactory = mock(ClientFactory.class);

Client client = mock(Client.class);
WebTarget webTarget = mock(WebTarget.class);

when(client.target(any(URI.class))).thenReturn(webTarget);
when(webTarget.path(anyString())).thenReturn(webTarget,webTarget);

when(webTarget.request(MediaType.APPLICATION_JSON)).thenReturn(invocationBuilder);

when(invocationBuilder.accept(MediaType.APPLICATION_JSON)).thenReturn(invocationBuilder);

when(clientFactory.buildFrom(any(ServerConfig.class))).thenReturn(client);

adminCliAdapter = new AdminCliAdapter(clientFactory);
}

public void onTearDown() {
verifyNoMoreInteractions(invocationBuilder);
}


@Test
public void help() throws Exception {
//new CliResult(0, true, false, null);
CliResult result = adminCliAdapter.execute("help");
assertThat(result).isNotNull();
assertThat(result.getConfig()).isNotPresent();
assertThat(result.isSuppressStartup()).isTrue();

}

@Test
public void addPeer() throws Exception {

Peer peer = new Peer("http://junit.com:8989");
Entity entity = Entity.entity(peer, MediaType.APPLICATION_JSON);

URI uri = UriBuilder.fromPath("/result").build();
when(invocationBuilder.put(entity)).thenReturn(Response.created(uri).build());

Path configFile = ElUtil.createAndPopulatePaths(getClass().getResource("/sample-config.json"));

CliResult result = adminCliAdapter.execute("-addpeer",peer.getUrl(),"-configfile",configFile.toString());
assertThat(result).isNotNull();
assertThat(result.getConfig()).isNotPresent();
assertThat(result.isSuppressStartup()).isTrue();

assertThat(result.getStatus()).isEqualTo(0);

verify(invocationBuilder).put(entity);


}

@Test
public void addPeerSomethingBadWentDown() throws Exception {

Peer peer = new Peer("http://junit.com:8989");
Entity entity = Entity.entity(peer, MediaType.APPLICATION_JSON);

URI uri = UriBuilder.fromPath("/result").build();
when(invocationBuilder.put(entity)).thenReturn(Response.serverError().build());

Path configFile = ElUtil.createAndPopulatePaths(getClass().getResource("/sample-config.json"));

CliResult result = adminCliAdapter.execute("-addpeer",peer.getUrl(),"-configfile",configFile.toString());
assertThat(result).isNotNull();
assertThat(result.getConfig()).isNotPresent();
assertThat(result.isSuppressStartup()).isTrue();

assertThat(result.getStatus()).isEqualTo(1);

verify(invocationBuilder).put(entity);


}
}
Loading