Skip to content

Commit

Permalink
Add code coverage (#3)
Browse files Browse the repository at this point in the history
Add code coverage and fix code smells in project.
  • Loading branch information
kdebisschop authored Feb 24, 2020
1 parent f803986 commit df28727
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 143 deletions.
9 changes: 7 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ scmVersion {

// Append .0 to satisfy SemVer if the tag version is only X.Y
def origDeserialize = deserialize
def orig
deserialize = { config, position, tagName ->
orig = origDeserialize(config, position, tagName)
String orig = origDeserialize(config, position, tagName)
if (orig.split('\\.').length < 3) {
orig += ".0"
}
Expand All @@ -80,6 +79,12 @@ configurations{
}
}

gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked"
}
}

dependencies {
implementation 'org.rundeck:rundeck-core:3.2.0-20191218'

Expand Down
27 changes: 15 additions & 12 deletions src/main/java/com/bioraft/rundeck/nexus/BranchOrVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
*/
public class BranchOrVersion {

public static final String buildSeparatorRegex = "[_+-]";
public static final String BUILD_SEPARATOR_REGEX = "[_+-]";

String artifactId;
String versionOrBranch;
Expand All @@ -76,20 +76,16 @@ public String getVersion() {
public BranchOrVersion(String path) {
artifactId = component(path);
String tag = tag(path);
build = tag.replaceFirst("^.*" + buildSeparatorRegex + "([a-zA-Z0-9]+)$", "$1");
versionOrBranch = tag.replaceFirst("^(.*)" + buildSeparatorRegex + build + "$", "$1");
sep = tag.replaceFirst("^.*(" + buildSeparatorRegex + ")" + build + "$", "$1");
build = tag.replaceFirst("^.*" + BUILD_SEPARATOR_REGEX + "([a-zA-Z0-9]+)$", "$1");
versionOrBranch = tag.replaceFirst("^(.*)" + BUILD_SEPARATOR_REGEX + build + "$", "$1");
sep = tag.replaceFirst("^.*(" + BUILD_SEPARATOR_REGEX + ")" + build + "$", "$1");
this.parts = versionOrBranch.split("[.]");
}

public Iterator<String> getIterator() {
return Arrays.asList(parts).iterator();
}

public int size() {
return parts.length;
}

/**
* Consider a component version to be anything with 2 consecutive integer parts.
* Anything else is considered to be a branch.
Expand All @@ -106,23 +102,30 @@ public boolean isVersion() {
* @return Return 0 if equal, 1 if this is greater, -1 if that is greater.
*/
public int compareTo(Object other) {
if (other == null) {
return 1;
}
BranchOrVersion that = (BranchOrVersion) other;
CompareToBuilder comparator = new CompareToBuilder();
Iterator<String> thatIterator = that.getIterator();
if (this.size() == 0 && that.size() == 0) {
return 0;
}
Iterator<String> thisIterator = getIterator();
while (thatIterator.hasNext()) {
if (!thisIterator.hasNext()) {
break;
if (comparator.toComparison() == 0) {
return -1;
} else {
break;
}
}
String thisOne = thisIterator.next();
String thatOne = thatIterator.next();
comparator.append(partOne(thisOne), partOne(thatOne));
comparator.append(partTwo(thisOne), partTwo(thatOne));
comparator.append(partThree(thisOne), partThree(thatOne));
}
if (comparator.toComparison() == 0 && thisIterator.hasNext()) {
return 1;
}
comparator.append(partOne(this.build), partOne(that.build));
comparator.append(partTwo(this.build), partTwo(that.build));
comparator.append(partThree(this.build), partThree(that.build));
Expand Down
67 changes: 26 additions & 41 deletions src/main/java/com/bioraft/rundeck/nexus/Nexus3OptionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
*/
package com.bioraft.rundeck.nexus;

import java.util.List;
import java.util.Map;

import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.core.plugins.configuration.PropertyScope;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty;
import com.dtolabs.rundeck.plugins.option.OptionValue;
import com.dtolabs.rundeck.plugins.option.OptionValuesPlugin;

import okhttp3.OkHttpClient;

import java.util.*;

@Plugin(name = Nexus3OptionProvider.PLUGIN_NAME, service = ServiceNameConstants.OptionValues)
@PluginDescription(title = "Nexus3 Images", description = "Filtered and sorted docker images on nexus server.")
public class Nexus3OptionProvider implements OptionValuesPlugin {
Expand All @@ -39,16 +37,16 @@ public class Nexus3OptionProvider implements OptionValuesPlugin {
@PluginProperty(title = "Endpoint scheme", description = "Nexus server scheme", required = true, defaultValue = "https", scope = PropertyScope.Project)
private String endpointScheme;

@PluginProperty(title = "Endpoint host", description = "Nexus server hostname", required = false, defaultValue = "", scope = PropertyScope.Project)
@PluginProperty(title = "Endpoint host", description = "Nexus server hostname", defaultValue = "", scope = PropertyScope.Project)
private String endpointHost;

@PluginProperty(title = "Endpoint path", description = "Nexus path with leading slash", required = true, defaultValue = "/service/rest/v1/search/assets", scope = PropertyScope.Project)
private String endpointPath;

@PluginProperty(title = "User", description = "Nexus server user name", required = false, defaultValue = "", scope = PropertyScope.Project)
@PluginProperty(title = "User", description = "Nexus server user name", defaultValue = "", scope = PropertyScope.Project)
private String user;

@PluginProperty(title = "Password", description = "Nexus server password", required = false, defaultValue = "", scope = PropertyScope.Project)
@PluginProperty(title = "Password", description = "Nexus server password", defaultValue = "", scope = PropertyScope.Project)
private String password;

@PluginProperty(title = "Repository", description = "Nexus repository", required = true, defaultValue = "docker", scope = PropertyScope.Project)
Expand All @@ -57,7 +55,7 @@ public class Nexus3OptionProvider implements OptionValuesPlugin {
@PluginProperty(title = "Component name", description = "Nexus component name", required = true, defaultValue = "*", scope = PropertyScope.Project)
private String componentName;

@PluginProperty(title = "Component version", description = "Nexus component version", required = false, scope = PropertyScope.Project)
@PluginProperty(title = "Component version", description = "Nexus component version", scope = PropertyScope.Project)
private String componentVersion;

public Nexus3OptionProvider() {
Expand All @@ -68,44 +66,31 @@ public Nexus3OptionProvider(OkHttpClient client) {
this.client = client;
}

Map<String, String> config;

@Override
public List<OptionValue> getOptionValues(Map configuration) {
@SuppressWarnings("unchecked")
Map<String, String> config = (Map<String, String>) configuration;

if (!config.containsKey("endpointScheme") && endpointScheme != null && endpointScheme.length() > 0) {
config.put("endpointScheme", endpointScheme);
}

if (!config.containsKey("endpointHost") && endpointHost != null && endpointHost.length() > 0) {
config.put("endpointHost", endpointHost);
}

if (!config.containsKey("endpointPath") && endpointPath != null && endpointPath.length() > 0) {
config.put("endpointPath", endpointPath);
}

if (!config.containsKey("user") && user != null && user.length() > 0) {
config.put("user", user);
}

if (!config.containsKey("password") && password != null && password.length() > 0) {
config.put("password", password);
}
config = new HashMap<>();

if (!config.containsKey("repository") && repository != null && repository.length() > 0) {
config.put("repository", repository);
}

if (!config.containsKey("componentName") && componentName != null && componentName.length() > 0) {
config.put("componentName", componentName);
}

if (!config.containsKey("componentVersion") && componentVersion != null && componentVersion.length() > 0) {
config.put("componentVersion", componentVersion);
}
setVariable(configuration,"endpointScheme", endpointScheme);
setVariable(configuration,"endpointHost", endpointHost);
setVariable(configuration,"endpointPath", endpointPath);
setVariable(configuration,"user", user);
setVariable(configuration,"password", password);
setVariable(configuration,"repository", repository);
setVariable(configuration,"componentName", componentName);
setVariable(configuration,"componentVersion",componentVersion);

OptionProviderImpl worker = new OptionProviderImpl(client);
return worker.getOptionValues(config);
}

@SuppressWarnings("rawtypes")
private void setVariable(Map configuration, String variableName, String variable) {
if (configuration.containsKey(variableName)) {
config.put(variableName, configuration.get(variableName).toString());
} else if (variable != null && variable.length() > 0) {
config.put(variableName, variable);
}
}
}
Loading

0 comments on commit df28727

Please sign in to comment.