forked from spring-io/initializr
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit improves the `info` endpoint to contain a `dependency-ranges` entry that is similar than the `bom-ranges` entry for BOMs. Each dependency that has a version mapping is listed with the range and the related version. Some dependencies weren't managed and are now. For those a `managed` version is used to indicate which Spring Boot versions do not require to specify a version for the dependency. Closes spring-iogh-453
- Loading branch information
Showing
5 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
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
94 changes: 94 additions & 0 deletions
94
...ator/src/main/java/io/spring/initializr/actuate/info/DependencyRangesInfoContributor.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,94 @@ | ||
/* | ||
* Copyright 2012-2017 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 | ||
* | ||
* http://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 io.spring.initializr.actuate.info; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import io.spring.initializr.metadata.InitializrMetadataProvider; | ||
import io.spring.initializr.util.Version; | ||
import io.spring.initializr.util.VersionRange; | ||
|
||
import org.springframework.boot.actuate.info.Info; | ||
import org.springframework.boot.actuate.info.InfoContributor; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
/** | ||
* An {@link InfoContributor} that exposes the actual ranges used by dependencies | ||
* defined in the project that have an explicit version (i.e. not relying on a bom). | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
public class DependencyRangesInfoContributor implements InfoContributor { | ||
|
||
private final InitializrMetadataProvider metadataProvider; | ||
|
||
public DependencyRangesInfoContributor(InitializrMetadataProvider metadataProvider) { | ||
this.metadataProvider = metadataProvider; | ||
} | ||
|
||
@Override | ||
public void contribute(Info.Builder builder) { | ||
Map<String, Object> details = new LinkedHashMap<>(); | ||
metadataProvider.get().getDependencies().getAll().forEach(d -> { | ||
if (d.getBom() == null) { | ||
if (!ObjectUtils.isEmpty(d.getMappings())) { | ||
Map<String, VersionRange> dep = new LinkedHashMap<>(); | ||
d.getMappings().forEach(it -> { | ||
if (it.getRange() != null && it.getVersion() != null) { | ||
dep.put(it.getVersion(), it.getRange()); | ||
} | ||
}); | ||
if (!dep.isEmpty()) { | ||
if (d.getRange() == null) { | ||
boolean openRange = dep.values().stream().anyMatch( | ||
v -> v.getHigherVersion() == null); | ||
if (!openRange) { | ||
Version higher = null; | ||
for (VersionRange versionRange : dep.values()) { | ||
Version candidate = versionRange.getHigherVersion(); | ||
if (higher == null) { | ||
higher = candidate; | ||
} | ||
else if (candidate.compareTo(higher) > 0) { | ||
higher = candidate; | ||
} | ||
} ; | ||
dep.put("managed", new VersionRange(higher)); | ||
} | ||
} | ||
Map<String, Object> depInfo = new LinkedHashMap<>(); | ||
dep.forEach((k, r) -> { | ||
depInfo.put(k, "Spring Boot " + r); | ||
}); | ||
details.put(d.getId(), depInfo); | ||
} | ||
} | ||
else if (d.getVersion() != null && d.getRange() != null) { | ||
Map<String, Object> dep = new LinkedHashMap<>(); | ||
String requirement = "Spring Boot " + d.getRange(); | ||
dep.put(d.getVersion(), requirement); | ||
details.put(d.getId(), dep); | ||
} | ||
} | ||
}); | ||
if (!details.isEmpty()) { | ||
builder.withDetail("dependency-ranges", details); | ||
} | ||
} | ||
|
||
} |
163 changes: 163 additions & 0 deletions
163
...src/test/java/io/spring/initializr/actuate/info/DependencyRangesInfoContributorTests.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,163 @@ | ||
/* | ||
* Copyright 2012-2017 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 | ||
* | ||
* http://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 io.spring.initializr.actuate.info; | ||
|
||
import java.util.Map; | ||
|
||
import io.spring.initializr.metadata.BillOfMaterials; | ||
import io.spring.initializr.metadata.Dependency; | ||
import io.spring.initializr.metadata.InitializrMetadata; | ||
import io.spring.initializr.metadata.SimpleInitializrMetadataProvider; | ||
import io.spring.initializr.test.metadata.InitializrMetadataTestBuilder; | ||
import org.junit.Test; | ||
|
||
import org.springframework.boot.actuate.info.Info; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.entry; | ||
|
||
/** | ||
* Tests for {@link DependencyRangesInfoContributor}. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
public class DependencyRangesInfoContributorTests { | ||
|
||
@Test | ||
public void noDependencyWithVersion() { | ||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() | ||
.build(); | ||
Info info = getInfo(metadata); | ||
assertThat(info.getDetails()).doesNotContainKeys("dependency-ranges"); | ||
} | ||
|
||
@Test | ||
public void dependencyWithNoMapping() { | ||
Dependency dependency = Dependency.withId("foo", "com.example", "foo", | ||
"1.2.3.RELEASE"); | ||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() | ||
.addDependencyGroup("foo", dependency).build(); | ||
Info info = getInfo(metadata); | ||
assertThat(info.getDetails()).doesNotContainKeys("dependency-ranges"); | ||
} | ||
|
||
|
||
@Test | ||
public void dependencyWithRangeOnArtifact() { | ||
Dependency dependency = Dependency.withId("foo", "com.example", "foo", | ||
"1.2.3.RELEASE"); | ||
dependency.getMappings().add(Dependency.Mapping | ||
.create("[1.1.0.RELEASE, 1.2.0.RELEASE)", null, "foo2", null)); | ||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() | ||
.addDependencyGroup("foo", dependency).build(); | ||
Info info = getInfo(metadata); | ||
assertThat(info.getDetails()).doesNotContainKeys("dependency-ranges"); | ||
} | ||
|
||
@Test | ||
public void dependencyWithRangeAndBom() { | ||
BillOfMaterials bom = BillOfMaterials.create("com.example", "bom", "1.0.0"); | ||
Dependency dependency = Dependency.withId("foo", "com.example", "foo", | ||
"1.2.3.RELEASE"); | ||
dependency.getMappings().add(Dependency.Mapping | ||
.create("[1.1.0.RELEASE, 1.2.0.RELEASE)", null, null, "0.1.0.RELEASE")); | ||
dependency.setBom("bom"); | ||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() | ||
.addBom("bom", bom) | ||
.addDependencyGroup("foo", dependency).build(); | ||
Info info = getInfo(metadata); | ||
assertThat(info.getDetails()).doesNotContainKeys("dependency-ranges"); | ||
} | ||
|
||
@Test | ||
public void dependencyNoMappingSimpleRange() { | ||
Dependency dependency = Dependency.withId("foo", "com.example", "foo", | ||
"1.2.3.RELEASE"); | ||
dependency.setVersionRange("[1.1.0.RELEASE, 1.5.0.RELEASE)"); | ||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() | ||
.addDependencyGroup("foo", dependency).build(); | ||
Info info = getInfo(metadata); | ||
assertThat(info.getDetails()).containsKeys("dependency-ranges"); | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> ranges = (Map<String, Object>) info.getDetails() | ||
.get("dependency-ranges"); | ||
assertThat(ranges).containsOnlyKeys("foo"); | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> foo = (Map<String, Object>) ranges.get("foo"); | ||
assertThat(foo).containsExactly( | ||
entry("1.2.3.RELEASE", "Spring Boot >=1.1.0.RELEASE and <1.5.0.RELEASE")); | ||
} | ||
|
||
@Test | ||
public void dependencyWithMappingAndOpenRange() { | ||
Dependency dependency = Dependency.withId("foo", null, null, "0.3.0.RELEASE"); | ||
dependency.getMappings().add(Dependency.Mapping | ||
.create("[1.1.0.RELEASE, 1.2.0.RELEASE)", null, null, "0.1.0.RELEASE")); | ||
dependency.getMappings().add(Dependency.Mapping | ||
.create("1.2.0.RELEASE", null, null, "0.2.0.RELEASE")); | ||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() | ||
.addDependencyGroup("test", dependency).build(); | ||
Info info = getInfo(metadata); | ||
assertDependencyId(info, "foo"); | ||
Map<String, Object> foo = getDependencyRangeInfo(info, "foo"); | ||
assertThat(foo).containsExactly( | ||
entry("0.1.0.RELEASE", "Spring Boot >=1.1.0.RELEASE and <1.2.0.RELEASE"), | ||
entry("0.2.0.RELEASE", "Spring Boot >=1.2.0.RELEASE")); | ||
} | ||
|
||
@Test | ||
public void dependencyWithMappingAndNoOpenRange() { | ||
Dependency dependency = Dependency.withId("foo", null, null, "0.3.0.RELEASE"); | ||
dependency.getMappings().add(Dependency.Mapping | ||
.create("[1.1.0.RELEASE, 1.2.0.RELEASE)", null, null, "0.1.0.RELEASE")); | ||
dependency.getMappings().add(Dependency.Mapping | ||
.create("[1.2.0.RELEASE, 1.3.0.RELEASE)", null, null, "0.2.0.RELEASE")); | ||
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() | ||
.addDependencyGroup("test", dependency).build(); | ||
Info info = getInfo(metadata); | ||
assertDependencyId(info, "foo"); | ||
Map<String, Object> foo = getDependencyRangeInfo(info, "foo"); | ||
assertThat(foo).containsExactly( | ||
entry("0.1.0.RELEASE", "Spring Boot >=1.1.0.RELEASE and <1.2.0.RELEASE"), | ||
entry("0.2.0.RELEASE", "Spring Boot >=1.2.0.RELEASE and <1.3.0.RELEASE"), | ||
entry("managed", "Spring Boot >=1.3.0.RELEASE")); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void assertDependencyId(Info info, String... dependencyIds) { | ||
assertThat(info.getDetails()).containsKeys("dependency-ranges"); | ||
Map<String, Object> ranges = (Map<String, Object>) info.getDetails() | ||
.get("dependency-ranges"); | ||
assertThat(ranges).containsOnlyKeys(dependencyIds); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Map<String,Object> getDependencyRangeInfo(Info info, String id) { | ||
assertThat(info.getDetails()).containsKeys("dependency-ranges"); | ||
Map<String, Object> ranges = (Map<String, Object>) info.getDetails() | ||
.get("dependency-ranges"); | ||
return (Map<String, Object>) ranges.get(id); | ||
} | ||
|
||
private static Info getInfo(InitializrMetadata metadata) { | ||
Info.Builder builder = new Info.Builder(); | ||
new DependencyRangesInfoContributor(new SimpleInitializrMetadataProvider(metadata)) | ||
.contribute(builder); | ||
return builder.build(); | ||
} | ||
|
||
} |
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