Skip to content

Commit

Permalink
Merge pull request #308 from olenagerasimova/307
Browse files Browse the repository at this point in the history
#307 - NodeHasPkgCount fully implemented
  • Loading branch information
olenagerasimova authored Jul 2, 2020
2 parents 3d1c15f + d3aa9f9 commit 2054c9d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 59 deletions.
56 changes: 32 additions & 24 deletions src/test/java/com/artipie/rpm/hm/NodeHasPkgCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,40 @@
package com.artipie.rpm.hm;

import com.jcabi.xml.XMLDocument;
import org.hamcrest.Description;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hamcrest.core.IsEqual;
import org.llorllale.cactoos.matchers.MatcherEnvelope;

/**
* Metadata has given amount of packages.
* @since 0.10
* @todo #285:30min This matcher should also verify that `packages` attribute of metadata files
* has correct value of packages (the same as packages count). Add this check, correct mismatch
* description to say how many packages was found, what is `packages` attribute value and what is
* expected. The tests are already implemented, so enable them after implementing. After that
* use this matcher in `RpmTest`, `RpmITCase` and possibly in ModifiableMetadataTest and
* MetadataFileTest.
* @todo #307:30min Use this matcher in `RpmTest` to validate that generated metadata files have
* correct packages amount.
*/
public final class NodeHasPkgCount extends MatcherEnvelope<XMLDocument> {

/**
* RegEx pattern for packages attribute.
*/
private static final Pattern ATTR = Pattern.compile("packages=\"(\\d+)\"");

/**
* Ctor.
* @param count Expected packages count
* @param tag Xml tag
*/
public NodeHasPkgCount(final int count, final String tag) {
super(
xml -> new IsEqual<>(count).matches(countPackages(tag, xml)),
desc -> desc.appendValue(count),
(xml, desc) -> desc.appendValue(countPackages(tag, xml))
);
}

/**
* Constructor with {@link Description}.
* @param count Expected packages count.
* @param tag Xml tag.
* @param description Hamcrest description for matcher.
*/
public NodeHasPkgCount(final int count, final String tag, final Description description) {
super(
xml -> new IsEqual<>(count).matches(countPackages(tag, xml)),
desc -> description.appendValue(count),
(xml, desc) -> description.appendValue(countPackages(tag, xml))
xml -> new IsEqual<>(count).matches(countPackages(tag, xml))
&& new IsEqual<>(count).matches(packagesAttributeValue(xml)),
desc -> desc.appendText(String.format("%d packages expected", count)),
(xml, desc) -> desc.appendText(
String.format(
"%d packages found, 'packages' attribute value is %d",
countPackages(tag, xml), packagesAttributeValue(xml)
)
)
);
}

Expand All @@ -81,4 +75,18 @@ private static int countPackages(final String tag, final XMLDocument xml) {
);
}

/**
* Returns `packages` attribute value.
* @param xml Xml document
* @return Value of the attribute
*/
private static int packagesAttributeValue(final XMLDocument xml) {
final Matcher matcher = ATTR.matcher(xml.toString());
int res = Integer.MIN_VALUE;
if (matcher.find()) {
res = Integer.parseInt(matcher.group(1));
}
return res;
}

}
46 changes: 17 additions & 29 deletions src/test/java/com/artipie/rpm/hm/NodeHasPkgCountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@
import com.jcabi.xml.XMLDocument;
import java.io.FileNotFoundException;
import java.nio.file.Paths;
import org.hamcrest.Description;
import org.hamcrest.MatcherAssert;
import org.hamcrest.StringDescription;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.llorllale.cactoos.matchers.Matches;
import org.llorllale.cactoos.matchers.Mismatches;

/**
* Test for {@link NodeHasPkgCount}.
* @since 0.10
* @todo #307:30min Test methods for description verification fail on windows: figure out why,
* fix it and remove disable annotation.
* @checkstyle MagicNumberCheck (500 lines)
*/
final class NodeHasPkgCountTest {
Expand Down Expand Up @@ -82,24 +83,19 @@ void doesNotMatchWhenPackagesAmountDiffers() throws FileNotFoundException {
}

@Test
@Disabled
@DisabledOnOs(OS.WINDOWS)
void describesCorrectlyWhenPackagesAmountDiffers() throws FileNotFoundException {
final Description desc = new StringDescription();
new NodeHasPkgCount(10, XmlPackage.PRIMARY.tag(), desc).matches(
new XMLDocument(
Paths.get(NodeHasPkgCountTest.PRIMARY)
)
);
MatcherAssert.assertThat(
desc.toString(),
new IsEqual<>(
"2 packages found, `packages` attribute value is 2, expected 10 package count"
new NodeHasPkgCount(10, XmlPackage.PRIMARY.tag()),
new Mismatches<>(
new XMLDocument(Paths.get(NodeHasPkgCountTest.PRIMARY)),
"10 packages expected",
"2 packages found, 'packages' attribute value is 2"
)
);
}

@Test
@Disabled
void doesNotMatchWhenPackageAttributeDiffers() throws FileNotFoundException {
MatcherAssert.assertThat(
new NodeHasPkgCount(2, XmlPackage.OTHER.tag()),
Expand All @@ -114,22 +110,14 @@ void doesNotMatchWhenPackageAttributeDiffers() throws FileNotFoundException {
}

@Test
@Disabled
@DisabledOnOs(OS.WINDOWS)
void describesCorrectlyWhenPackageAttributeDiffers() throws FileNotFoundException {
final Description desc = new StringDescription();
new NodeHasPkgCount(
2,
XmlPackage.OTHER.tag(),
desc
).matches(
new XMLDocument(
Paths.get(NodeHasPkgCountTest.WRONG)
)
);
MatcherAssert.assertThat(
desc.toString(),
new IsEqual<>(
"2 packages found, `packages` attribute value is 3, expected 2 package count"
new NodeHasPkgCount(2, XmlPackage.OTHER.tag()),
new Mismatches<>(
new XMLDocument(Paths.get(NodeHasPkgCountTest.WRONG)),
"2 packages expected",
"2 packages found, 'packages' attribute value is 3"
)
);
}
Expand Down
17 changes: 11 additions & 6 deletions src/test/java/com/artipie/rpm/pkg/ModifiableMetadataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

import com.artipie.rpm.Digest;
import com.artipie.rpm.StandardNamingPolicy;
import com.artipie.rpm.hm.NodeHasPkgCount;
import com.artipie.rpm.meta.XmlPackage;
import com.artipie.rpm.meta.XmlRepomd;
import com.jcabi.aspects.Tv;
import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -38,6 +40,7 @@
import javax.xml.stream.XMLStreamException;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsNot;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -70,12 +73,14 @@ void generatesMetadataFile(@TempDir final Path temp) throws IOException {
);
MatcherAssert.assertThat(
"Has 'abc' and 'nginx' packages, writes `packages` attribute correctly",
new String(Files.readAllBytes(res), StandardCharsets.UTF_8),
XhtmlMatchers.hasXPath(
//@checkstyle LineLengthCheck (3 lines)
"/*[local-name()='metadata' and @packages='2']",
"/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='abc']",
"/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='nginx']"
new XMLDocument(res),
Matchers.allOf(
XhtmlMatchers.hasXPath(
//@checkstyle LineLengthCheck (3 lines)
"/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='abc']",
"/*[local-name()='metadata']/*[local-name()='package']/*[local-name()='name' and text()='nginx']"
),
new NodeHasPkgCount(2, XmlPackage.PRIMARY.tag())
)
);
MatcherAssert.assertThat(
Expand Down

3 comments on commit 2054c9d

@0pdd
Copy link

@0pdd 0pdd commented on 2054c9d Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 285-b9db6ed3 disappeared from src/test/java/com/artipie/rpm/hm/NodeHasPkgCount.java, that's why I closed #307. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link

@0pdd 0pdd commented on 2054c9d Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 307-d58d9aca discovered in src/test/java/com/artipie/rpm/hm/NodeHasPkgCountTest.java and submitted as #310. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link

@0pdd 0pdd commented on 2054c9d Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 307-cda9f9c1 discovered in src/test/java/com/artipie/rpm/hm/NodeHasPkgCount.java and submitted as #311. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.