-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added metaanalyzerProcessorTest Signed-off-by: mehab <meha.bhargava@citi.com> * improved test coverage Signed-off-by: mehab <meha.bhargava@citi.com> --------- Signed-off-by: mehab <meha.bhargava@citi.com>
- Loading branch information
Showing
9 changed files
with
213 additions
and
14 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
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
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
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
170 changes: 170 additions & 0 deletions
170
repository-meta-analyzer/src/test/java/org/hyades/processor/MetaAnalyzerProcessorTest.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,170 @@ | ||
package org.hyades.processor; | ||
|
||
import com.github.packageurl.MalformedPackageURLException; | ||
import com.github.packageurl.PackageURL; | ||
import io.quarkus.cache.Cache; | ||
import io.quarkus.cache.CacheName; | ||
import io.quarkus.test.TestTransaction; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
import io.quarkus.test.junit.TestProfile; | ||
import jakarta.inject.Inject; | ||
import jakarta.persistence.EntityManager; | ||
import org.apache.kafka.streams.StreamsBuilder; | ||
import org.apache.kafka.streams.TestInputTopic; | ||
import org.apache.kafka.streams.TestOutputTopic; | ||
import org.apache.kafka.streams.TopologyTestDriver; | ||
import org.apache.kafka.streams.errors.StreamsException; | ||
import org.apache.kafka.streams.kstream.Consumed; | ||
import org.apache.kafka.streams.kstream.Produced; | ||
import org.apache.kafka.streams.test.TestRecord; | ||
import org.hyades.common.SecretDecryptor; | ||
import org.hyades.persistence.model.RepositoryType; | ||
import org.hyades.persistence.repository.RepoEntityRepository; | ||
import org.hyades.proto.KafkaProtobufSerde; | ||
import org.hyades.proto.repometaanalysis.v1.AnalysisResult; | ||
import org.hyades.proto.repometaanalysis.v1.Component; | ||
import org.hyades.repositories.RepositoryAnalyzerFactory; | ||
import org.hyades.serde.KafkaPurlSerde; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@QuarkusTest | ||
@TestProfile(MetaAnalyzerProcessorTest.TestProfile.class) | ||
class MetaAnalyzerProcessorTest { | ||
|
||
public static class TestProfile implements QuarkusTestProfile { | ||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
return Map.of( | ||
"quarkus.kafka.snappy.enabled", "false" | ||
); | ||
} | ||
} | ||
|
||
private static final String TEST_PURL_JACKSON_BIND = "pkg:maven/com.fasterxml.jackson.core/jackson-databind@2.13.4"; | ||
|
||
private TopologyTestDriver testDriver; | ||
private TestInputTopic<PackageURL, Component> inputTopic; | ||
private TestOutputTopic<PackageURL, AnalysisResult> outputTopic; | ||
@Inject | ||
RepoEntityRepository repoEntityRepository; | ||
|
||
@Inject | ||
RepositoryAnalyzerFactory analyzerFactory; | ||
|
||
@Inject | ||
EntityManager entityManager; | ||
|
||
@Inject | ||
@CacheName("metaAnalyzer") | ||
Cache cache; | ||
|
||
@Inject | ||
SecretDecryptor secretDecryptor; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
final var processorSupplier = new MetaAnalyzerProcessorSupplier(repoEntityRepository, analyzerFactory, secretDecryptor, cache); | ||
|
||
final var valueSerde = new KafkaProtobufSerde<>(Component.parser()); | ||
final var purlSerde = new KafkaPurlSerde(); | ||
final var valueSerdeResult = new KafkaProtobufSerde<>(AnalysisResult.parser()); | ||
|
||
final var streamsBuilder = new StreamsBuilder(); | ||
streamsBuilder | ||
.stream("input-topic", Consumed.with(purlSerde, valueSerde)) | ||
.processValues(processorSupplier) | ||
.to("output-topic", Produced.with(purlSerde, valueSerdeResult)); | ||
|
||
testDriver = new TopologyTestDriver(streamsBuilder.build()); | ||
inputTopic = testDriver.createInputTopic("input-topic", purlSerde.serializer(), valueSerde.serializer()); | ||
outputTopic = testDriver.createOutputTopic("output-topic", purlSerde.deserializer(), valueSerdeResult.deserializer()); | ||
} | ||
|
||
@AfterEach | ||
void afterEach() { | ||
testDriver.close(); | ||
cache.invalidateAll().await().indefinitely(); | ||
} | ||
|
||
@Test | ||
void testWithNoSupportedRepositoryTypes() throws Exception { | ||
final TestRecord<PackageURL, Component> inputRecord = new TestRecord<>(new PackageURL(TEST_PURL_JACKSON_BIND), Component.newBuilder() | ||
.setPurl(TEST_PURL_JACKSON_BIND).build()); | ||
inputTopic.pipeInput(inputRecord); | ||
assertThat(outputTopic.getQueueSize()).isEqualTo(1); | ||
assertThat(outputTopic.readRecordsToList()).satisfiesExactly( | ||
record -> { | ||
assertThat(record.key().getType()).isEqualTo(RepositoryType.MAVEN.toString().toLowerCase()); | ||
}); | ||
} | ||
|
||
@Test | ||
void testMalformedPurl() throws Exception { | ||
final TestRecord<PackageURL, Component> inputRecord = new TestRecord<>(new PackageURL(TEST_PURL_JACKSON_BIND), Component.newBuilder() | ||
.setPurl("invalid purl").build()); | ||
Assertions.assertThrows(StreamsException.class, () -> { | ||
inputTopic.pipeInput(inputRecord); | ||
}, "no exception thrown"); | ||
|
||
} | ||
|
||
@Test | ||
void testNoAnalyzerApplicable() throws Exception { | ||
final TestRecord<PackageURL, Component> inputRecord = new TestRecord<>(new PackageURL("pkg:test/com.fasterxml.jackson.core/jackson-databind@2.13.4"), Component.newBuilder() | ||
.setPurl("pkg:test/com.fasterxml.jackson.core/jackson-databind@2.13.4").build()); | ||
inputTopic.pipeInput(inputRecord); | ||
assertThat(outputTopic.getQueueSize()).isEqualTo(1); | ||
assertThat(outputTopic.readRecordsToList()).satisfiesExactly( | ||
record -> { | ||
assertThat(record.key().getType()).isEqualTo("test"); | ||
}); | ||
|
||
} | ||
|
||
@Test | ||
@TestTransaction | ||
void testInternalRepositoryExternalComponent() throws MalformedPackageURLException { | ||
entityManager.createNativeQuery(""" | ||
INSERT INTO "REPOSITORY" ("TYPE", "ENABLED","IDENTIFIER", "INTERNAL", "URL", "AUTHENTICATIONREQUIRED", "RESOLUTION_ORDER") VALUES | ||
('MAVEN',true, 'central', true, 'test.com', false,1); | ||
""").executeUpdate(); | ||
|
||
final TestRecord<PackageURL, Component> inputRecord = new TestRecord<>(new PackageURL("pkg:maven/com.fasterxml.jackson.core/jackson-databind@2.13.4"), Component.newBuilder() | ||
.setPurl("pkg:maven/com.fasterxml.jackson.core/jackson-databind@2.13.4").setInternal(false).build()); | ||
inputTopic.pipeInput(inputRecord); | ||
assertThat(outputTopic.getQueueSize()).isEqualTo(1); | ||
assertThat(outputTopic.readRecordsToList()).satisfiesExactly( | ||
record -> { | ||
assertThat(record.key().getType()).isEqualTo(RepositoryType.MAVEN.toString().toLowerCase()); | ||
}); | ||
|
||
} | ||
|
||
@Test | ||
@TestTransaction | ||
void testExternalRepositoryInternalComponent() throws MalformedPackageURLException { | ||
entityManager.createNativeQuery(""" | ||
INSERT INTO "REPOSITORY" ("TYPE", "ENABLED","IDENTIFIER", "INTERNAL", "URL", "AUTHENTICATIONREQUIRED", "RESOLUTION_ORDER") VALUES | ||
('MAVEN',true, 'central', false, 'test.com', false,1); | ||
""").executeUpdate(); | ||
|
||
final TestRecord<PackageURL, Component> inputRecord = new TestRecord<>(new PackageURL("pkg:maven/com.fasterxml.jackson.core/jackson-databind@2.13.4"), Component.newBuilder() | ||
.setPurl("pkg:maven/com.fasterxml.jackson.core/jackson-databind@2.13.4").setInternal(true).build()); | ||
inputTopic.pipeInput(inputRecord); | ||
assertThat(outputTopic.getQueueSize()).isEqualTo(1); | ||
assertThat(outputTopic.readRecordsToList()).satisfiesExactly( | ||
record -> { | ||
assertThat(record.key().getType()).isEqualTo(RepositoryType.MAVEN.toString().toLowerCase()); | ||
}); | ||
|
||
} | ||
|
||
} |
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