Skip to content

Commit

Permalink
test: final touches
Browse files Browse the repository at this point in the history
Signed-off-by: Zvi Grinberg <zgrinber@redhat.com>
  • Loading branch information
zvigrinberg committed Mar 5, 2024
1 parent b17c33a commit 3542331
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 16 deletions.
18 changes: 18 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.6/apache-maven-3.9.6-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
3 changes: 0 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,6 @@ limitations under the License.]]>
<artifactId>junit-platform-maven-plugin</artifactId>
<version>${junit-platform-maven-plugin.version}</version>
<configuration>
<!-- <tags>-->
<!-- <tag>!gitTest</tag>-->
<!-- </tags>-->
</configuration>
</plugin>
</plugins>
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/redhat/exhort/ExhortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected String getFileFromResource(String fileName, String... pathList) {
try {
var tmpDir = Files.createTempDirectory("exhort_test_");
tmpFile = Files.createFile(tmpDir.resolve(fileName));
try (var is = getResourceAsStreamDecision(pathList)) {
try (var is = getResourceAsStreamDecision(this.getClass(), pathList)) {
if(Objects.nonNull(is)) {
Files.write(tmpFile, is.readAllBytes());
}
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/com/redhat/exhort/ExhortTest.java.orig
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ public class ExhortTest {
return new String(bytes);
}

<<<<<<< HEAD
private static InputStream getResourceAsStreamDecision(String[] list) throws IOException {
InputStream resourceAsStreamFromModule = ExhortTest.class.getModule().getResourceAsStream(String.join("/", list));
=======
public static InputStream getResourceAsStreamDecision(Class<? extends ExhortTest> theClass, String[] list) throws IOException {
InputStream resourceAsStreamFromModule = theClass.getModule().getResourceAsStream(String.join("/", list));
>>>>>>> 73f7443 (test: fix and tailor tests also for new java versions)
if (Objects.isNull(resourceAsStreamFromModule)) {
return theClass.getClassLoader().getResourceAsStream(String.join("/", list));
}
Expand All @@ -54,7 +49,11 @@ public class ExhortTest {
try {
var tmpDir = Files.createTempDirectory("exhort_test_");
tmpFile = Files.createFile(tmpDir.resolve(fileName));
<<<<<<< HEAD
try (var is = getResourceAsStreamDecision(pathList)) {
=======
try (var is = getResourceAsStreamDecision(this.getClass(), pathList)) {
>>>>>>> java-enhanced-it-working
if(Objects.nonNull(is)) {
Files.write(tmpFile, is.readAllBytes());
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/redhat/exhort/impl/ExhortApiIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ class ExhortApiIT extends ExhortTest {
@BeforeAll
static void beforeAll() {
api = new ExhortApi();
System.setProperty("EXHORT_DEV_MODE","true");
System.setProperty("RHDA_SOURCE","exhort-java-api-it");
System.setProperty("EXHORT_DEV_MODE","false");
ecoSystemsManifestNames = Map.of("golang", "go.mod","maven","pom.xml","npm","package.json","pypi","requirements.txt");

}

@Tag("IntegrationTest")
@AfterAll
static void afterAll() {
System.clearProperty("RHDA_SOURCE");
System.clearProperty("EXHORT_DEV_MODE");
api = null;
}
Expand Down
226 changes: 226 additions & 0 deletions src/test/java/com/redhat/exhort/impl/ExhortApiIT.java.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
* Copyright © 2023 Red Hat, Inc.
*
* 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.
*/
package com.redhat.exhort.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.exhort.Api;
import com.redhat.exhort.ExhortTest;
import com.redhat.exhort.api.AnalysisReport;
import com.redhat.exhort.api.ProviderReport;
import com.redhat.exhort.providers.HelperExtension;
import com.redhat.exhort.tools.Ecosystem;
import com.redhat.exhort.tools.Operations;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.extension.ExtendWith;
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 org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mockStatic;

@Tag("IntegrationTest")
@ExtendWith(HelperExtension.class)
@ExtendWith(MockitoExtension.class)
class ExhortApiIT extends ExhortTest {

private static Api api;
private static Map<String,String> ecoSystemsManifestNames;

private MockedStatic<Operations> mockedOperations;
@BeforeAll
static void beforeAll() {
api = new ExhortApi();
<<<<<<< HEAD
System.setProperty("EXHORT_DEV_MODE","true");
=======
System.setProperty("RHDA_SOURCE","exhort-java-api-it");
System.setProperty("EXHORT_DEV_MODE","false");
>>>>>>> java-enhanced-it-working
ecoSystemsManifestNames = Map.of("golang", "go.mod","maven","pom.xml","npm","package.json","pypi","requirements.txt");

}

@Tag("IntegrationTest")
@AfterAll
static void afterAll() {
<<<<<<< HEAD
=======
System.clearProperty("RHDA_SOURCE");
>>>>>>> java-enhanced-it-working
System.clearProperty("EXHORT_DEV_MODE");
api = null;
}
@Tag("IntegrationTest")
@ParameterizedTest
@EnumSource(value = Ecosystem.Type.class, names = { "GOLANG", "MAVEN", "NPM", "PYTHON" })
void Integration_Test_End_To_End_Stack_Analysis(Ecosystem.Type packageManager) throws IOException, ExecutionException, InterruptedException {
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType());
String pathToManifest = getFileFromResource(manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName);
preparePythonEnvironment(packageManager);
// 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);
AnalysisReport analysisReportResult = api.stackAnalysis(pathToManifest).get();
handleJsonResponse(analysisReportResult,true);
releaseStaticMock(packageManager);
}

private void releaseStaticMock(Ecosystem.Type packageManager) {
if(packageManager.equals(Ecosystem.Type.MAVEN)) {
this.mockedOperations.close();
}
}


@Tag("IntegrationTest")
@ParameterizedTest
@EnumSource(value = Ecosystem.Type.class, names = { "GOLANG", "MAVEN", "NPM", "PYTHON" })
void Integration_Test_End_To_End_Stack_Analysis_Mixed(Ecosystem.Type packageManager) throws IOException, ExecutionException, InterruptedException {
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType());
String pathToManifest = getFileFromResource(manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName);
preparePythonEnvironment(packageManager);
// 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);
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")
@ParameterizedTest
@EnumSource(value = Ecosystem.Type.class, names = { "GOLANG", "MAVEN", "NPM", "PYTHON" })
void Integration_Test_End_To_End_Stack_Analysis_Html(Ecosystem.Type packageManager) throws IOException, ExecutionException, InterruptedException {
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType());
String pathToManifest = getFileFromResource(manifestFileName, "tst_manifests", "it", packageManager.getType(), manifestFileName);
preparePythonEnvironment(packageManager);
// 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);
String analysisReportHtml = new String(api.stackAnalysisHtml(pathToManifest).get());
releaseStaticMock(packageManager);
handleHtmlResponse(analysisReportHtml);
}


@Tag("IntegrationTest")
@ParameterizedTest
@EnumSource(value = Ecosystem.Type.class, names = { "GOLANG", "MAVEN", "NPM", "PYTHON" })
void Integration_Test_End_To_End_Component_Analysis(Ecosystem.Type packageManager) throws IOException, ExecutionException, InterruptedException {
String manifestFileName = ecoSystemsManifestNames.get(packageManager.getType());
byte[] manifestContent = getStringFromFile("tst_manifests", "it", packageManager.getType(), manifestFileName).getBytes();
preparePythonEnvironment(packageManager);
AnalysisReport analysisReportResult = api.componentAnalysis(manifestFileName,manifestContent).get();
handleJsonResponse(analysisReportResult,false);
}


private static void preparePythonEnvironment(Ecosystem.Type packageManager) {
if(packageManager.equals(Ecosystem.Type.PYTHON)) {
System.setProperty("EXHORT_PYTHON_VIRTUAL_ENV","true");
System.setProperty("EXHORT_PYTHON_INSTALL_BEST_EFFORTS","true");
System.setProperty("MATCH_MANIFEST_VERSIONS","false");
}
else {
System.clearProperty("EXHORT_PYTHON_VIRTUAL_ENV");
System.clearProperty("EXHORT_PYTHON_INSTALL_BEST_EFFORTS");
System.clearProperty("MATCH_MANIFEST_VERSIONS");
}
}

private static void handleJsonResponse(AnalysisReport analysisReportResult, boolean positiveNumberOfTransitives) {
analysisReportResult.getProviders().entrySet().stream().forEach(provider -> { assertTrue(provider.getValue().getStatus().getOk());
assertTrue(provider.getValue().getStatus().getCode() == HttpURLConnection.HTTP_OK);
});
analysisReportResult.getProviders().entrySet().stream()
.map(Map.Entry::getValue)
.map(ProviderReport::getSources)
.map(Map::entrySet)
.flatMap(Collection::stream)
.map(Map.Entry::getValue)
.forEach( source -> assertTrue(source.getSummary().getTotal() > 0 ));

if(positiveNumberOfTransitives) {
assertTrue(analysisReportResult.getScanned().getTransitive() > 0);
}
else {
assertEquals(0,analysisReportResult.getScanned().getTransitive());
}
}

private void handleHtmlResponse(String analysisReportHtml) throws JsonProcessingException {
ObjectMapper om = new ObjectMapper();
assertTrue(analysisReportHtml.contains("svg") && analysisReportHtml.contains("html"));
int jsonStart = analysisReportHtml.indexOf("\"report\":");
int jsonEnd = analysisReportHtml.indexOf("}}}}}");
String embeddedJson = analysisReportHtml.substring(jsonStart + 9 ,jsonEnd + 5);
JsonNode jsonInHtml = om.readTree(embeddedJson);
JsonNode scannedNode = jsonInHtml.get("scanned");
assertTrue(scannedNode.get("total").asInt(0) > 0);
assertTrue(scannedNode.get("transitive").asInt(0) > 0);
JsonNode status = jsonInHtml.get("providers").get("snyk").get("status");
assertTrue(status.get("code").asInt(0) == 200);
assertTrue(status.get("ok").asBoolean(false));

}
private void mockMavenDependencyTree(Ecosystem.Type packageManager) throws IOException {
if(packageManager.equals(Ecosystem.Type.MAVEN)) {
mockedOperations = mockStatic(Operations.class);
String depTree;
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 -> { 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;
}

}



4 changes: 3 additions & 1 deletion src/test/java/com/redhat/exhort/impl/Exhort_Api_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.redhat.exhort.impl;

import static com.redhat.exhort.ExhortTest.getResourceAsStreamDecision;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
Expand All @@ -30,6 +31,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.redhat.exhort.ExhortTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
Expand All @@ -54,7 +56,7 @@
@ClearEnvironmentVariable(key="RHDA_TOKEN")
@ClearEnvironmentVariable(key="RHDA_SOURCE")
@SuppressWarnings("unchecked")
class Exhort_Api_Test {
class Exhort_Api_Test extends ExhortTest {
@Mock
Provider mockProvider;
@Mock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.exhort.Api;
import com.redhat.exhort.ExhortTest;
import com.redhat.exhort.Provider;
import com.redhat.exhort.sbom.Sbom;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
// var tmpPythonModuleDir = Files.createTempDirectory("exhort_test_");
// var tmpPythonFile = Files.createFile(tmpPythonModuleDir.resolve("requirements.txt"));
// Python_Provider_Test.testFolders().forEach( test -> {
// try (var is = getClass().getModule().getResourceAsStream(String.join("/","tst_manifests", "pip", test, "requirements.txt"))) {
// try (var is = getClass().getClassLoader().getResourceAsStream(String.join("/","tst_manifests", "pip", test, "requirements.txt"))) {
// Files.write(tmpPythonFile, is.readAllBytes());
// pythonController.installPackage(tmpPythonFile.toAbsolutePath().toString());
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.redhat.exhort.ExhortTest;
import com.redhat.exhort.utils.PythonControllerBase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand Down

0 comments on commit 3542331

Please sign in to comment.