Skip to content

Commit

Permalink
fix: Resolve several issues discovered in code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Killough committed May 29, 2020
1 parent fc0d762 commit 9284bef
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
Expand Up @@ -23,7 +23,6 @@
package com.synopsys.integration.detect.tool.detector.inspectors.nuget;

import java.io.File;
import java.util.Optional;

import com.synopsys.integration.detect.workflow.airgap.AirGapInspectorPaths;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;
Expand Down Expand Up @@ -55,10 +54,8 @@ public File locateExeInspector() throws DetectableException {
}

private File locateInspector(String childName) throws DetectableException {
Optional<File> nugetAirGapPath = airGapInspectorPaths.getNugetInspectorAirGapFile();
if (nugetAirGapPath.isPresent()) {
return new File(nugetAirGapPath.get(), childName);
}
throw new DetectableException("Could not get the nuget air gap path");
return airGapInspectorPaths.getNugetInspectorAirGapFile()
.map(nugetAirGapPath -> new File(nugetAirGapPath, childName))
.orElseThrow(() -> new DetectableException("Could not get the nuget air gap path"));
}
}
Expand Up @@ -137,7 +137,7 @@ private NugetInspector findInspector(File nupkgFolder, String inspectorName, Fun
logger.debug("Found nuget inspector: {}", inspectorExecutable);
return inspectorInitializer.apply(inspectorExecutable);
} else {
throw new DetectableException("Unable to find nuget inspector, looking for " + inspectorName + " in " + toolsFolder.toString());
throw new DetectableException(String.format("Unable to find nuget inspector, looking for %s in %s", inspectorName, toolsFolder.toString()));
}
}

Expand Down
Expand Up @@ -25,9 +25,11 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Optional;

import javax.annotation.Nullable;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -45,24 +47,24 @@ public NugetInspectorInstaller(ArtifactResolver artifactResolver) {
this.artifactResolver = artifactResolver;
}

public File installDotNet3(File destination, Optional<String> overrideVersion) throws DetectableException {
public File installDotNet3(File destination, @Nullable String overrideVersion) throws DetectableException {
logger.debug("Will attempt to resolve the dotnet3 inspector version.");
return installInspector(destination, overrideVersion, ArtifactoryConstants.NUGET_DOTNET3_INSPECTOR_REPO, ArtifactoryConstants.NUGET_DOTNET3_INSPECTOR_PROPERTY, ArtifactoryConstants.NUGET_DOTNET3_INSPECTOR_VERSION_OVERRIDE);
}

public File installDotNet(File destination, Optional<String> overrideVersion) throws DetectableException {
public File installDotNet(File destination, @Nullable String overrideVersion) throws DetectableException {
logger.debug("Will attempt to resolve the dotnet inspector version.");
return installInspector(destination, overrideVersion, ArtifactoryConstants.NUGET_INSPECTOR_REPO, ArtifactoryConstants.NUGET_INSPECTOR_PROPERTY, ArtifactoryConstants.NUGET_INSPECTOR_VERSION_OVERRIDE);
}

public File installExeInspector(File destination, Optional<String> overrideVersion) throws DetectableException {
public File installExeInspector(File destination, @Nullable String overrideVersion) throws DetectableException {
logger.debug("Will attempt to resolve the classic inspector version.");
return installInspector(destination, overrideVersion, ArtifactoryConstants.CLASSIC_NUGET_INSPECTOR_REPO, ArtifactoryConstants.CLASSIC_NUGET_INSPECTOR_PROPERTY, ArtifactoryConstants.CLASSIC_NUGET_INSPECTOR_VERSION_OVERRIDE);
}

private File installInspector(File destination, Optional<String> overrideVersion, String inspectorRepo, String inspectorProperty, String inspectorVersionOverride) throws DetectableException {
private File installInspector(File destination, @Nullable String overrideVersion, String inspectorRepo, String inspectorProperty, String inspectorVersionOverride) throws DetectableException {
try {
String source = artifactResolver.resolveArtifactLocation(ArtifactoryConstants.ARTIFACTORY_URL, inspectorRepo, inspectorProperty, overrideVersion.orElse(""), inspectorVersionOverride);
String source = artifactResolver.resolveArtifactLocation(ArtifactoryConstants.ARTIFACTORY_URL, inspectorRepo, inspectorProperty, StringUtils.defaultString(overrideVersion), inspectorVersionOverride);
return installFromSource(destination, source);
} catch (Exception e) {
throw new DetectableException("Unable to install the nuget inspector from Artifactory.", e);
Expand Down
Expand Up @@ -23,7 +23,6 @@
package com.synopsys.integration.detect.tool.detector.inspectors.nuget;

import java.io.File;
import java.util.Optional;

import com.synopsys.integration.detect.workflow.file.DirectoryManager;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;
Expand Down Expand Up @@ -55,10 +54,10 @@ public File locateExeInspector() throws DetectableException {
return locateInspector(nugetInspectorInstaller::installExeInspector);
}

private File locateInspector(ThrowingBiFunction<File, Optional<String>, File, DetectableException> inspectorInstaller) throws DetectableException {
private File locateInspector(ThrowingBiFunction<File, String, File, DetectableException> inspectorInstaller) throws DetectableException {
try {
File nugetDirectory = directoryManager.getPermanentDirectory("nuget");
return inspectorInstaller.apply(nugetDirectory, Optional.ofNullable(overrideVersion));
return inspectorInstaller.apply(nugetDirectory, overrideVersion);
} catch (Exception e) {
throw new DetectableException("Unable to install the nuget inspector from Artifactory.", e);
}
Expand Down
Expand Up @@ -24,7 +24,6 @@

import java.io.File;
import java.io.IOException;
import java.util.Optional;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -57,10 +56,10 @@ public void installNugetDependencies(File nugetFolder) throws DetectUserFriendly
installThenCopy(nugetFolder, AirgapNugetInspectorLocator.INSPECTOR_DIR_CLASSIC, nugetInspectorInstaller::installExeInspector);
}

private void installThenCopy(File nugetFolder, String folderName, ThrowingBiFunction<File, Optional<String>, File, DetectableException> installer) throws DetectUserFriendlyException {
private void installThenCopy(File nugetFolder, String folderName, ThrowingBiFunction<File, String, File, DetectableException> installer) throws DetectUserFriendlyException {
try {
File inspectorFolder = new File(nugetFolder, folderName);
File installTarget = installer.apply(inspectorFolder, Optional.empty());
File installTarget = installer.apply(inspectorFolder, null);
FileUtils.copyDirectory(installTarget, inspectorFolder);
FileUtils.deleteDirectory(installTarget);
} catch (DetectableException | IOException e) {
Expand Down
@@ -1,52 +1,55 @@
package com.synopsys.integration.detect.tool.detector.inspectors.nuget;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;

import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.synopsys.integration.detectable.detectable.exception.DetectableException;

public class DotNetRuntimeAvailabilityVerifierTest {
private static final List<String> TEST_RUNTIME_STRINGS = Arrays.asList(
private static final List<String> VALID_RUNTIME_STRINGS = Arrays.asList(
"Microsoft.AspNetCore.All 2.1.18 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]",
"Microsoft.AspNetCore.All 2.1.18 [/usr/local/share/dotnet/2.1.18/shared/Microsoft.AspNetCore.All]",
"Microsoft.AspNetCore.App 2.1.18 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]",
"Microsoft.AspNetCore.App 2.1.18 [/usr/local/share/dotnet_1.0.0/shared/Microsoft.AspNetCore.App]",
"Microsoft.NETCore.App 2.1.18 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]",
"Microsoft.NETCore.App 3.1.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]"
);

@Test
public void isRuntimeAvailableTest() throws DetectableException {
DotNetRuntimeFinder runtimeFinder = Mockito.mock(DotNetRuntimeFinder.class);
Mockito.when(runtimeFinder.listAvailableRuntimes()).thenReturn(TEST_RUNTIME_STRINGS);
Mockito.when(runtimeFinder.listAvailableRuntimes()).thenReturn(VALID_RUNTIME_STRINGS);

DotNetRuntimeAvailabilityVerifier runtimeResolver = new DotNetRuntimeAvailabilityVerifier(runtimeFinder);
assertAvailability(Assert::assertTrue, runtimeResolver, 2, 1);
assertAvailability(Assert::assertTrue, runtimeResolver, 3, 1);
assertTrue("Expected 2.1.18 runtime to be available when passed as an array of Integers", runtimeResolver.isRuntimeAvailable(2, 1, 18));
assertAvailability(Assertions::assertTrue, runtimeResolver, 2, 1);
assertAvailability(Assertions::assertTrue, runtimeResolver, 3, 1);
assertTrue(runtimeResolver.isRuntimeAvailable(2, 1, 18), "Expected 2.1.18 runtime to be available when passed as an array of Integers");

assertAvailability(Assert::assertFalse, runtimeResolver, 4, 0);
assertAvailability(Assert::assertFalse, runtimeResolver, 2, 2);
assertAvailability(Assertions::assertFalse, runtimeResolver, 4, 0);
assertAvailability(Assertions::assertFalse, runtimeResolver, 2, 2);
assertAvailability(Assertions::assertFalse, runtimeResolver, 1, 0);
}

@Test
public void isRuntimeAvailableSemanticVersionStringTest() throws DetectableException {
DotNetRuntimeFinder runtimeFinder = Mockito.mock(DotNetRuntimeFinder.class);
Mockito.when(runtimeFinder.listAvailableRuntimes()).thenReturn(TEST_RUNTIME_STRINGS);
Mockito.when(runtimeFinder.listAvailableRuntimes()).thenReturn(VALID_RUNTIME_STRINGS);

DotNetRuntimeAvailabilityVerifier runtimeResolver = new DotNetRuntimeAvailabilityVerifier(runtimeFinder);
assertTrue("Expected 2.1.18 runtime to be available when passed as a semanticVersion string", runtimeResolver.isRuntimeAvailable("2.1.18"));
assertFalse("Expected 4.0 runtime not to be available when passed as a semanticVersion string", runtimeResolver.isRuntimeAvailable("4.0"));
assertTrue(runtimeResolver.isRuntimeAvailable("2.1.18"), "Expected 2.1.18 runtime to be available when passed as a semanticVersion string");
assertFalse(runtimeResolver.isRuntimeAvailable("4.0"), "Expected 4.0 runtime not to be available when passed as a semanticVersion string");
}

private void assertAvailability(BiConsumer<String, Boolean> assertion, DotNetRuntimeAvailabilityVerifier nugetRuntimeResolver, Integer majorVersion, Integer minorVersion) throws DetectableException {
private void assertAvailability(BiConsumer<Boolean, String> assertion, DotNetRuntimeAvailabilityVerifier nugetRuntimeResolver, Integer majorVersion, Integer minorVersion) throws DetectableException {
boolean isVersionAvailable = nugetRuntimeResolver.isRuntimeAvailable(majorVersion, minorVersion);
assertion.accept(String.format("Different runtime availability expected for %d.%d runtime", majorVersion, minorVersion), isVersionAvailable);
assertion.accept(isVersionAvailable, String.format("Different runtime availability expected for %d.%d runtime", majorVersion, minorVersion));
}
}

0 comments on commit 9284bef

Please sign in to comment.