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

[PIE-2052] Besu CLI -V to print plugin versions #123

Merged
merged 9 commits into from
Oct 22, 2019
Prev Previous commit
Next Next commit
[PIE-2052] CLI -V to print plugin versions
 -- Introduce PluginVersionsProvider interface
 -- Modify VersionProvider to obtain plugins in versions method instead of constructor because of late initialization

Signed-off-by: Usman Saleem <usman@usmans.info>
  • Loading branch information
usmansaleem committed Oct 22, 2019
commit 134f7c9772daed5e3f51644d770b7cd1b0d256cf
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.hyperledger.besu.cli.util;

import org.hyperledger.besu.services.BesuPluginContextImpl;
import org.hyperledger.besu.services.PluginVersionsProvider;

import java.lang.reflect.Constructor;

Expand All @@ -25,18 +25,17 @@
* same logic as PicoCLI DefaultFactory.
*/
public class BesuCommandCustomFactory implements CommandLine.IFactory {
private final BesuPluginContextImpl besuPluginContext;
private final PluginVersionsProvider pluginVersionsProvider;

public BesuCommandCustomFactory(final BesuPluginContextImpl besuPluginContext) {
this.besuPluginContext = besuPluginContext;
public BesuCommandCustomFactory(final PluginVersionsProvider pluginVersionsProvider) {
this.pluginVersionsProvider = pluginVersionsProvider;
}

@SuppressWarnings("unchecked")
@Override
public <T> T create(final Class<T> cls) throws Exception {
if (CommandLine.IVersionProvider.class.isAssignableFrom(cls)) {
// the besuPluginContext has registered required plugins by this time.
return (T) new VersionProvider(besuPluginContext.getPluginVersions());
return (T) new VersionProvider(pluginVersionsProvider);
}

final Constructor<T> constructor = cls.getDeclaredConstructor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@
package org.hyperledger.besu.cli.util;

import org.hyperledger.besu.BesuInfo;
import org.hyperledger.besu.services.PluginVersionsProvider;

import java.util.ArrayList;
import java.util.List;

import picocli.CommandLine;

public class VersionProvider implements CommandLine.IVersionProvider {
private final String[] versions;
private final PluginVersionsProvider pluginVersionsProvider;

public VersionProvider(final List<String> pluginVersions) {
final List<String> versionsList = new ArrayList<>();
versionsList.add(BesuInfo.version());
if (!pluginVersions.isEmpty()) {
versionsList.addAll(pluginVersions);
}
versions = versionsList.toArray(String[]::new);
public VersionProvider(final PluginVersionsProvider pluginVersionsProvider) {
this.pluginVersionsProvider = pluginVersionsProvider;
}

@Override
public String[] getVersion() {
return versions;
// the PluginVersionsProvider has registered plugins and their versions by this time.
final List<String> pluginVersionsList = this.pluginVersionsProvider.getPluginVersions();
usmansaleem marked this conversation as resolved.
Show resolved Hide resolved
final List<String> versionsList = new ArrayList<>();
versionsList.add(BesuInfo.version());
versionsList.addAll(pluginVersionsList);
return versionsList.toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class BesuPluginContextImpl implements BesuContext {
public class BesuPluginContextImpl implements BesuContext, PluginVersionsProvider {

private static final Logger LOG = LogManager.getLogger();

Expand Down Expand Up @@ -118,7 +118,6 @@ private void addPluginVersion(final BesuPlugin plugin) {
.filter(Predicate.not(String::isBlank))
.orElse("<Unknown Version>");
final String pluginVersion = implTitle + "/" + implVersion;
LOG.debug("Plugin Version: {}", pluginVersion);
pluginVersions.add(pluginVersion);
}

Expand Down Expand Up @@ -171,6 +170,7 @@ public void stopPlugins() {
state = Lifecycle.STOPPED;
}

@Override
public List<String> getPluginVersions() {
usmansaleem marked this conversation as resolved.
Show resolved Hide resolved
return Collections.unmodifiableList(pluginVersions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.services;

import java.util.List;

public interface PluginVersionsProvider {
List<String> getPluginVersions();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static org.mockito.Mockito.when;

import org.hyperledger.besu.BesuInfo;
import org.hyperledger.besu.services.BesuPluginContextImpl;
import org.hyperledger.besu.services.PluginVersionsProvider;

import java.util.Arrays;

Expand All @@ -31,17 +31,17 @@
@RunWith(MockitoJUnitRunner.class)
public class BesuCommandCustomFactoryTest {

@Mock private BesuPluginContextImpl besuPluginContextImpl;
@Mock private PluginVersionsProvider pluginVersionsProvider;

@Before
public void initMocks() {
when(besuPluginContextImpl.getPluginVersions()).thenReturn(Arrays.asList("v1", "v2"));
when(pluginVersionsProvider.getPluginVersions()).thenReturn(Arrays.asList("v1", "v2"));
}

@Test
public void testCreateVersionProviderInstance() throws Exception {
BesuCommandCustomFactory besuCommandCustomFactory =
usmansaleem marked this conversation as resolved.
Show resolved Hide resolved
new BesuCommandCustomFactory(besuPluginContextImpl);
new BesuCommandCustomFactory(pluginVersionsProvider);
VersionProvider versionProvider = besuCommandCustomFactory.create(VersionProvider.class);
usmansaleem marked this conversation as resolved.
Show resolved Hide resolved
assertThat(versionProvider.getVersion()).containsExactly(BesuInfo.version(), "v1", "v2");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,36 @@
*/
package org.hyperledger.besu.cli.util;

import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import org.hyperledger.besu.BesuInfo;
import org.hyperledger.besu.services.PluginVersionsProvider;

import java.util.Collections;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class VersionProviderTest {

@Mock private PluginVersionsProvider pluginVersionsProvider;

@Test
public void validateEmptyListGenerateBesuInfoVersionOnly() {
VersionProvider versionProvider = new VersionProvider(Collections.emptyList());
when(pluginVersionsProvider.getPluginVersions()).thenReturn(emptyList());
VersionProvider versionProvider = new VersionProvider(pluginVersionsProvider);
assertThat(versionProvider.getVersion()).containsOnly(BesuInfo.version());
}

@Test
public void validateVersionListGenerateValidValues() {
VersionProvider versionProvider = new VersionProvider(Collections.singletonList("test"));
when(pluginVersionsProvider.getPluginVersions()).thenReturn(Collections.singletonList("test"));
VersionProvider versionProvider = new VersionProvider(pluginVersionsProvider);
assertThat(versionProvider.getVersion()).containsExactly(BesuInfo.version(), "test");
}
}