Skip to content

336 fill all fields #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.springframework.sbm.build.api.Plugin;
import org.springframework.sbm.build.api.RepositoryDefinition;
import org.springframework.sbm.build.api.RewriteMavenParentDeclaration;
import org.springframework.sbm.build.impl.inner.PluginRepositoryHandler;
import org.springframework.sbm.build.migration.recipe.AddMavenPlugin;
import org.springframework.sbm.build.migration.recipe.RemoveMavenPlugin;
import org.springframework.sbm.build.migration.visitor.AddOrUpdateDependencyManagement;
Expand Down Expand Up @@ -77,9 +78,8 @@
@Slf4j
public class OpenRewriteMavenBuildFile extends RewriteSourceFileHolder<Xml.Document> implements BuildFile {

public static final String PLUGIN_REPOSITORIES = "pluginRepositories";
public static final String PLUGIN_REPOSITORY = "pluginRepository";
private final ApplicationEventPublisher eventPublisher;
private PluginRepositoryHandler pluginRepositoryHandler = new PluginRepositoryHandler();

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

@Override
public List<RepositoryDefinition> getPluginRepositories() {
List<Xml.Tag> tags = getSourceFile()
.getRoot()
.getChild(PLUGIN_REPOSITORIES)
.map(t -> t.getChildren(PLUGIN_REPOSITORY))
.orElse(List.of());

return tags.stream()
.map(k -> k.getChild("url"))
.map(k -> k.orElseThrow(() -> new RuntimeException("url is not set for plugin repository")).getValue())
.map(k -> k.orElseThrow(() -> new RuntimeException("url value is not set")))
.map(k -> RepositoryDefinition.builder().url(k).build()).collect(Collectors.toList());

return pluginRepositoryHandler.getRepositoryDefinitions(getSourceFile());
}

private boolean anyRegexMatchesCoordinate(Plugin p, String... regex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.sbm.build.impl.inner;

import org.openrewrite.xml.tree.Xml;
import org.springframework.sbm.build.api.RepositoryDefinition;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

public class PluginRepositoryHandler {
public static final String PLUGIN_REPOSITORIES = "pluginRepositories";
public static final String PLUGIN_REPOSITORY = "pluginRepository";

public List<RepositoryDefinition> getRepositoryDefinitions(Xml.Document sourceFile) {
List<Xml.Tag> tags = sourceFile
.getRoot()
.getChild(PLUGIN_REPOSITORIES)
.map(t -> t.getChildren(PLUGIN_REPOSITORY))
.orElse(List.of());

List<RepositoryDefinition> result = new ArrayList<>();
for (Xml.Tag t : tags) {
RepositoryDefinition.RepositoryDefinitionBuilder builder = RepositoryDefinition.builder();
getRepositoryAttribute(t, "url", builder::url, true);
getRepositoryAttribute(t, "id", builder::id, true);
getRepositoryAttribute(t, "layout", builder::layout, false);
getRepositoryAttribute(t, "snapshots.enabled", (k) -> builder.snapshotsEnabled(Boolean.valueOf(k)), false);
getRepositoryAttribute(t, "snapshots.checksumPolicy", builder::snapshotsChecksumPolicy, false);
getRepositoryAttribute(t, "snapshots.updatePolicy", builder::snapShotsUpdatePolicy, false);
getRepositoryAttribute(t, "releases.enabled", (k) -> builder.releasesEnabled(Boolean.valueOf(k)), false);
getRepositoryAttribute(t, "releases.checksumPolicy", builder::releasesChecksumPolicy, false);
getRepositoryAttribute(t, "releases.updatePolicy", builder::releasesUpdatePolicy, false);
result.add(builder.build());
}
return result;
}

private void getRepositoryAttribute(Xml.Tag tag, String attributeName, Consumer<String> initDefinition, boolean mandatory) {

String[] hierarchy = attributeName.split("\\.");

for (int i = 0; i < hierarchy.length - 1; i++) {
Optional<Xml.Tag> tagOptional = tag.getChild(hierarchy[i]);

if (tagOptional.isPresent()) {
tag = tagOptional.get();
}
}

attributeName = hierarchy[hierarchy.length - 1];
Optional<String> attributeValue = tag.getChildValue(attributeName);
if (mandatory && attributeValue.isEmpty()) {
throw new RuntimeException(attributeName + " is not set for plugin repository");
}
attributeValue.ifPresent(initDefinition);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.sbm.build.impl.inner;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.maven.MavenParser;
import org.openrewrite.xml.tree.Xml;
import org.springframework.sbm.build.api.RepositoryDefinition;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class PluginRepositoryHandlerTest {

private final PluginRepositoryHandler sut = new PluginRepositoryHandler();
private final MavenParser mavenParser = MavenParser.builder().build();

@Test
public void shouldConvertAllFieldsOfPluginRepo() {

Xml.Document sourceFile = getSourceFile("""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>boot-upgrade-27_30</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-upgrade-27_30</name>
<description>boot-upgrade-27_30</description>

<pluginRepositories>
<pluginRepository>
<id>spring-milestone</id>
<url>https://repo.spring.io/milestone</url>
<layout>default</layout>
<snapshots>
<checksumPolicy>fail</checksumPolicy>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
</snapshots>
<releases>
<checksumPolicy>warn</checksumPolicy>
<enabled>false</enabled>
<updatePolicy>always</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
""");
List<RepositoryDefinition> output = sut.getRepositoryDefinitions(sourceFile);
assertThat(output).hasSize(1);

assertThat(output.get(0).getUrl()).isEqualTo("https://repo.spring.io/milestone");
assertThat(output.get(0).getId()).isEqualTo("spring-milestone");
assertThat(output.get(0).getLayout()).isEqualTo("default");
assertThat(output.get(0).getSnapshotsEnabled()).isTrue();
assertThat(output.get(0).getSnapshotsChecksumPolicy()).isEqualTo("fail");
assertThat(output.get(0).getSnapShotsUpdatePolicy()).isEqualTo("daily");
assertThat(output.get(0).getReleasesEnabled()).isFalse();
assertThat(output.get(0).getReleasesChecksumPolicy()).isEqualTo("warn");
assertThat(output.get(0).getReleasesUpdatePolicy()).isEqualTo("always");
}

@Test
public void shouldThrowExceptionForMissingIdMandatoryAttribute() {

Xml.Document sourceFile = getSourceFile("""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>boot-upgrade-27_30</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-upgrade-27_30</name>
<description>boot-upgrade-27_30</description>

<pluginRepositories>
<pluginRepository>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>

""");
RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> sut.getRepositoryDefinitions(sourceFile));
assertThat(runtimeException).hasMessageContaining("id");
}

@Test
public void shouldThrowExceptionForMissingUrlMandatoryAttribute() {

Xml.Document sourceFile = getSourceFile("""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>boot-upgrade-27_30</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-upgrade-27_30</name>
<description>boot-upgrade-27_30</description>

<pluginRepositories>
<pluginRepository>
<id>someId</id>
</pluginRepository>
</pluginRepositories>
</project>

""");
RuntimeException runtimeException = assertThrows(RuntimeException.class, () -> sut.getRepositoryDefinitions(sourceFile));
assertThat(runtimeException).hasMessageContaining("url");
}

private Xml.Document getSourceFile(@Language("xml") String xml) {

return mavenParser.parse(xml).get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void repositoryWithSameUrlAndSnapshotsAndReleasesNotSet() {
" <pluginRepositories>\n" +
" <pluginRepository>\n" +
" <url>https://repo.spring.io/milestone</url>\n" +
" <id>milestone</id>\n" +
" </pluginRepository>\n" +
" </pluginRepositories>\n" +
"</project>";
Expand Down Expand Up @@ -69,6 +70,7 @@ void repositoryWithSameUrlAndSnapshotsFalseAndReleasesNotSet() {
" <pluginRepositories>\n" +
" <pluginRepository>\n" +
" <url>https://repo.spring.io/milestone</url>\n" +
" <id>milestone</id>\n" +
" <snapshots>\n" +
" <enabled>false</enabled>\n" +
" </snapshots>\n" +
Expand Down Expand Up @@ -102,6 +104,7 @@ void repositoryWithSameUrlAndSnapshotsTrueAndReleasesNotSet() {
" <pluginRepositories>\n" +
" <pluginRepository>\n" +
" <url>https://repo.spring.io/milestone</url>\n" +
" <id>milestone</id>\n" +
" <snapshots>\n" +
" <enabled>true</enabled>\n" +
" </snapshots>\n" +
Expand Down Expand Up @@ -135,6 +138,7 @@ void repositoryWithSameUrlAndSnapshotsNotSetAndReleasesTrue() {
" <pluginRepositories>\n" +
" <pluginRepository>\n" +
" <url>https://repo.spring.io/milestone</url>\n" +
" <id>milestone</id>\n" +
" <releases>\n" +
" <enabled>true</enabled>\n" +
" </releases>\n" +
Expand Down Expand Up @@ -168,6 +172,7 @@ void repositoryWithSameUrlAndSnapshotsNotSetAndReleasesFalse() {
" <pluginRepositories>\n" +
" <pluginRepository>\n" +
" <url>https://repo.spring.io/milestone</url>\n" +
" <id>milestone</id>\n" +
" <releases>\n" +
" <enabled>false</enabled>\n" +
" </releases>\n" +
Expand Down Expand Up @@ -201,6 +206,7 @@ void repositoryWithDifferentUrlAndSnapshotsSetToFalse() {
" <pluginRepositories>\n" +
" <pluginRepository>\n" +
" <url>https://repo.spring.io/milestone</url>\n" +
" <id>milestone</id>\n" +
" </pluginRepository>\n" +
" </pluginRepositories>\n" +
"</project>";
Expand Down