Skip to content

Commit

Permalink
fix: fix static mocking
Browse files Browse the repository at this point in the history
Signed-off-by: Zvi Grinberg <zgrinber@redhat.com>

return matrix strategy

Signed-off-by: Zvi Grinberg <zgrinber@redhat.com>
  • Loading branch information
zvigrinberg committed Mar 5, 2024
1 parent c6d2d17 commit c3ed57e
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/test/java/com/redhat/exhort/impl/ExhortApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.mockito.MockedStatic;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

import static com.redhat.exhort.providers.Java_Maven_Provider_Test.getOutputFileAndOverwriteItWithMock;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;
Expand Down Expand Up @@ -78,9 +82,9 @@ void Integration_Test_End_To_End_Stack_Analysis(Ecosystem.Type packageManager) t
// Github action runner with all maven and java versions seems to enter infinite loop in integration tests of MAVEN when runnig dependency maven plugin to produce verbose text dependenct tree format.
// locally it's not recreated with same versions
mockMavenDependencyTree(packageManager);
releaseStaticMock(packageManager);
AnalysisReport analysisReportResult = api.stackAnalysis(pathToManifest).get();
handleJsonResponse(analysisReportResult,true);
releaseStaticMock(packageManager);
}

private void releaseStaticMock(Ecosystem.Type packageManager) {
Expand All @@ -100,11 +104,11 @@ void Integration_Test_End_To_End_Stack_Analysis_Mixed(Ecosystem.Type packageMana
// Github action runner with all maven and java versions seems to enter infinite loop in integration tests of MAVEN when runnig dependency maven plugin to produce verbose text dependenct tree format.
// locally it's not recreated with same versions
mockMavenDependencyTree(packageManager);
releaseStaticMock(packageManager);
AnalysisReport analysisReportJson = api.stackAnalysisMixed(pathToManifest).get().json;
String analysisReportHtml = new String(api.stackAnalysisMixed(pathToManifest).get().html);
handleJsonResponse(analysisReportJson,true);
handleHtmlResponse(analysisReportHtml);
releaseStaticMock(packageManager);
}

@Tag("IntegrationTest")
Expand All @@ -117,8 +121,8 @@ void Integration_Test_End_To_End_Stack_Analysis_Html(Ecosystem.Type packageManag
// Github action runner with all maven and java versions seems to enter infinite loop in integration tests of MAVEN when runnig dependency maven plugin to produce verbose text dependenct tree format.
// locally it's not recreated with same versions
mockMavenDependencyTree(packageManager);
releaseStaticMock(packageManager);
String analysisReportHtml = new String(api.stackAnalysisHtml(pathToManifest).get());
releaseStaticMock(packageManager);
handleHtmlResponse(analysisReportHtml);
}

Expand Down Expand Up @@ -190,8 +194,21 @@ private void mockMavenDependencyTree(Ecosystem.Type packageManager) throws IOExc
try (var is = getResourceAsStreamDecision(getClass(), new String [] { "tst_manifests", "it","maven", "depTree.txt"})) {
depTree = new String(is.readAllBytes());
}
mockedOperations.when(() -> Operations.runProcess(any(),any())).thenAnswer(invocationOnMock -> getOutputFileAndOverwriteItWithMock(depTree, invocationOnMock, "-DoutputFile"));
mockedOperations.when(() -> Operations.runProcess(any(),any())).thenAnswer(invocationOnMock -> { return getOutputFileAndOverwriteItWithMock(depTree, invocationOnMock, "-DoutputFile");});
}
}

public static String getOutputFileAndOverwriteItWithMock(String outputFileContent, InvocationOnMock invocationOnMock, String parameterPrefix) throws IOException {
String[] rawArguments = (String[]) invocationOnMock.getRawArguments()[0];
Optional<String> outputFileArg = Arrays.stream(rawArguments).filter(arg -> arg!= null && arg.startsWith(parameterPrefix)).findFirst();
String outputFilePath=null;
if(outputFileArg.isPresent())
{
String outputFile = outputFileArg.get();
outputFilePath = outputFile.substring(outputFile.indexOf("=") + 1);
Files.writeString(Path.of(outputFilePath), outputFileContent);
}
return outputFilePath;
}

}
Expand Down

0 comments on commit c3ed57e

Please sign in to comment.