|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.sbm.boot.upgrade_27_30.checks; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.sbm.boot.asciidoctor.ChangeSection; |
| 21 | +import org.springframework.sbm.boot.asciidoctor.Section; |
| 22 | +import org.springframework.sbm.boot.asciidoctor.TodoList; |
| 23 | +import org.springframework.sbm.boot.upgrade_27_30.checks.RedeclaredDependenciesFinder.RedeclaredDependency; |
| 24 | +import org.springframework.sbm.build.api.ApplicationModule; |
| 25 | +import org.springframework.sbm.build.api.Dependency; |
| 26 | +import org.springframework.sbm.engine.context.ProjectContext; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.when; |
| 35 | + |
| 36 | +/* |
| 37 | + * Copyright 2021 - 2022 the original author or authors. |
| 38 | + * |
| 39 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 40 | + * you may not use this file except in compliance with the License. |
| 41 | + * You may obtain a copy of the License at |
| 42 | + * |
| 43 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 44 | + * |
| 45 | + * Unless required by applicable law or agreed to in writing, software |
| 46 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 47 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 48 | + * See the License for the specific language governing permissions and |
| 49 | + * limitations under the License. |
| 50 | + */ |
| 51 | + |
| 52 | +public class RedeclaredDependenciesBuilderTest { |
| 53 | + |
| 54 | + @Test |
| 55 | + void shouldBuildSectionWhenFinderHasMatches() { |
| 56 | + Set<RedeclaredDependency> matches = Set.of(new RedeclaredDependency( |
| 57 | + Dependency.builder() |
| 58 | + .groupId("test.group") |
| 59 | + .artifactId("test-artifact") |
| 60 | + .version("2.0.0") |
| 61 | + .build(), |
| 62 | + "1.0.0" |
| 63 | + )); |
| 64 | + |
| 65 | + ProjectContext context = mock(ProjectContext.class); |
| 66 | + RedeclaredDependenciesFinder finder = mock(RedeclaredDependenciesFinder.class); |
| 67 | + when(finder.findMatches(context)).thenReturn(matches); |
| 68 | + RedeclaredDependenciesBuilder builder = new RedeclaredDependenciesBuilder(finder); |
| 69 | + assertThat(builder.isApplicable(context)).isTrue(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void shouldBuildSectionWhenFinderHasNoMatches() { |
| 74 | + Set<RedeclaredDependency> matches = Set.of(); |
| 75 | + ProjectContext context = mock(ProjectContext.class); |
| 76 | + RedeclaredDependenciesFinder finder = mock(RedeclaredDependenciesFinder.class); |
| 77 | + when(finder.findMatches(context)).thenReturn(matches); |
| 78 | + RedeclaredDependenciesBuilder builder = new RedeclaredDependenciesBuilder(finder); |
| 79 | + assertThat(builder.isApplicable(context)).isFalse(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void assertContent() { |
| 84 | + |
| 85 | + Set<RedeclaredDependency> matches = Set.of(new RedeclaredDependency( |
| 86 | + Dependency.builder() |
| 87 | + .groupId("test.group") |
| 88 | + .artifactId("test-artifact") |
| 89 | + .version("2.0.0") |
| 90 | + .build(), |
| 91 | + "1.0.0" |
| 92 | + ), |
| 93 | + new RedeclaredDependency( |
| 94 | + Dependency.builder() |
| 95 | + .groupId("test.group") |
| 96 | + .artifactId("test-artifact2") |
| 97 | + .version("3.0.0") |
| 98 | + .build(), |
| 99 | + "2.0.0" |
| 100 | + )); |
| 101 | + |
| 102 | + ProjectContext context = mock(ProjectContext.class); |
| 103 | + RedeclaredDependenciesFinder finder = mock(RedeclaredDependenciesFinder.class); |
| 104 | + when(finder.findMatches(context)).thenReturn(matches); |
| 105 | + RedeclaredDependenciesBuilder builder = new RedeclaredDependenciesBuilder(finder); |
| 106 | + |
| 107 | + Section section = builder.build(context); |
| 108 | + |
| 109 | + assertThat(section).isInstanceOf(ChangeSection.class); |
| 110 | + ChangeSection relevantChangeSection = ChangeSection.class.cast(section); |
| 111 | + assertThat(relevantChangeSection.getTitle()).isEqualTo("Remove redundant explicit version declaration"); |
| 112 | + assertThat(relevantChangeSection.getRelevanceSection()).isNotNull(); |
| 113 | + assertThat(relevantChangeSection.getRelevanceSection().getParagraphs()).hasSize(1); |
| 114 | + assertThat(relevantChangeSection.getRelevanceSection().getParagraphs().get(0).getText()).isEqualTo(""" |
| 115 | + The scan found one or more redeclared dependencies in build files. |
| 116 | + Please check them and remove redundant declarations. |
| 117 | + """); |
| 118 | + List<TodoList.Todo> todos = relevantChangeSection.getTodoSection().getTodoLists().get(0).getTodos(); |
| 119 | + List<String> stringToDos = todos.stream() |
| 120 | + .map(TodoList.Todo::getText) |
| 121 | + .collect(Collectors.toList()); |
| 122 | + assertThat(stringToDos).contains("Remove explicit declaration of version for artifact: test.group:test-artifact:2.0.0, its already declared with version 1.0.0"); |
| 123 | + assertThat(stringToDos).contains("Remove explicit declaration of version for artifact: test.group:test-artifact2:3.0.0, its already declared with version 2.0.0"); |
| 124 | + } |
| 125 | +} |
0 commit comments