-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Dependency): add builder to initialize Dependencies
- Loading branch information
1 parent
84be39b
commit 6fa1d40
Showing
13 changed files
with
211 additions
and
54 deletions.
There are no files selected for viewing
53 changes: 39 additions & 14 deletions
53
src/main/java/com/globaldashboard/dependencies/domain/Dependency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/com/globaldashboard/dependencies/domain/DependencyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.