@Test void resolveRangeOverrideAndMapping() { BillOfMaterials bom = BillOfMaterials.create("com.example", "bom", "1.0.0"); bom.setVersionProperty("example.version"); bom.getMappings().add(Mapping.create("[1.2.0.RELEASE,1.3.0.M1)", "1.1.0")); bom.validate(); BillOfMaterials resolved = bom.resolve(Version.parse("1.2.3.RELEASE")); assertThat(resolved.getGroupId()).isEqualTo("com.example"); assertThat(resolved.getArtifactId()).isEqualTo("bom"); assertThat(resolved.getVersion()).isEqualTo("1.1.0"); assertThat(resolved.getVersionProperty().toStandardFormat()) .isEqualTo("example.version"); }
private static JsonNode mapBom(BillOfMaterials bom) { ObjectNode node = nodeFactory.objectNode(); node.put("groupId", bom.getGroupId()); node.put("artifactId", bom.getArtifactId()); if (bom.getVersion() != null) { node.put("version", bom.getVersion()); } if (bom.getRepositories() != null) { ArrayNode array = nodeFactory.arrayNode(); bom.getRepositories().forEach(array::add); node.set("repositories", array); } return node; }
/** * Resolve this instance according to the specified Spring Boot {@link Version}. * Return a {@link BillOfMaterials} instance that holds the version, repositories and * additional BOMs to use, if any. * @param bootVersion the Spring Boot version * @return the bill of materials */ public BillOfMaterials resolve(Version bootVersion) { if (this.mappings.isEmpty()) { return this; } for (Mapping mapping : this.mappings) { if (mapping.range.match(bootVersion)) { BillOfMaterials resolvedBom = new BillOfMaterials( (mapping.groupId != null) ? mapping.groupId : this.groupId, (mapping.artifactId != null) ? mapping.artifactId : this.artifactId, mapping.version); resolvedBom.setVersionProperty(this.versionProperty); resolvedBom.setOrder(this.order); resolvedBom.repositories.addAll(!mapping.repositories.isEmpty() ? mapping.repositories : this.repositories); resolvedBom.additionalBoms.addAll(!mapping.additionalBoms.isEmpty() ? mapping.additionalBoms : this.additionalBoms); return resolvedBom; } } throw new IllegalStateException("No suitable mapping was found for " + this + " and version " + bootVersion); }
private Map<String, String> toBomModel(ProjectRequest request, BillOfMaterials bom) { Map<String, String> model = new HashMap<>(); model.put("groupId", bom.getGroupId()); model.put("artifactId", bom.getArtifactId()); model.put("versionToken", (bom.getVersionProperty() != null) ? "${" + computeVersionProperty(request, bom.getVersionProperty()) + "}" : bom.getVersion()); return model; }
/** * Create a {@link BillOfMaterials} for the spring boot BOM. * @param bootVersion the Spring Boot version * @param versionProperty the property that contains the version * @return a new {@link BillOfMaterials} instance */ public BillOfMaterials createSpringBootBom(String bootVersion, String versionProperty) { BillOfMaterials bom = BillOfMaterials.create("org.springframework.boot", "spring-boot-dependencies", bootVersion); bom.setVersionProperty(VersionProperty.of(versionProperty)); bom.setOrder(100); return bom; }
@Test void mergeMetadataWithBom() { InitializrMetadata metadata = InitializrMetadataBuilder.create() .withInitializrMetadata( new ClassPathResource("metadata/config/test-bom.json")) .build(); Map<String, BillOfMaterials> boms = metadata.getConfiguration().getEnv() .getBoms(); assertThat(boms).hasSize(2); BillOfMaterials myBom = boms.get("my-bom"); assertThat(myBom).isNotNull(); assertThat(myBom.getGroupId()).isEqualTo("org.acme"); assertThat(myBom.getArtifactId()).isEqualTo("my-bom"); assertThat(myBom.getVersion()).isEqualTo("2.1.1.RELEASE"); BillOfMaterials anotherBom = boms.get("another-bom"); assertThat(anotherBom).isNotNull(); assertThat(anotherBom.getGroupId()).isEqualTo("org.acme"); assertThat(anotherBom.getArtifactId()).isEqualTo("another-bom"); assertThat(anotherBom.getVersion()).isEqualTo("4.5.6.RELEASE"); }
@Test void addBomAndRemoveDuplicates() { Dependency first = Dependency.withId("first", "org.foo", "first"); first.setBom("bom-foo"); Dependency second = Dependency.withId("second", "org.foo", "second"); Dependency third = Dependency.withId("third", "org.foo", "third"); third.setBom("bom-foo"); BillOfMaterials bom = BillOfMaterials.create("org.foo", "bom"); bom.getMappings().add(BillOfMaterials.Mapping .create("[1.0.0.RELEASE, 1.1.8.RELEASE)", "1.0.0.RELEASE")); bom.getMappings() .add(BillOfMaterials.Mapping.create("1.1.8.RELEASE", "2.0.0.RELEASE")); InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() .addBom("bom-foo", bom).addDependencyGroup("test", first, second, third) .build(); DependencyMetadata dependencyMetadata = this.provider.get(metadata, Version.parse("1.1.5.RELEASE")); assertThat(dependencyMetadata.getDependencies()).hasSize(3); assertThat(dependencyMetadata.getRepositories()).isEmpty(); assertThat(dependencyMetadata.getBoms()).hasSize(1); assertThat(dependencyMetadata.getBoms().get("bom-foo").getGroupId()) .isEqualTo("org.foo"); assertThat(dependencyMetadata.getBoms().get("bom-foo").getArtifactId()) .isEqualTo("bom"); assertThat(dependencyMetadata.getBoms().get("bom-foo").getVersion()) .isEqualTo("1.0.0.RELEASE"); }
@Test void resolveRangeWithVariablePatch() { BillOfMaterials bom = BillOfMaterials.create("com.example", "bom", "1.0.0"); bom.getMappings().add(Mapping.create("[1.3.0.RELEASE,1.3.x.RELEASE]", "1.1.0")); bom.getMappings().add(BillOfMaterials.Mapping .create("[1.3.x.BUILD-SNAPSHOT,1.4.0.RELEASE)", "1.1.1-SNAPSHOT")); bom.validate(); bom.updateVersionRange(new VersionParser(Arrays.asList( Version.parse("1.3.8.RELEASE"), Version.parse("1.3.9.BUILD-SNAPSHOT")))); assertThat(bom.resolve(Version.parse("1.3.8.RELEASE")).getVersion()) .isEqualTo("1.1.0"); assertThat(bom.resolve(Version.parse("1.3.9.RELEASE")).getVersion()) .isEqualTo("1.1.1-SNAPSHOT"); bom.updateVersionRange(new VersionParser(Arrays.asList( Version.parse("1.3.9.RELEASE"), Version.parse("1.3.10.BUILD-SNAPSHOT")))); assertThat(bom.resolve(Version.parse("1.3.8.RELEASE")).getVersion()) .isEqualTo("1.1.0"); assertThat(bom.resolve(Version.parse("1.3.9.RELEASE")).getVersion()) .isEqualTo("1.1.0"); }
@Test void invalidBomVersionRangeMapping() { InitializrMetadata metadata = initializeMetadata(); BillOfMaterials bom = BillOfMaterials.create("org.acme", "foo-bom"); bom.getMappings().add(Mapping.create("[1.2.0.RELEASE,1.3.0.M1)", "1.0.0")); bom.getMappings().add(Mapping.create("FOO_BAR", "1.2.0")); metadata.getConfiguration().getEnv().getBoms().put("foo-bom", bom); assertThatExceptionOfType(InvalidInitializrMetadataException.class) .isThrownBy(metadata::validate).withMessageContaining("FOO_BAR") .withMessageContaining("foo-bom"); }
@Test void updateSpringBootVersions() { InitializrMetadata metadata = initializeMetadata(); BillOfMaterials bom = BillOfMaterials.create("org.acme", "foo-bom"); bom.getMappings().add(Mapping.create("[1.2.0.RELEASE,1.3.x.RELEASE]", "1.0.0")); bom.getMappings() .add(Mapping.create("1.3.x.BUILD-SNAPSHOT", "1.1.0-BUILD-SNAPSHOT")); Dependency dependency = Dependency.withId("bar"); metadata.updateSpringBootVersions(bootVersions); assertThat(metadata.getConfiguration().getEnv().getBoms().get("foo-bom") .resolve(Version.parse("1.3.6.RELEASE")).getVersion()).isEqualTo("1.0.0"); assertThat(metadata.getConfiguration().getEnv().getBoms().get("foo-bom") .resolve(Version.parse("1.3.7.BUILD-SNAPSHOT")).getVersion()) .isEqualTo("1.1.0-BUILD-SNAPSHOT"); assertThat(metadata.getDependencies().get("bar")
NodeList scope = element.getElementsByTagName("scope"); if (isBom(type, scope)) { BillOfMaterials bom = new BillOfMaterials(); NodeList groupId = element.getElementsByTagName("groupId"); if (groupId.getLength() > 0) { bom.setGroupId(groupId.item(0).getTextContent()); bom.setArtifactId(artifactId.item(0).getTextContent()); bom.setVersion(version.item(0).getTextContent()); String id = generateBomId(bom.getGroupId(), bom.getArtifactId()); assertThat(this.boms).doesNotContainKeys(id); this.boms.put(id, bom);
@Test void noRangeAvailable() { BillOfMaterials bom = BillOfMaterials.create("com.example", "bom"); bom.getMappings().add(Mapping.create("[1.2.0.RELEASE,1.3.0.M1)", "1.1.0")); bom.getMappings().add(Mapping.create("[1.3.0.M1, 1.4.0.M1)", "1.2.0")); bom.validate(); assertThatIllegalStateException() .isThrownBy(() -> bom.resolve(Version.parse("1.4.1.RELEASE"))) .withMessageContaining("1.4.1.RELEASE"); }
@ParameterizedTest @MethodSource("parameters") public void bomWithOrdering(String build, String fileName) { Dependency foo = Dependency.withId("foo", "org.acme", "foo"); foo.setBom("foo-bom"); BillOfMaterials barBom = BillOfMaterials.create("org.acme", "bar-bom", "1.0"); barBom.setOrder(50); BillOfMaterials bizBom = BillOfMaterials.create("org.acme", "biz-bom"); bizBom.setOrder(40); bizBom.getAdditionalBoms().add("bar-bom"); bizBom.getMappings().add(BillOfMaterials.Mapping.create("1.0.0.RELEASE", "1.0")); BillOfMaterials fooBom = BillOfMaterials.create("org.acme", "foo-bom", "1.0"); fooBom.setOrder(20); fooBom.getAdditionalBoms().add("biz-bom"); InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() .addDependencyGroup("foo", foo).addBom("foo-bom", fooBom) .addBom("bar-bom", barBom).addBom("biz-bom", bizBom).build(); applyMetadata(metadata); ProjectRequest request = createProjectRequestForType(build, "foo"); ProjectAssert project = generateProject(request); project.sourceCodeAssert(fileName).equalsTo(new ClassPathResource( "project/" + build + "/bom-ordering-" + getAssertFileName(fileName))); }
@Test void mavenBomWithVersionMappingAndExtraRepositories() { Dependency foo = Dependency.withId("foo", "org.acme", "foo"); foo.setBom("the-bom"); BillOfMaterials bom = BillOfMaterials.create("org.acme", "foo-bom"); bom.getRepositories().add("foo-repo"); bom.getMappings() .add(BillOfMaterials.Mapping.create("[2.2.0.RELEASE,2.3.0.M1)", "1.0.0")); bom.getMappings().add(BillOfMaterials.Mapping.create("2.3.0.M1", "1.2.0", "foo-repo", "bar-repo")); InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() .addDependencyGroup("foo", foo).addBom("the-bom", bom) .addRepository("foo-repo", "repo", "http://example.com/foo", true) .addRepository("bar-repo", "repo", "http://example.com/bar", false) .build(); applyMetadata(metadata); // Second version ProjectRequest request = createProjectRequest("foo"); request.setBootVersion("2.3.0.RELEASE"); generateMavenPom(request).hasDependency(foo).hasSpringBootParent("2.3.0.RELEASE") .hasBom("org.acme", "foo-bom", "1.2.0") .hasRepository("foo-repo", "repo", "http://example.com/foo", true) .hasRepository("bar-repo", "repo", "http://example.com/bar", false) .hasRepositoriesCount(2); }
@Test void invalidBomUnknownRepository() { InitializrMetadata metadata = initializeMetadata(); BillOfMaterials bom = BillOfMaterials.create("org.acme", "foo-bom", "1.0.0.RELEASE"); bom.getRepositories().add("foo-repo"); metadata.getConfiguration().getEnv().getBoms().put("foo-bom", bom); assertThatExceptionOfType(InvalidInitializrMetadataException.class) .isThrownBy(metadata::validate) .withMessageContaining("invalid repository id foo-repo") .withMessageContaining("foo-bom"); }
for (String r : bom.getRepositories()) { if (!repositories.containsKey(r)) { throw new InvalidInitializrMetadataException( for (String b : bom.getAdditionalBoms()) { if (!boms.containsKey(b)) { throw new InvalidInitializrMetadataException( for (BillOfMaterials.Mapping m : bom.getMappings()) { for (String r : m.getRepositories()) { if (!repositories.containsKey(r)) {
@Test void invalidBomUnknownAdditionalBom() { InitializrMetadata metadata = initializeMetadata(); BillOfMaterials bom = BillOfMaterials.create("org.acme", "foo-bom", "1.0.0.RELEASE"); bom.getAdditionalBoms().addAll(Arrays.asList("bar-bom", "biz-bom")); BillOfMaterials barBom = BillOfMaterials.create("org.acme", "bar-bom", "1.0.0.RELEASE"); metadata.getConfiguration().getEnv().getBoms().put("foo-bom", bom); metadata.getConfiguration().getEnv().getBoms().put("bar-bom", barBom); assertThatExceptionOfType(InvalidInitializrMetadataException.class) .isThrownBy(metadata::validate) .withMessageContaining("invalid additional bom") .withMessageContaining("biz-bom"); }
if (dependency.getBom() != null) { boms.put(dependency.getBom(), metadata.getConfiguration().getEnv() .getBoms().get(dependency.getBom()).resolve(bootVersion)); for (String id : bom.getRepositories()) { repositories.put(id, metadata.getConfiguration().getEnv().getRepositories().get(id));
private void resolveBom(InitializrMetadata metadata, String bomId, Version requestedVersion) { if (!this.boms.containsKey(bomId)) { BillOfMaterials bom = metadata.getConfiguration().getEnv().getBoms() .get(bomId).resolve(requestedVersion); bom.getAdditionalBoms() .forEach((id) -> resolveBom(metadata, id, requestedVersion)); this.boms.put(bomId, bom); } }