Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
targets validation out
Browse files Browse the repository at this point in the history
  • Loading branch information
abrams27 committed Feb 10, 2022
1 parent 09d9b0b commit daca08b
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,19 @@ public Builder javaPath(Optional<ProjectViewJavaPathSection> javaPath) {
}

public Try<ProjectView> build() {
return Try.sequence(importedProjectViews)
.map(Seq::toJavaList)
.flatMap(this::buildWithImports);
return Try.sequence(importedProjectViews).map(Seq::toJavaList).map(this::buildWithImports);
}

private Try<ProjectView> buildWithImports(List<ProjectView> importedProjectViews) {
return combineTargetsSection(importedProjectViews)
.map(targets -> buildWithImportsAndRequiredFields(importedProjectViews, targets));
}

private ProjectView buildWithImportsAndRequiredFields(
List<ProjectView> importedProjectViews, ProjectViewTargetsSection targets) {
private ProjectView buildWithImports(List<ProjectView> importedProjectViews) {
var targets = combineTargetsSection(importedProjectViews);
var bazelPath = combineBazelPathSection(importedProjectViews);
var debuggerAddress = combineDebuggerAddressSection(importedProjectViews);
var javaPath = combineJavaPathSection(importedProjectViews);

return new ProjectView(targets, bazelPath, debuggerAddress, javaPath);
}

private Try<ProjectViewTargetsSection> combineTargetsSection(
private ProjectViewTargetsSection combineTargetsSection(
List<ProjectView> importedProjectViews) {
var includedTargets =
combineListValuesWithImported(
Expand All @@ -167,9 +160,8 @@ private Try<ProjectViewTargetsSection> combineTargetsSection(
targets,
ProjectView::getTargets,
ProjectViewListSection::getExcludedValues);
var rawSection = new ProjectViewTargetsSection(includedTargets, excludedTargets);

return getListSectionOrFailureIfIsEmpty(rawSection);
return new ProjectViewTargetsSection(includedTargets, excludedTargets);
}

private <T extends ProjectViewListSection> List<String> combineListValuesWithImported(
Expand Down Expand Up @@ -215,18 +207,5 @@ private <T extends ProjectViewSingletonSection> Optional<T> getLastImportedSingl
.map(Optional::get)
.reduce((first, second) -> second);
}

private <T extends ProjectViewListSection> Try<T> getListSectionOrFailureIfIsEmpty(T section) {
if (isListSectionIsEmpty(section)) {
return Try.failure(
new IllegalStateException(
section.getSectionName() + " section cannot have an empty included list!"));
}
return Try.success(section);
}

private boolean isListSectionIsEmpty(ProjectViewListSection section) {
return section.getIncludedValues().isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public interface ProjectViewParser {
* <p><code>Try.success</code> with <code>ProjectView</code> if parsing has finished with
* success, it means:
* <p>1) files under <code>projectViewFilePath</code> and <code>defaultProjectViewFilePath
* </code> together provide all values (some might be <code>Optional.empty</code>). <br>
* </code> were successfully parsed (not all values have to be provided -- some fields in
* <code>ProjectView</code> might be <code>Optional.empty</code>). <br>
* File under <code>projectViewFilePath</code> can contain all values, then <code>
* defaultProjectViewFilePath</code> won't be used, or file under <code>projectViewFilePath
* </code> can be empty, then all values from file under <code>defaultProjectViewFilePath
Expand All @@ -33,9 +34,7 @@ public interface ProjectViewParser {
* <br>
* <p><code>Try.failure</code> with if:
* <p>1) file under <code>defaultProjectViewFilePath</code> doesn't exist
* <p>2) file under <code>defaultProjectViewFilePath</code> doesn't provide all required
* fields
* <p>3) any other fail happen
* <p>2) any other fail happen
*/
default Try<ProjectView> parse(Path projectViewFilePath, Path defaultProjectViewFilePath) {
return BetterFiles.tryReadFileContent(defaultProjectViewFilePath)
Expand Down Expand Up @@ -63,15 +62,15 @@ private Try<ProjectView> parseWithDefault(
* <p><code>Try.success</code> with <code>ProjectView</code> if parsing has finished with
* success, it means:
* <p>1) <code>projectViewFileContent</code> and <code>defaultProjectViewFileContent
* </code> together provide all values (some might be <code>Optional.empty</code>). <br>
* </code> were successfully parsed (not all values have to be provided -- some fields in
* <code>ProjectView</code> might be <code>Optional.empty</code>). <br>
* <code>projectViewFileContent</code> can contain all values, then <code>
* defaultProjectViewFileContent</code> won't be used, <code>projectViewFileContent
* </code> can be empty, then all values from <code>defaultProjectViewFileContent</code> will
* be used, any other configuration is possible as well.<br>
* <br>
* <p><code>Try.failure</code> with if:
* <p>1) <code>defaultProjectViewFilePath</code> doesn't provide all required fields
* <p>2) any other fail happen
* <p>1) any fail happen
*/
Try<ProjectView> parse(String projectViewFileContent, String defaultProjectViewFileContent);

Expand All @@ -82,12 +81,11 @@ private Try<ProjectView> parseWithDefault(
* @return
* <p><code>Try.success</code> with <code>ProjectView</code> if parsing has finished with
* success, it means:
* <p>1) file under <code>projectViewFilePath</code> provide all values (some might be <code>
* Optional.empty</code>).<br>
* <br>
* <p>1) file under <code>projectViewFilePath</code> was successfully parsed (not all values
* have to be provided -- some fields in <code>ProjectView</code> might be <code>
* Optional.empty</code>). <br>
* <p><code>Try.failure</code> with if:
* <p>1) file under <code>projectViewFilePath</code> doesn't exist
* <p>1) file under <code>projectViewFilePath</code> doesn't provide all required fields
* <p>2) any other fail happen
*/
default Try<ProjectView> parse(Path projectViewFilePath) {
Expand All @@ -101,12 +99,11 @@ default Try<ProjectView> parse(Path projectViewFilePath) {
* @return
* <p><code>Try.success</code> with <code>ProjectView</code> if parsing has finished with
* success, it means:
* <p>1) <code>projectViewFileContent</code> provide all values (some might be <code>
* Optional.empty</code>).<br>
* <br>
* <p>1) <code>projectViewFileContent</code> was successfully parsed (not all values have to
* be provided -- some fields in <code>ProjectView</code> might be <code>Optional.empty
* </code>). <br>
* <p><code>Try.failure</code> with if:
* <p>1) <code>projectViewFileContent</code> doesn't provide all required fields
* <p>2) any other fail happen
* <p>1) any fail happen
*/
Try<ProjectView> parse(String projectViewFileContent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void shouldReturnFailureWithFirstCauseForBuilderWithFailureImports() {
// targets specific tests

@Test
public void shouldReturnFailureForBuilderWithoutTargets() {
public void shouldReturnEmptyTargetsSectionForBuilderWithoutTargets() {
// given & when
var projectViewTry =
ProjectView.builder()
Expand All @@ -86,28 +86,11 @@ public void shouldReturnFailureForBuilderWithoutTargets() {
.build();

// then
assertTrue(projectViewTry.isFailure());
assertEquals(
"targets section cannot have an empty included list!",
projectViewTry.getCause().getMessage());
}

@Test
public void shouldReturnFailureForBuilderWithoutIncludedTargets() {
// given & when
var projectViewTry =
ProjectView.builder()
.targets(new ProjectViewTargetsSection(List.of(), List.of("//excluded_target")))
.bazelPath(dummyBazelPathSection)
.debuggerAddress(dummyDebuggerAddress)
.javaPath(dummyJavaPath)
.build();
assertTrue(projectViewTry.isSuccess());
var projectView = projectViewTry.get();

// then
assertTrue(projectViewTry.isFailure());
assertEquals(
"targets section cannot have an empty included list!",
projectViewTry.getCause().getMessage());
var expectedTargetsSection = new ProjectViewTargetsSection(List.of(), List.of());
assertEquals(expectedTargetsSection, projectView.getTargets());
}

// singleton values specific tests
Expand Down Expand Up @@ -261,6 +244,44 @@ public void shouldReturnImportedSingletonValuesAndListValues() {
assertEquals(expectedJavaPathSection, projectView.getJavaPath().get());
}

@Test
public void shouldReturnSingletonValuesAndListValuesForEmptyImport() {
// given
var importedProjectViewTry =
ProjectView.builder()
.targets(new ProjectViewTargetsSection())
.bazelPath(Optional.empty())
.debuggerAddress(Optional.empty())
.javaPath(Optional.empty())
.build();

// when
var projectViewTry =
ProjectView.builder()
.imports(List.of())
.targets(new ProjectViewTargetsSection(List.of(), List.of()))
.bazelPath(Optional.of(new ProjectViewBazelPathSection("path/to/bazel")))
.debuggerAddress(Optional.of(new ProjectViewDebuggerAddressSection("0.0.0.1:8000")))
.javaPath(Optional.of(new ProjectViewJavaPathSection("path/to/java")))
.build();

// then
assertTrue(projectViewTry.isSuccess());
var projectView = projectViewTry.get();

var expectedProjectViewTargetsSection = new ProjectViewTargetsSection(List.of(), List.of());
assertEquals(expectedProjectViewTargetsSection, projectView.getTargets());

var expectedProjectViewBazelPathSection = new ProjectViewBazelPathSection("path/to/bazel");
assertEquals(expectedProjectViewBazelPathSection, projectView.getBazelPath().get());

var expectedDebuggerAddressSection = new ProjectViewDebuggerAddressSection("0.0.0.1:8000");
assertEquals(expectedDebuggerAddressSection, projectView.getDebuggerAddress().get());

var expectedJavaPathSection = new ProjectViewJavaPathSection("path/to/java");
assertEquals(expectedJavaPathSection, projectView.getJavaPath().get());
}

@Test
public void shouldReturnCurrentSingletonValuesAndCombinedListValues() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ public void shouldReturnFailureForNotExistingFile() {
}

@Test
public void shouldThrowExceptionForFileWithoutTargetsSection() {
public void shouldReturnEmptyTargetsSectionForFileWithoutTargetsSection() {
// given
var projectViewFilePath = Paths.get("/projectview/without/targets.bazelproject");

// when
var projectViewTry = parser.parse(projectViewFilePath);

// then
assertTrue(projectViewTry.isFailure());
assertEquals(
"targets section cannot have an empty included list!",
projectViewTry.getCause().getMessage());
assertTrue(projectViewTry.isSuccess());
var projectView = projectViewTry.get();

var expectedTargetsSection = new ProjectViewTargetsSection();
assertEquals(expectedTargetsSection, projectView.getTargets());
}

@Test
Expand Down Expand Up @@ -88,7 +89,7 @@ public void shouldReturnEmptyDebuggerAddressForFileWithoutDebuggerAddressSection
}

@Test
public void shouldReturnEmptyDebuggerAddressForFileWithoutJavaPathSection() {
public void shouldReturnEmptyJavaPathSectionForFileWithoutJavaPathSection() {
// given
var projectViewFilePath = Paths.get("/projectview/without/javapath.bazelproject");

Expand All @@ -102,6 +103,29 @@ public void shouldReturnEmptyDebuggerAddressForFileWithoutJavaPathSection() {
assertTrue(projectView.getJavaPath().isEmpty());
}

@Test
public void shouldParseEmptyFile() {
// given
var projectViewFilePath = Paths.get("/projectview/empty.bazelproject");

// when
var projectViewTry = parser.parse(projectViewFilePath);

// then
assertTrue(projectViewTry.isSuccess());
var projectView = projectViewTry.get();

var expectedProjectView =
ProjectView.builder()
.targets(new ProjectViewTargetsSection())
.bazelPath(Optional.empty())
.debuggerAddress(Optional.empty())
.javaPath(Optional.empty())
.build()
.get();
assertEquals(expectedProjectView, projectView);
}

@Test
public void shouldParseFileWithAllSections() {
// given
Expand Down Expand Up @@ -182,6 +206,32 @@ public void shouldParseFileWithSingleImportedFileWithSingletonValues() {
assertEquals(expectedProjectView, projectView);
}

@Test
public void shouldParseFileWithEmptyImportedFile() {
// given
var projectViewFilePath = Paths.get("/projectview/file8ImportsEmpty.bazelproject");

// when
var projectViewTry = parser.parse(projectViewFilePath);

// then
assertTrue(projectViewTry.isSuccess());
var projectView = projectViewTry.get();

var expectedProjectView =
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target8.1"),
List.of("//excluded_target8.1", "//excluded_target8.2")))
.bazelPath(Optional.of(new ProjectViewBazelPathSection("path8/to/bazel")))
.debuggerAddress(Optional.of(new ProjectViewDebuggerAddressSection("0.0.0.8:8000")))
.javaPath(Optional.of(new ProjectViewJavaPathSection("path8/to/java")))
.build()
.get();
assertEquals(expectedProjectView, projectView);
}

@Test
public void shouldParseFileWithThreeImportedFiles() {
// given
Expand Down Expand Up @@ -251,7 +301,7 @@ public void shouldParseFileWithNestedImportedFiles() {
assertEquals(expectedProjectView, projectView);
}

// ProjectView parse(projectViewFileContent, defaultProjectViewFileContent)
// ProjectView parse(projectViewFilePath, defaultProjectViewFilePath)

@Test
public void shouldReturnFailureForNotExistingDefaultFile() {
Expand Down Expand Up @@ -286,7 +336,7 @@ public void shouldReturnFailureForNotExistingDefaultFileAndNotExistingFile() {
}

@Test
public void shouldReturnFailureForEmptyDefaultFile() {
public void shouldReturnFile1ForEmptyDefaultFile() {
// given
var projectViewFilePath = Paths.get("/projectview/file1.bazelproject");
var defaultProjectViewFilePath = Paths.get("/projectview/empty.bazelproject");
Expand All @@ -295,26 +345,38 @@ public void shouldReturnFailureForEmptyDefaultFile() {
var projectViewTry = parser.parse(projectViewFilePath, defaultProjectViewFilePath);

// then
assertTrue(projectViewTry.isFailure());
assertEquals(
"targets section cannot have an empty included list!",
projectViewTry.getCause().getMessage());
assertTrue(projectViewTry.isSuccess());
var projectView = projectViewTry.get();

var expectedProjectView =
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target1.1", "//included_target1.2"),
List.of("//excluded_target1.1")))
.bazelPath(Optional.of(new ProjectViewBazelPathSection("path1/to/bazel")))
.debuggerAddress(Optional.of(new ProjectViewDebuggerAddressSection("0.0.0.1:8000")))
.javaPath(Optional.of(new ProjectViewJavaPathSection("path1/to/java")))
.build()
.get();
assertEquals(expectedProjectView, projectView);
}

@Test
public void shouldReturnFailureForDefaultFileWithoutTargetsSection() {
public void shouldReturnEmptyTargetsForDefaultFileWithoutTargetsSection() {
// given
var projectViewFilePath = Paths.get("/projectview/file1.bazelproject");
var projectViewFilePath = Paths.get("/projectview/empty.bazelproject");
var defaultProjectViewFilePath = Paths.get("/projectview/without/targets.bazelproject");

// when
var projectViewTry = parser.parse(projectViewFilePath, defaultProjectViewFilePath);

// then
assertTrue(projectViewTry.isFailure());
assertEquals(
"targets section cannot have an empty included list!",
projectViewTry.getCause().getMessage());
assertTrue(projectViewTry.isSuccess());
var projectView = projectViewTry.get();

var expectedTargetsSection = new ProjectViewTargetsSection();
assertEquals(expectedTargetsSection, projectView.getTargets());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void before() {
// ProjectViewTargetsSection parse(rawSection)

@Test
public void shouldThrowIllegalArgumentExceptionForWrongSectionName() {
public void shouldReturnFailureForWrongSectionName() {
// given
var rawSection = new ProjectViewRawSection("wrongsection", "-bodyelement");

Expand Down
Loading

0 comments on commit daca08b

Please sign in to comment.