Skip to content

Commit

Permalink
refactor(Dependency): add builder to initialize Dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
maximedezette committed Oct 29, 2023
1 parent 84be39b commit 6fa1d40
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
package com.globaldashboard.dependencies.domain;

import com.globaldashboard.domain.ArtifactId;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

public record Dependency(GroupId groupId, ArtifactId artifactId, Optional<SemanticVersion> version,
Optional<List<CVE>> CVEList) {

public Dependency(String groupId, String artifactId) {
this(new GroupId(groupId), new ArtifactId(artifactId), Optional.empty(), Optional.empty());
public Dependency(Builder builder) {
this(builder.groupId, builder.artifactId, builder.version, builder.cveList);
}

public Dependency(String groupId, String artifactId, String version) {
this(new GroupId(groupId), new ArtifactId(artifactId), Optional.of(SemanticVersion.from(version)), Optional.empty());
public static Builder builder() {
return new Builder();
}

public Dependency(String groupId, String artifactId, String version, List<String> identifiers) {
this(new GroupId(groupId), new ArtifactId(artifactId), Optional.of(SemanticVersion.from(version)), getCves(identifiers));
}
public static final class Builder {
private GroupId groupId;
private ArtifactId artifactId;
private Optional<SemanticVersion> version = Optional.empty();
private Optional<List<CVE>> cveList = Optional.empty();

public Builder withGroupId(String groupId) {
this.groupId = new GroupId(groupId);
return this;
}

public Builder withArtifactId(String artifactId) {
this.artifactId = new ArtifactId(artifactId);
return this;
}

public Builder withVersion(String version) {
if (StringUtils.isNotBlank(version)) {
this.version = Optional.ofNullable(SemanticVersion.from(version));
}
return this;
}

public Builder withCVEList(List<String> identifierList) {
if (!CollectionUtils.isEmpty(identifierList)) {
this.cveList = Optional.of(identifierList.stream()
.map(CVE::new)
.toList());
}
return this;
}

private static Optional<List<CVE>> getCves(List<String> identifiers) {
if (identifiers == null || identifiers.size() == 0) {
return Optional.empty();
public Dependency build() {
return new Dependency(this);
}
return Optional.of(identifiers
.stream()
.map(CVE::new)
.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,23 @@ public static DependencyEntity from(Dependency dependency) {

public Dependency toDomain() {
if (this.version.equals(NO_VERSION)) {
return new Dependency(this.groupId, this.artifactId);
return Dependency.builder()
.withGroupId(this.groupId)
.withArtifactId(this.artifactId)
.build();
}

List<String> CVEIdentifiers = this.getCVEList()
.stream()
.map(CVEEntity::getIdentifier)
.toList();

return new Dependency(this.groupId, this.artifactId, this.getVersion(), CVEIdentifiers);
return Dependency.builder()
.withGroupId(this.groupId)
.withArtifactId(this.artifactId)
.withVersion(this.getVersion())
.withCVEList(CVEIdentifiers)
.build();
}

public String getGroupId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.globaldashboard.dependencies.infrastructure.secondary;

import com.globaldashboard.dependencies.domain.Dependency;
import com.globaldashboard.dependencies.domain.GroupId;
import com.globaldashboard.dependencies.domain.Project;
import com.globaldashboard.dependencies.domain.SemanticVersion;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PomFactory {

Expand Down Expand Up @@ -42,7 +43,7 @@ public Project getPomFrom(Map<String, Document> pomXMLs, String pomURL, String p

}

return Project.builder()
return Project.builder()
.version(projectVersion)
.name(projectName)
.description(description)
Expand Down Expand Up @@ -111,11 +112,11 @@ private Dependency getDependency(Node currentDependencyNode, Map<String, String>
}
}

if (version != null && version.length() > 0) {
return new Dependency(groupIdLabel, artifactId, version);
}

return new Dependency(groupIdLabel, artifactId);
return Dependency.builder()
.withGroupId(groupIdLabel)
.withArtifactId(artifactId)
.withVersion(version)
.build();

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.globaldashboard.dependencies.domain;

import com.globaldashboard.fixture.CVEFixture;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

class DependencyTest {

@Test
void shouldNotHaveCVEWhenNoneProvided() {
Dependency dependency = Dependency.builder()
.withCVEList(List.of())
.build();

Optional<List<CVE>> actual = dependency.CVEList();

assertThat(actual).isEqualTo(Optional.empty());
}

@Test
void shouldAddCVEWhenProvided() {
Dependency dependency = Dependency.builder()
.withCVEList(List.of(CVEFixture.validIdentifier))
.build();
Optional<List<CVE>> expected = Optional.of(List.of(CVEFixture.cve()));

Optional<List<CVE>> actual = dependency.CVEList();

assertThat(actual).isEqualTo(expected);
}

@Test
void shouldNotHaveVersionWhenNoneProvided() {
Dependency dependency = Dependency.builder()
.withVersion("")
.build();

Optional<SemanticVersion> actual = dependency.version();

assertThat(actual).isEqualTo(Optional.empty());
}

@Test
void shouldAddVersionWhenProvided() {
Dependency dependency = Dependency.builder()
.withVersion("2.7.0")
.build();
Optional<SemanticVersion> expected = Optional.of(SemanticVersion.from("2.7.0"));

Optional<SemanticVersion> actual = dependency.version();

assertThat(actual).isEqualTo(expected);
}

}
12 changes: 12 additions & 0 deletions src/test/java/com/globaldashboard/fixture/CVEFixture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.globaldashboard.fixture;

import com.globaldashboard.dependencies.domain.CVE;

public class CVEFixture {

public static String validIdentifier = "CVE-2023-35116";

public static CVE cve() {
return new CVE(validIdentifier);
}
}
14 changes: 13 additions & 1 deletion src/test/java/com/globaldashboard/fixture/DependencyFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
public class DependencyFixture {

public static Dependency getCucumber() {
return new Dependency("io.cucumber", "cucumber-bom", "7.6.0");
return Dependency.builder()
.withGroupId("io.cucumber")
.withArtifactId("cucumber-bom")
.withVersion("7.6.0")
.build();
}

public static Dependency getJunit() {
return Dependency.builder()
.withGroupId("org.junit")
.withArtifactId("junit-bom")
.withVersion("5.9.0")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ void shouldBeAchievedWhenDependencyAtHigherVersion() {
@Test
void shouldNotBeAchievedWhenArtifactIdDoesntMatch() {
Objective objective = getObjective();
Dependency dependency = new Dependency("org.apache.logging.log4j", "different-artifact-id", "2.20.0");
Dependency dependency = getDefaultBuilder()
.withArtifactId("different-artifact-id")
.build();

boolean achieved = objective.isAchievedBy(dependency);

Expand All @@ -41,7 +43,9 @@ void shouldNotBeAchievedWhenArtifactIdDoesntMatch() {
@Test
void shouldNotBeAchievedWhenGroupIdDoesntMatch() {
Objective objective = getObjective();
Dependency dependency = new Dependency("differentGroupId", "log4j", "2.20.0");
Dependency dependency = getDefaultBuilder()
.withGroupId("differentGroupId")
.build();

boolean achieved = objective.isAchievedBy(dependency);

Expand All @@ -50,7 +54,9 @@ void shouldNotBeAchievedWhenGroupIdDoesntMatch() {

@Test
void shouldNotBeAchievedWhenDependencyAtLowerVersion() {
Dependency dependency = new Dependency("org.apache.logging.log4j", "log4j", "0.0.0");
Dependency dependency = getDefaultBuilder()
.withVersion("0.0.0")
.build();
Objective objective = getObjective();

boolean achieved = objective.isAchievedBy(dependency);
Expand All @@ -63,6 +69,13 @@ private Objective getObjective() {
}

private Dependency getDependency() {
return new Dependency("org.apache.logging.log4j", "log4j", "2.20.0");
return getDefaultBuilder().build();
}

private Dependency.Builder getDefaultBuilder() {
return Dependency.builder()
.withGroupId("org.apache.logging.log4j")
.withArtifactId("log4j")
.withVersion("2.20.0");
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.globaldashboard.unit.dependencies.infrastructure.primary;

import com.globaldashboard.dependencies.domain.Dependency;
import com.globaldashboard.dependencies.domain.GroupId;
import com.globaldashboard.dependencies.infrastructure.primary.RestDependency;
import com.globaldashboard.domain.ArtifactId;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

class RestDependencyTest {

@Test
void shouldBeBuildableFromDomain() {
Dependency dependency = new Dependency("groupId", "artifact-id", "1.2.3-SNAPSHOT", List.of("CVE-2023-35116"));
Dependency dependency = Dependency.builder()
.withGroupId("groupId")
.withArtifactId("artifact-id")
.withVersion("1.2.3-SNAPSHOT")
.withCVEList(List.of("CVE-2023-35116"))
.build();
RestDependency expectedRestDependency = new RestDependency("groupId", "artifact-id", "1.2.3-SNAPSHOT", List.of("CVE-2023-35116"));

RestDependency restDependency = RestDependency.from(dependency);
Expand All @@ -25,7 +27,10 @@ void shouldBeBuildableFromDomain() {

@Test
void shouldBeBuildableFromDomainWithNoVersion() {
Dependency dependency = new Dependency(new GroupId("groupId"), new ArtifactId("artifact-id"), Optional.empty(), Optional.empty());
Dependency dependency = Dependency.builder()
.withGroupId("groupId")
.withArtifactId("artifact-id")
.build();
RestDependency expectedRestDependency = new RestDependency("groupId", "artifact-id", "", List.of());

RestDependency restDependency = RestDependency.from(dependency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ private List<RestDependency> getRestDependencies() {
}

private List<Dependency> getDependencies() {
return List.of(new Dependency("goupId", "artifact-id", "1.0.0"));
Dependency dependency = Dependency.builder()
.withGroupId("goupId")
.withArtifactId("artifact-id")
.withVersion("1.0.0")
.build();
return List.of(dependency);
}
}
Loading

0 comments on commit 6fa1d40

Please sign in to comment.