forked from hyperledger/besu
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PIE-2052] Besu CLI -V to print plugin versions (hyperledger#123)
-- Use (PicoCLI) custom factory to construct version provider which can return optional plugin versions -- Use plugin's jar manifest implementation and version to build plugin version during plugin registration Signed-off-by: Usman Saleem <[email protected]> Signed-off-by: edwardmack <[email protected]>
- Loading branch information
1 parent
ada6f23
commit 7530cad
Showing
7 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
besu/src/main/java/org/hyperledger/besu/cli/util/BesuCommandCustomFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.cli.util; | ||
|
||
import org.hyperledger.besu.services.PluginVersionsProvider; | ||
|
||
import java.lang.reflect.Constructor; | ||
|
||
import picocli.CommandLine; | ||
|
||
/** | ||
* Custom PicoCli IFactory to handle version provider construction with plugin versions. Based on | ||
* same logic as PicoCLI DefaultFactory. | ||
*/ | ||
public class BesuCommandCustomFactory implements CommandLine.IFactory { | ||
private final PluginVersionsProvider pluginVersionsProvider; | ||
|
||
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)) { | ||
return (T) new VersionProvider(pluginVersionsProvider); | ||
} | ||
|
||
final Constructor<T> constructor = cls.getDeclaredConstructor(); | ||
try { | ||
return constructor.newInstance(); | ||
} catch (Exception e) { | ||
constructor.setAccessible(true); | ||
return constructor.newInstance(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
besu/src/main/java/org/hyperledger/besu/services/PluginVersionsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Collection; | ||
|
||
public interface PluginVersionsProvider { | ||
Collection<String> getPluginVersions(); | ||
} |
48 changes: 48 additions & 0 deletions
48
besu/src/test/java/org/hyperledger/besu/cli/util/BesuCommandCustomFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* 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.cli.util; | ||
|
||
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.Arrays; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class BesuCommandCustomFactoryTest { | ||
|
||
@Mock private PluginVersionsProvider pluginVersionsProvider; | ||
|
||
@Before | ||
public void initMocks() { | ||
when(pluginVersionsProvider.getPluginVersions()).thenReturn(Arrays.asList("v1", "v2")); | ||
} | ||
|
||
@Test | ||
public void testCreateVersionProviderInstance() throws Exception { | ||
final BesuCommandCustomFactory besuCommandCustomFactory = | ||
new BesuCommandCustomFactory(pluginVersionsProvider); | ||
final VersionProvider versionProvider = besuCommandCustomFactory.create(VersionProvider.class); | ||
assertThat(versionProvider.getVersion()).containsExactly(BesuInfo.version(), "v1", "v2"); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
besu/src/test/java/org/hyperledger/besu/cli/util/VersionProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.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() { | ||
when(pluginVersionsProvider.getPluginVersions()).thenReturn(emptyList()); | ||
final VersionProvider versionProvider = new VersionProvider(pluginVersionsProvider); | ||
assertThat(versionProvider.getVersion()).containsOnly(BesuInfo.version()); | ||
} | ||
|
||
@Test | ||
public void validateVersionListGenerateValidValues() { | ||
when(pluginVersionsProvider.getPluginVersions()).thenReturn(Collections.singletonList("test")); | ||
final VersionProvider versionProvider = new VersionProvider(pluginVersionsProvider); | ||
assertThat(versionProvider.getVersion()).containsExactly(BesuInfo.version(), "test"); | ||
} | ||
} |