Skip to content

Commit

Permalink
#364 gray badge
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 7, 2019
1 parent 934a53f commit 589599c
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 9 deletions.
26 changes: 26 additions & 0 deletions src/main/java/org/jpeek/web/Results.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ elements, artifact, new Version().value(), rank, score
}
}

/**
* Has this artifact?
* @param artifact The artifact, e.g. "org.jpeek:jpeek"
* @return TRUE if it exists
*/
public boolean exists(final String artifact) {
return !this.table.frame()
.where("good", "true")
.where("artifact", artifact)
.isEmpty();
}

/**
* Get score of this.
* @param artifact The artifact, e.g. "org.jpeek:jpeek"
* @return The score
*/
public double score(final String artifact) throws IOException {
final Item item = this.table.frame()
.where("good", "true")
.where("artifact", artifact)
.iterator()
.next();
return new DyNum(item, "score").doubleValue();
}

/**
* Recent artifacts..
* @return List of them
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jpeek/web/TkApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private static Take make(final Path home) throws IOException {
),
new FkRegex(
"/([^/]+)/([^/]+)(.*)",
new TkReport(reports)
new TkReport(reports, new Results())
)
)
),
Expand Down
46 changes: 44 additions & 2 deletions src/main/java/org/jpeek/web/TkReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,26 @@
*/
package org.jpeek.web;

import com.jcabi.xml.ClasspathSources;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSLDocument;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.regex.Matcher;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.cactoos.func.IoCheckedBiFunc;
import org.cactoos.func.IoCheckedFunc;
import org.jpeek.App;
import org.takes.Response;
import org.takes.facets.fork.RqRegex;
import org.takes.facets.fork.TkRegex;
import org.takes.facets.forward.RsForward;
import org.takes.facets.hamcrest.HmRsStatus;
import org.takes.rs.RsText;
import org.takes.rs.RsWithType;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* Report page.
Expand All @@ -52,29 +62,61 @@ final class TkReport implements TkRegex {
*/
private final BiFunc<String, String, Func<String, Response>> reports;

/**
* Results.
*/
private final Results results;

/**
* Ctor.
* @param rpts Reports
*/
TkReport(final BiFunc<String, String, Func<String, Response>> rpts) {
TkReport(final BiFunc<String, String, Func<String, Response>> rpts,
final Results rslts) {
this.reports = rpts;
this.results = rslts;
}

@Override
public Response act(final RqRegex req) throws IOException {
final Matcher matcher = req.matcher();
System.out.println(matcher);
// @checkstyle MagicNumber (1 line)
final String path = matcher.group(3);
if (path.isEmpty()) {
throw new RsForward(
String.format("%s/index.html", matcher.group(0))
);
}
return new IoCheckedFunc<>(
Response response = new IoCheckedFunc<>(
new IoCheckedBiFunc<>(this.reports).apply(
matcher.group(1), matcher.group(2)
)
).apply(path.substring(1));
if (new HmRsStatus(HttpURLConnection.HTTP_NOT_FOUND).matches(response)
&& "badge.svg".equals(matcher.group(3))) {
final String artifact = String.format(
"%s:%s", matcher.group(1), matcher.group(2)
);
final Directives dirs = new Directives().add("badge")
.attr("style", "round");
if (this.results.exists(artifact)) {
dirs.set(this.results.score(artifact));
} else {
dirs.set(0).attr("unknown", "true");
}
response = new RsWithType(
new RsText(
new XSLDocument(
App.class.getResourceAsStream("xsl/badge.xsl")
).with(new ClasspathSources()).transform(
new XMLDocument(new Xembler(dirs).xmlQuietly())
).toString()
),
"image/svg+xml"
);
}
return response;
}

}
3 changes: 3 additions & 0 deletions src/main/resources/org/jpeek/xsl/badge.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ SOFTWARE.
<path d="M27 0h77v20H27z">
<xsl:attribute name="fill">
<xsl:choose>
<xsl:when test="@unknown">
<xsl:text>#ccc</xsl:text>
</xsl:when>
<xsl:when test=". &gt; 8">
<xsl:text>#44cc11</xsl:text>
</xsl:when>
Expand Down
79 changes: 79 additions & 0 deletions src/test/java/org/jpeek/web/TkReportTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.jpeek.web;

import com.jcabi.matchers.XhtmlMatchers;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.llorllale.cactoos.matchers.Assertion;
import org.takes.facets.fork.RqRegex;
import org.takes.rq.RqFake;
import org.takes.rs.RsPrint;

/**
* Test case for {@link TkReport}.
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.23
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle JavadocTagsCheck (500 lines)
*/
public final class TkReportTest {

/**
* Temp folder for all tests.
*/
@Rule
@SuppressWarnings("PMD.BeanMembersShouldSerialize")
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void rendersEmptySvgBadge() throws IOException {
new Assertion<>(
"Must render the badge",
XhtmlMatchers.xhtml(
new RsPrint(
new TkReport(
new AsyncReports(
new Futures(
new Reports(this.folder.newFolder().toPath())
)
),
new Results()
).act(
new RqRegex.Fake(
new RqFake(),
"/([^/]+)/([^/]+)/([^/]+)",
"/org.jpeek/jpeek/badge.svg"
)
)
).printBody()
),
XhtmlMatchers.hasXPath("//svg:svg")
).affirm();
}

}
7 changes: 1 addition & 6 deletions src/test/java/org/jpeek/web/TkUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
package org.jpeek.web;

import java.io.IOException;
import org.cactoos.BiFunc;
import org.cactoos.Func;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.takes.Response;
import org.takes.rq.RqFake;
import org.takes.rq.RqWithHeader;
import org.takes.rq.multipart.RqMtFake;
Expand All @@ -47,12 +44,10 @@ public final class TkUploadTest {

@Test
public void rendersIndexPage() throws IOException {
final BiFunc<String, String, Func<String, Response>> reports =
(artifact, group) -> null;
new Assertion<>(
"Must upload body",
new RsPrint(
new TkUpload(reports).act(
new TkUpload((artifact, group) -> null).act(
new RqMtFake(
new RqFake(),
new RqWithHeader(
Expand Down

0 comments on commit 589599c

Please sign in to comment.