Skip to content

Commit e7575ad

Browse files
336 fill all fields (#342)
Co-authored-by: Andrei Shakirin <[email protected]>
1 parent 7efe389 commit e7575ad

File tree

4 files changed

+221
-13
lines changed

4 files changed

+221
-13
lines changed

components/sbm-core/src/main/java/org/springframework/sbm/build/impl/OpenRewriteMavenBuildFile.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.sbm.build.api.Plugin;
4949
import org.springframework.sbm.build.api.RepositoryDefinition;
5050
import org.springframework.sbm.build.api.RewriteMavenParentDeclaration;
51+
import org.springframework.sbm.build.impl.inner.PluginRepositoryHandler;
5152
import org.springframework.sbm.build.migration.recipe.AddMavenPlugin;
5253
import org.springframework.sbm.build.migration.recipe.RemoveMavenPlugin;
5354
import org.springframework.sbm.build.migration.visitor.AddOrUpdateDependencyManagement;
@@ -77,9 +78,8 @@
7778
@Slf4j
7879
public class OpenRewriteMavenBuildFile extends RewriteSourceFileHolder<Xml.Document> implements BuildFile {
7980

80-
public static final String PLUGIN_REPOSITORIES = "pluginRepositories";
81-
public static final String PLUGIN_REPOSITORY = "pluginRepository";
8281
private final ApplicationEventPublisher eventPublisher;
82+
private PluginRepositoryHandler pluginRepositoryHandler = new PluginRepositoryHandler();
8383

8484
// TODO: #7 clarify if RefreshPomModel is still required?
8585
// Execute separately since RefreshPomModel caches the refreshed maven files after the first visit
@@ -739,17 +739,8 @@ public List<RepositoryDefinition> getRepositories() {
739739

740740
@Override
741741
public List<RepositoryDefinition> getPluginRepositories() {
742-
List<Xml.Tag> tags = getSourceFile()
743-
.getRoot()
744-
.getChild(PLUGIN_REPOSITORIES)
745-
.map(t -> t.getChildren(PLUGIN_REPOSITORY))
746-
.orElse(List.of());
747-
748-
return tags.stream()
749-
.map(k -> k.getChild("url"))
750-
.map(k -> k.orElseThrow(() -> new RuntimeException("url is not set for plugin repository")).getValue())
751-
.map(k -> k.orElseThrow(() -> new RuntimeException("url value is not set")))
752-
.map(k -> RepositoryDefinition.builder().url(k).build()).collect(Collectors.toList());
742+
743+
return pluginRepositoryHandler.getRepositoryDefinitions(getSourceFile());
753744
}
754745

755746
private boolean anyRegexMatchesCoordinate(Plugin p, String... regex) {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.build.impl.inner;
18+
19+
import org.openrewrite.xml.tree.Xml;
20+
import org.springframework.sbm.build.api.RepositoryDefinition;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Optional;
25+
import java.util.function.Consumer;
26+
27+
public class PluginRepositoryHandler {
28+
public static final String PLUGIN_REPOSITORIES = "pluginRepositories";
29+
public static final String PLUGIN_REPOSITORY = "pluginRepository";
30+
31+
public List<RepositoryDefinition> getRepositoryDefinitions(Xml.Document sourceFile) {
32+
List<Xml.Tag> tags = sourceFile
33+
.getRoot()
34+
.getChild(PLUGIN_REPOSITORIES)
35+
.map(t -> t.getChildren(PLUGIN_REPOSITORY))
36+
.orElse(List.of());
37+
38+
List<RepositoryDefinition> result = new ArrayList<>();
39+
for (Xml.Tag t : tags) {
40+
RepositoryDefinition.RepositoryDefinitionBuilder builder = RepositoryDefinition.builder();
41+
getRepositoryAttribute(t, "url", builder::url, true);
42+
getRepositoryAttribute(t, "id", builder::id, true);
43+
getRepositoryAttribute(t, "layout", builder::layout, false);
44+
getRepositoryAttribute(t, "snapshots.enabled", (k) -> builder.snapshotsEnabled(Boolean.valueOf(k)), false);
45+
getRepositoryAttribute(t, "snapshots.checksumPolicy", builder::snapshotsChecksumPolicy, false);
46+
getRepositoryAttribute(t, "snapshots.updatePolicy", builder::snapShotsUpdatePolicy, false);
47+
getRepositoryAttribute(t, "releases.enabled", (k) -> builder.releasesEnabled(Boolean.valueOf(k)), false);
48+
getRepositoryAttribute(t, "releases.checksumPolicy", builder::releasesChecksumPolicy, false);
49+
getRepositoryAttribute(t, "releases.updatePolicy", builder::releasesUpdatePolicy, false);
50+
result.add(builder.build());
51+
}
52+
return result;
53+
}
54+
55+
private void getRepositoryAttribute(Xml.Tag tag, String attributeName, Consumer<String> initDefinition, boolean mandatory) {
56+
57+
String[] hierarchy = attributeName.split("\\.");
58+
59+
for (int i = 0; i < hierarchy.length - 1; i++) {
60+
Optional<Xml.Tag> tagOptional = tag.getChild(hierarchy[i]);
61+
62+
if (tagOptional.isPresent()) {
63+
tag = tagOptional.get();
64+
}
65+
}
66+
67+
attributeName = hierarchy[hierarchy.length - 1];
68+
Optional<String> attributeValue = tag.getChildValue(attributeName);
69+
if (mandatory && attributeValue.isEmpty()) {
70+
throw new RuntimeException(attributeName + " is not set for plugin repository");
71+
}
72+
attributeValue.ifPresent(initDefinition);
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
package org.springframework.sbm.build.impl.inner;
17+
18+
import org.intellij.lang.annotations.Language;
19+
import org.junit.jupiter.api.Test;
20+
import org.openrewrite.maven.MavenParser;
21+
import org.openrewrite.xml.tree.Xml;
22+
import org.springframework.sbm.build.api.RepositoryDefinition;
23+
24+
import java.util.List;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
29+
class PluginRepositoryHandlerTest {
30+
31+
private final PluginRepositoryHandler sut = new PluginRepositoryHandler();
32+
private final MavenParser mavenParser = MavenParser.builder().build();
33+
34+
@Test
35+
public void shouldConvertAllFieldsOfPluginRepo() {
36+
37+
Xml.Document sourceFile = getSourceFile("""
38+
<?xml version="1.0" encoding="UTF-8"?>
39+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
40+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
41+
<modelVersion>4.0.0</modelVersion>
42+
<groupId>com.example</groupId>
43+
<artifactId>boot-upgrade-27_30</artifactId>
44+
<version>0.0.1-SNAPSHOT</version>
45+
<name>boot-upgrade-27_30</name>
46+
<description>boot-upgrade-27_30</description>
47+
48+
<pluginRepositories>
49+
<pluginRepository>
50+
<id>spring-milestone</id>
51+
<url>https://repo.spring.io/milestone</url>
52+
<layout>default</layout>
53+
<snapshots>
54+
<checksumPolicy>fail</checksumPolicy>
55+
<enabled>true</enabled>
56+
<updatePolicy>daily</updatePolicy>
57+
</snapshots>
58+
<releases>
59+
<checksumPolicy>warn</checksumPolicy>
60+
<enabled>false</enabled>
61+
<updatePolicy>always</updatePolicy>
62+
</releases>
63+
</pluginRepository>
64+
</pluginRepositories>
65+
</project>
66+
""");
67+
List<RepositoryDefinition> output = sut.getRepositoryDefinitions(sourceFile);
68+
assertThat(output).hasSize(1);
69+
70+
assertThat(output.get(0).getUrl()).isEqualTo("https://repo.spring.io/milestone");
71+
assertThat(output.get(0).getId()).isEqualTo("spring-milestone");
72+
assertThat(output.get(0).getLayout()).isEqualTo("default");
73+
assertThat(output.get(0).getSnapshotsEnabled()).isTrue();
74+
assertThat(output.get(0).getSnapshotsChecksumPolicy()).isEqualTo("fail");
75+
assertThat(output.get(0).getSnapShotsUpdatePolicy()).isEqualTo("daily");
76+
assertThat(output.get(0).getReleasesEnabled()).isFalse();
77+
assertThat(output.get(0).getReleasesChecksumPolicy()).isEqualTo("warn");
78+
assertThat(output.get(0).getReleasesUpdatePolicy()).isEqualTo("always");
79+
}
80+
81+
@Test
82+
public void shouldThrowExceptionForMissingIdMandatoryAttribute() {
83+
84+
Xml.Document sourceFile = getSourceFile("""
85+
<?xml version="1.0" encoding="UTF-8"?>
86+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
87+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
88+
<modelVersion>4.0.0</modelVersion>
89+
<groupId>com.example</groupId>
90+
<artifactId>boot-upgrade-27_30</artifactId>
91+
<version>0.0.1-SNAPSHOT</version>
92+
<name>boot-upgrade-27_30</name>
93+
<description>boot-upgrade-27_30</description>
94+
95+
<pluginRepositories>
96+
<pluginRepository>
97+
<url>https://repo.spring.io/milestone</url>
98+
</pluginRepository>
99+
</pluginRepositories>
100+
</project>
101+
102+
""");
103+
RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> sut.getRepositoryDefinitions(sourceFile));
104+
assertThat(runtimeException).hasMessageContaining("id");
105+
}
106+
107+
@Test
108+
public void shouldThrowExceptionForMissingUrlMandatoryAttribute() {
109+
110+
Xml.Document sourceFile = getSourceFile("""
111+
<?xml version="1.0" encoding="UTF-8"?>
112+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
113+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
114+
<modelVersion>4.0.0</modelVersion>
115+
<groupId>com.example</groupId>
116+
<artifactId>boot-upgrade-27_30</artifactId>
117+
<version>0.0.1-SNAPSHOT</version>
118+
<name>boot-upgrade-27_30</name>
119+
<description>boot-upgrade-27_30</description>
120+
121+
<pluginRepositories>
122+
<pluginRepository>
123+
<id>someId</id>
124+
</pluginRepository>
125+
</pluginRepositories>
126+
</project>
127+
128+
""");
129+
RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> sut.getRepositoryDefinitions(sourceFile));
130+
assertThat(runtimeException).hasMessageContaining("url");
131+
}
132+
133+
private Xml.Document getSourceFile(@Language("xml") String xml) {
134+
135+
return mavenParser.parse(xml).get(0);
136+
}
137+
}

components/sbm-core/src/test/java/org/springframework/sbm/build/migration/conditions/NoPluginRepositoryExistsConditionTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void repositoryWithSameUrlAndSnapshotsAndReleasesNotSet() {
3939
" <pluginRepositories>\n" +
4040
" <pluginRepository>\n" +
4141
" <url>https://repo.spring.io/milestone</url>\n" +
42+
" <id>milestone</id>\n" +
4243
" </pluginRepository>\n" +
4344
" </pluginRepositories>\n" +
4445
"</project>";
@@ -69,6 +70,7 @@ void repositoryWithSameUrlAndSnapshotsFalseAndReleasesNotSet() {
6970
" <pluginRepositories>\n" +
7071
" <pluginRepository>\n" +
7172
" <url>https://repo.spring.io/milestone</url>\n" +
73+
" <id>milestone</id>\n" +
7274
" <snapshots>\n" +
7375
" <enabled>false</enabled>\n" +
7476
" </snapshots>\n" +
@@ -102,6 +104,7 @@ void repositoryWithSameUrlAndSnapshotsTrueAndReleasesNotSet() {
102104
" <pluginRepositories>\n" +
103105
" <pluginRepository>\n" +
104106
" <url>https://repo.spring.io/milestone</url>\n" +
107+
" <id>milestone</id>\n" +
105108
" <snapshots>\n" +
106109
" <enabled>true</enabled>\n" +
107110
" </snapshots>\n" +
@@ -135,6 +138,7 @@ void repositoryWithSameUrlAndSnapshotsNotSetAndReleasesTrue() {
135138
" <pluginRepositories>\n" +
136139
" <pluginRepository>\n" +
137140
" <url>https://repo.spring.io/milestone</url>\n" +
141+
" <id>milestone</id>\n" +
138142
" <releases>\n" +
139143
" <enabled>true</enabled>\n" +
140144
" </releases>\n" +
@@ -168,6 +172,7 @@ void repositoryWithSameUrlAndSnapshotsNotSetAndReleasesFalse() {
168172
" <pluginRepositories>\n" +
169173
" <pluginRepository>\n" +
170174
" <url>https://repo.spring.io/milestone</url>\n" +
175+
" <id>milestone</id>\n" +
171176
" <releases>\n" +
172177
" <enabled>false</enabled>\n" +
173178
" </releases>\n" +
@@ -201,6 +206,7 @@ void repositoryWithDifferentUrlAndSnapshotsSetToFalse() {
201206
" <pluginRepositories>\n" +
202207
" <pluginRepository>\n" +
203208
" <url>https://repo.spring.io/milestone</url>\n" +
209+
" <id>milestone</id>\n" +
204210
" </pluginRepository>\n" +
205211
" </pluginRepositories>\n" +
206212
"</project>";

0 commit comments

Comments
 (0)