Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Update and move SVG Badge support to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
bclozel committed Sep 21, 2020
1 parent 65f9d72 commit 58850eb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 62 deletions.
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package sagan.projects.support;
package sagan.site.projects.badge;

import static org.springframework.web.bind.annotation.RequestMethod.*;

Expand All @@ -32,9 +32,10 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import sagan.projects.Project;
import sagan.projects.ProjectRelease;
import sagan.projects.ProjectRelease.ReleaseStatus;
import sagan.site.projects.Project;
import sagan.site.projects.Release;
import sagan.site.projects.ReleaseStatus;
import sagan.site.projects.ProjectMetadataService;

/**
* Controller that handles request to version badges.
Expand Down Expand Up @@ -84,28 +85,29 @@ public ResponseEntity<byte[]> latestBadge(@PathVariable("projectId") String proj
*/
private ResponseEntity<byte[]> badgeFor(String projectId, ReleaseStatus releaseStatus) throws IOException {

Project project = service.getProject(projectId);
Project project = service.fetchFullProject(projectId);

if (project == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

Optional<ProjectRelease> gaRelease = getRelease(project.getProjectReleases(),
Optional<Release> gaRelease = getRelease(project.getReleases(),
projectRelease -> projectRelease.getReleaseStatus() == releaseStatus);

if (!gaRelease.isPresent()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

byte[] svgBadge = versionBadgeService.createSvgBadge(project, gaRelease.get());
return ResponseEntity.ok().eTag(gaRelease.get().getVersion()).cacheControl(CacheControl.maxAge(1L, TimeUnit.HOURS))
return ResponseEntity.ok().eTag(gaRelease.get().getVersion().toString())
.cacheControl(CacheControl.maxAge(1L, TimeUnit.HOURS))
.body(svgBadge);
}

private Optional<ProjectRelease> getRelease(Collection<ProjectRelease> projectReleases,
Predicate<ProjectRelease> predicate) {
private Optional<Release> getRelease(Collection<Release> releases,
Predicate<Release> predicate) {

Optional<ProjectRelease> first = projectReleases //
Optional<Release> first = releases //
.stream() //
.filter(projectRelease -> predicate.test(projectRelease) && projectRelease.isCurrent()) //
.findFirst();
Expand All @@ -114,7 +116,7 @@ private Optional<ProjectRelease> getRelease(Collection<ProjectRelease> projectRe
return first;
}

return projectReleases //
return releases //
.stream() //
.filter(projectRelease -> predicate.test(projectRelease)) //
.findFirst();
Expand Down
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package sagan.projects.support;
package sagan.site.projects.badge;

import java.util.List;

Expand Down
@@ -1,4 +1,4 @@
package sagan.projects.support;
package sagan.site.projects.badge;

import java.awt.*;
import java.awt.image.BufferedImage;
Expand All @@ -14,11 +14,11 @@
import org.xmlbeam.XBProjector.Flags;
import org.xmlbeam.config.DefaultXMLFactoriesConfig;
import org.xmlbeam.config.DefaultXMLFactoriesConfig.NamespacePhilosophy;
import sagan.projects.Project;
import sagan.projects.ProjectRelease;
import sagan.projects.ProjectRelease.ReleaseStatus;
import sagan.projects.support.BadgeSvg.GraphicElement;
import sagan.projects.support.BadgeSvg.Path;
import sagan.site.projects.Project;
import sagan.site.projects.Release;
import sagan.site.projects.ReleaseStatus;
import sagan.site.projects.badge.BadgeSvg.GraphicElement;
import sagan.site.projects.badge.BadgeSvg.Path;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -62,27 +62,27 @@ public void preDestroy() {
}

/**
* Create a version badge for a given {@link Project} and {@link ProjectRelease}. The badge uses SVG and is returned as byte
* Create a version badge for a given {@link Project} and {@link Release}. The badge uses SVG and is returned as byte
* array.
*
* @param project must not be {@literal null}.
* @param projectRelease must not be {@literal null}.
* @param release must not be {@literal null}.
* @return
* @throws IOException
*/
public byte[] createSvgBadge(Project project, ProjectRelease projectRelease) throws IOException {
public byte[] createSvgBadge(Project project, Release release) throws IOException {

Assert.notNull(project, "Project must not be null!");
Assert.notNull(projectRelease, "ProjectRelease must not be null!");
Assert.notNull(release, "ProjectRelease must not be null!");

URL template = getTemplate(projectRelease.getReleaseStatus());
URL template = getTemplate(release.getReleaseStatus());

BadgeSvg svgDocument = xbProjector.io().url(template.toString()).read(BadgeSvg.class);

List<Path> paths = svgDocument.getPaths();

String label = project.getName();
String version = projectRelease.getVersion();
String version = release.getVersion().toString();

return createSvgBadge(svgDocument, paths, label, version);
}
Expand Down
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package sagan.projects.support;
package sagan.site.projects.badge;

import org.junit.After;
import org.junit.Before;
Expand All @@ -24,20 +24,18 @@
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import sagan.projects.Project;
import sagan.projects.ProjectRelease;
import sagan.projects.ProjectRelease.ReleaseStatus;
import sagan.site.projects.Project;
import sagan.site.projects.ProjectMetadataService;
import sagan.site.projects.Release;
import sagan.site.projects.Version;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;

/**
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class BadgeControllerTests {

Expand All @@ -48,33 +46,31 @@ public class BadgeControllerTests {

private BadgeController controller;
private Project project;
private List<ProjectRelease> releases = new ArrayList<>();
private SortedSet<Release> releases = new TreeSet<>();

@Before
public void setUp() throws Exception {

versionBadgeService.postConstruct();
controller = new BadgeController(projectMetadataServiceMock, versionBadgeService);
project = new Project("spring-data-redis", "Spring Data Redis", "http", "http", releases, "data");
when(projectMetadataServiceMock.getProject("spring-data-redis")).thenReturn(project);
this.versionBadgeService.postConstruct();
this.controller = new BadgeController(projectMetadataServiceMock, versionBadgeService);
this.project = new Project("spring-data-redis", "Spring Data Redis");
this.project.setReleases(this.releases);
when(this.projectMetadataServiceMock.fetchFullProject("spring-data-redis")).thenReturn(project);
}

@After
public void tearDown() throws Exception {
versionBadgeService.preDestroy();
this.versionBadgeService.preDestroy();
}

@Test
public void badgeNotFound() throws Exception {

ResponseEntity<byte[]> response = controller.releaseBadge("spring-data-redis");
assertThat(response.getStatusCode(), is(equalTo(HttpStatus.NOT_FOUND)));
}

@Test
public void badgeShouldBeGenerated() throws Exception {

releases.add(new ProjectRelease("1.0.RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, true, "", "", "", ""));
this.releases.add(new Release(Version.of("1.0.RELEASE"), true));
ResponseEntity<byte[]> response = controller.releaseBadge("spring-data-redis");

assertThat(response.getStatusCode(), is(equalTo(HttpStatus.OK)));
Expand All @@ -89,9 +85,8 @@ public void badgeShouldBeGenerated() throws Exception {

@Test
public void projecWithTwoReleasesShouldBeGenerated() throws Exception {

releases.add(new ProjectRelease("1.0.RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
releases.add(new ProjectRelease("1.1.RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, true, "", "", "", ""));
this.releases.add(new Release(Version.of("1.0.RELEASE"), false));
this.releases.add(new Release(Version.of("1.1.RELEASE"), true));
ResponseEntity<byte[]> response = controller.releaseBadge("spring-data-redis");

String content = new String(response.getBody());
Expand All @@ -100,20 +95,17 @@ public void projecWithTwoReleasesShouldBeGenerated() throws Exception {

@Test
public void projecWithTwoReleasesWithoutCurrentFlagPicksHighestRelease() throws Exception {

releases.add(new ProjectRelease("1.0.RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
releases.add(new ProjectRelease("1.1.RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
this.releases.add(new Release(Version.of("1.0.RELEASE"), false));
this.releases.add(new Release(Version.of("1.1.RELEASE"), false));
ResponseEntity<byte[]> response = controller.releaseBadge("spring-data-redis");

String content = new String(response.getBody());
assertThat(content, containsString("1.1.RELEASE"));
}

@Test
public void projecWithTwoReleasesFlagPicksCurrentRelease() throws Exception {

releases.add(new ProjectRelease("1.0.RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, true, "", "", "", ""));
releases.add(new ProjectRelease("1.1.RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
this.releases.add(new Release(Version.of("1.0.RELEASE"), true));
this.releases.add(new Release(Version.of("1.1.RELEASE"), false));
ResponseEntity<byte[]> response = controller.releaseBadge("spring-data-redis");

String content = new String(response.getBody());
Expand All @@ -123,9 +115,8 @@ public void projecWithTwoReleasesFlagPicksCurrentRelease() throws Exception {
@Test
public void projecWithTwoReleasesUsingSymbolicNamesWithNumbersWithoutCurrentFlagPicksMostRecentRelease()
throws Exception {

releases.add(new ProjectRelease("Angel-SR6", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
releases.add(new ProjectRelease("Brixton-SR2", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
this.releases.add(new Release(Version.of("Angel-SR6"), false));
this.releases.add(new Release(Version.of("Brixton-SR2"), false));
ResponseEntity<byte[]> response = controller.releaseBadge("spring-data-redis");

String content = new String(response.getBody());
Expand All @@ -134,9 +125,8 @@ public void projecWithTwoReleasesUsingSymbolicNamesWithNumbersWithoutCurrentFlag

@Test
public void projecWithTwoReleasesUsingSymbolicNamesWithoutCurrentFlagPicksFirstRelease() throws Exception {

releases.add(new ProjectRelease("Angel-SR6", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
releases.add(new ProjectRelease("Brixton-RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
this.releases.add(new Release(Version.of("Angel-SR6"), false));
this.releases.add(new Release(Version.of("Brixton-RELEASE"), false));
ResponseEntity<byte[]> response = controller.releaseBadge("spring-data-redis");

String content = new String(response.getBody());
Expand All @@ -145,9 +135,8 @@ public void projecWithTwoReleasesUsingSymbolicNamesWithoutCurrentFlagPicksFirstR

@Test
public void projecWithTwoReleasesUsingSymbolicNamesFlagPicksCurrentRelease() throws Exception {

releases.add(new ProjectRelease("Angel-SR1", ReleaseStatus.GENERAL_AVAILABILITY, false, "", "", "", ""));
releases.add(new ProjectRelease("Brixton-RELEASE", ReleaseStatus.GENERAL_AVAILABILITY, true, "", "", "", ""));
this.releases.add(new Release(Version.of("Angel-SR1"), false));
this.releases.add(new Release(Version.of("Brixton-RELEASE"), true));
ResponseEntity<byte[]> response = controller.releaseBadge("spring-data-redis");

String content = new String(response.getBody());
Expand Down

0 comments on commit 58850eb

Please sign in to comment.