Skip to content

Commit

Permalink
Parameter skip removed from RpmMetadata.Append (#430)
Browse files Browse the repository at this point in the history
* Added licence to benchmarks pom

* Parameter `skip` removed from `RpmMetadata.Append`

Co-authored-by: olenagerasimova <olena.gereasimova@gmail.com>
  • Loading branch information
olenagerasimova and olenagerasimova committed Jul 1, 2021
1 parent 01f7728 commit 5cc5ee9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 229 deletions.
24 changes: 4 additions & 20 deletions src/main/java/com/artipie/rpm/RpmMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,12 @@ final class Append {
*/
private final Collection<MetadataItem> items;

/**
* Should invalid packages be skipped? Default value is true.
*/
private final boolean skip;

/**
* Ctor.
* @param skip Should invalid packages be skipped?
* @param items Metadata items
*/
public Append(final boolean skip, final MetadataItem... items) {
this.skip = skip;
this.items = Arrays.asList(items);
}

/**
* Ctor.
* @param items Metadata items
*/
public Append(final MetadataItem... items) {
this(true, items);
this.items = Arrays.asList(items);
}

/**
Expand All @@ -151,7 +136,7 @@ public void perform(final Collection<Package.Meta> packages) {
final MetadataItem primary = this.items.stream()
.filter(item -> item.type == XmlPackage.PRIMARY).findFirst().get();
try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(temp))) {
res = new MergedXmlPrimary(primary.input, out, this.skip)
res = new MergedXmlPrimary(primary.input, out)
.merge(packages, new XmlEvent.Primary());
}
final ExecutorService service = Executors.newFixedThreadPool(3);
Expand Down Expand Up @@ -185,8 +170,7 @@ private Runnable updateFilelist(final Collection<Package.Meta> packages,
if (filelist.isPresent()) {
try {
new MergedXmlPackage(
filelist.get().input, filelist.get().out, XmlPackage.FILELISTS,
res, this.skip
filelist.get().input, filelist.get().out, XmlPackage.FILELISTS, res
).merge(packages, new XmlEvent.Filelists());
} catch (final IOException err) {
throw new ArtipieIOException(err);
Expand All @@ -207,7 +191,7 @@ private Runnable updateOther(final Collection<Package.Meta> packages,
try {
final MetadataItem other = this.items.stream()
.filter(item -> item.type == XmlPackage.OTHER).findFirst().get();
new MergedXmlPackage(other.input, other.out, XmlPackage.OTHER, res, this.skip)
new MergedXmlPackage(other.input, other.out, XmlPackage.OTHER, res)
.merge(packages, new XmlEvent.Other());
} catch (final IOException err) {
throw new ArtipieIOException(err);
Expand Down
18 changes: 4 additions & 14 deletions src/main/java/com/artipie/rpm/meta/MergedXmlPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,20 @@ public final class MergedXmlPackage implements MergedXml {
*/
private final MergedXml.Result res;

/**
* Should invalid packages be skipped?
*/
private final boolean skip;

/**
* Ctor.
* @param input Input stream
* @param out Output stream
* @param type Xml package type
* @param res Result of the primary.xml merging
* @param skip Should invalid packages be skipped?
* @checkstyle ParameterNumberCheck (5 lines)
*/
public MergedXmlPackage(final Optional<InputStream> input, final OutputStream out,
final XmlPackage type, final MergedXml.Result res, final boolean skip) {
final XmlPackage type, final MergedXml.Result res) {
this.input = input;
this.out = out;
this.type = type;
this.res = res;
this.skip = skip;
}

/**
Expand All @@ -79,12 +72,11 @@ public MergedXmlPackage(final Optional<InputStream> input, final OutputStream ou
* @param out Output stream
* @param type Xml package type
* @param res Result of the primary.xml merging
* @param skip Should invalid packages be skipped?
* @checkstyle ParameterNumberCheck (5 lines)
*/
public MergedXmlPackage(final InputStream input, final OutputStream out,
final XmlPackage type, final MergedXml.Result res, final boolean skip) {
this(Optional.of(input), out, type, res, skip);
final XmlPackage type, final MergedXml.Result res) {
this(Optional.of(input), out, type, res);
}

@Override
Expand All @@ -103,9 +95,7 @@ public MergedXml.Result merge(final Collection<Package.Meta> packages,
this.process(this.res.checksums(), reader.get(), writer);
}
for (final Package.Meta item : packages) {
new MergedXml.InvalidPackage(
() -> event.add(writer, item), this.skip
).handle();
event.add(writer, item);
}
writer.add(events.createSpace("\n"));
writer.add(
Expand Down
24 changes: 5 additions & 19 deletions src/main/java/com/artipie/rpm/meta/MergedXmlPrimary.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,23 @@ public final class MergedXmlPrimary implements MergedXml {
*/
private final OutputStream out;

/**
* Should invalid packages be skipped?
*/
private final boolean skip;

/**
* Ctor.
* @param input Input stream
* @param out Output stream
* @param skip Should invalid packages be skipped?
*/
public MergedXmlPrimary(final Optional<InputStream> input, final OutputStream out,
final boolean skip) {
public MergedXmlPrimary(final Optional<InputStream> input, final OutputStream out) {
this.input = input;
this.out = out;
this.skip = skip;
}

/**
* Ctor.
* @param input Input stream
* @param out Output stream
* @param skip Should invalid packages be skipped?
*/
public MergedXmlPrimary(final InputStream input, final OutputStream out, final boolean skip) {
this(Optional.of(input), out, skip);
public MergedXmlPrimary(final InputStream input, final OutputStream out) {
this(Optional.of(input), out);
}

// @checkstyle ExecutableStatementCountCheck (100 lines)
Expand All @@ -93,13 +84,8 @@ public Result merge(final Collection<Package.Meta> packages, final XmlEvent even
);
}
for (final Package.Meta item : packages) {
new InvalidPackage(
() -> {
event.add(writer, item);
res.incrementAndGet();
},
this.skip
).handle();
event.add(writer, item);
res.incrementAndGet();
}
writer.add(events.createSpace("\n"));
writer.add(
Expand Down
90 changes: 5 additions & 85 deletions src/test/java/com/artipie/rpm/meta/MergedXmlPackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@
import com.artipie.rpm.TestRpm;
import com.artipie.rpm.pkg.FilePackage;
import com.artipie.rpm.pkg.FilePackageHeader;
import com.artipie.rpm.pkg.InvalidPackageException;
import com.jcabi.matchers.XhtmlMatchers;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Locale;
import java.util.Optional;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

Expand All @@ -47,7 +41,7 @@ void addsRecords(final String filename) throws IOException {
final XmlPackage type = XmlPackage.valueOf(filename.toUpperCase(Locale.US));
new MergedXmlPackage(
input, out, type,
new MergedXmlPrimary.Result(3L, Collections.emptyList()), true
new MergedXmlPrimary.Result(3L, Collections.emptyList())
).merge(
new ListOf<>(
new FilePackage.Headers(
Expand Down Expand Up @@ -84,7 +78,7 @@ void replacesAndAddsRecord(final String filename) throws IOException {
final XmlPackage type = XmlPackage.valueOf(filename.toUpperCase(Locale.US));
new MergedXmlPackage(
input, out, type,
new MergedXmlPrimary.Result(2L, Collections.singleton("abc123")), true
new MergedXmlPrimary.Result(2L, Collections.singleton("abc123"))
).merge(
new ListOf<>(
new FilePackage.Headers(
Expand Down Expand Up @@ -125,7 +119,7 @@ void appendsSeveralPackages(final String filename) throws IOException {
final XmlPackage type = XmlPackage.valueOf(filename.toUpperCase(Locale.US));
new MergedXmlPackage(
input, out, type,
new MergedXmlPrimary.Result(4L, Collections.singleton("abc123")), true
new MergedXmlPrimary.Result(4L, Collections.singleton("abc123"))
).merge(
new ListOf<>(
new FilePackage.Headers(
Expand Down Expand Up @@ -174,7 +168,7 @@ void worksWithEmptyInput(final String filename) throws IOException {
);
new MergedXmlPackage(
input, out, type,
new MergedXmlPrimary.Result(1L, Collections.emptyList()), true
new MergedXmlPrimary.Result(1L, Collections.emptyList())
).merge(
new ListOf<>(
new FilePackage.Headers(
Expand All @@ -196,80 +190,6 @@ void worksWithEmptyInput(final String filename) throws IOException {
}
}

@ParameterizedTest
@ValueSource(strings = {"other", "filelists"})
@Disabled
void skipsInvalidPackage(final String filename, @TempDir final Path tmp) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Path invalid = tmp.resolve("invalid.rpm");
Files.write(invalid, "abc123".getBytes());
final TestRpm time = new TestRpm.Time();
try (InputStream input = new TestResource(
String.format("repodata/MergedXmlTest/libdeflt-nginx-%s.xml.example", filename)
).asInputStream()
) {
final XmlPackage type = XmlPackage.valueOf(filename.toUpperCase(Locale.US));
new MergedXmlPackage(
input, out, type,
new MergedXmlPrimary.Result(3L, Collections.emptyList()), true
).merge(
new ListOf<>(
new FilePackage.Headers(
new FilePackageHeader(time.path()).header(),
time.path(), Digest.SHA256, time.path().getFileName().toString()
),
new FilePackage.Headers(
new FilePackageHeader(invalid).header(),
invalid, Digest.SHA256, invalid.getFileName().toString()
)
),
this.event(type)
);
final String actual = out.toString(StandardCharsets.UTF_8.name());
MatcherAssert.assertThat(
actual,
XhtmlMatchers.hasXPaths(
// @checkstyle LineLengthCheck (4 lines)
String.format("/*[local-name()='%s' and @packages='3']", type.tag()),
String.format("/*[local-name()='%s']/*[local-name()='package' and @name='libdeflt1_0']", type.tag()),
String.format("/*[local-name()='%s']/*[local-name()='package' and @name='nginx']", type.tag()),
String.format("/*[local-name()='%s']/*[local-name()='package' and @name='time']", type.tag())
)
);
}
}

@ParameterizedTest
@ValueSource(strings = {"other", "filelists"})
@Disabled
void failsWhenInvalidPackageProvided(final String filename, @TempDir final Path tmp)
throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Path invalid = tmp.resolve("invalid.rpm");
Files.write(invalid, "123".getBytes());
try (InputStream input = new TestResource(
String.format("repodata/MergedXmlTest/libdeflt-nginx-%s.xml.example", filename)
).asInputStream()
) {
final XmlPackage type = XmlPackage.valueOf(filename.toUpperCase(Locale.US));
Assertions.assertThrows(
InvalidPackageException.class,
() -> new MergedXmlPackage(
input, out, type,
new MergedXmlPrimary.Result(3L, Collections.emptyList()), false
).merge(
new ListOf<>(
new FilePackage.Headers(
new FilePackageHeader(invalid).header(),
invalid, Digest.SHA256, invalid.getFileName().toString()
)
),
this.event(type)
)
);
}
}

@ParameterizedTest
@ValueSource(strings = {"other", "filelists"})
void worksWithAbsentInput(final String filename) throws IOException {
Expand All @@ -279,7 +199,7 @@ void worksWithAbsentInput(final String filename) throws IOException {
final TestRpm abc = new TestRpm.Abc();
new MergedXmlPackage(
Optional.empty(), out, type,
new MergedXmlPrimary.Result(2L, Collections.emptyList()), true
new MergedXmlPrimary.Result(2L, Collections.emptyList())
).merge(
new ListOf<>(
new FilePackage.Headers(
Expand Down
Loading

0 comments on commit 5cc5ee9

Please sign in to comment.